Merge pull request #452 from Arthur-Milchior/noDefaultDeckInBrowser

No default deck in browser
This commit is contained in:
Damien Elmes 2020-02-21 12:10:36 +10:00 committed by GitHub
commit b93d30795c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 14 deletions

View File

@ -92,6 +92,16 @@ defaultConf = {
"usn": 0, "usn": 0,
} }
# How to select list of decks
# difference between WITHOUT_EMPTY_DEFAULT and
# WITHOUT_EMPTY_LEAF_DEFAULT is that the first contains default when
# it has a child. This is required for deck tree.
WITHOUT_EMPTY_DEFAULT = 0
ALL_DECKS = 1
WITHOUT_EMPTY_LEAF_DEFAULT = 2
class DeckManager: class DeckManager:
decks: Dict[str, Any] decks: Dict[str, Any]
@ -219,7 +229,7 @@ class DeckManager:
self.select(int(list(self.decks.keys())[0])) self.select(int(list(self.decks.keys())[0]))
self.save() self.save()
def allNames(self, dyn: bool = True, forceDefault: bool = True) -> List: def allNames(self, dyn: bool = True, forceDefault: int = ALL_DECKS) -> List:
"An unsorted list of all deck names." "An unsorted list of all deck names."
if dyn: if dyn:
return [x["name"] for x in self.all(forceDefault=forceDefault)] return [x["name"] for x in self.all(forceDefault=forceDefault)]
@ -228,14 +238,10 @@ class DeckManager:
x["name"] for x in self.all(forceDefault=forceDefault) if not x["dyn"] x["name"] for x in self.all(forceDefault=forceDefault) if not x["dyn"]
] ]
def all(self, forceDefault: bool = True) -> List: def all(self, forceDefault: int = ALL_DECKS) -> List:
"A list of all decks." "A list of all decks."
decks = list(self.decks.values()) decks = list(self.decks.values())
if ( if not forceDefault and not self.shouldDefaultBeDisplayed(forceDefault):
not forceDefault
and not self.col.db.scalar("select 1 from cards where did = 1 limit 1")
and len(decks) > 1
):
decks = [deck for deck in decks if deck["id"] != 1] decks = [deck for deck in decks if deck["id"] != 1]
return decks return decks
@ -513,6 +519,35 @@ class DeckManager:
self._recoverOrphans() self._recoverOrphans()
self._checkDeckTree() self._checkDeckTree()
def shouldDeckBeDisplayed(self, deck, forceDefault: int = ALL_DECKS) -> bool:
"""Whether the deck should appear in main window, browser side list, filter, deck selection...
True, except for empty default deck without children"""
if deck["id"] != "1":
return True
return self.shouldDefaultBeDisplayed(forceDefault)
def shouldDefaultBeDisplayed(
self, forceDefault: int = ALL_DECKS, defaultDeck=None
) -> bool:
"""Whether the default deck should appear in main window, browser side list, filter, deck selection...
True, except for empty default deck (without children)"""
if forceDefault == ALL_DECKS:
return True
if self.col.db.scalar("select 1 from cards where did = 1 limit 1"):
return True
if len(self.decks) == 1:
return True
if forceDefault == WITHOUT_EMPTY_LEAF_DEFAULT:
if defaultDeck is None:
defaultDeck = self.get(1)
defaultName = defaultDeck["name"]
for name in self.allNames():
if name.startswith(f"{defaultName}::"):
return True
return False
# Deck selection # Deck selection
############################################################# #############################################################

View File

@ -21,6 +21,7 @@ from anki import hooks
from anki.cards import Card from anki.cards import Card
from anki.collection import _Collection from anki.collection import _Collection
from anki.consts import * from anki.consts import *
from anki.decks import WITHOUT_EMPTY_DEFAULT
from anki.lang import _, ngettext from anki.lang import _, ngettext
from anki.models import NoteType from anki.models import NoteType
from anki.notes import Note from anki.notes import Note
@ -1156,16 +1157,25 @@ by clicking on one on the left."""
def fillGroups(root, grps, head=""): def fillGroups(root, grps, head=""):
for g in grps: for g in grps:
baseName = g[0]
did = g[1]
children = g[5]
if str(did) == "1" and not children:
if not self.mw.col.decks.shouldDefaultBeDisplayed(
WITHOUT_EMPTY_DEFAULT
):
# No need to test for children, we know there are not
continue
item = SidebarItem( item = SidebarItem(
g[0], baseName,
":/icons/deck.svg", ":/icons/deck.svg",
lambda g=g: self.setFilter("deck", head + g[0]), lambda baseName=baseName: self.setFilter("deck", head + baseName),
lambda expanded, g=g: self.mw.col.decks.collapseBrowser(g[1]), lambda expanded, did=did: self.mw.col.decks.collapseBrowser(did),
not self.mw.col.decks.get(g[1]).get("browserCollapsed", False), not self.mw.col.decks.get(g[1]).get("browserCollapsed", False),
) )
root.addChild(item) root.addChild(item)
newhead = head + g[0] + "::" newhead = head + baseName + "::"
fillGroups(item, g[5], newhead) fillGroups(item, children, newhead)
fillGroups(root, grps) fillGroups(root, grps)
@ -1312,7 +1322,12 @@ by clicking on one on the left."""
subm.addSeparator() subm.addSeparator()
addDecks(subm, children) addDecks(subm, children)
else: else:
parent.addItem(shortname, self._filterFunc("deck", name)) if did != 1 or self.col.decks.shouldDefaultBeDisplayed(
WITHOUT_EMPTY_DEFAULT
# no need to check for children, we know
# there are none in this else branch
):
parent.addItem(shortname, self._filterFunc("deck", name))
# fixme: could rewrite to avoid calculating due # in the future # fixme: could rewrite to avoid calculating due # in the future
alldecks = self.col.sched.deckDueTree() alldecks = self.col.sched.deckDueTree()

View File

@ -3,6 +3,7 @@
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import aqt import aqt
from anki.decks import WITHOUT_EMPTY_LEAF_DEFAULT
from anki.lang import _ from anki.lang import _
from aqt import gui_hooks from aqt import gui_hooks
from aqt.qt import * from aqt.qt import *
@ -51,7 +52,11 @@ class StudyDeck(QDialog):
if title: if title:
self.setWindowTitle(title) self.setWindowTitle(title)
if not names: if not names:
names = sorted(self.mw.col.decks.allNames(dyn=dyn, forceDefault=False)) names = sorted(
self.mw.col.decks.allNames(
dyn=dyn, forceDefault=WITHOUT_EMPTY_LEAF_DEFAULT
)
)
self.nameFunc = None self.nameFunc = None
self.origNames = names self.origNames = names
else: else: