mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
tidy up deckbrowser code
This commit is contained in:
parent
2897f0a54a
commit
87f5fe8769
1 changed files with 71 additions and 87 deletions
|
@ -3,6 +3,7 @@
|
||||||
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
|
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
|
||||||
|
|
||||||
import time, os, stat, shutil
|
import time, os, stat, shutil
|
||||||
|
from operator import itemgetter
|
||||||
from PyQt4.QtCore import *
|
from PyQt4.QtCore import *
|
||||||
from PyQt4.QtGui import *
|
from PyQt4.QtGui import *
|
||||||
from anki import Deck
|
from anki import Deck
|
||||||
|
@ -46,27 +47,6 @@ class DeckBrowser(object):
|
||||||
self._decks = []
|
self._decks = []
|
||||||
addHook("deckClosing", self.onClose)
|
addHook("deckClosing", self.onClose)
|
||||||
|
|
||||||
def _linkHandler(self, url):
|
|
||||||
(cmd, arg) = url.split(":")
|
|
||||||
if cmd == "open":
|
|
||||||
deck = self._decks[int(arg)]['path']
|
|
||||||
self.mw.loadDeck(deck)
|
|
||||||
elif cmd == "opts":
|
|
||||||
self._optsForRow(int(arg))
|
|
||||||
|
|
||||||
def _keyHandler(self, evt):
|
|
||||||
txt = evt.text()
|
|
||||||
if ((txt >= "0" and txt <= "9") or
|
|
||||||
(txt >= "a" and txt <= "z")):
|
|
||||||
self._openAccel(txt)
|
|
||||||
evt.accept()
|
|
||||||
evt.ignore()
|
|
||||||
|
|
||||||
def _openAccel(self, txt):
|
|
||||||
for d in self._decks:
|
|
||||||
if d['accel'] == txt:
|
|
||||||
self.mw.loadDeck(d['path'])
|
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
self.web.setLinkHandler(self._linkHandler)
|
self.web.setLinkHandler(self._linkHandler)
|
||||||
self.mw.setKeyHandler(self._keyHandler)
|
self.mw.setKeyHandler(self._keyHandler)
|
||||||
|
@ -110,6 +90,33 @@ later by using File>Close.
|
||||||
d['time'] = self.deck._dailyStats.reviewTime
|
d['time'] = self.deck._dailyStats.reviewTime
|
||||||
d['reps'] = self.deck._dailyStats.reps
|
d['reps'] = self.deck._dailyStats.reps
|
||||||
|
|
||||||
|
# Event handlers
|
||||||
|
##########################################################################
|
||||||
|
|
||||||
|
def _linkHandler(self, url):
|
||||||
|
(cmd, arg) = url.split(":")
|
||||||
|
if cmd == "open":
|
||||||
|
deck = self._decks[int(arg)]['path']
|
||||||
|
self.mw.loadDeck(deck)
|
||||||
|
elif cmd == "opts":
|
||||||
|
self._optsForRow(int(arg))
|
||||||
|
|
||||||
|
def _keyHandler(self, evt):
|
||||||
|
txt = evt.text()
|
||||||
|
if ((txt >= "0" and txt <= "9") or
|
||||||
|
(txt >= "a" and txt <= "z")):
|
||||||
|
self._openAccel(txt)
|
||||||
|
evt.accept()
|
||||||
|
evt.ignore()
|
||||||
|
|
||||||
|
def _openAccel(self, txt):
|
||||||
|
for d in self._decks:
|
||||||
|
if d['accel'] == txt:
|
||||||
|
self.mw.loadDeck(d['path'])
|
||||||
|
|
||||||
|
# HTML generation
|
||||||
|
##########################################################################
|
||||||
|
|
||||||
def _deckRow(self, c, max, deck):
|
def _deckRow(self, c, max, deck):
|
||||||
buf = "<tr>"
|
buf = "<tr>"
|
||||||
ok = deck['state'] == 'ok'
|
ok = deck['state'] == 'ok'
|
||||||
|
@ -159,36 +166,6 @@ later by using File>Close.
|
||||||
buf += "<tr><td colspan=4><hr noshade></td></tr>"
|
buf += "<tr><td colspan=4><hr noshade></td></tr>"
|
||||||
return buf
|
return buf
|
||||||
|
|
||||||
def _optsForRow(self, n):
|
|
||||||
m = QMenu(self.mw)
|
|
||||||
# hide
|
|
||||||
a = m.addAction(QIcon(":/icons/edit-undo.png"), _("Hide From List"))
|
|
||||||
a.connect(a, SIGNAL("activated()"), lambda n=n: self._hideRow(n))
|
|
||||||
# delete
|
|
||||||
a = m.addAction(QIcon(":/icons/editdelete.png"), _("Delete"))
|
|
||||||
a.connect(a, SIGNAL("activated()"), lambda n=n: self._deleteRow(n))
|
|
||||||
m.exec_(QCursor.pos())
|
|
||||||
|
|
||||||
def _buttons(self):
|
|
||||||
# refresh = QPushButton(_("Refresh"))
|
|
||||||
# refresh.setToolTip(_("Check due counts again (F5)"))
|
|
||||||
# refresh.setShortcut(_("F5"))
|
|
||||||
# self.connect(refresh, SIGNAL("clicked()"),
|
|
||||||
# self.refresh)
|
|
||||||
# layout.addItem(QSpacerItem(1,20, QSizePolicy.Preferred,
|
|
||||||
# QSizePolicy.Preferred), c+2, 5)
|
|
||||||
# layout.addWidget(refresh, c+3, 5)
|
|
||||||
# more = QPushButton(_("More"))
|
|
||||||
# moreMenu = QMenu()
|
|
||||||
# a = moreMenu.addAction(QIcon(":/icons/edit-undo.png"),
|
|
||||||
# _("Forget Inaccessible Decks"))
|
|
||||||
# a.connect(a, SIGNAL("triggered()"),
|
|
||||||
# self.onDeckBrowserForgetInaccessible)
|
|
||||||
# more.setMenu(moreMenu)
|
|
||||||
# layout.addWidget(more, c+3, 6)
|
|
||||||
# self.moreMenus.append(moreMenu)
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def _summary(self):
|
def _summary(self):
|
||||||
# summarize
|
# summarize
|
||||||
reps = 0
|
reps = 0
|
||||||
|
@ -217,6 +194,47 @@ later by using File>Close.
|
||||||
'rev': rev, 'new': new}
|
'rev': rev, 'new': new}
|
||||||
return line1+'<br>'+line2
|
return line1+'<br>'+line2
|
||||||
|
|
||||||
|
# Options
|
||||||
|
##########################################################################
|
||||||
|
|
||||||
|
def _optsForRow(self, n):
|
||||||
|
m = QMenu(self.mw)
|
||||||
|
# hide
|
||||||
|
a = m.addAction(QIcon(":/icons/edit-undo.png"), _("Hide From List"))
|
||||||
|
a.connect(a, SIGNAL("activated()"), lambda n=n: self._hideRow(n))
|
||||||
|
# delete
|
||||||
|
a = m.addAction(QIcon(":/icons/editdelete.png"), _("Delete"))
|
||||||
|
a.connect(a, SIGNAL("activated()"), lambda n=n: self._deleteRow(n))
|
||||||
|
m.exec_(QCursor.pos())
|
||||||
|
|
||||||
|
def _buttons(self):
|
||||||
|
# refresh = QPushButton(_("Refresh"))
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def _hideRow(self, c):
|
||||||
|
if aqt.utils.askUser(_("""\
|
||||||
|
Hide %s from the list? You can File>Open it again later.""") %
|
||||||
|
self._decks[c]['name']):
|
||||||
|
self.mw.config.delRecentDeck(self._decks[c]['path'])
|
||||||
|
del self._decks[c]
|
||||||
|
self.refresh()
|
||||||
|
|
||||||
|
def _deleteRow(self, c):
|
||||||
|
if aqt.utils.askUser(_("""\
|
||||||
|
Delete %s? If this deck is synchronized the online version will \
|
||||||
|
not be touched.""") % self._decks[c]['name']):
|
||||||
|
deck = self._decks[c]['path']
|
||||||
|
os.unlink(deck)
|
||||||
|
try:
|
||||||
|
shutil.rmtree(re.sub(".anki$", ".media", deck))
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
self.mw.config.delRecentDeck(deck)
|
||||||
|
self.refresh()
|
||||||
|
|
||||||
|
# Data gathering
|
||||||
|
##########################################################################
|
||||||
|
|
||||||
def _checkDecks(self, forget=False):
|
def _checkDecks(self, forget=False):
|
||||||
self._decks = []
|
self._decks = []
|
||||||
decks = self.mw.config.recentDecks()
|
decks = self.mw.config.recentDecks()
|
||||||
|
@ -272,21 +290,8 @@ later by using File>Close.
|
||||||
self._reorderDecks()
|
self._reorderDecks()
|
||||||
|
|
||||||
def _reorderDecks(self):
|
def _reorderDecks(self):
|
||||||
print "reorder decks"
|
# for now, sort by deck name
|
||||||
# return
|
self._decks.sort(key=itemgetter('name'))
|
||||||
# if self.mw.config['deckBrowserOrder'] == 0:
|
|
||||||
# self._decks.sort(key=itemgetter('mod'),
|
|
||||||
# reverse=True)
|
|
||||||
# else:
|
|
||||||
# def custcmp(a, b):
|
|
||||||
# x = cmp(not not b['due'], not not a['due'])
|
|
||||||
# if x:
|
|
||||||
# return x
|
|
||||||
# x = cmp(not not b['new'], not not a['new'])
|
|
||||||
# if x:
|
|
||||||
# return x
|
|
||||||
# return cmp(a['mod'], b['mod'])
|
|
||||||
# self._decks.sort(cmp=custcmp)
|
|
||||||
# after the decks are sorted, assign shortcut keys to them
|
# after the decks are sorted, assign shortcut keys to them
|
||||||
for c, d in enumerate(self._decks):
|
for c, d in enumerate(self._decks):
|
||||||
if c > 35:
|
if c > 35:
|
||||||
|
@ -299,24 +304,3 @@ later by using File>Close.
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
self._browserLastRefreshed = 0
|
self._browserLastRefreshed = 0
|
||||||
self.show()
|
self.show()
|
||||||
|
|
||||||
def _hideRow(self, c):
|
|
||||||
if aqt.utils.askUser(_("""\
|
|
||||||
Hide %s from the list? You can File>Open it again later.""") %
|
|
||||||
self._decks[c]['name']):
|
|
||||||
self.mw.config.delRecentDeck(self._decks[c]['path'])
|
|
||||||
del self._decks[c]
|
|
||||||
self.refresh()
|
|
||||||
|
|
||||||
def _deleteRow(self, c):
|
|
||||||
if aqt.utils.askUser(_("""\
|
|
||||||
Delete %s? If this deck is synchronized the online version will \
|
|
||||||
not be touched.""") % self._decks[c]['name']):
|
|
||||||
deck = self._decks[c]['path']
|
|
||||||
os.unlink(deck)
|
|
||||||
try:
|
|
||||||
shutil.rmtree(re.sub(".anki$", ".media", deck))
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
self.mw.config.delRecentDeck(deck)
|
|
||||||
self.refresh()
|
|
||||||
|
|
Loading…
Reference in a new issue