From 3e238e8ba76850ce5050364dd69d7ad1fcabcaeb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 23 Jan 2020 16:07:57 +0530 Subject: [PATCH] Comments editor: Fix a regression when switching between normal and source view --- src/calibre/gui2/comments_editor.py | 4 ++-- src/calibre/gui2/tweak_book/widgets.py | 15 ++------------- src/calibre/gui2/widgets2.py | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/calibre/gui2/comments_editor.py b/src/calibre/gui2/comments_editor.py index 7dcc909d6a..79c0952fc2 100644 --- a/src/calibre/gui2/comments_editor.py +++ b/src/calibre/gui2/comments_editor.py @@ -25,6 +25,7 @@ from calibre.ebooks.chardet import xml_to_unicode from calibre.gui2 import NO_URL_FORMATTING, choose_files, error_dialog, gprefs from calibre.gui2.book_details import css from calibre.gui2.widgets import LineEditECM +from calibre.gui2.widgets2 import to_plain_text from calibre.utils.config import tweaks from calibre.utils.imghdr import what from polyglot.builtins import filter, iteritems, itervalues, unicode_type @@ -1138,8 +1139,7 @@ class Editor(QWidget): # {{{ self.wyswyg_dirty = False elif index == 0: # changing to wyswyg if self.source_dirty: - from calibre.gui2.tweak_book.widgets import PlainTextEdit - self.editor.html = PlainTextEdit.toPlainText(self.code_edit) + self.editor.html = to_plain_text(self.code_edit) self.source_dirty = False @property diff --git a/src/calibre/gui2/tweak_book/widgets.py b/src/calibre/gui2/tweak_book/widgets.py index 6eed3d02b0..1135135fff 100644 --- a/src/calibre/gui2/tweak_book/widgets.py +++ b/src/calibre/gui2/tweak_book/widgets.py @@ -22,14 +22,13 @@ from calibre.ebooks.oeb.polish.cover import get_raster_cover_name from calibre.ebooks.oeb.polish.utils import lead_text, guess_type from calibre.gui2 import error_dialog, choose_files, choose_save_file, info_dialog, choose_images from calibre.gui2.tweak_book import tprefs, current_container -from calibre.gui2.widgets2 import Dialog as BaseDialog, HistoryComboBox +from calibre.gui2.widgets2 import Dialog as BaseDialog, HistoryComboBox, to_plain_text, PARAGRAPH_SEPARATOR from calibre.utils.icu import primary_sort_key, sort_key, primary_contains, numeric_sort_key from calibre.utils.matcher import get_char, Matcher from calibre.gui2.complete2 import EditWithComplete from polyglot.builtins import iteritems, unicode_type, zip, getcwd, filter as ignore_me ROOT = QModelIndex() -PARAGRAPH_SEPARATOR = '\u2029' ignore_me @@ -1199,17 +1198,7 @@ class PlainTextEdit(QPlainTextEdit): # {{{ self.syntax = None def toPlainText(self): - # QPlainTextEdit's toPlainText implementation replaces nbsp with normal - # space, so we re-implement it using QTextCursor, which does not do - # that - c = self.textCursor() - c.clearSelection() - c.movePosition(c.Start) - c.movePosition(c.End, c.KeepAnchor) - ans = c.selectedText().replace(PARAGRAPH_SEPARATOR, '\n') - # QTextCursor pads the return value of selectedText with null bytes if - # non BMP characters such as 0x1f431 are present. - return ans.rstrip('\0') + return to_plain_text(self) def selected_text_from_cursor(self, cursor): return unicodedata.normalize('NFC', unicode_type(cursor.selectedText()).replace(PARAGRAPH_SEPARATOR, '\n').rstrip('\0')) diff --git a/src/calibre/gui2/widgets2.py b/src/calibre/gui2/widgets2.py index 9f2401beb4..c489597e35 100644 --- a/src/calibre/gui2/widgets2.py +++ b/src/calibre/gui2/widgets2.py @@ -511,6 +511,23 @@ class ScrollingTabWidget(QTabWidget): return QTabWidget.addTab(self, self.wrap_widget(page), *args) +PARAGRAPH_SEPARATOR = '\u2029' + + +def to_plain_text(self): + # QPlainTextEdit's toPlainText implementation replaces nbsp with normal + # space, so we re-implement it using QTextCursor, which does not do + # that + c = self.textCursor() + c.clearSelection() + c.movePosition(c.Start) + c.movePosition(c.End, c.KeepAnchor) + ans = c.selectedText().replace(PARAGRAPH_SEPARATOR, '\n') + # QTextCursor pads the return value of selectedText with null bytes if + # non BMP characters such as 0x1f431 are present. + return ans.rstrip('\0') + + if __name__ == '__main__': from calibre.gui2 import Application app = Application([])