Skip to content

Commit

Permalink
Edit book: Allow adding tags to the list of tags for the insert tag b…
Browse files Browse the repository at this point in the history
…utton
  • Loading branch information
kovidgoyal committed Nov 24, 2020
1 parent 41d58e5 commit fd798ac
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/calibre/gui2/tweak_book/editor/widget.py
Expand Up @@ -5,25 +5,30 @@
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'

import unicodedata, math
import math
import unicodedata
from functools import partial

from PyQt5.Qt import (
QMainWindow, Qt, QApplication, pyqtSignal, QMenu, qDrawShadeRect, QPainter,
QImage, QColor, QIcon, QPixmap, QToolButton, QAction, QTextCursor, QSize)
QAction, QApplication, QColor, QIcon, QImage, QInputDialog, QMainWindow, QMenu,
QPainter, QPixmap, QSize, Qt, QTextCursor, QToolButton, pyqtSignal,
qDrawShadeRect
)

from calibre import prints
from calibre.constants import DEBUG
from calibre.ebooks.chardet import replace_encoding_declarations
from calibre.gui2.tweak_book import (
actions, current_container, tprefs, dictionaries, editor_toolbar_actions,
editor_name, editors, update_mark_text_action)
from calibre.gui2 import error_dialog, open_url
from calibre.gui2.tweak_book.editor import SPELL_PROPERTY, LINK_PROPERTY, TAG_NAME_PROPERTY, CSS_PROPERTY
from calibre.gui2.tweak_book import (
actions, current_container, dictionaries, editor_name, editor_toolbar_actions,
editors, tprefs, update_mark_text_action
)
from calibre.gui2.tweak_book.editor import (
CSS_PROPERTY, LINK_PROPERTY, SPELL_PROPERTY, TAG_NAME_PROPERTY
)
from calibre.gui2.tweak_book.editor.help import help_url
from calibre.gui2.tweak_book.editor.text import TextEdit
from calibre.utils.icu import utf16_length
from polyglot.builtins import itervalues, unicode_type, string_or_bytes
from polyglot.builtins import itervalues, string_or_bytes, unicode_type


def create_icon(text, palette=None, sz=None, divider=2, fill='white'):
Expand Down Expand Up @@ -244,8 +249,9 @@ def _build_insert_tag_button_menu(self):
names = tprefs['insert_tag_mru']
for name in names:
m.addAction(name, partial(self.insert_tag, name))
m.addSeparator()
m.addAction(_('Add a tag to this menu'), self.add_insert_tag)
if names:
m.addSeparator()
m = m.addMenu(_('Remove from this menu'))
for name in names:
m.addAction(name, partial(self.remove_insert_tag, name))
Expand All @@ -261,6 +267,15 @@ def insert_tag(self, name):
tprefs['insert_tag_mru'] = mru
self._build_insert_tag_button_menu()

def add_insert_tag(self):
name, ok = QInputDialog.getText(self, _('Name of tag to add'), _(
'Enter the name of the tag'))
if ok:
mru = tprefs['insert_tag_mru']
mru.insert(0, name)
tprefs['insert_tag_mru'] = mru
self._build_insert_tag_button_menu()

def remove_insert_tag(self, name):
mru = tprefs['insert_tag_mru']
try:
Expand Down Expand Up @@ -491,7 +506,9 @@ def fix_html(self):
return False

def pretty_print(self, name):
from calibre.ebooks.oeb.polish.pretty import pretty_html, pretty_css, pretty_xml
from calibre.ebooks.oeb.polish.pretty import (
pretty_css, pretty_html, pretty_xml
)
if self.syntax in {'css', 'html', 'xml'}:
func = {'css':pretty_css, 'xml':pretty_xml}.get(self.syntax, pretty_html)
original_text = unicode_type(self.editor.toPlainText())
Expand Down Expand Up @@ -606,9 +623,9 @@ def _nuke_word(self, dic, word, locale):
def launch_editor(path_to_edit, path_is_raw=False, syntax='html', callback=None):
from calibre.gui2 import Application
from calibre.gui2.tweak_book import dictionaries
from calibre.gui2.tweak_book.editor.syntax.html import refresh_spell_check_status
from calibre.gui2.tweak_book.main import option_parser
from calibre.gui2.tweak_book.ui import Main
from calibre.gui2.tweak_book.editor.syntax.html import refresh_spell_check_status
dictionaries.initialize()
refresh_spell_check_status()
opts = option_parser().parse_args([])
Expand Down

0 comments on commit fd798ac

Please sign in to comment.