diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index cf19710fbb..478f8648fa 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -13,7 +13,7 @@ import regex from PyQt4.Qt import ( QPlainTextEdit, QFontDatabase, QToolTip, QPalette, QFont, QKeySequence, QTextEdit, QTextFormat, QWidget, QSize, QPainter, Qt, QRect, pyqtSlot, - QApplication, QMimeData) + QApplication, QMimeData, QColor, QColorDialog) from calibre.gui2.tweak_book import tprefs, TOP from calibre.gui2.tweak_book.editor import SYNTAX_PROPERTY @@ -485,6 +485,16 @@ class TextEdit(QPlainTextEdit): def format_text(self, formatting): if self.syntax != 'html': return + color = 'currentColor' + if formatting in {'color', 'background-color'}: + color = QColorDialog.getColor(QColor(Qt.black if formatting == 'color' else Qt.white), self, _('Choose color'), QColorDialog.ShowAlphaChannel) + if not color.isValid(): + return + r, g, b, a = color.getRgb() + if a == 255: + color = 'rgb(%d, %d, %d)' % (r, g, b) + else: + color = 'rgba(%d, %d, %d, %.2g)' % (r, g, b, a/255) prefix, suffix = { 'bold': ('', ''), 'italic': ('', ''), @@ -492,6 +502,8 @@ class TextEdit(QPlainTextEdit): 'strikethrough': ('', ''), 'superscript': ('', ''), 'subscript': ('', ''), + 'color': ('' % color, ''), + 'background-color': ('' % color, ''), }[formatting] left, right = self.get_range_inside_tag() c = self.textCursor() diff --git a/src/calibre/gui2/tweak_book/editor/widget.py b/src/calibre/gui2/tweak_book/editor/widget.py index 22335a42dd..8201a0083e 100644 --- a/src/calibre/gui2/tweak_book/editor/widget.py +++ b/src/calibre/gui2/tweak_book/editor/widget.py @@ -30,6 +30,11 @@ def register_text_editor_actions(reg): ac = reg('format-text-subscript', _('&Subscript'), ('format_text', 'subscript'), 'format-text-subscript', (), _('Make the selected text a subscript')) ac.setToolTip(_('

Subscript

Set the selected text slightly smaller and below the normal line')) + ac = reg('format-text-color', _('&Color'), ('format_text', 'color'), 'format-text-color', (), _('Change text color')) + ac.setToolTip(_('

Color

Change the color of the selected text')) + ac = reg('format-fill-color', _('&Background Color'), ('format_text', 'background-color'), + 'format-text-background-color', (), _('Change background color of text')) + ac.setToolTip(_('

Background Color

Change the background color of the selected text')) class Editor(QMainWindow): @@ -159,7 +164,7 @@ class Editor(QMainWindow): b.addAction(actions['pretty-current']) if self.syntax == 'html': self.format_bar = b = self.addToolBar(_('Format text')) - for x in ('bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript'): + for x in ('bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript', 'color', 'background-color'): b.addAction(actions['format-text-%s' % x]) def break_cycles(self):