diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index a4a97b78d9..b617241272 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -1844,6 +1844,13 @@ class Boss(QObject): if ed is not None: ed.paste() + def toggle_line_wrapping_in_all_editors(self): + tprefs['editor_line_wrap'] ^= True + yes = tprefs['editor_line_wrap'] + for ed in editors.values(): + if getattr(ed, 'editor', None) and hasattr(ed.editor, 'apply_line_wrap_mode'): + ed.editor.apply_line_wrap_mode(yes) + def editor_data_changed(self, editor): self.gui.preview.start_refresh_timer() for name, ed in iteritems(editors): diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index d8981fb741..2249dae1f2 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -226,10 +226,13 @@ class TextEdit(PlainTextEdit): def sizeHint(self): return self.size_hint + def apply_line_wrap_mode(self, yes: bool = True) -> None: + self.setLineWrapMode(QPlainTextEdit.LineWrapMode.WidgetWidth if yes else QPlainTextEdit.LineWrapMode.NoWrap) + def apply_settings(self, prefs=None, dictionaries_changed=False): # {{{ prefs = prefs or tprefs self.setAcceptDrops(prefs.get('editor_accepts_drops', True)) - self.setLineWrapMode(QPlainTextEdit.LineWrapMode.WidgetWidth if prefs['editor_line_wrap'] else QPlainTextEdit.LineWrapMode.NoWrap) + self.apply_line_wrap_mode(prefs['editor_line_wrap']) with suppress(Exception): self.setCursorWidth(int(prefs.get('editor_cursor_width', 1))) theme = get_theme(prefs['editor_theme']) diff --git a/src/calibre/gui2/tweak_book/ui.py b/src/calibre/gui2/tweak_book/ui.py index cfaab76354..e4bbda5ebe 100644 --- a/src/calibre/gui2/tweak_book/ui.py +++ b/src/calibre/gui2/tweak_book/ui.py @@ -411,6 +411,9 @@ class Main(MainWindow): self.boss.import_book, 'import-book', (), _('Import an HTML or DOCX file as a new book')) self.action_quick_edit = treg('modified.png', _('&Quick open a file to edit'), self.boss.quick_open, 'quick-open', ('Ctrl+T'), _( 'Quickly open a file from the book to edit it')) + self.action_editor_toggle_wrap = treg( + 'format-justify-fill.png', _('Toggle code line &wrapping'), self.boss.toggle_line_wrapping_in_all_editors, 'editor-toggle-wrap', (), _( + 'Toggle line wrapping in all code editor tabs')) # Editor actions group = _('Editor actions')