From 37e345ffe8d6e199fa542bbfe996d000516e74ec Mon Sep 17 00:00:00 2001 From: Simone Gaiarin Date: Fri, 6 Jan 2017 15:56:35 +0100 Subject: [PATCH 1/6] Replace _addButton method with HTML based one Support loading icon from anki qrc resource file or using the absolute path --- aqt/editor.py | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/aqt/editor.py b/aqt/editor.py index f3f26971b..ecb7e77bb 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -467,34 +467,13 @@ class Editor(object): # Top buttons ###################################################################### - def _addButton(self, name, func, key=None, tip=None, size=True, text="", - check=False, native=False, canDisable=True): - b = QPushButton(text) - if check: - b.clicked["bool"].connect(func) + def _addButton(self, icon, cmd, tip=""): + if os.path.isabs(icon): + iconstr = icon else: - b.clicked.connect(func) - if size: - b.setFixedHeight(20) - b.setFixedWidth(20) - if not native: - if self.plastiqueStyle: - b.setStyle(self.plastiqueStyle) - b.setFocusPolicy(Qt.NoFocus) - else: - b.setAutoDefault(False) - if not text: - b.setIcon(QIcon(":/icons/%s.png" % name)) - if key: - b.setShortcut(QKeySequence(key)) - if tip: - b.setToolTip(shortcut(tip)) - if check: - b.setCheckable(True) - self.iconsBox.addWidget(b) - if canDisable: - self._buttons[name] = b - return b + iconstr = "qrc:/icons/{}.png".format(icon) + return ''''''.format(icon=iconstr, cmd=cmd, tip=_(tip)) def setupShortcuts(self): cuts = [ From 5051dcedb684a3ba8d855169d9fdce8fdc2a0198 Mon Sep 17 00:00:00 2001 From: Simone Gaiarin Date: Fri, 6 Jan 2017 15:54:55 +0100 Subject: [PATCH 2/6] Add anki buttons using the new method The html of each anki button is stored in a list that is joined in a string at the end. This make it easier to run the filter. Drawback: Only buttons on the right side of the editor can be added using the filter. --- aqt/editor.py | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/aqt/editor.py b/aqt/editor.py index ecb7e77bb..b6a48439d 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -438,26 +438,35 @@ class Editor(object): self.outerLayout.addWidget(self.web, 1) self.web.onLoadFinished = self._loadFinished + righttopbtns = list() + righttopbtns.append(self._addButton('text_bold', 'bold', "Bold text (Ctrl+B)")) + righttopbtns.append(self._addButton('text_italic', 'italic', "Italic text (Ctrl+I)")) + righttopbtns.append(self._addButton('text_under', 'underline', "Underline text (Ctrl+U)")) + righttopbtns.append(self._addButton('text_super', 'super', "Superscript (Ctrl+Shift+=)")) + righttopbtns.append(self._addButton('text_sub', 'sub', "Subscript (Ctrl+=)")) + righttopbtns.append(self._addButton('text_clear', 'clear', "Remove formatting (Ctrl+R)")) + # The color selection buttons do not use an icon so the HTML must be specified manually + righttopbtns.append('''''') + righttopbtns.append('''''') + righttopbtns.append(self._addButton('text_cloze', 'cloze', "Cloze deletion (Ctrl+Shift+C)")) + righttopbtns.append(self._addButton('paperclip', 'attach', "Attach pictures/audio/video (F3)")) + righttopbtns.append(self._addButton('media-record', 'record', "Record audio (F5)")) + righttopbtns.append(self._addButton('more', 'more')) topbuts = """ -
- - -
-
- - - - - - - - - - - - -
- """ % dict(flds=_("Fields"), cards=_("Cards")) +
+ + +
+
+ %(rightbts)s +
+ """ % dict(flds=_("Fields"), cards=_("Cards"), rightbts="".join(righttopbtns)) topbuts = runFilter("setupEditorButtons", topbuts) self.web.stdHtml(_html % ( self.mw.baseHTML(), anki.js.jquery, From 4e4947bd92d8d140648abfe3f7f1b1c9b262da3a Mon Sep 17 00:00:00 2001 From: Simone Gaiarin Date: Fri, 6 Jan 2017 16:37:57 +0100 Subject: [PATCH 3/6] Run filter on right buttons list --- aqt/editor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aqt/editor.py b/aqt/editor.py index b6a48439d..9f0aeb23c 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -458,6 +458,7 @@ class Editor(object): righttopbtns.append(self._addButton('paperclip', 'attach', "Attach pictures/audio/video (F3)")) righttopbtns.append(self._addButton('media-record', 'record', "Record audio (F5)")) righttopbtns.append(self._addButton('more', 'more')) + righttopbtns = runFilter("setupEditorButtons", righttopbtns, self) topbuts = """
@@ -467,7 +468,6 @@ class Editor(object): %(rightbts)s
""" % dict(flds=_("Fields"), cards=_("Cards"), rightbts="".join(righttopbtns)) - topbuts = runFilter("setupEditorButtons", topbuts) self.web.stdHtml(_html % ( self.mw.baseHTML(), anki.js.jquery, topbuts, From abc3224fe390f31e05ad3ede5d8b0e18d4360a7e Mon Sep 17 00:00:00 2001 From: Simone Gaiarin Date: Fri, 6 Jan 2017 15:56:20 +0100 Subject: [PATCH 4/6] Remove old tooltips --- aqt/editor.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/aqt/editor.py b/aqt/editor.py index 9f0aeb23c..3a3052d9b 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -509,21 +509,6 @@ class Editor(object): for keys, fn in cuts: QShortcut(QKeySequence(keys), self.widget, activated=fn) - # fixme: need to add back hover labels for toolbuttons - # def setupButtons(self): - # _("Customize Cards (Ctrl+L)") - # _("Bold text (Ctrl+B)"), - # _("Italic text (Ctrl+I)"), - # _("Underline text (Ctrl+U)") - # _("Superscript (Ctrl+Shift+=)") - # _("Subscript (Ctrl+=)") - # _("Remove formatting (Ctrl+R)") - # _("Set foreground colour (F7)") - # _("Change colour (F8)") - # _("Cloze deletion (Ctrl+Shift+C)") - # _("Attach pictures/audio/video (F3)") - # _("Record audio (F5)") - def onFields(self): self.saveNow(self._onFields) From 1c8c34b4430dea4fafeb427dc57a20ae0b292d9f Mon Sep 17 00:00:00 2001 From: Simone Gaiarin Date: Fri, 6 Jan 2017 16:39:20 +0100 Subject: [PATCH 5/6] Add padding among buttons For some reason after using the new _addButton method the padding among the buttons is not there any more. So I add it manually. --- aqt/editor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aqt/editor.py b/aqt/editor.py index 3a3052d9b..bb2c22ef4 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -50,7 +50,7 @@ background-image: -webkit-gradient(linear, left top, left bottom, color-stop(50%%, #7f7), color-stop(100%%, #77f)); } -.linkb { -webkit-appearance: none; border: 0; padding: 0px; background: transparent; } +.linkb { -webkit-appearance: none; border: 0; padding: 0px 2px; background: transparent; } .linkb:disabled { opacity: 0.3; cursor: not-allowed; } .highlighted { From 42ea7c53dd0e33dc225eacbc906529a8159d0488 Mon Sep 17 00:00:00 2001 From: Simone Gaiarin Date: Fri, 6 Jan 2017 16:40:10 +0100 Subject: [PATCH 6/6] Add optional id in parameters --- aqt/editor.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/aqt/editor.py b/aqt/editor.py index bb2c22ef4..de3d39616 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -439,11 +439,11 @@ class Editor(object): self.web.onLoadFinished = self._loadFinished righttopbtns = list() - righttopbtns.append(self._addButton('text_bold', 'bold', "Bold text (Ctrl+B)")) - righttopbtns.append(self._addButton('text_italic', 'italic', "Italic text (Ctrl+I)")) - righttopbtns.append(self._addButton('text_under', 'underline', "Underline text (Ctrl+U)")) - righttopbtns.append(self._addButton('text_super', 'super', "Superscript (Ctrl+Shift+=)")) - righttopbtns.append(self._addButton('text_sub', 'sub', "Subscript (Ctrl+=)")) + righttopbtns.append(self._addButton('text_bold', 'bold', "Bold text (Ctrl+B)", id='bold')) + righttopbtns.append(self._addButton('text_italic', 'italic', "Italic text (Ctrl+I)", id='italic')) + righttopbtns.append(self._addButton('text_under', 'underline', "Underline text (Ctrl+U)", 'underline')) + righttopbtns.append(self._addButton('text_super', 'super', "Superscript (Ctrl+Shift+=)", 'superscipt')) + righttopbtns.append(self._addButton('text_sub', 'sub', "Subscript (Ctrl+=)", id='subscript')) righttopbtns.append(self._addButton('text_clear', 'clear', "Remove formatting (Ctrl+R)")) # The color selection buttons do not use an icon so the HTML must be specified manually righttopbtns.append(''''''.format(icon=iconstr, cmd=cmd, tip=_(tip)) + if id: + idstr = 'id={}'.format(id) + else: + idstr = "" + return ''''''.format(icon=iconstr, cmd=cmd, tip=_(tip), id=idstr) def setupShortcuts(self): cuts = [