mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
switch DeckID to a NewType
Not sure at this point whether this will buy us much in the Python codebase over a simple int alias, but let's give it a go.
This commit is contained in:
parent
01161c8ed2
commit
84b0c8ba88
3 changed files with 21 additions and 19 deletions
|
@ -7,7 +7,7 @@ import copy
|
||||||
import pprint
|
import pprint
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union
|
from typing import Any, Dict, Iterable, List, NewType, Optional, Sequence, Tuple, Union
|
||||||
|
|
||||||
import anki # pylint: disable=unused-import
|
import anki # pylint: disable=unused-import
|
||||||
import anki._backend.backend_pb2 as _pb
|
import anki._backend.backend_pb2 as _pb
|
||||||
|
@ -39,7 +39,7 @@ DeckConfig = Union[FilteredDeck, Config]
|
||||||
""" New/lrn/rev conf, from deck config"""
|
""" New/lrn/rev conf, from deck config"""
|
||||||
QueueConfig = Dict[str, Any]
|
QueueConfig = Dict[str, Any]
|
||||||
|
|
||||||
DeckID = int
|
DeckID = NewType("DeckID", int)
|
||||||
|
|
||||||
|
|
||||||
class DecksDictProxy:
|
class DecksDictProxy:
|
||||||
|
@ -301,7 +301,7 @@ class DeckManager:
|
||||||
onto = 0
|
onto = 0
|
||||||
else:
|
else:
|
||||||
onto = int(ontoDeckDid)
|
onto = int(ontoDeckDid)
|
||||||
self.reparent([int(draggedDeckDid)], onto)
|
self.reparent([DeckID(int(draggedDeckDid))], DeckID(onto))
|
||||||
|
|
||||||
# Deck configurations
|
# Deck configurations
|
||||||
#############################################################
|
#############################################################
|
||||||
|
|
|
@ -9,7 +9,7 @@ from typing import Any
|
||||||
|
|
||||||
import aqt
|
import aqt
|
||||||
from anki.collection import OpChanges
|
from anki.collection import OpChanges
|
||||||
from anki.decks import DeckTreeNode
|
from anki.decks import DeckID, DeckTreeNode
|
||||||
from anki.utils import intTime
|
from anki.utils import intTime
|
||||||
from aqt import AnkiQt, gui_hooks
|
from aqt import AnkiQt, gui_hooks
|
||||||
from aqt.deck_ops import add_deck_dialog, remove_decks, rename_deck, reparent_decks
|
from aqt.deck_ops import add_deck_dialog, remove_decks, rename_deck, reparent_decks
|
||||||
|
@ -99,7 +99,7 @@ class DeckBrowser:
|
||||||
self._on_create()
|
self._on_create()
|
||||||
elif cmd == "drag":
|
elif cmd == "drag":
|
||||||
source, target = arg.split(",")
|
source, target = arg.split(",")
|
||||||
self._handle_drag_and_drop(int(source), int(target or 0))
|
self._handle_drag_and_drop(DeckID(int(source)), DeckID(int(target or 0)))
|
||||||
elif cmd == "collapse":
|
elif cmd == "collapse":
|
||||||
self._collapse(int(arg))
|
self._collapse(int(arg))
|
||||||
elif cmd == "v2upgrade":
|
elif cmd == "v2upgrade":
|
||||||
|
@ -251,20 +251,20 @@ class DeckBrowser:
|
||||||
def _showOptions(self, did: str) -> None:
|
def _showOptions(self, did: str) -> None:
|
||||||
m = QMenu(self.mw)
|
m = QMenu(self.mw)
|
||||||
a = m.addAction(tr(TR.ACTIONS_RENAME))
|
a = m.addAction(tr(TR.ACTIONS_RENAME))
|
||||||
qconnect(a.triggered, lambda b, did=did: self._rename(int(did)))
|
qconnect(a.triggered, lambda b, did=did: self._rename(DeckID(int(did))))
|
||||||
a = m.addAction(tr(TR.ACTIONS_OPTIONS))
|
a = m.addAction(tr(TR.ACTIONS_OPTIONS))
|
||||||
qconnect(a.triggered, lambda b, did=did: self._options(did))
|
qconnect(a.triggered, lambda b, did=did: self._options(DeckID(int(did))))
|
||||||
a = m.addAction(tr(TR.ACTIONS_EXPORT))
|
a = m.addAction(tr(TR.ACTIONS_EXPORT))
|
||||||
qconnect(a.triggered, lambda b, did=did: self._export(int(did)))
|
qconnect(a.triggered, lambda b, did=did: self._export(int(did)))
|
||||||
a = m.addAction(tr(TR.ACTIONS_DELETE))
|
a = m.addAction(tr(TR.ACTIONS_DELETE))
|
||||||
qconnect(a.triggered, lambda b, did=did: self._delete(int(did)))
|
qconnect(a.triggered, lambda b, did=did: self._delete(DeckID(int(did))))
|
||||||
gui_hooks.deck_browser_will_show_options_menu(m, int(did))
|
gui_hooks.deck_browser_will_show_options_menu(m, int(did))
|
||||||
m.exec_(QCursor.pos())
|
m.exec_(QCursor.pos())
|
||||||
|
|
||||||
def _export(self, did: int) -> None:
|
def _export(self, did: int) -> None:
|
||||||
self.mw.onExport(did=did)
|
self.mw.onExport(did=did)
|
||||||
|
|
||||||
def _rename(self, did: int) -> None:
|
def _rename(self, did: DeckID) -> None:
|
||||||
deck = self.mw.col.decks.get(did)
|
deck = self.mw.col.decks.get(did)
|
||||||
current_name = deck["name"]
|
current_name = deck["name"]
|
||||||
new_name = getOnlyText(tr(TR.DECKS_NEW_DECK_NAME), default=current_name)
|
new_name = getOnlyText(tr(TR.DECKS_NEW_DECK_NAME), default=current_name)
|
||||||
|
@ -273,10 +273,10 @@ class DeckBrowser:
|
||||||
|
|
||||||
rename_deck(mw=self.mw, deck_id=did, new_name=new_name)
|
rename_deck(mw=self.mw, deck_id=did, new_name=new_name)
|
||||||
|
|
||||||
def _options(self, did: str) -> None:
|
def _options(self, did: DeckID) -> None:
|
||||||
# select the deck first, because the dyn deck conf assumes the deck
|
# select the deck first, because the dyn deck conf assumes the deck
|
||||||
# we're editing is the current one
|
# we're editing is the current one
|
||||||
self.mw.col.decks.select(int(did))
|
self.mw.col.decks.select(did)
|
||||||
self.mw.onDeckConf()
|
self.mw.onDeckConf()
|
||||||
|
|
||||||
def _collapse(self, did: int) -> None:
|
def _collapse(self, did: int) -> None:
|
||||||
|
@ -286,10 +286,10 @@ class DeckBrowser:
|
||||||
node.collapsed = not node.collapsed
|
node.collapsed = not node.collapsed
|
||||||
self._renderPage(reuse=True)
|
self._renderPage(reuse=True)
|
||||||
|
|
||||||
def _handle_drag_and_drop(self, source: int, target: int) -> None:
|
def _handle_drag_and_drop(self, source: DeckID, target: DeckID) -> None:
|
||||||
reparent_decks(mw=self.mw, parent=self.mw, deck_ids=[source], new_parent=target)
|
reparent_decks(mw=self.mw, parent=self.mw, deck_ids=[source], new_parent=target)
|
||||||
|
|
||||||
def _delete(self, did: int) -> None:
|
def _delete(self, did: DeckID) -> None:
|
||||||
remove_decks(mw=self.mw, parent=self.mw, deck_ids=[did])
|
remove_decks(mw=self.mw, parent=self.mw, deck_ids=[did])
|
||||||
|
|
||||||
# Top buttons
|
# Top buttons
|
||||||
|
|
|
@ -8,7 +8,7 @@ from typing import Dict, Iterable, List, Optional, Tuple, cast
|
||||||
|
|
||||||
import aqt
|
import aqt
|
||||||
from anki.collection import Config, OpChanges, SearchJoiner, SearchNode
|
from anki.collection import Config, OpChanges, SearchJoiner, SearchNode
|
||||||
from anki.decks import DeckTreeNode
|
from anki.decks import DeckID, DeckTreeNode
|
||||||
from anki.errors import InvalidInput
|
from anki.errors import InvalidInput
|
||||||
from anki.notes import Note
|
from anki.notes import Note
|
||||||
from anki.tags import TagTreeNode
|
from anki.tags import TagTreeNode
|
||||||
|
@ -603,12 +603,14 @@ class SidebarTreeView(QTreeView):
|
||||||
self, sources: List[SidebarItem], target: SidebarItem
|
self, sources: List[SidebarItem], target: SidebarItem
|
||||||
) -> bool:
|
) -> bool:
|
||||||
deck_ids = [
|
deck_ids = [
|
||||||
source.id for source in sources if source.item_type == SidebarItemType.DECK
|
DeckID(source.id)
|
||||||
|
for source in sources
|
||||||
|
if source.item_type == SidebarItemType.DECK
|
||||||
]
|
]
|
||||||
if not deck_ids:
|
if not deck_ids:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
new_parent = target.id
|
new_parent = DeckID(target.id)
|
||||||
|
|
||||||
reparent_decks(
|
reparent_decks(
|
||||||
mw=self.mw, parent=self.browser, deck_ids=deck_ids, new_parent=new_parent
|
mw=self.mw, parent=self.browser, deck_ids=deck_ids, new_parent=new_parent
|
||||||
|
@ -1161,7 +1163,7 @@ class SidebarTreeView(QTreeView):
|
||||||
|
|
||||||
rename_deck(
|
rename_deck(
|
||||||
mw=self.mw,
|
mw=self.mw,
|
||||||
deck_id=item.id,
|
deck_id=DeckID(item.id),
|
||||||
new_name=new_name,
|
new_name=new_name,
|
||||||
after_rename=lambda: self.refresh(
|
after_rename=lambda: self.refresh(
|
||||||
lambda other: other.item_type == SidebarItemType.DECK
|
lambda other: other.item_type == SidebarItemType.DECK
|
||||||
|
@ -1284,9 +1286,9 @@ class SidebarTreeView(QTreeView):
|
||||||
def _selected_items(self) -> List[SidebarItem]:
|
def _selected_items(self) -> List[SidebarItem]:
|
||||||
return [self.model().item_for_index(idx) for idx in self.selectedIndexes()]
|
return [self.model().item_for_index(idx) for idx in self.selectedIndexes()]
|
||||||
|
|
||||||
def _selected_decks(self) -> List[int]:
|
def _selected_decks(self) -> List[DeckID]:
|
||||||
return [
|
return [
|
||||||
item.id
|
DeckID(item.id)
|
||||||
for item in self._selected_items()
|
for item in self._selected_items()
|
||||||
if item.item_type == SidebarItemType.DECK
|
if item.item_type == SidebarItemType.DECK
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue