Implement bold and italic toggles

This commit is contained in:
Kovid Goyal 2019-07-02 18:25:31 +05:30
parent 93fc64184e
commit 9c5aaade6f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -13,11 +13,11 @@ from html5_parser import parse
from lxml import html from lxml import html
from PyQt5.Qt import ( from PyQt5.Qt import (
QAction, QApplication, QBrush, QByteArray, QCheckBox, QColor, QColorDialog, QAction, QApplication, QBrush, QByteArray, QCheckBox, QColor, QColorDialog,
QDialog, QDialogButtonBox, QFontInfo, QFontMetrics, QFormLayout, QHBoxLayout, QDialog, QDialogButtonBox, QFont, QFontInfo, QFontMetrics, QFormLayout,
QIcon, QKeySequence, QLabel, QLineEdit, QMenu, QPlainTextEdit, QPushButton, QHBoxLayout, QIcon, QKeySequence, QLabel, QLineEdit, QMenu, QPlainTextEdit,
QSize, QSyntaxHighlighter, Qt, QTabWidget, QTextBlockFormat, QTextCharFormat, QPushButton, QSize, QSyntaxHighlighter, Qt, QTabWidget, QTextBlockFormat,
QTextCursor, QTextEdit, QTextListFormat, QToolBar, QUrl, QVBoxLayout, QWidget, QTextCharFormat, QTextCursor, QTextEdit, QTextListFormat, QToolBar, QUrl,
pyqtSignal, pyqtSlot QVBoxLayout, QWidget, pyqtSignal, pyqtSlot
) )
from calibre import prepare_string_for_xml, xml_replace_entities from calibre import prepare_string_for_xml, xml_replace_entities
@ -28,9 +28,9 @@ from calibre.utils.config import tweaks
from calibre.utils.imghdr import what from calibre.utils.imghdr import what
from polyglot.builtins import filter, iteritems, itervalues, unicode_type from polyglot.builtins import filter, iteritems, itervalues, unicode_type
# Cleanup Qt markup {{{ # Cleanup Qt markup {{{
def parse_style(style): def parse_style(style):
props = filter(None, (x.strip() for x in style.split(';'))) props = filter(None, (x.strip() for x in style.split(';')))
ans = {} ans = {}
@ -283,6 +283,8 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
vert = tcf.verticalAlignment() vert = tcf.verticalAlignment()
self.action_superscript.setChecked(vert == QTextCharFormat.AlignSuperScript) self.action_superscript.setChecked(vert == QTextCharFormat.AlignSuperScript)
self.action_subscript.setChecked(vert == QTextCharFormat.AlignSubScript) self.action_subscript.setChecked(vert == QTextCharFormat.AlignSubScript)
self.action_bold.setChecked(tcf.intProperty(QTextCharFormat.FontWeight) == QFont.Bold)
self.action_italic.setChecked(tcf.boolProperty(QTextCharFormat.FontItalic))
def set_readonly(self, what): def set_readonly(self, what):
self.readonly = what self.readonly = what
@ -301,10 +303,17 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
clear_text = do_clear clear_text = do_clear
def do_bold(self): def do_bold(self):
raise NotImplementedError('TODO') with self.editing_cursor() as c:
fmt = QTextCharFormat()
fmt.setFontWeight(
QFont.Bold if c.charFormat().fontWeight() != QFont.Bold else QFont.Normal)
c.mergeCharFormat(fmt)
def do_italic(self): def do_italic(self):
raise NotImplementedError('TODO') with self.editing_cursor() as c:
fmt = QTextCharFormat()
fmt.setFontItalic(not c.charFormat().fontItalic())
c.mergeCharFormat(fmt)
def do_underline(self): def do_underline(self):
raise NotImplementedError('TODO') raise NotImplementedError('TODO')
@ -412,7 +421,7 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
bcf = c.blockCharFormat() bcf = c.blockCharFormat()
lvl = self.level_for_block_type(name) lvl = self.level_for_block_type(name)
hmargin = 0 hmargin = 0
wt = 75 if lvl else None wt = QFont.Bold if lvl else None
adjust = (0, 3, 2, 1, 0, -1, -1)[lvl] adjust = (0, 3, 2, 1, 0, -1, -1)[lvl]
if name == 'blockquote': if name == 'blockquote':
hmargin = self.em_size * 3 hmargin = self.em_size * 3