mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02: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 sys
|
||||
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._backend.backend_pb2 as _pb
|
||||
|
@ -39,7 +39,7 @@ DeckConfig = Union[FilteredDeck, Config]
|
|||
""" New/lrn/rev conf, from deck config"""
|
||||
QueueConfig = Dict[str, Any]
|
||||
|
||||
DeckID = int
|
||||
DeckID = NewType("DeckID", int)
|
||||
|
||||
|
||||
class DecksDictProxy:
|
||||
|
@ -301,7 +301,7 @@ class DeckManager:
|
|||
onto = 0
|
||||
else:
|
||||
onto = int(ontoDeckDid)
|
||||
self.reparent([int(draggedDeckDid)], onto)
|
||||
self.reparent([DeckID(int(draggedDeckDid))], DeckID(onto))
|
||||
|
||||
# Deck configurations
|
||||
#############################################################
|
||||
|
|
|
@ -9,7 +9,7 @@ from typing import Any
|
|||
|
||||
import aqt
|
||||
from anki.collection import OpChanges
|
||||
from anki.decks import DeckTreeNode
|
||||
from anki.decks import DeckID, DeckTreeNode
|
||||
from anki.utils import intTime
|
||||
from aqt import AnkiQt, gui_hooks
|
||||
from aqt.deck_ops import add_deck_dialog, remove_decks, rename_deck, reparent_decks
|
||||
|
@ -99,7 +99,7 @@ class DeckBrowser:
|
|||
self._on_create()
|
||||
elif cmd == "drag":
|
||||
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":
|
||||
self._collapse(int(arg))
|
||||
elif cmd == "v2upgrade":
|
||||
|
@ -251,20 +251,20 @@ class DeckBrowser:
|
|||
def _showOptions(self, did: str) -> None:
|
||||
m = QMenu(self.mw)
|
||||
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))
|
||||
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))
|
||||
qconnect(a.triggered, lambda b, did=did: self._export(int(did)))
|
||||
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))
|
||||
m.exec_(QCursor.pos())
|
||||
|
||||
def _export(self, did: int) -> None:
|
||||
self.mw.onExport(did=did)
|
||||
|
||||
def _rename(self, did: int) -> None:
|
||||
def _rename(self, did: DeckID) -> None:
|
||||
deck = self.mw.col.decks.get(did)
|
||||
current_name = deck["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)
|
||||
|
||||
def _options(self, did: str) -> None:
|
||||
def _options(self, did: DeckID) -> None:
|
||||
# select the deck first, because the dyn deck conf assumes the deck
|
||||
# we're editing is the current one
|
||||
self.mw.col.decks.select(int(did))
|
||||
self.mw.col.decks.select(did)
|
||||
self.mw.onDeckConf()
|
||||
|
||||
def _collapse(self, did: int) -> None:
|
||||
|
@ -286,10 +286,10 @@ class DeckBrowser:
|
|||
node.collapsed = not node.collapsed
|
||||
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)
|
||||
|
||||
def _delete(self, did: int) -> None:
|
||||
def _delete(self, did: DeckID) -> None:
|
||||
remove_decks(mw=self.mw, parent=self.mw, deck_ids=[did])
|
||||
|
||||
# Top buttons
|
||||
|
|
|
@ -8,7 +8,7 @@ from typing import Dict, Iterable, List, Optional, Tuple, cast
|
|||
|
||||
import aqt
|
||||
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.notes import Note
|
||||
from anki.tags import TagTreeNode
|
||||
|
@ -603,12 +603,14 @@ class SidebarTreeView(QTreeView):
|
|||
self, sources: List[SidebarItem], target: SidebarItem
|
||||
) -> bool:
|
||||
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:
|
||||
return False
|
||||
|
||||
new_parent = target.id
|
||||
new_parent = DeckID(target.id)
|
||||
|
||||
reparent_decks(
|
||||
mw=self.mw, parent=self.browser, deck_ids=deck_ids, new_parent=new_parent
|
||||
|
@ -1161,7 +1163,7 @@ class SidebarTreeView(QTreeView):
|
|||
|
||||
rename_deck(
|
||||
mw=self.mw,
|
||||
deck_id=item.id,
|
||||
deck_id=DeckID(item.id),
|
||||
new_name=new_name,
|
||||
after_rename=lambda: self.refresh(
|
||||
lambda other: other.item_type == SidebarItemType.DECK
|
||||
|
@ -1284,9 +1286,9 @@ class SidebarTreeView(QTreeView):
|
|||
def _selected_items(self) -> List[SidebarItem]:
|
||||
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 [
|
||||
item.id
|
||||
DeckID(item.id)
|
||||
for item in self._selected_items()
|
||||
if item.item_type == SidebarItemType.DECK
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue