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 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'))
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) style.drawPrimitive(QStyle.PrimitiveElement.PE_PanelItemViewItem, option, painter, widget=option.widget) rect = style.subElementRect( QStyle.SubElement.SE_ItemViewItemDecoration, option, self.parent()) ic = option.icon if rect.isValid() and not ic.isNull(): sz = ic.actualSize(option.decorationSize) painter.drawPixmap(rect.topLeft(), ic.pixmap(sz)) ctx = QAbstractTextDocumentLayout.PaintContext() ctx.palette = option.palette if option.state & QStyle.StateFlag.State_Selected: ctx.palette.setColor( QPalette.ColorRole.Text, ctx.palette.color(QPalette.ColorRole.HighlightedText)) textRect = style.subElementRect(QStyle.SubElement.SE_ItemViewItemText, option, self.parent()) 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()] if check_key_modifier(Qt.KeyboardModifier.ControlModifier): text = '' else: 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, (editor.textbox.html), Qt.ItemDataRole.EditRole) return None def setModelData(self, editor, model, index): model.setData(index, (editor.textbox.html), Qt.ItemDataRole.EditRole)
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 to_doc(self, index): doc = QTextDocument() doc.setHtml(index.data()) return doc