Created
August 23, 2017 21:59
-
-
Save glutanimate/13df7e7f15f01c10fba71906f2beb0e5 to your computer and use it in GitHub Desktop.
Small test add-on for a few pull requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
from aqt.qt import * | |
from anki.hooks import addHook | |
from aqt.utils import shortcut | |
from aqt.editor import Editor | |
def _addButton(self, icon, cmd, tip="", label="", id=None, toggleable=False): | |
if icon: | |
if os.path.isabs(icon): | |
iconstr = self.resourceToData(icon) | |
else: | |
iconstr = "/_anki/imgs/{}.png".format(icon) | |
imgelm = '''<img class=topbut src="{}">'''.format(iconstr) | |
else: | |
imgelm = "" | |
if label or not imgelm: | |
labelelm = '''<span class=blabel>{}</span>'''.format(label or cmd) | |
else: | |
labelelm = "" | |
if id: | |
idstr = 'id={}'.format(id) | |
else: | |
idstr = "" | |
if toggleable: | |
toggleScript = 'toggleEditorButton(this);' | |
else: | |
toggleScript = '' | |
tip = shortcut(tip) | |
return ('''<button tabindex=-1 {id} class=linkb type="button" title="{tip}"''' | |
''' onclick="pycmd('{cmd}');{togglesc}return false;">''' | |
'''{imgelm}{labelelm}</button>'''.format( | |
imgelm=imgelm, cmd=cmd, tip=tip, labelelm=labelelm, id=idstr, | |
togglesc=toggleScript) | |
) | |
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: | |
print(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 testFunc(editor): | |
print("testFunc") | |
def onSetupEditorButtons(btns, self): | |
new = self.addButton(None, "dostuff", testFunc, label="test", keys="F6") | |
btns.append(new) | |
new = self._addButton(None, "mycmd", "tooltip", "DM") | |
btns.append(new) | |
return btns | |
addHook("setupEditorButtons", onSetupEditorButtons) | |
Editor.addButton = addButton | |
Editor._addButton = _addButton |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment