Edit book: Add buttons for quick color setting while editing HTML

This commit is contained in:
Kovid Goyal 2013-12-16 18:31:42 +05:30
parent 13deb9bab0
commit 4315089af3
2 changed files with 19 additions and 2 deletions

View File

@ -13,7 +13,7 @@ import regex
from PyQt4.Qt import ( from PyQt4.Qt import (
QPlainTextEdit, QFontDatabase, QToolTip, QPalette, QFont, QKeySequence, QPlainTextEdit, QFontDatabase, QToolTip, QPalette, QFont, QKeySequence,
QTextEdit, QTextFormat, QWidget, QSize, QPainter, Qt, QRect, pyqtSlot, 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 import tprefs, TOP
from calibre.gui2.tweak_book.editor import SYNTAX_PROPERTY from calibre.gui2.tweak_book.editor import SYNTAX_PROPERTY
@ -485,6 +485,16 @@ class TextEdit(QPlainTextEdit):
def format_text(self, formatting): def format_text(self, formatting):
if self.syntax != 'html': if self.syntax != 'html':
return 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 = { prefix, suffix = {
'bold': ('<b>', '</b>'), 'bold': ('<b>', '</b>'),
'italic': ('<i>', '</i>'), 'italic': ('<i>', '</i>'),
@ -492,6 +502,8 @@ class TextEdit(QPlainTextEdit):
'strikethrough': ('<strike>', '</strike>'), 'strikethrough': ('<strike>', '</strike>'),
'superscript': ('<sup>', '</sup>'), 'superscript': ('<sup>', '</sup>'),
'subscript': ('<sub>', '</sub>'), 'subscript': ('<sub>', '</sub>'),
'color': ('<span style="color: %s">' % color, '</span>'),
'background-color': ('<span style="background-color: %s">' % color, '</span>'),
}[formatting] }[formatting]
left, right = self.get_range_inside_tag() left, right = self.get_range_inside_tag()
c = self.textCursor() c = self.textCursor()

View File

@ -30,6 +30,11 @@ def register_text_editor_actions(reg):
ac = reg('format-text-subscript', _('&Subscript'), ('format_text', 'subscript'), ac = reg('format-text-subscript', _('&Subscript'), ('format_text', 'subscript'),
'format-text-subscript', (), _('Make the selected text a subscript')) 'format-text-subscript', (), _('Make the selected text a subscript'))
ac.setToolTip(_('<h3>Subscript</h3>Set the selected text slightly smaller and below the normal line')) ac.setToolTip(_('<h3>Subscript</h3>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(_('<h3>Color</h3>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(_('<h3>Background Color</h3>Change the background color of the selected text'))
class Editor(QMainWindow): class Editor(QMainWindow):
@ -159,7 +164,7 @@ class Editor(QMainWindow):
b.addAction(actions['pretty-current']) b.addAction(actions['pretty-current'])
if self.syntax == 'html': if self.syntax == 'html':
self.format_bar = b = self.addToolBar(_('Format text')) 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]) b.addAction(actions['format-text-%s' % x])
def break_cycles(self): def break_cycles(self):