From 90661c4bfcf6b72aa66c27d98774fb1433d5f8ee Mon Sep 17 00:00:00 2001 From: Taylor Obyen <162023405+taylorobyen@users.noreply.github.com> Date: Sun, 22 Sep 2024 04:02:17 -0400 Subject: [PATCH] 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 --- qt/aqt/addcards.py | 7 +++++++ qt/aqt/browser/browser.py | 22 +++++++++++++++++++++- qt/aqt/browser/sidebar/item.py | 3 +++ qt/aqt/browser/sidebar/tree.py | 8 ++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/qt/aqt/addcards.py b/qt/aqt/addcards.py index c99a8e99d..f99829fd0 100644 --- a/qt/aqt/addcards.py +++ b/qt/aqt/addcards.py @@ -60,6 +60,12 @@ class AddCards(QMainWindow): self.setMenuBar(None) 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: """Set tags, field contents and notetype according to `note`. Deck is set to `deck_id` or the deck last used with the notetype. @@ -86,6 +92,7 @@ class AddCards(QMainWindow): defaults = self.col.defaults_for_adding( current_review_card=self.mw.reviewer.card ) + self.notetype_chooser = NotetypeChooser( mw=self.mw, widget=self.form.modelArea, diff --git a/qt/aqt/browser/browser.py b/qt/aqt/browser/browser.py index 45924b03c..3056f9632 100644 --- a/qt/aqt/browser/browser.py +++ b/qt/aqt/browser/browser.py @@ -7,7 +7,7 @@ import json import math import re from collections.abc import Callable, Sequence -from typing import Any +from typing import Any, cast import aqt import aqt.browser @@ -18,8 +18,10 @@ from anki._legacy import deprecated from anki.cards import Card, CardId from anki.collection import Collection, Config, OpChanges, SearchNode from anki.consts import * +from anki.decks import DeckId from anki.errors import NotFoundError from anki.lang import without_unicode_isolation +from anki.models import NotetypeId from anki.notes import NoteId from anki.scheduler.base import ScheduleCardsAsNew from anki.tags import MARKED_TAG @@ -72,6 +74,7 @@ from aqt.utils import ( tr, ) +from ..addcards import AddCards from ..changenotetype import change_notetype_dialog from .card_info import BrowserCardInfo from .find_and_replace import FindAndReplaceDialog @@ -249,6 +252,23 @@ class Browser(QMainWindow): 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: # actions f = self.form diff --git a/qt/aqt/browser/sidebar/item.py b/qt/aqt/browser/sidebar/item.py index 8da2f3df6..8e1f166c8 100644 --- a/qt/aqt/browser/sidebar/item.py +++ b/qt/aqt/browser/sidebar/item.py @@ -48,6 +48,9 @@ class SidebarItemType(Enum): SidebarItemType.TAG, ) + def can_be_added_to(self) -> bool: + return self == SidebarItemType.DECK + def is_deletable(self) -> bool: return self in ( SidebarItemType.SAVED_SEARCH, diff --git a/qt/aqt/browser/sidebar/tree.py b/qt/aqt/browser/sidebar/tree.py index 89600f231..a10cc2ced 100644 --- a/qt/aqt/browser/sidebar/tree.py +++ b/qt/aqt/browser/sidebar/tree.py @@ -464,6 +464,9 @@ class SidebarTreeView(QTreeView): 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: if item.item_type == SidebarItemType.SAVED_SEARCH: self.remove_saved_searches(item) @@ -893,6 +896,7 @@ class SidebarTreeView(QTreeView): menu = QMenu() self._maybe_add_type_specific_actions(menu, item) menu.addSeparator() + self._maybe_add_add_action(menu, item) self._maybe_add_delete_action(menu, item, index) self._maybe_add_rename_actions(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, ) + 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( self, menu: QMenu, item: SidebarItem, index: QModelIndex ) -> None: