Skip to content

Commit

Permalink
Merge pull request ankitects#1044 from RumovZ/sidebar-tools
Browse files Browse the repository at this point in the history
Add sidebar modes for different click behaviour
  • Loading branch information
dae authored Mar 11, 2021
2 parents e20c5ed + f4c2fe6 commit 71789eb
Show file tree
Hide file tree
Showing 22 changed files with 876 additions and 314 deletions.
3 changes: 3 additions & 0 deletions ftl/core/actions.ftl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
actions-add = Add
actions-all-selected = All selected
actions-any-selected = Any selected
actions-blue-flag = Blue Flag
actions-cancel = Cancel
actions-choose = Choose
Expand Down Expand Up @@ -30,6 +32,7 @@ actions-replay-audio = Replay Audio
actions-reposition = Reposition
actions-save = Save
actions-search = Search
actions-select = Select
actions-shortcut-key = Shortcut key: { $val }
actions-suspend-card = Suspend Card
actions-set-due-date = Set Due Date
Expand Down
16 changes: 13 additions & 3 deletions ftl/core/browsing.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ browsing-card = Card
browsing-card-list = Card List
browsing-card-state = Card State
browsing-cards-cant-be-manually-moved-into = Cards can't be manually moved into a filtered deck.
browsing-cards-deleted =
{ $count ->
[one] { $count } card deleted.
*[other] { $count } cards deleted.
}
browsing-change-deck = Change Deck
browsing-change-deck2 = Change Deck...
browsing-change-note-type = Change Note Type
browsing-change-note-type2 = Change Note Type...
browsing-change-to = Change { $val } to:
browsing-clear-unused = Clear Unused
browsing-clear-unused-tags = Clear Unused Tags
browsing-confirm-saved-search-overwrite = A saved search with the name { $name } already exists. Do you want to overwrite it?
browsing-created = Created
browsing-ctrlandshiftande = Ctrl+Shift+E
browsing-current-deck = Current Deck
Expand Down Expand Up @@ -70,14 +76,11 @@ browsing-question = Question
browsing-queue-bottom = Queue bottom: { $val }
browsing-queue-top = Queue top: { $val }
browsing-randomize-order = Randomize order
browsing-remove-current-filter = Remove Current Filter...
browsing-remove-from-your-saved-searches = Remove { $val } from your saved searches?
browsing-remove-tags = Remove Tags...
browsing-replace-with = <b>Replace With</b>:
browsing-reposition = Reposition...
browsing-reposition-new-cards = Reposition New Cards
browsing-reschedule = Reschedule
browsing-save-current-filter = Save Current Filter...
browsing-search-bar-hint = Search cards/notes (type text, then press Enter)
browsing-search-in = Search in:
browsing-search-within-formatting-slow = Search within formatting (slow)
Expand Down Expand Up @@ -112,7 +115,14 @@ browsing-note-deleted =
[one] { $count } note deleted.
*[other] { $count } notes deleted.
}
browsing-notes-updated =
{ $count ->
[one] { $count } note updated.
*[other] { $count } notes updated.
}
browsing-window-title = Browse ({ $selected } of { $total } cards selected)
browsing-sidebar-expand = Expand
browsing-sidebar-collapse = Collapse
browsing-sidebar-expand-children = Expand Children
browsing-sidebar-collapse-children = Collapse Children
browsing-sidebar-decks = Decks
Expand Down
6 changes: 0 additions & 6 deletions ftl/core/decks.ftl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
decks-add-new-deck-ctrlandn = Add New Deck (Ctrl+N)
decks-are-you-sure-you-wish-to = Are you sure you wish to delete { $val }?
decks-build = Build
decks-cards-selected-by = cards selected by
decks-create-deck = Create Deck
Expand Down Expand Up @@ -32,8 +31,3 @@ decks-study = Study
decks-study-deck = Study Deck
decks-the-provided-search-did-not-match = The provided search did not match any cards. Would you like to revise it?
decks-unmovable-cards = Show any excluded cards
decks-it-has-card =
{ $count ->
[one] It has { $count } card.
*[other] It has { $count } cards.
}
19 changes: 14 additions & 5 deletions pylib/anki/decks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import anki._backend.backend_pb2 as _pb
from anki.consts import *
from anki.errors import NotFoundError
from anki.utils import from_json_bytes, ids2str, intTime, to_json_bytes
from anki.utils import from_json_bytes, ids2str, intTime, legacy_func, to_json_bytes

