11
from PyQt4.Qt import QMainWindow, Qt, QApplication, pyqtSignal, QMenu
11
from PyQt4.Qt import (
12
QMainWindow, Qt, QApplication, pyqtSignal, QMenu, qDrawShadeRect, QPainter,
13
QImage, QColor, QIcon, QPixmap, QToolButton)
13
15
from calibre.gui2 import error_dialog
14
16
from calibre.gui2.tweak_book import actions, current_container
15
17
from calibre.gui2.tweak_book.editor.text import TextEdit
17
def register_text_editor_actions(reg):
19
def create_icon(text, palette=None, sz=32, divider=2):
21
palette = QApplication.palette()
22
img = QImage(sz, sz, QImage.Format_ARGB32)
23
img.fill(Qt.transparent)
25
p.setRenderHints(p.TextAntialiasing | p.Antialiasing)
26
qDrawShadeRect(p, img.rect(), palette, fill=QColor('#ffffff'), lineWidth=1, midLineWidth=1)
28
f.setFamily('Liberation Sans'), f.setPixelSize(sz // divider), f.setBold(True)
29
p.setFont(f), p.setPen(Qt.black)
30
p.drawText(img.rect().adjusted(2, 2, -2, -2), Qt.AlignCenter, text)
32
return QIcon(QPixmap.fromImage(img))
34
def register_text_editor_actions(reg, palette):
18
35
ac = reg('format-text-bold', _('&Bold'), ('format_text', 'bold'), 'format-text-bold', 'Ctrl+B', _('Make the selected text bold'))
19
36
ac.setToolTip(_('<h3>Bold</h3>Make the selected text bold'))
20
37
ac = reg('format-text-italic', _('&Italic'), ('format_text', 'italic'), 'format-text-italic', 'Ctrl+I', _('Make the selected text italic'))
39
56
ac = reg('view-image', _('&Insert image'), ('insert_resource', 'image'), 'insert-image', (), _('Insert an image into the text'))
40
57
ac.setToolTip(_('<h3>Insert image</h3>Insert an image into the text'))
59
for i, name in enumerate(('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p')):
60
text = ('&' + name) if name == 'p' else (name[0] + '&' + name[1])
61
desc = _('Convert the paragraph to <%s>') % name
62
ac = reg(create_icon(name), text, ('rename_block_tag', name), 'rename-block-tag-' + name, 'Ctrl+%d' % (i + 1), desc)
42
65
class Editor(QMainWindow):
44
67
has_line_numbers = True
95
118
self.editor.load_text(template, syntax=self.syntax, process_template=True)
97
120
def get_raw_data(self):
98
return unicodedata.normalize('NFC', unicode(self.editor.toPlainText()))
121
return unicodedata.normalize('NFC', unicode(self.editor.toPlainText()).rstrip('\0'))
100
123
def replace_data(self, raw, only_if_different=True):
101
124
if isinstance(raw, bytes):
176
199
self.format_bar = b = self.addToolBar(_('Format text'))
177
200
for x in ('bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript', 'color', 'background-color'):
178
201
b.addAction(actions['format-text-%s' % x])
202
ac = b.addAction(QIcon(I('format-text-heading.png')), _('Change paragraph to heading'))
205
b.widgetForAction(ac).setPopupMode(QToolButton.InstantPopup)
206
for name in tuple('h%d' % d for d in range(1, 7)) + ('p',):
207
m.addAction(actions['rename-block-tag-%s' % name])
180
209
def break_cycles(self):
181
210
self.modification_state_changed.disconnect()
190
219
self.editor.copyAvailable.disconnect()
191
220
self.editor.cursorPositionChanged.disconnect()
192
221
self.editor.setPlainText('')
222
self.editor.smarts = None
194
224
def _modification_state_changed(self):
195
225
self.is_synced_to_container = self.is_modified
221
251
if not c.atStart():
222
252
c.clearSelection()
223
253
c.setPosition(c.position()-1, c.KeepAnchor)
224
char = unicode(c.selectedText())
254
char = unicode(c.selectedText()).rstrip('\0')
225
255
return (c.blockNumber() + 1, c.positionInBlock(), char)