Top row of comments editor implemented

This commit is contained in:
Kovid Goyal 2019-07-01 14:55:37 +05:30
parent 69ad2e2984
commit 943f5c00b1
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -10,11 +10,12 @@ import weakref
from html5_parser import parse from html5_parser import parse
from lxml import html from lxml import html
from PyQt5.Qt import ( from PyQt5.Qt import (
QAction, QApplication, QByteArray, QCheckBox, QColor, QColorDialog, QDialog, QAction, QApplication, QBrush, QByteArray, QCheckBox, QColor, QColorDialog,
QDialogButtonBox, QFontInfo, QFormLayout, QHBoxLayout, QIcon, QKeySequence, QDialog, QDialogButtonBox, QFontInfo, QFormLayout, QHBoxLayout, QIcon,
QLabel, QLineEdit, QMenu, QPlainTextEdit, QPushButton, QSize, QSyntaxHighlighter, QKeySequence, QLabel, QLineEdit, QMenu, QPlainTextEdit, QPushButton, QSize,
Qt, QTabWidget, QTextCursor, QTextEdit, QToolBar, QUrl, QVBoxLayout, QWidget, QSyntaxHighlighter, Qt, QTabWidget, QTextBlockFormat, QTextCharFormat,
pyqtSignal, pyqtSlot QTextCursor, QTextEdit, QToolBar, QUrl, QVBoxLayout, QWidget, pyqtSignal,
pyqtSlot
) )
from calibre import prepare_string_for_xml, xml_replace_entities from calibre import prepare_string_for_xml, xml_replace_entities
@ -122,8 +123,25 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
connect_lambda(ac.triggered, self, lambda self: self.do_format_block(name)) connect_lambda(ac.triggered, self, lambda self: self.do_format_block(name))
self.setHtml('') self.setHtml('')
self.copyAvailable.connect(self.update_clipboard_actions)
self.update_clipboard_actions(False)
self.selectionChanged.connect(self.update_selection_based_actions)
self.update_selection_based_actions()
connect_lambda(self.undoAvailable, self, lambda self, yes: self.action_undo.setEnabled(yes))
connect_lambda(self.redoAvailable, self, lambda self, yes: self.action_redo.setEnabled(yes))
self.action_undo.setEnabled(False), self.action_redo.setEnabled(False)
# self.textChanged.connect(self.update_action_state)
# self.cursorPositionChanged.connect(self.update_action_state)
self.textChanged.connect(self.data_changed) self.textChanged.connect(self.data_changed)
def update_clipboard_actions(self, copy_available):
self.action_copy.setEnabled(copy_available)
self.action_cut.setEnabled(copy_available)
def update_selection_based_actions(self):
has_selection = self.textCursor().hasSelection()
self.action_remove_format.setEnabled(has_selection)
def set_readonly(self, what): def set_readonly(self, what):
self.readonly = what self.readonly = what
@ -178,21 +196,34 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
def do_undo(self): def do_undo(self):
self.undo() self.undo()
self.focus_self()
def do_redo(self): def do_redo(self):
self.redo() self.redo()
self.focus_self()
def do_remove_format(self): def do_remove_format(self):
raise NotImplementedError('TODO') c = self.textCursor()
c.beginEditBlock()
c.setBlockFormat(QTextBlockFormat())
text = c.selectedText()
c.removeSelectedText()
c.insertText(text)
c.endEditBlock()
self.setTextCursor(c)
self.focus_self()
def do_copy(self): def do_copy(self):
self.copy() self.copy()
self.focus_self()
def do_paste(self): def do_paste(self):
self.paste() self.paste()
self.focus_self()
def do_cut(self): def do_cut(self):
self.cut() self.cut()
self.focus_self()
def do_indent(self): def do_indent(self):
raise NotImplementedError('TODO') raise NotImplementedError('TODO')
@ -201,7 +232,10 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
raise NotImplementedError('TODO') raise NotImplementedError('TODO')
def do_select_all(self): def do_select_all(self):
raise NotImplementedError('TODO') c = self.textCursor()
c.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor)
c.movePosition(QTextCursor.End, QTextCursor.KeepAnchor)
self.setTextCursor(c)
def do_format_block(self, name): def do_format_block(self, name):
raise NotImplementedError('TODO') raise NotImplementedError('TODO')
@ -217,8 +251,11 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
col = QColorDialog.getColor(Qt.white, self, col = QColorDialog.getColor(Qt.white, self,
_('Choose background color'), QColorDialog.ShowAlphaChannel) _('Choose background color'), QColorDialog.ShowAlphaChannel)
if col.isValid(): if col.isValid():
raise NotImplementedError('TODO') fmt = QTextCharFormat()
self.exec_command('hiliteColor', unicode_type(col.name())) fmt.setBackground(QBrush(col))
c = self.textCursor()
c.mergeCharFormat(fmt)
self.setTextCursor(c)
def do_insert_hr(self, *args): def do_insert_hr(self, *args):
raise NotImplementedError('TODO') raise NotImplementedError('TODO')