# public exports
DeckTreeNode = _pb.DeckTreeNode
Expand Down Expand Up @@ -130,12 +130,16 @@ def id(

return deck["id"]

@legacy_func(sub="remove")
def rem(self, did: int, cardsToo: bool = True, childrenToo: bool = True) -> None:
"Remove the deck. If cardsToo, delete any cards inside."
if isinstance(did, str):
did = int(did)
assert cardsToo and childrenToo
self.col._backend.remove_deck(did)
self.remove([did])

def remove(self, dids: List[int]) -> int:
return self.col._backend.remove_decks(dids)

def all_names_and_ids(
self, skip_empty_default: bool = False, include_filtered: bool = True
Expand Down Expand Up @@ -212,10 +216,15 @@ def collapseBrowser(self, did: int) -> None:
def count(self) -> int:
return len(self.all_names_and_ids())

def card_count(self, did: int, include_subdecks: bool) -> Any:
dids: List[int] = [did]
def card_count(
self, dids: Union[int, Iterable[int]], include_subdecks: bool
) -> Any:
if isinstance(dids, int):
dids = {dids}
else:
dids = set(dids)
if include_subdecks:
dids += [r[1] for r in self.children(did)]
dids.update([child[1] for did in dids for child in self.children(did)])
count = self.col.db.scalar(
"select count() from cards where did in {0} or "
"odid in {0}".format(ids2str(dids))
Expand Down
25 changes: 24 additions & 1 deletion pylib/anki/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from contextlib import contextmanager
from hashlib import sha1
from html.entities import name2codepoint
from typing import Any, Iterable, Iterator, List, Match, Optional, Union
from typing import Any, Callable, Iterable, Iterator, List, Match, Optional, Union

from anki.dbproxy import DBProxy

Expand Down Expand Up @@ -372,3 +372,26 @@ def pointVersion() -> int:
from anki.buildinfo import version

return int(version.split(".")[-1])


# Legacy support
##############################################################################


def legacy_func(sub: Optional[str] = None) -> Callable:
"""Print a deprecation warning for the decorated callable recommending the use of
'sub' instead, if provided.
"""
if sub:
hint = f", use '{sub}' instead"
else:
hint = ""

def decorater(func: Callable) -> Callable:
def decorated_func(*args: Any, **kwargs: Any) -> Any:
print(f"'{func.__name__}' is deprecated{hint}.")
return func(*args, **kwargs)

return decorated_func

return decorater
1 change: 1 addition & 0 deletions qt/aqt/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def onCopy() -> None:
"Gustavo Costa",
"余时行",
"叶峻峣",
"RumovZ",
)
)

Expand Down
16 changes: 9 additions & 7 deletions qt/aqt/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from aqt.previewer import Previewer
from aqt.qt import *
from aqt.scheduling import forget_cards, set_due_date_dialog
from aqt.sidebar import SidebarSearchBar, SidebarTreeView
from aqt.sidebar import SidebarSearchBar, SidebarToolbar, SidebarTreeView
from aqt.theme import theme_manager
from aqt.utils import (
TR,
Expand Down Expand Up @@ -941,18 +941,20 @@ def setupSidebar(self) -> None:
self.sidebar = SidebarTreeView(self)
self.sidebarTree = self.sidebar # legacy alias
dw.setWidget(self.sidebar)
self.sidebar.toolbar = toolbar = SidebarToolbar(self.sidebar)
self.sidebar.searchBar = searchBar = SidebarSearchBar(self.sidebar)
qconnect(
self.form.actionSidebarFilter.triggered,
self.focusSidebarSearchBar,
)
l = QVBoxLayout()
l.addWidget(searchBar)
l.addWidget(self.sidebar)
l.setContentsMargins(0, 0, 0, 0)
l.setSpacing(0)
grid = QGridLayout()
grid.addWidget(searchBar, 0, 0)
grid.addWidget(toolbar, 0, 1)
grid.addWidget(self.sidebar, 1, 0, 1, 2)
grid.setContentsMargins(0, 0, 0, 0)
grid.setSpacing(0)
w = QWidget()
w.setLayout(l)
w.setLayout(grid)
dw.setWidget(w)
self.sidebarDockWidget.setFloating(False)

Expand Down
33 changes: 8 additions & 25 deletions qt/aqt/deckbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
shortcut,
showInfo,
showWarning,
tooltip,
tr,
)

Expand Down Expand Up @@ -303,34 +304,16 @@ def on_done(fut: Future) -> None:

self.mw.taskman.with_progress(process, on_done)

def ask_delete_deck(self, did: int) -> bool:
deck = self.mw.col.decks.get(did)
if deck["dyn"]:
return True

count = self.mw.col.decks.card_count(did, include_subdecks=True)
if not count:
return True

extra = tr(TR.DECKS_IT_HAS_CARD, count=count)
if askUser(
f"{tr(TR.DECKS_ARE_YOU_SURE_YOU_WISH_TO, val=deck['name'])} {extra}"
):
return True
return False

def _delete(self, did: int) -> None:
if self.ask_delete_deck(did):

def do_delete() -> None:
return self.mw.col.decks.rem(did, True)
def do_delete() -> int:
return self.mw.col.decks.remove([did])

def on_done(fut: Future) -> None:
self.mw.update_undo_actions()
self.show()
res = fut.result() # Required to check for errors
def on_done(fut: Future) -> None:
self.mw.update_undo_actions()
self.show()
tooltip(tr(TR.BROWSING_CARDS_DELETED, count=fut.result()))

self.mw.taskman.with_progress(do_delete, on_done)
self.mw.taskman.with_progress(do_delete, on_done)

# Top buttons
######################################################################
Expand Down
2 changes: 2 additions & 0 deletions qt/aqt/forms/icons.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
<file>icons/clock.svg</file>
<file>icons/card-state.svg</file>
<file>icons/flag.svg</file>
<file>icons/select.svg</file>
<file>icons/magnifying_glass.svg</file>
</qresource>
</RCC>
84 changes: 84 additions & 0 deletions qt/aqt/forms/icons/magnifying_glass.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 71789eb

Please sign in to comment.