Merge branch 'cc-markdown-delegate' of https://github.com/un-pogaz/calibre

This commit is contained in:
Kovid Goyal 2023-04-28 08:43:19 +05:30
commit 8ead6c26f0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 61 additions and 11 deletions

View File

@ -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): # {{{
'''

View File

@ -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':