def __init__(self, parent, text, hlclass): QTextDocument.__init__(self, parent) self.l = QPlainTextDocumentLayout(self) self.setDocumentLayout(self.l) self.highlighter = hlclass() self.highlighter.apply_theme(get_theme(tprefs['editor_theme'])) self.highlighter.set_document(self) self.setPlainText(text)
def __init__(self, parent, text, syntax): QTextDocument.__init__(self, parent) self.l = QPlainTextDocumentLayout(self) self.setDocumentLayout(self.l) self.highlighter = get_highlighter(syntax)(self) self.highlighter.apply_theme(get_theme()) self.highlighter.setDocument(self) self.setPlainText(text)
def __init__(self, parent, text, hlclass): QTextDocument.__init__(self, parent) self.l = QPlainTextDocumentLayout(self) self.setDocumentLayout(self.l) self.highlighter = hlclass(self) self.highlighter.apply_theme(get_theme()) self.highlighter.setDocument(self) self.setPlainText(text)
def copy_to_clipboard(self, *args): d = QTextDocument() d.setHtml(self.msg_label.text()) QApplication.clipboard().setText( u'calibre, version %s (%s, isfrozen: %s)\n%s: %s\n\n%s' % (__version__, sys.platform, isfrozen, unicode(self.windowTitle()), unicode(d.toPlainText()), unicode(self.det_msg.toPlainText()))) if hasattr(self, 'ctc_button'): self.ctc_button.setText(_('Copied'))
def to_doc(self, index, option=None): doc = QTextDocument() if option is not None and option.state & QStyle.State_Selected: p = option.palette group = p.Active if option.state & QStyle.State_Active else p.Inactive c = p.color(group, p.HighlightedText) c = "rgb(%d, %d, %d)" % c.getRgb()[:3] doc.setDefaultStyleSheet(" * { color: %s }" % c) doc.setHtml(index.data().toString()) return doc
def to_doc(self, index): data = index.data(Qt.UserRole).toPyObject() if data is None: html = _("<b>This shortcut no longer exists</b>") elif data.is_shortcut: shortcut = data.data # Shortcut keys = [unicode(k.toString(k.NativeText)) for k in shortcut["keys"]] if not keys: keys = _("None") else: keys = ", ".join(keys) html = "<b>%s</b><br>%s: %s" % (shortcut["name"], _("Shortcuts"), keys) else: # Group html = "<h3>%s</h3>" % data.data doc = QTextDocument() doc.setHtml(html) return doc
def to_doc(self, index): data = index.data(Qt.UserRole).toPyObject() if data is None: html = _('<b>This shortcut no longer exists</b>') elif data.is_shortcut: shortcut = data.data # Shortcut keys = [unicode(k.toString(k.NativeText)) for k in shortcut['keys']] if not keys: keys = _('None') else: keys = ', '.join(keys) html = '<b>%s</b><br>%s: %s'%(shortcut['name'], _('Shortcuts'), keys) else: # Group html = '<h3>%s</h3>'%data.data doc = QTextDocument() doc.setHtml(html) return doc
class CcCommentsDelegate(QStyledItemDelegate): # {{{ ''' Delegate for comments data. ''' def __init__(self, parent): QStyledItemDelegate.__init__(self, parent) self.document = QTextDocument() def paint(self, painter, option, index): self.initStyleOption(option, index) style = QApplication.style() if option.widget is None \ else option.widget.style() self.document.setHtml(option.text) option.text = u'' if hasattr(QStyle, 'CE_ItemViewItem'): style.drawControl(QStyle.CE_ItemViewItem, option, painter) ctx = QAbstractTextDocumentLayout.PaintContext() ctx.palette = option.palette # .setColor(QPalette.Text, QColor("red")); if hasattr(QStyle, 'SE_ItemViewItemText'): textRect = style.subElementRect(QStyle.SE_ItemViewItemText, option) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) self.document.documentLayout().draw(painter, ctx) painter.restore() def createEditor(self, parent, option, index): m = index.model() col = m.column_map[index.column()] text = m.db.data[index.row()][m.custom_columns[col]['rec_index']] editor = CommentsDialog(parent, text, column_name=m.custom_columns[col]['name']) d = editor.exec_() if d: m.setData(index, QVariant(editor.textbox.html), Qt.EditRole) return None def setModelData(self, editor, model, index): model.setData(index, QVariant(editor.textbox.html), Qt.EditRole)
def to_doc(self, index): data = index.data(Qt.UserRole).toPyObject() if data is None: html = _('<b>This shortcut no longer exists</b>') elif data.is_shortcut: shortcut = data.data # Shortcut keys = [ unicode(k.toString(k.NativeText)) for k in shortcut['keys'] ] if not keys: keys = _('None') else: keys = ', '.join(keys) html = '<b>%s</b><br>%s: %s' % (shortcut['name'], _('Shortcuts'), keys) else: # Group html = '<h3>%s</h3>' % data.data doc = QTextDocument() doc.setHtml(html) return doc
class CcCommentsDelegate(QStyledItemDelegate): # {{{ ''' Delegate for comments data. ''' def __init__(self, parent): QStyledItemDelegate.__init__(self, parent) self.document = QTextDocument() def paint(self, painter, option, index): self.initStyleOption(option, index) style = QApplication.style() if option.widget is None \ else option.widget.style() self.document.setHtml(option.text) option.text = u'' if hasattr(QStyle, 'CE_ItemViewItem'): style.drawControl(QStyle.CE_ItemViewItem, option, painter) ctx = QAbstractTextDocumentLayout.PaintContext() ctx.palette = option.palette # .setColor(QPalette.Text, QColor("red")); if hasattr(QStyle, 'SE_ItemViewItemText'): textRect = style.subElementRect(QStyle.SE_ItemViewItemText, option) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) self.document.documentLayout().draw(painter, ctx) painter.restore() def createEditor(self, parent, option, index): m = index.model() col = m.column_map[index.column()] text = m.db.data[index.row()][m.custom_columns[col]['rec_index']] editor = CommentsDialog(parent, text) d = editor.exec_() if d: m.setData(index, QVariant(editor.textbox.html), Qt.EditRole) return None def setModelData(self, editor, model, index): model.setData(index, QVariant(editor.textbox.html), Qt.EditRole)
def profile(): import sys from PyQt4.Qt import QTextDocument from calibre.gui2 import Application from calibre.gui2.tweak_book import set_book_locale from calibre.gui2.tweak_book.editor.themes import get_theme app = Application([]) set_book_locale('en') raw = open(sys.argv[-2], 'rb').read().decode('utf-8') doc = QTextDocument() doc.setPlainText(raw) h = HTMLHighlighter() theme = get_theme(tprefs['editor_theme']) h.apply_theme(theme) h.set_document(doc) h.join() import cProfile print ('Running profile on', sys.argv[-2]) h.rehighlight() cProfile.runctx('h.join()', {}, {'h':h}, sys.argv[-1]) print ('Stats saved to:', sys.argv[-1]) del h del doc del app
def profile(): import sys from PyQt4.Qt import QTextDocument from calibre.gui2 import Application from calibre.gui2.tweak_book import set_book_locale from calibre.gui2.tweak_book.editor.themes import get_theme app = Application([]) set_book_locale('en') raw = open(sys.argv[-2], 'rb').read().decode('utf-8') doc = QTextDocument() doc.setPlainText(raw) h = HTMLHighlighter() theme = get_theme(tprefs['editor_theme']) h.apply_theme(theme) h.set_document(doc) h.join() import cProfile print('Running profile on', sys.argv[-2]) h.rehighlight() cProfile.runctx('h.join()', {}, {'h': h}, sys.argv[-1]) print('Stats saved to:', sys.argv[-1]) del h del doc del app
def to_doc(self, index, option=None): doc = QTextDocument() if option is not None and option.state & QStyle.State_Selected: p = option.palette group = (p.Active if option.state & QStyle.State_Active else p.Inactive) c = p.color(group, p.HighlightedText) c = 'rgb(%d, %d, %d)' % c.getRgb()[:3] doc.setDefaultStyleSheet(' * { color: %s }' % c) doc.setHtml(index.data().toString()) return doc
def _findNextOrPrevious(self, findPrevious = False): """ Find the next or previous matching string depending on the findPrevious flag. It also considers into account various findFlags user might have set (e.g. case sensitive search) @param findPrevious: If true, this method will find the previous occurance of the search string. @type findPrevious: boolean """ findFlags = QTextDocument.FindFlags() if findPrevious: findFlags |= QTextDocument.FindBackward if self.caseSensitiveFindAction.isChecked(): findFlags |= QTextDocument.FindCaseSensitively if not self.sequenceTextEdit.hasFocus(): self.sequenceTextEdit.setFocus() searchString = self.findLineEdit.text() cursor = self.sequenceTextEdit.textCursor() found = self.sequenceTextEdit.find(searchString, findFlags) #May be the cursor reached the end of the document, set it at position 0 #to redo the search. This makes sure that the search loops over as #user executes findNext multiple times. if not found: if findPrevious: sequence_QString = self.sequenceTextEdit.toPlainText() newCursorStartPosition = sequence_QString.length() else: newCursorStartPosition = 0 cursor.setPosition( newCursorStartPosition, QTextCursor.MoveAnchor) self.sequenceTextEdit.setTextCursor(cursor) found = self.sequenceTextEdit.find(searchString, findFlags) #Display or hide the warning widgets (that say 'sequence not found' #based on the boolean 'found' self._toggleWarningWidgets(found) return
def to_doc(self, index): doc = QTextDocument() doc.setHtml(index.data().toString()) return doc
def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) text, positions = index.data(Qt.UserRole).toPyObject() self.initStyleOption(option, index) painter.save() painter.setFont(option.font) p = option.palette c = p.HighlightedText if option.state & QStyle.State_Selected else p.Text group = (p.Active if option.state & QStyle.State_Active else p.Inactive) c = p.color(group, c) painter.setClipRect(option.rect) if positions is None or -1 in positions: painter.setPen(c) painter.drawText(option.rect, Qt.AlignLeft | Qt.AlignVCenter | Qt.TextSingleLine, text) else: to = QTextOption() to.setWrapMode(to.NoWrap) to.setAlignment(Qt.AlignLeft | Qt.AlignVCenter) positions = sorted(set(positions) - {-1}, reverse=True) text = '<body>%s</body>' % make_highlighted_text(Results.EMPH, text, positions) doc = QTextDocument() c = 'rgb(%d, %d, %d)'%c.getRgb()[:3] doc.setDefaultStyleSheet(' body { color: %s }'%c) doc.setHtml(text) doc.setDefaultFont(option.font) doc.setDocumentMargin(0.0) doc.setDefaultTextOption(to) height = doc.size().height() painter.translate(option.rect.left(), option.rect.top() + (max(0, option.rect.height() - height) // 2)) doc.drawContents(painter) painter.restore()
def __init__(self, parent): QStyledItemDelegate.__init__(self, parent) self.document = QTextDocument()
def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) text, positions = index.data(Qt.UserRole).toPyObject() self.initStyleOption(option, index) painter.save() painter.setFont(option.font) p = option.palette c = p.HighlightedText if option.state & QStyle.State_Selected else p.Text group = (p.Active if option.state & QStyle.State_Active else p.Inactive) c = p.color(group, c) painter.setClipRect(option.rect) if positions is None or -1 in positions: painter.setPen(c) painter.drawText( option.rect, Qt.AlignLeft | Qt.AlignVCenter | Qt.TextSingleLine, text) else: to = QTextOption() to.setWrapMode(to.NoWrap) to.setAlignment(Qt.AlignLeft | Qt.AlignVCenter) positions = sorted(set(positions) - {-1}, reverse=True) text = '<body>%s</body>' % make_highlighted_text( Results.EMPH, text, positions) doc = QTextDocument() c = 'rgb(%d, %d, %d)' % c.getRgb()[:3] doc.setDefaultStyleSheet(' body { color: %s }' % c) doc.setHtml(text) doc.setDefaultFont(option.font) doc.setDocumentMargin(0.0) doc.setDefaultTextOption(to) height = doc.size().height() painter.translate( option.rect.left(), option.rect.top() + (max(0, option.rect.height() - height) // 2)) doc.drawContents(painter) painter.restore()