~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-05-14 18:17:50 UTC
  • mfrom: (1.5.10)
  • Revision ID: package-import@ubuntu.com-20140514181750-xyrxqa47dbw0qfhu
Tags: 1.36.0+dfsg-1
* New upstream release:
  - Fixes editing of metadata (Closes: #741638)

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
8
8
 
9
9
import unicodedata
 
10
from functools import partial
10
11
 
11
12
from PyQt4.Qt import (
12
13
    QMainWindow, Qt, QApplication, pyqtSignal, QMenu, qDrawShadeRect, QPainter,
13
14
    QImage, QColor, QIcon, QPixmap, QToolButton)
14
15
 
15
16
from calibre.gui2 import error_dialog
16
 
from calibre.gui2.tweak_book import actions, current_container
 
17
from calibre.gui2.tweak_book import actions, current_container, tprefs
17
18
from calibre.gui2.tweak_book.editor.text import TextEdit
18
19
 
19
20
def create_icon(text, palette=None, sz=32, divider=2):
56
57
    ac = reg('view-image', _('&Insert image'), ('insert_resource', 'image'), 'insert-image', (), _('Insert an image into the text'))
57
58
    ac.setToolTip(_('<h3>Insert image</h3>Insert an image into the text'))
58
59
 
 
60
    ac = reg('insert-link', _('Insert &hyperlink'), ('insert_hyperlink',), 'insert-hyperlink', (), _('Insert hyperlink'))
 
61
    ac.setToolTip(_('<h3>Insert hyperlink</h3>Insert a hyperlink into the text'))
 
62
 
59
63
    for i, name in enumerate(('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p')):
60
64
        text = ('&' + name) if name == 'p' else (name[0] + '&' + name[1])
61
65
        desc = _('Convert the paragraph to &lt;%s&gt;') % name
62
66
        ac = reg(create_icon(name), text, ('rename_block_tag', name), 'rename-block-tag-' + name, 'Ctrl+%d' % (i + 1), desc)
63
67
        ac.setToolTip(desc)
64
68
 
 
69
    ac = reg('code', _('Insert &tag'), ('insert_tag',), 'insert-tag', ('Ctrl+<'), _('Insert tag'))
 
70
    ac.setToolTip(_('<h3>Insert tag</h3>Insert a tag, if some text is selected the tag will be inserted around the selected text'))
 
71
 
 
72
 
65
73
class Editor(QMainWindow):
66
74
 
67
75
    has_line_numbers = True
127
135
        if current != raw:
128
136
            self.editor.replace_text(raw)
129
137
 
130
 
    def apply_settings(self, prefs=None):
131
 
        self.editor.apply_settings(prefs=None)
 
138
    def apply_settings(self, prefs=None, dictionaries_changed=False):
 
139
        self.editor.apply_settings(prefs=None, dictionaries_changed=dictionaries_changed)
132
140
 
133
141
    def set_focus(self):
134
142
        self.editor.setFocus(Qt.OtherFocusReason)
141
149
    def insert_image(self, href):
142
150
        self.editor.insert_image(href)
143
151
 
 
152
    def insert_hyperlink(self, href, text):
 
153
        self.editor.insert_hyperlink(href, text)
 
154
 
 
155
    def _build_insert_tag_button_menu(self):
 
156
        m = self.insert_tag_button.menu()
 
157
        m.clear()
 
158
        for name in tprefs['insert_tag_mru']:
 
159
            m.addAction(name, partial(self.insert_tag, name))
 
160
 
 
161
    def insert_tag(self, name):
 
162
        self.editor.insert_tag(name)
 
163
        mru = tprefs['insert_tag_mru']
 
164
        try:
 
165
            mru.remove(name)
 
166
        except ValueError:
 
167
            pass
 
168
        mru.insert(0, name)
 
169
        tprefs['insert_tag_mru'] = mru
 
170
        self._build_insert_tag_button_menu()
 
171
 
144
172
    def undo(self):
145
173
        self.editor.undo()
146
174
 
151
179
    def selected_text(self):
152
180
        return self.editor.selected_text
153
181
 
 
182
    def get_smart_selection(self, update=True):
 
183
        return self.editor.smarts.get_smart_selection(self.editor, update=update)
 
184
 
154
185
    # Search and replace {{{
155
186
    def mark_selected_text(self):
156
187
        self.editor.mark_selected_text()
158
189
    def find(self, *args, **kwargs):
159
190
        return self.editor.find(*args, **kwargs)
160
191
 
 
192
    def find_spell_word(self, *args, **kwargs):
 
193
        return self.editor.find_spell_word(*args, **kwargs)
 
194
 
161
195
    def replace(self, *args, **kwargs):
162
196
        return self.editor.replace(*args, **kwargs)
163
197
 
189
223
        for x in ('cut', 'copy', 'paste'):
190
224
            b.addAction(actions['editor-%s' % x])
191
225
        self.tools_bar = b = self.addToolBar(_('Editor tools'))
 
226
        b.setObjectName('tools_bar')
192
227
        if self.syntax == 'html':
193
228
            b.addAction(actions['fix-html-current'])
194
229
        if self.syntax in {'xml', 'html', 'css'}:
196
231
        if self.syntax in {'html', 'css'}:
197
232
            b.addAction(actions['insert-image'])
198
233
        if self.syntax == 'html':
 
234
            b.addAction(actions['insert-hyperlink'])
 
235
        if self.syntax in {'xml', 'html'}:
 
236
            b.addAction(actions['insert-tag'])
 
237
            w = self.insert_tag_button = b.widgetForAction(actions['insert-tag'])
 
238
            w.setPopupMode(QToolButton.MenuButtonPopup)
 
239
            w.m = m = QMenu()
 
240
            w.setMenu(m)
 
241
            w.setContextMenuPolicy(Qt.CustomContextMenu)
 
242
            w.customContextMenuRequested.connect(self.insert_tag_button.showMenu)
 
243
            self._build_insert_tag_button_menu()
 
244
        if self.syntax == 'html':
199
245
            self.format_bar = b = self.addToolBar(_('Format text'))
 
246
            b.setObjectName('html_format_bar')
200
247
            for x in ('bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript', 'color', 'background-color'):
201
248
                b.addAction(actions['format-text-%s' % x])
202
249
            ac = b.addAction(QIcon(I('format-text-heading.png')), _('Change paragraph to heading'))
248
295
    def cursor_position(self):
249
296
        c = self.editor.textCursor()
250
297
        char = ''
 
298
        col = c.positionInBlock()
251
299
        if not c.atStart():
252
300
            c.clearSelection()
253
 
            c.setPosition(c.position()-1, c.KeepAnchor)
 
301
            c.movePosition(c.PreviousCharacter, c.KeepAnchor)
254
302
            char = unicode(c.selectedText()).rstrip('\0')
255
 
        return (c.blockNumber() + 1, c.positionInBlock(), char)
 
303
        return (c.blockNumber() + 1, col, char)
256
304
 
257
305
    def cut(self):
258
306
        self.editor.cut()
283
331
        from calibre.ebooks.oeb.polish.pretty import pretty_html, pretty_css, pretty_xml
284
332
        if self.syntax in {'css', 'html', 'xml'}:
285
333
            func = {'css':pretty_css, 'xml':pretty_xml}.get(self.syntax, pretty_html)
286
 
            self.editor.replace_text(func(current_container(), name, unicode(self.editor.toPlainText())).decode('utf-8'))
 
334
            original_text = unicode(self.editor.toPlainText())
 
335
            prettied_text = func(current_container(), name, original_text).decode('utf-8')
 
336
            if original_text != prettied_text:
 
337
                self.editor.replace_text(prettied_text)
287
338
            return True
288
339
        return False
289
340