mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Edit book: Add buttons for quick color setting while editing HTML
This commit is contained in:
parent
13deb9bab0
commit
4315089af3
@ -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': ('<b>', '</b>'),
|
||||
'italic': ('<i>', '</i>'),
|
||||
@ -492,6 +502,8 @@ class TextEdit(QPlainTextEdit):
|
||||
'strikethrough': ('<strike>', '</strike>'),
|
||||
'superscript': ('<sup>', '</sup>'),
|
||||
'subscript': ('<sub>', '</sub>'),
|
||||
'color': ('<span style="color: %s">' % color, '</span>'),
|
||||
'background-color': ('<span style="background-color: %s">' % color, '</span>'),
|
||||
}[formatting]
|
||||
left, right = self.get_range_inside_tag()
|
||||
c = self.textCursor()
|
||||
|
@ -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(_('<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):
|
||||
|
||||
@ -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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user