mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
Track Current Deck Context from Browser (#3385)
* Update current deck from browser * Update contributors * Fix formatting * Allow Add window to be opened with parameters * Add explicit return * Fix formatting * Accomplish context in a more simple fashion * Implement context menu add * Fix contributors * Add additional context * Removed uneeded logic and inlined redundant funcs * Resolve contributors conflict * Update qt/aqt/deckbrowser.py * Update qt/aqt/deckbrowser.py
This commit is contained in:
parent
722b9b53f4
commit
90661c4bfc
4 changed files with 39 additions and 1 deletions
|
@ -60,6 +60,12 @@ class AddCards(QMainWindow):
|
||||||
self.setMenuBar(None)
|
self.setMenuBar(None)
|
||||||
self.show()
|
self.show()
|
||||||
|
|
||||||
|
def set_deck(self, deck_id: DeckId) -> None:
|
||||||
|
self.deck_chooser.selected_deck_id = deck_id
|
||||||
|
|
||||||
|
def set_note_type(self, note_type_id: NotetypeId) -> None:
|
||||||
|
self.notetype_chooser.selected_notetype_id = note_type_id
|
||||||
|
|
||||||
def set_note(self, note: Note, deck_id: DeckId | None = None) -> None:
|
def set_note(self, note: Note, deck_id: DeckId | None = None) -> None:
|
||||||
"""Set tags, field contents and notetype according to `note`. Deck is set
|
"""Set tags, field contents and notetype according to `note`. Deck is set
|
||||||
to `deck_id` or the deck last used with the notetype.
|
to `deck_id` or the deck last used with the notetype.
|
||||||
|
@ -86,6 +92,7 @@ class AddCards(QMainWindow):
|
||||||
defaults = self.col.defaults_for_adding(
|
defaults = self.col.defaults_for_adding(
|
||||||
current_review_card=self.mw.reviewer.card
|
current_review_card=self.mw.reviewer.card
|
||||||
)
|
)
|
||||||
|
|
||||||
self.notetype_chooser = NotetypeChooser(
|
self.notetype_chooser = NotetypeChooser(
|
||||||
mw=self.mw,
|
mw=self.mw,
|
||||||
widget=self.form.modelArea,
|
widget=self.form.modelArea,
|
||||||
|
|
|
@ -7,7 +7,7 @@ import json
|
||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
from collections.abc import Callable, Sequence
|
from collections.abc import Callable, Sequence
|
||||||
from typing import Any
|
from typing import Any, cast
|
||||||
|
|
||||||
import aqt
|
import aqt
|
||||||
import aqt.browser
|
import aqt.browser
|
||||||
|
@ -18,8 +18,10 @@ from anki._legacy import deprecated
|
||||||
from anki.cards import Card, CardId
|
from anki.cards import Card, CardId
|
||||||
from anki.collection import Collection, Config, OpChanges, SearchNode
|
from anki.collection import Collection, Config, OpChanges, SearchNode
|
||||||
from anki.consts import *
|
from anki.consts import *
|
||||||
|
from anki.decks import DeckId
|
||||||
from anki.errors import NotFoundError
|
from anki.errors import NotFoundError
|
||||||
from anki.lang import without_unicode_isolation
|
from anki.lang import without_unicode_isolation
|
||||||
|
from anki.models import NotetypeId
|
||||||
from anki.notes import NoteId
|
from anki.notes import NoteId
|
||||||
from anki.scheduler.base import ScheduleCardsAsNew
|
from anki.scheduler.base import ScheduleCardsAsNew
|
||||||
from anki.tags import MARKED_TAG
|
from anki.tags import MARKED_TAG
|
||||||
|
@ -72,6 +74,7 @@ from aqt.utils import (
|
||||||
tr,
|
tr,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from ..addcards import AddCards
|
||||||
from ..changenotetype import change_notetype_dialog
|
from ..changenotetype import change_notetype_dialog
|
||||||
from .card_info import BrowserCardInfo
|
from .card_info import BrowserCardInfo
|
||||||
from .find_and_replace import FindAndReplaceDialog
|
from .find_and_replace import FindAndReplaceDialog
|
||||||
|
@ -249,6 +252,23 @@ class Browser(QMainWindow):
|
||||||
|
|
||||||
QMainWindow.resizeEvent(self, event)
|
QMainWindow.resizeEvent(self, event)
|
||||||
|
|
||||||
|
def get_active_note_type_id(self) -> NotetypeId | None:
|
||||||
|
"""
|
||||||
|
If multiple cards are selected the note type will be derived
|
||||||
|
from the final card selected
|
||||||
|
"""
|
||||||
|
if current_note := self.table.get_current_note():
|
||||||
|
return current_note.mid
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def add_card(self, deck_id: DeckId):
|
||||||
|
add_cards = cast(AddCards, aqt.dialogs.open("AddCards", self.mw))
|
||||||
|
add_cards.set_deck(deck_id)
|
||||||
|
|
||||||
|
if note_type_id := self.get_active_note_type_id():
|
||||||
|
add_cards.set_note_type(note_type_id)
|
||||||
|
|
||||||
def setupMenus(self) -> None:
|
def setupMenus(self) -> None:
|
||||||
# actions
|
# actions
|
||||||
f = self.form
|
f = self.form
|
||||||
|
|
|
@ -48,6 +48,9 @@ class SidebarItemType(Enum):
|
||||||
SidebarItemType.TAG,
|
SidebarItemType.TAG,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def can_be_added_to(self) -> bool:
|
||||||
|
return self == SidebarItemType.DECK
|
||||||
|
|
||||||
def is_deletable(self) -> bool:
|
def is_deletable(self) -> bool:
|
||||||
return self in (
|
return self in (
|
||||||
SidebarItemType.SAVED_SEARCH,
|
SidebarItemType.SAVED_SEARCH,
|
||||||
|
|
|
@ -464,6 +464,9 @@ class SidebarTreeView(QTreeView):
|
||||||
s.item_type == item.item_type for s in self._selected_items()
|
s.item_type == item.item_type for s in self._selected_items()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _on_add(self, item: SidebarItem):
|
||||||
|
self.browser.add_card(DeckId(item.id))
|
||||||
|
|
||||||
def _on_delete(self, item: SidebarItem) -> None:
|
def _on_delete(self, item: SidebarItem) -> None:
|
||||||
if item.item_type == SidebarItemType.SAVED_SEARCH:
|
if item.item_type == SidebarItemType.SAVED_SEARCH:
|
||||||
self.remove_saved_searches(item)
|
self.remove_saved_searches(item)
|
||||||
|
@ -893,6 +896,7 @@ class SidebarTreeView(QTreeView):
|
||||||
menu = QMenu()
|
menu = QMenu()
|
||||||
self._maybe_add_type_specific_actions(menu, item)
|
self._maybe_add_type_specific_actions(menu, item)
|
||||||
menu.addSeparator()
|
menu.addSeparator()
|
||||||
|
self._maybe_add_add_action(menu, item)
|
||||||
self._maybe_add_delete_action(menu, item, index)
|
self._maybe_add_delete_action(menu, item, index)
|
||||||
self._maybe_add_rename_actions(menu, item, index)
|
self._maybe_add_rename_actions(menu, item, index)
|
||||||
self._maybe_add_find_and_replace_action(menu, item, index)
|
self._maybe_add_find_and_replace_action(menu, item, index)
|
||||||
|
@ -932,6 +936,10 @@ class SidebarTreeView(QTreeView):
|
||||||
self.remove_tags_from_selected_notes,
|
self.remove_tags_from_selected_notes,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _maybe_add_add_action(self, menu: QMenu, item: SidebarItem) -> None:
|
||||||
|
if item.item_type.can_be_added_to():
|
||||||
|
menu.addAction(tr.browsing_add_notes(), lambda: self._on_add(item))
|
||||||
|
|
||||||
def _maybe_add_delete_action(
|
def _maybe_add_delete_action(
|
||||||
self, menu: QMenu, item: SidebarItem, index: QModelIndex
|
self, menu: QMenu, item: SidebarItem, index: QModelIndex
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
Loading…
Reference in a new issue