diff --git a/ankiqt/ui/cardlist.py b/ankiqt/ui/cardlist.py index db17db556..6416f9183 100644 --- a/ankiqt/ui/cardlist.py +++ b/ankiqt/ui/cardlist.py @@ -16,6 +16,7 @@ from ankiqt.ui.utils import saveGeom, restoreGeom, saveSplitter, restoreSplitter from anki.errors import * from anki.db import * from anki.stats import CardStats +from anki.hooks import runHook # Deck editor ########################################################################## @@ -396,7 +397,7 @@ class EditDeck(QMainWindow): self.connect(self.dialog.actionSelectFacts, SIGNAL("triggered()"), self.selectFacts) self.connect(self.dialog.actionUndo, SIGNAL("triggered()"), self.onUndo) self.connect(self.dialog.actionRedo, SIGNAL("triggered()"), self.onRedo) - self.parent.runHook('editor.setupMenus', self) + runHook('editor.setupMenus', self) def onClose(self): saveSplitter(self.dialog.splitter, "editor") diff --git a/ankiqt/ui/help.py b/ankiqt/ui/help.py index 4494e2a0e..1ecbc3d79 100644 --- a/ankiqt/ui/help.py +++ b/ankiqt/ui/help.py @@ -5,6 +5,7 @@ import sys from PyQt4.QtGui import * from PyQt4.QtCore import * import ankiqt.forms +from anki.hooks import runHook # Hideable help area widget ########################################################################## @@ -33,7 +34,7 @@ class HelpArea(object): self.helpFrame.hide() self.widget.hide() if self.mainWindow: - self.mainWindow.runHook("helpChanged") + runHook("helpChanged") def showText(self, text, py={}): self.show() @@ -42,7 +43,7 @@ class HelpArea(object): self.handlers = py self.flush() if self.mainWindow: - self.mainWindow.runHook("helpChanged") + runHook("helpChanged") def flush(self): if sys.platform.startswith("darwin"): diff --git a/ankiqt/ui/main.py b/ankiqt/ui/main.py index 8bccdf0a1..38fbb6e18 100644 --- a/ankiqt/ui/main.py +++ b/ankiqt/ui/main.py @@ -18,6 +18,7 @@ from anki.utils import addTags, deleteTags, parseTags from anki.media import rebuildMediaDir from anki.db import OperationalError from anki.stdmodels import BasicModel +from anki.hooks import runHook, addHook, removeHook, _hooks import anki.latex import anki.lang import ankiqt @@ -39,10 +40,7 @@ class AnkiQt(QMainWindow): self.setLang() self.setupFonts() self.setupBackupDir() - self.setupHooks() - self.loadPlugins() self.setupMainWindow() - self.rebuildPluginsMenu() self.alterShortcuts() self.setupTray() self.connectMenuActions() @@ -61,15 +59,17 @@ class AnkiQt(QMainWindow): if not self.maybeLoadLastDeck(args): self.setEnabled(True) self.moveToState("auto") - # run after-init hook - try: - self.runHook('init') - except: - ui.utils.showWarning(_("Broken plugin:\n\n%s") % - traceback.format_exc()) # check for updates self.setupAutoUpdate() self.setupErrorHandler() + self.loadPlugins() + self.rebuildPluginsMenu() + # run after-init hook + try: + runHook('init') + except: + ui.utils.showWarning(_("Broken plugin:\n\n%s") % + traceback.format_exc()) def setupMainWindow(self): self.mainWin = ankiqt.forms.main.Ui_MainWindow() @@ -117,9 +117,17 @@ class AnkiQt(QMainWindow): self.timer.setInterval(interval) def onTimeout(self): + stdText = _("""\ +An error occurred. Please copy the following into a bug report.\n\n""") + pluginText = _("""\ +An error occurred in a plugin. Please contact the plugin author. +Please do not file a bug report with Anki.\n\n""") print self.pool - ui.utils.showText(_("""\ -An error occurred. Please copy the following message into a bug report.\n\n""" + self.pool[0:10000])) + if "plugin" in self.pool: + txt = pluginText + else: + txt = stdText + ui.utils.showText(txt + self.pool[0:10000]) self.pool = "" self.timer = None pipe = ErrorPipe(self) @@ -222,7 +230,7 @@ An error occurred. Please copy the following message into a bug report.\n\n""" + os.chdir(self.deck.mediaDir()) self.showAnswerButton() self.updateMarkAction() - self.runHook('showQuestion') + runHook('showQuestion') elif state == "showAnswer": self.showEaseButtons() self.enableCardMenuItems() @@ -404,29 +412,6 @@ getCard() returns: text = css + '' + text + "" return text - # Hooks - ########################################################################## - - def setupHooks(self): - self.hooks = {} - - def addHook(self, hookName, func): - if not self.hooks.get(hookName, None): - self.hooks[hookName] = [] - if func not in self.hooks[hookName]: - self.hooks[hookName].append(func) - - def removeHook(self, hookName, func): - hook = self.hooks.get(hookName, []) - if func in hook: - hook.remove(func) - - def runHook(self, hookName, *args): - hook = self.hooks.get(hookName, None) - if hook: - for func in hook: - func(*args) - # Deck loading & saving: backend ########################################################################## @@ -761,7 +746,7 @@ Error was:\n%(f1)s\n...\n%(f2)s""") % {'f1': fmt1, 'f2': fmt2}) def prepareForExit(self): "Save config and window geometry." - self.runHook('quit') + runHook("quit") self.help.hide() self.config['mainWindowGeom'] = self.saveGeometry() # save config @@ -925,7 +910,7 @@ Error was:\n%(f1)s\n...\n%(f2)s""") % {'f1': fmt1, 'f2': fmt2}) self.help.showText(txt) def onCardStats(self): - self.addHook("showQuestion", self.onCardStats) + addHook("showQuestion", self.onCardStats) txt = "" if self.currentCard: txt += _("

Current card

") @@ -939,8 +924,7 @@ Error was:\n%(f1)s\n...\n%(f2)s""") % {'f1': fmt1, 'f2': fmt2}) def removeCardStatsHook(self): "Remove the update hook if the help menu was changed." - print "rem" - self.removeHook("showQuestion", self.onCardStats) + removeHook("showQuestion", self.onCardStats) def onShowGraph(self): self.setStatus(_("Loading graphs (may take time)...")) @@ -1669,3 +1653,6 @@ tag or delete references to missing files?""")) "%(b)d unused files removed.") % { 'a': missing, 'b': unused}) + + def addHook(self, *args): + addHook(*args) diff --git a/ankiqt/ui/tray.py b/ankiqt/ui/tray.py index d15a93375..615738ce4 100644 --- a/ankiqt/ui/tray.py +++ b/ankiqt/ui/tray.py @@ -2,6 +2,7 @@ # License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html from PyQt4 import QtGui, QtCore +from anki.hooks import addHook Qt = QtCore.Qt class AnkiTrayIcon(QtCore.QObject): @@ -21,7 +22,7 @@ class AnkiTrayIcon(QtCore.QObject): self.ti.setObjectName("trayIcon") if self.ti: QtGui.QApplication.setQuitOnLastWindowClosed(False) - self.mw.addHook("quit", self.onQuit) + addHook("quit", self.onQuit) self.ti.setIcon(QtGui.QIcon(":/icons/anki.png")) self.ti.setToolTip("Anki") # hook signls, and Anki state changes