Use appropriate widgets for editing the new custom comments column types int he book list

This commit is contained in:
Kovid Goyal 2016-07-23 13:48:52 +05:30
parent a3bb5e2cae
commit 5d8782daa0
3 changed files with 71 additions and 9 deletions

View File

@ -3,11 +3,12 @@ __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'
__license__ = 'GPL v3'
from PyQt5.Qt import Qt, QDialog, QDialogButtonBox
from PyQt5.Qt import Qt, QDialog, QDialogButtonBox, QVBoxLayout, QPlainTextEdit, QSize
from calibre.gui2 import gprefs, Application
from calibre.gui2.dialogs.comments_dialog_ui import Ui_CommentsDialog
from calibre.library.comments import comments_to_html
from calibre.gui2.widgets2 import Dialog
class CommentsDialog(QDialog, Ui_CommentsDialog):
@ -48,6 +49,31 @@ class CommentsDialog(QDialog, Ui_CommentsDialog):
self.save_geometry()
return QDialog.closeEvent(self, ev)
class PlainTextDialog(Dialog):
def __init__(self, parent, text, column_name=None):
title = _('Edit "{0}"').format(column_name) if column_name else _('Edit text')
Dialog.__init__(self, title, 'edit-plain-text-dialog', parent=parent)
self.text = text
def setup_ui(self):
self.l = l = QVBoxLayout(self)
self._text = QPlainTextEdit(self)
l.addWidget(self._text)
l.addWidget(self.bb)
@property
def text(self):
return self._text.toPlainText()
@text.setter
def text(self, val):
self._text.setPlainText(val or '')
def sizeHint(self):
return QSize(600, 400)
if __name__ == '__main__':
app = Application([])
d = CommentsDialog(None, 'testing', 'Comments')

View File

@ -20,7 +20,7 @@ from calibre.gui2.complete2 import EditWithComplete
from calibre.utils.date import now, format_date, qt_to_dt, is_date_undefined
from calibre.utils.config import tweaks
from calibre.utils.icu import sort_key
from calibre.gui2.dialogs.comments_dialog import CommentsDialog
from calibre.gui2.dialogs.comments_dialog import CommentsDialog, PlainTextDialog
from calibre.gui2.dialogs.template_dialog import TemplateDialog
from calibre.gui2.dialogs.tag_editor import TagEditor
from calibre.gui2.languages import LanguagesEdit
@ -457,11 +457,14 @@ class CcTextDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
def createEditor(self, parent, option, index):
m = index.model()
col = m.column_map[index.column()]
editor = EditWithComplete(parent)
editor.set_separator(None)
complete_items = sorted(list(m.db.all_custom(label=m.db.field_metadata.key_to_label(col))),
key=sort_key)
editor.update_items_cache(complete_items)
key = m.db.field_metadata.key_to_label(col)
if m.db.field_metadata[col]['is_multiple']:
editor = EditWithComplete(parent)
editor.set_separator(None)
complete_items = sorted(list(m.db.all_custom(label=key)), key=sort_key)
editor.update_items_cache(complete_items)
else:
editor = QStyledItemDelegate.createEditor(self, parent, option, index)
return editor
def setEditorData(self, editor, index):
@ -473,6 +476,32 @@ class CcTextDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
model.setData(index, (val), Qt.EditRole)
# }}}
class CcLongTextDelegate(QStyledItemDelegate): # {{{
'''
Delegate for comments data.
'''
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
self.document = QTextDocument()
def createEditor(self, parent, option, index):
m = index.model()
col = m.column_map[index.column()]
if check_key_modifier(Qt.ControlModifier):
text = ''
else:
text = m.db.data[index.row()][m.custom_columns[col]['rec_index']]
d = PlainTextDialog(parent, text, column_name=m.custom_columns[col]['name'])
if d.exec_() == d.Accepted:
m.setData(index, d.text, Qt.EditRole)
return None
def setModelData(self, editor, model, index):
model.setData(index, (editor.textbox.html), Qt.EditRole)
# }}}
class CcNumberDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
'''

View File

@ -17,7 +17,7 @@ from PyQt5.Qt import (
from calibre.constants import islinux
from calibre.gui2.library.delegates import (RatingDelegate, PubDateDelegate,
TextDelegate, DateDelegate, CompleteDelegate, CcTextDelegate,
TextDelegate, DateDelegate, CompleteDelegate, CcTextDelegate, CcLongTextDelegate,
CcBoolDelegate, CcCommentsDelegate, CcDateDelegate, CcTemplateDelegate,
CcEnumDelegate, CcNumberDelegate, LanguagesDelegate)
from calibre.gui2.library.models import BooksModel, DeviceBooksModel
@ -236,6 +236,7 @@ class BooksView(QTableView): # {{{
self.publisher_delegate = TextDelegate(self)
self.text_delegate = TextDelegate(self)
self.cc_text_delegate = CcTextDelegate(self)
self.cc_longtext_delegate = CcLongTextDelegate(self)
self.cc_enum_delegate = CcEnumDelegate(self)
self.cc_bool_delegate = CcBoolDelegate(self)
self.cc_comments_delegate = CcCommentsDelegate(self)
@ -774,7 +775,13 @@ class BooksView(QTableView): # {{{
delegate.set_format(cc['display'].get('date_format',''))
self.setItemDelegateForColumn(cm.index(colhead), delegate)
elif cc['datatype'] == 'comments':
self.setItemDelegateForColumn(cm.index(colhead), self.cc_comments_delegate)
ctype = cc['display'].get('interpret_as', 'html')
if ctype == 'short-text':
self.setItemDelegateForColumn(cm.index(colhead), self.cc_text_delegate)
elif ctype in ('long-text', 'markdown'):
self.setItemDelegateForColumn(cm.index(colhead), self.cc_longtext_delegate)
else:
self.setItemDelegateForColumn(cm.index(colhead), self.cc_comments_delegate)
elif cc['datatype'] == 'text':
if cc['is_multiple']:
if cc['display'].get('is_names', False):