From 2d0e74ee5f9ed3e8c61c97b9d59f6163ea79fc5c Mon Sep 17 00:00:00 2001 From: Glutanimate Date: Wed, 23 Aug 2017 23:53:57 +0200 Subject: [PATCH 1/2] Convenience function to assign function a button, bridge cmd & shortcut This is meant to more closely replicate Anki 2.0.x`s _addButton method than the current one does. Its primary purpose is to reduce the boilerplate code needed for add-on authors to implement a new button alongside its hotkey. --- aqt/editor.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/aqt/editor.py b/aqt/editor.py index e96274fa4..8eae68c1d 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -125,6 +125,18 @@ class Editor: data64 = b''.join(base64.encodestring(data).splitlines()) return 'data:%s;base64,%s' % (mime, data64.decode('ascii')) + def addButton(self, icon, cmd, func, tip="", label="", + id=None, toggleable=False, keys=None): + """Assign func to bridge cmd, register shortcut, return button""" + if cmd not in self._links: + self._links[cmd] = func + if keys: + s = QShortcut(QKeySequence(keys), self.widget, + activated = lambda s=self: func(s)) + btn = self._addButton(icon, cmd, tip=tip, label=label, + id=id, toggleable=toggleable) + return btn + def _addButton(self, icon, cmd, tip="", id=None, toggleable=False): if os.path.isabs(icon): iconstr = self.resourceToData(icon) From e0038f8003011edc3ac749293b3e1db43c6e6867 Mon Sep 17 00:00:00 2001 From: Glutanimate Date: Thu, 24 Aug 2017 00:17:28 +0200 Subject: [PATCH 2/2] No need to assign QShortcut to variable --- aqt/editor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aqt/editor.py b/aqt/editor.py index 8eae68c1d..aea9b2ffb 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -131,8 +131,8 @@ class Editor: if cmd not in self._links: self._links[cmd] = func if keys: - s = QShortcut(QKeySequence(keys), self.widget, - activated = lambda s=self: func(s)) + QShortcut(QKeySequence(keys), self.widget, + activated = lambda s=self: func(s)) btn = self._addButton(icon, cmd, tip=tip, label=label, id=id, toggleable=toggleable) return btn