From 21b5aba0f81fcc855e226512f1883f58ad9c5121 Mon Sep 17 00:00:00 2001 From: Herman Sheremetyev Date: Sun, 25 Nov 2012 19:02:00 +0900 Subject: [PATCH] Couple of small improvements to make it easier to add new toolbar buttons. * Force icon size to 16x16, otherwise they blow up the toolbar. * Refactor button callbacks into a dict instead of the "case" statement to make it easier to add new callbacks for buttons from within plugins. --- aqt/toolbar.py | 55 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/aqt/toolbar.py b/aqt/toolbar.py index 5411062dc..70851c54f 100644 --- a/aqt/toolbar.py +++ b/aqt/toolbar.py @@ -3,7 +3,6 @@ # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html from aqt.qt import * -from aqt.webview import AnkiWebView class Toolbar(object): @@ -13,6 +12,14 @@ class Toolbar(object): self.web.page().mainFrame().setScrollBarPolicy( Qt.Vertical, Qt.ScrollBarAlwaysOff) self.web.setLinkHandler(self._linkHandler) + self.link_handlers = { + "decks": self._deckLinkHandler, + "study": self._studyLinkHandler, + "add": self._addLinkHandler, + "browse": self._browseLinkHandler, + "stats": self._studyLinkHandler, + "sync": self._syncLinkHandler, + } def draw(self): self.web.stdHtml(self._body % ( @@ -52,34 +59,42 @@ class Toolbar(object): def _rightIcons(self): buf = "" for ln, icon, title in self._rightIconsList(): - buf += '' % ( + buf += '' % ( title, ln, icon) return buf # Link handling ###################################################################### - def _linkHandler(self, l): + def _linkHandler(self, link): # first set focus back to main window, or we're left with an ugly # focus ring around the clicked item self.mw.web.setFocus() - if l == "decks": - self.mw.moveToState("deckBrowser") - elif l == "study": - # if overview already shown, switch to review - if self.mw.state == "overview": - self.mw.col.startTimebox() - self.mw.moveToState("review") - else: - self.mw.onOverview() - elif l == "add": - self.mw.onAddCard() - elif l == "browse": - self.mw.onBrowse() - elif l == "stats": - self.mw.onStats() - elif l == "sync": - self.mw.onSync() + if link in self.link_handlers: + self.link_handlers[link]() + + def _deckLinkHandler(self): + self.mw.moveToState("deckBrowser") + + def _studyLinkHandler(self): + # if overview already shown, switch to review + if self.mw.state == "overview": + self.mw.col.startTimebox() + self.mw.moveToState("review") + else: + self.mw.onOverview() + + def _addLinkHandler(self): + self.mw.onAddCard() + + def _browseLinkHandler(self): + self.mw.onBrowse() + + def _statsLinkHandler(self): + self.mw.onStats() + + def _syncLinkHandler(self): + self.mw.onSync() # HTML & CSS ######################################################################