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.
This commit is contained in:
Glutanimate 2017-08-23 23:53:57 +02:00
parent db5d23d9dc
commit 2d0e74ee5f

View File

@ -125,6 +125,18 @@ class Editor:
data64 = b''.join(base64.encodestring(data).splitlines()) data64 = b''.join(base64.encodestring(data).splitlines())
return 'data:%s;base64,%s' % (mime, data64.decode('ascii')) 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): def _addButton(self, icon, cmd, tip="", id=None, toggleable=False):
if os.path.isabs(icon): if os.path.isabs(icon):
iconstr = self.resourceToData(icon) iconstr = self.resourceToData(icon)