From 07f5a9e619fef5c4f481e0dd8c722897e96ee6ce Mon Sep 17 00:00:00 2001 From: un-pogaz <46523284+un-pogaz@users.noreply.github.com> Date: Thu, 27 Apr 2023 17:15:00 +0200 Subject: [PATCH] create CcMarkdownDelegate --- src/calibre/gui2/library/delegates.py | 65 +++++++++++++++++++++++---- src/calibre/gui2/library/views.py | 7 ++- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py index eda62deb9e..c88571a574 100644 --- a/src/calibre/gui2/library/delegates.py +++ b/src/calibre/gui2/library/delegates.py @@ -26,7 +26,7 @@ from calibre.utils.icu import sort_key from calibre.gui2.dialogs.comments_dialog import CommentsDialog, PlainTextDialog from calibre.gui2.dialogs.tag_editor import TagEditor from calibre.gui2.languages import LanguagesEdit - +from calibre.library.comments import markdown class UpdateEditorGeometry: @@ -535,14 +535,7 @@ class CcLongTextDelegate(QStyledItemDelegate): # {{{ text = '' else: text = m.db.data[index.row()][m.custom_columns[col]['rec_index']] - column_format = m.custom_columns[m.column_map[index.column()]]['display'].get('interpret_as') - if column_format == 'markdown': - path = m.db.abspath(index.row(), index_is_id=False) - base_url = QUrl.fromLocalFile(os.path.join(path, 'metadata.html')) if path else None - d = MarkdownEditDialog(parent, text, column_name=m.custom_columns[col]['name'], - base_url=base_url) - else: - d = PlainTextDialog(parent, text, column_name=m.custom_columns[col]['name']) + d = PlainTextDialog(parent, text, column_name=m.custom_columns[col]['name']) if d.exec() == QDialog.DialogCode.Accepted: m.setData(index, d.text, Qt.ItemDataRole.EditRole) return None @@ -552,6 +545,60 @@ class CcLongTextDelegate(QStyledItemDelegate): # {{{ # }}} +class CcMarkdownDelegate(QStyledItemDelegate): # {{{ + + ''' + Delegate for markdown 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() + option.text = markdown(option.text) + 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']] + + path = m.db.abspath(index.row(), index_is_id=False) + base_url = QUrl.fromLocalFile(os.path.join(path, 'metadata.html')) if path else None + d = MarkdownEditDialog(parent, text, column_name=m.custom_columns[col]['name'], + base_url=base_url) + if d.exec() == QDialog.DialogCode.Accepted: + m.setData(index, (d.text), Qt.ItemDataRole.EditRole) + return None + + def setModelData(self, editor, model, index): + model.setData(index, (editor.textbox.html), Qt.ItemDataRole.EditRole) +# }}} + + class CcNumberDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{ ''' diff --git a/src/calibre/gui2/library/views.py b/src/calibre/gui2/library/views.py index c2c3e36a5a..31598cd3e5 100644 --- a/src/calibre/gui2/library/views.py +++ b/src/calibre/gui2/library/views.py @@ -27,7 +27,7 @@ from calibre.gui2.library.alternate_views import ( ) from calibre.gui2.library.delegates import ( CcBoolDelegate, CcCommentsDelegate, CcDateDelegate, CcEnumDelegate, - CcLongTextDelegate, CcNumberDelegate, CcSeriesDelegate, CcTemplateDelegate, + CcLongTextDelegate, CcMarkdownDelegate, CcNumberDelegate, CcSeriesDelegate, CcTemplateDelegate, CcTextDelegate, CompleteDelegate, DateDelegate, LanguagesDelegate, PubDateDelegate, RatingDelegate, SeriesDelegate, TextDelegate, ) @@ -396,6 +396,7 @@ class BooksView(QTableView): # {{{ self.cc_text_delegate = CcTextDelegate(self) self.cc_series_delegate = CcSeriesDelegate(self) self.cc_longtext_delegate = CcLongTextDelegate(self) + self.cc_markdown_delegate = CcMarkdownDelegate(self) self.cc_enum_delegate = CcEnumDelegate(self) self.cc_bool_delegate = CcBoolDelegate(self) self.cc_comments_delegate = CcCommentsDelegate(self) @@ -1127,8 +1128,10 @@ class BooksView(QTableView): # {{{ ctype = cc['display'].get('interpret_as', 'html') if ctype == 'short-text': set_item_delegate(colhead, self.cc_text_delegate) - elif ctype in ('long-text', 'markdown'): + elif ctype == 'long-text': set_item_delegate(colhead, self.cc_longtext_delegate) + elif ctype == 'markdown': + set_item_delegate(colhead, self.cc_markdown_delegate) else: set_item_delegate(colhead, self.cc_comments_delegate) elif cc['datatype'] == 'text':