Comments editor: Fix a regression when switching between normal and source view

This commit is contained in:
Kovid Goyal 2020-01-23 16:07:57 +05:30
parent 7a59f3a024
commit 3e238e8ba7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 21 additions and 15 deletions

View File

@ -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

View File

@ -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'))

View File

@ -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([])