~ubuntu-branches/debian/sid/calibre/sid

« back to all changes in this revision

Viewing changes to src/calibre/gui2/tweak_book/editor/widget.py

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-02-27 07:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 74.
  • Revision ID: package-import@ubuntu.com-20140227074806-64wdebb3ptosxhhx
Tags: upstream-1.25.0+dfsg
ImportĀ upstreamĀ versionĀ 1.25.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 
9
9
import unicodedata
10
10
 
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)
12
14
 
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
16
18
 
17
 
def register_text_editor_actions(reg):
 
19
def create_icon(text, palette=None, sz=32, divider=2):
 
20
    if palette is None:
 
21
        palette = QApplication.palette()
 
22
    img = QImage(sz, sz, QImage.Format_ARGB32)
 
23
    img.fill(Qt.transparent)
 
24
    p = QPainter(img)
 
25
    p.setRenderHints(p.TextAntialiasing | p.Antialiasing)
 
26
    qDrawShadeRect(p, img.rect(), palette, fill=QColor('#ffffff'), lineWidth=1, midLineWidth=1)
 
27
    f = p.font()
 
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)
 
31
    p.end()
 
32
    return QIcon(QPixmap.fromImage(img))
 
33
 
 
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'))
41
58
 
 
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 &lt;%s&gt;') % name
 
62
        ac = reg(create_icon(name), text, ('rename_block_tag', name), 'rename-block-tag-' + name, 'Ctrl+%d' % (i + 1), desc)
 
63
        ac.setToolTip(desc)
 
64
 
42
65
class Editor(QMainWindow):
43
66
 
44
67
    has_line_numbers = True
95
118
        self.editor.load_text(template, syntax=self.syntax, process_template=True)
96
119
 
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'))
99
122
 
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'))
 
203
            m = QMenu()
 
204
            ac.setMenu(m)
 
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])
179
208
 
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
193
223
 
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)
226
256
 
227
257
    def cut(self):