def to_doc(self, index): data = index.data(Qt.ItemDataRole.UserRole) if data is None: html = _('<b>This shortcut no longer exists</b>') elif data.is_shortcut: shortcut = data.data # Shortcut keys = [ unicode_type(k.toString( QKeySequence.SequenceFormat.NativeText)) for k in shortcut['keys'] ] if not keys: keys = _('None') else: keys = ', '.join(keys) html = '<b>%s</b><br>%s: %s' % (prepare_string_for_xml( shortcut['name']), _('Shortcuts'), prepare_string_for_xml(keys)) else: # Group html = data.data doc = QTextDocument() doc.setHtml(html) return doc
def profile(): import sys from qt.core 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') with open(sys.argv[-2], 'rb') as f: raw = f.read().decode('utf-8') doc = QTextDocument() doc.setPlainText(raw) h = Highlighter() 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 paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) text, positions = index.data(Qt.ItemDataRole.UserRole) self.initStyleOption(option, index) painter.save() painter.setFont(option.font) p = option.palette c = QPalette.ColorRole.HighlightedText if option.state & QStyle.StateFlag.State_Selected else QPalette.ColorRole.Text group = (QPalette.ColorGroup.Active if option.state & QStyle.StateFlag.State_Active else QPalette.ColorGroup.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.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter | Qt.TextFlag.TextSingleLine, text) else: to = QTextOption() to.setWrapMode(QTextOption.WrapMode.NoWrap) to.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter) positions = sorted(set(positions) - {-1}, reverse=True) text = '<body>%s</body>' % make_highlighted_text(emphasis_style(), 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 copy_to_clipboard(self, *args): d = QTextDocument() d.setHtml(self.msg_label.text()) QApplication.clipboard().setText( 'calibre, version %s (%s, embedded-python: %s)\n%s: %s\n\n%s' % (__version__, sys.platform, isfrozen, str(self.windowTitle()), str(d.toPlainText()), str(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.StateFlag.State_Selected: p = option.palette group = (QPalette.ColorGroup.Active if option.state & QStyle.StateFlag.State_Active else QPalette.ColorGroup.Inactive) c = p.color(group, QPalette.ColorRole.HighlightedText) c = 'rgb(%d, %d, %d)'%c.getRgb()[:3] doc.setDefaultStyleSheet(' * { color: %s }'%c) doc.setHtml(index.data() or '') return doc
def __init__(self, parent): QStyledItemDelegate.__init__(self, parent) self.document = QTextDocument()
def to_doc(self, index): doc = QTextDocument() doc.setHtml(index.data()) return doc