Fixes #1922526 [Template editor: Toggle word-wrap](https://bugs.launchpad.net/calibre/+bug/1922526)
This commit is contained in:
Kovid Goyal 2021-04-05 18:23:14 +05:30
commit 30f6fc5262
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 32 additions and 10 deletions

View File

@ -11,7 +11,7 @@ from qt.core import (Qt, QDialog, QDialogButtonBox, QSyntaxHighlighter, QFont,
QRegExp, QApplication, QTextCharFormat, QColor, QCursor, QRegExp, QApplication, QTextCharFormat, QColor, QCursor,
QIcon, QSize, QPalette, QLineEdit, QByteArray, QFontInfo, QIcon, QSize, QPalette, QLineEdit, QByteArray, QFontInfo,
QFontDatabase, QVBoxLayout, QTableWidget, QTableWidgetItem, QFontDatabase, QVBoxLayout, QTableWidget, QTableWidgetItem,
QComboBox, QAbstractItemView) QComboBox, QAbstractItemView, QTextOption)
from calibre import sanitize_file_name from calibre import sanitize_file_name
from calibre.constants import config_dir from calibre.constants import config_dir
@ -187,14 +187,14 @@ class TemplateHighlighter(QSyntaxHighlighter):
QSyntaxHighlighter.rehighlight(self) QSyntaxHighlighter.rehighlight(self)
QApplication.restoreOverrideCursor() QApplication.restoreOverrideCursor()
def check_cursor_pos(self, chr, block, pos_in_block): def check_cursor_pos(self, chr_, block, pos_in_block):
found_pp = -1 found_pp = -1
for i, pp in enumerate(self.paren_positions): for i, pp in enumerate(self.paren_positions):
pp.set_highlight(False) pp.set_highlight(False)
if pp.block == block and pp.pos == pos_in_block: if pp.block == block and pp.pos == pos_in_block:
found_pp = i found_pp = i
if chr not in ['(', ')']: if chr_ not in ['(', ')']:
if self.highlighted_paren: if self.highlighted_paren:
self.rehighlight() self.rehighlight()
self.highlighted_paren = False self.highlighted_paren = False
@ -202,12 +202,12 @@ class TemplateHighlighter(QSyntaxHighlighter):
if found_pp >= 0: if found_pp >= 0:
stack = 0 stack = 0
if chr == '(': if chr_ == '(':
list = self.paren_positions[found_pp+1:] list_ = self.paren_positions[found_pp+1:]
else: else:
list = reversed(self.paren_positions[0:found_pp]) list_ = reversed(self.paren_positions[0:found_pp])
for pp in list: for pp in list_:
if pp.paren == chr: if pp.paren == chr_:
stack += 1 stack += 1
elif stack: elif stack:
stack -= 1 stack -= 1
@ -445,6 +445,10 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
self.load_button.clicked.connect(self.load_template) self.load_button.clicked.connect(self.load_template)
self.save_button.clicked.connect(self.save_template) self.save_button.clicked.connect(self.save_template)
self.textbox.setWordWrapMode(QTextOption.WordWrap)
self.textbox.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.textbox.customContextMenuRequested.connect(self.show_context_menu)
# Now geometry # Now geometry
try: try:
geom = gprefs.get('template_editor_dialog_geometry', None) geom = gprefs.get('template_editor_dialog_geometry', None)
@ -453,6 +457,20 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
except Exception: except Exception:
pass pass
def show_context_menu(self, point):
m = self.textbox.createStandardContextMenu()
m.addSeparator()
ca = m.addAction(_('Toggle word wrap'))
to_what = (QTextOption.NoWrap if self.textbox.wordWrapMode() == QTextOption.WordWrap
else QTextOption.WordWrap)
ca.triggered.connect(lambda: self.textbox.setWordWrapMode(to_what))
m.addSeparator()
ca = m.addAction(_('Load template from file'))
ca.triggered.connect(self.load_template)
ca = m.addAction(_('Save template to file'))
ca.triggered.connect(self.save_template)
m.exec_(self.textbox.mapToGlobal(point))
def load_template(self): def load_template(self):
filename = choose_files(self, 'template_dialog_save_templates', filename = choose_files(self, 'template_dialog_save_templates',
_('Load template from file'), _('Load template from file'),
@ -517,6 +535,8 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
self.toggle_button.setEnabled(new_state != 0) self.toggle_button.setEnabled(new_state != 0)
self.breakpoint_line_box.setEnabled(new_state != 0) self.breakpoint_line_box.setEnabled(new_state != 0)
self.breakpoint_line_box_label.setEnabled(new_state != 0) self.breakpoint_line_box_label.setEnabled(new_state != 0)
if new_state == 0:
self.display_values(unicode_type(self.textbox.toPlainText()))
def go_button_pressed(self): def go_button_pressed(self):
self.display_values(unicode_type(self.textbox.toPlainText())) self.display_values(unicode_type(self.textbox.toPlainText()))
@ -748,7 +768,7 @@ class BreakReporter(QDialog):
bb.rejected.connect(self.reject) bb.rejected.connect(self.reject)
self.setLayout(l) self.setLayout(l)
self.setWindowTitle(_('Book "%s": break on line number %d') % (self.mi.title,line_number)) self.setWindowTitle(_('Break: line {0}, Book {1}').format(line_number, self.mi.title))
local_names = sorted(locals_.keys()) local_names = sorted(locals_.keys())
rows = len(local_names) rows = len(local_names)

View File

@ -48,7 +48,9 @@ class CodeEditor(QPlainTextEdit):
def highlight_cursor_line(self): def highlight_cursor_line(self):
sel = QTextEdit.ExtraSelection() sel = QTextEdit.ExtraSelection()
sel.format.setBackground(self.palette().alternateBase()) # Don't highlight if no text so that the placeholder text shows
if not (self.blockCount() == 1 and len(self.toPlainText().strip()) == 0):
sel.format.setBackground(self.palette().alternateBase())
sel.format.setProperty(QTextFormat.Property.FullWidthSelection, True) sel.format.setProperty(QTextFormat.Property.FullWidthSelection, True)
sel.cursor = self.textCursor() sel.cursor = self.textCursor()
sel.cursor.clearSelection() sel.cursor.clearSelection()