mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Take two.
I tried for a while to use updateEditorGeometry. It turned out to be as or more complicated as the current implementation, so I stayed with doing the calculations when setting the contents of the widget.
This commit is contained in:
parent
1ff334698f
commit
5f9bb60587
@ -9,7 +9,8 @@ import sys
|
|||||||
|
|
||||||
from PyQt5.Qt import (Qt, QApplication, QStyle, QIcon, QDoubleSpinBox, QStyleOptionViewItem,
|
from PyQt5.Qt import (Qt, QApplication, QStyle, QIcon, QDoubleSpinBox, QStyleOptionViewItem,
|
||||||
QSpinBox, QStyledItemDelegate, QComboBox, QTextDocument, QSize, QMenu, QKeySequence,
|
QSpinBox, QStyledItemDelegate, QComboBox, QTextDocument, QSize, QMenu, QKeySequence,
|
||||||
QAbstractTextDocumentLayout, QFont, QFontInfo, QDate, QDateTimeEdit, QDateTime)
|
QAbstractTextDocumentLayout, QFont, QFontInfo, QDate, QDateTimeEdit, QDateTime,
|
||||||
|
QStyleOptionComboBox, QStyleOptionSpinBox)
|
||||||
|
|
||||||
from calibre.gui2 import UNDEFINED_QDATETIME, error_dialog, rating_font
|
from calibre.gui2 import UNDEFINED_QDATETIME, error_dialog, rating_font
|
||||||
from calibre.constants import iswindows
|
from calibre.constants import iswindows
|
||||||
@ -126,7 +127,7 @@ class DateDelegate(QStyledItemDelegate): # {{{
|
|||||||
def __init__(self, parent, tweak_name='gui_timestamp_display_format',
|
def __init__(self, parent, tweak_name='gui_timestamp_display_format',
|
||||||
default_format='dd MMM yyyy'):
|
default_format='dd MMM yyyy'):
|
||||||
QStyledItemDelegate.__init__(self, parent)
|
QStyledItemDelegate.__init__(self, parent)
|
||||||
self.parent = parent
|
self.table_widget = parent
|
||||||
self.tweak_name = tweak_name
|
self.tweak_name = tweak_name
|
||||||
self.format = tweaks[self.tweak_name]
|
self.format = tweaks[self.tweak_name]
|
||||||
if self.format is None:
|
if self.format is None:
|
||||||
@ -143,7 +144,7 @@ class DateDelegate(QStyledItemDelegate): # {{{
|
|||||||
|
|
||||||
def setEditorData(self, editor, index):
|
def setEditorData(self, editor, index):
|
||||||
QStyledItemDelegate.setEditorData(self, editor, index)
|
QStyledItemDelegate.setEditorData(self, editor, index)
|
||||||
resize_line_edit_to_contents(self.parent, editor)
|
resize_line_edit_to_contents(self.table_widget, editor)
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
@ -152,7 +153,7 @@ class PubDateDelegate(QStyledItemDelegate): # {{{
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
QStyledItemDelegate.__init__(self, *args, **kwargs)
|
QStyledItemDelegate.__init__(self, *args, **kwargs)
|
||||||
self.format = tweaks['gui_pubdate_display_format']
|
self.format = tweaks['gui_pubdate_display_format']
|
||||||
self.parent = args[0]
|
self.table_widget = args[0]
|
||||||
if self.format is None:
|
if self.format is None:
|
||||||
self.format = 'MMM yyyy'
|
self.format = 'MMM yyyy'
|
||||||
|
|
||||||
@ -172,37 +173,41 @@ class PubDateDelegate(QStyledItemDelegate): # {{{
|
|||||||
if isinstance(val, QDateTime):
|
if isinstance(val, QDateTime):
|
||||||
val = val.date()
|
val = val.date()
|
||||||
editor.setDate(val)
|
editor.setDate(val)
|
||||||
resize_line_edit_to_contents(self.parent, editor)
|
resize_line_edit_to_contents(self.table_widget, editor)
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
def resize_line_edit_to_contents(parent, line_edit):
|
def resize_line_edit_to_contents(table_widget, line_edit):
|
||||||
if isinstance(line_edit, DelegateCB):
|
if isinstance(line_edit, DelegateCB):
|
||||||
text = line_edit.currentText()
|
text = line_edit.currentText()
|
||||||
else:
|
else:
|
||||||
text = line_edit.text();
|
text = line_edit.text();
|
||||||
|
|
||||||
fm = line_edit.fontMetrics();
|
fm = line_edit.fontMetrics();
|
||||||
style = QApplication.style()
|
|
||||||
orig_width = line_edit.width()
|
orig_width = line_edit.width()
|
||||||
|
|
||||||
# I can't figure out how to find the size of the area around where the text
|
# The line edit box seems to extend by the space consumed by an 'M'. So add
|
||||||
# goes. A constant of 10 seems to work.
|
# that to the text
|
||||||
srect = style.itemTextRect(fm, line_edit.geometry(), Qt.AlignLeft, True, text)
|
|
||||||
new_width = (srect.right() - srect.left()) + 10
|
|
||||||
|
|
||||||
if isinstance(line_edit, (QComboBox, QDateTimeEdit, QSpinBox, QDoubleSpinBox)):
|
|
||||||
# And again, I can't find the size of the down arrow that opens the
|
|
||||||
# combo box. A constant of 20 seems to work
|
|
||||||
new_width += 20
|
|
||||||
|
|
||||||
style = QApplication.style()
|
style = QApplication.style()
|
||||||
srect = style.itemTextRect(fm, line_edit.geometry(), Qt.AlignLeft, True, text)
|
srect = style.itemTextRect(fm, line_edit.geometry(), Qt.AlignLeft, False, text + 'M')
|
||||||
|
new_width = srect.right() - srect.left()
|
||||||
|
|
||||||
|
# Now compute the size of the combo/spinner arrow
|
||||||
|
if isinstance(line_edit, (QComboBox, QDateTimeEdit)):
|
||||||
|
r = style.subControlRect(QStyle.CC_ComboBox, QStyleOptionComboBox(),
|
||||||
|
QStyle.SC_ComboBoxArrow)
|
||||||
|
new_width -= r.left()
|
||||||
|
elif isinstance(line_edit, (QSpinBox, QDoubleSpinBox)):
|
||||||
|
r = style.subControlRect(QStyle.CC_SpinBox, QStyleOptionSpinBox(),
|
||||||
|
QStyle.SC_SpinBoxUp)
|
||||||
|
new_width -= r.left()
|
||||||
|
|
||||||
# Compute the space available from the left edge of the widget to the
|
# Compute the space available from the left edge of the widget to the
|
||||||
# right edge of the table
|
# right edge of the displayed table (the viewport). We can't display any
|
||||||
max_width = (parent.horizontalScrollBar().geometry().width() -
|
# more than that
|
||||||
parent.verticalHeader().width() -
|
max_width = (table_widget.horizontalScrollBar().geometry().width() -
|
||||||
|
table_widget.verticalHeader().width() -
|
||||||
line_edit.pos().x())
|
line_edit.pos().x())
|
||||||
new_width = new_width if new_width < max_width else max_width
|
new_width = new_width if new_width < max_width else max_width
|
||||||
|
|
||||||
@ -218,7 +223,7 @@ class TextDelegate(QStyledItemDelegate): # {{{
|
|||||||
auto-complete will be used.
|
auto-complete will be used.
|
||||||
'''
|
'''
|
||||||
QStyledItemDelegate.__init__(self, parent)
|
QStyledItemDelegate.__init__(self, parent)
|
||||||
self.parent = parent
|
self.table_widget = parent
|
||||||
self.auto_complete_function = None
|
self.auto_complete_function = None
|
||||||
|
|
||||||
def set_auto_complete_function(self, f):
|
def set_auto_complete_function(self, f):
|
||||||
@ -238,7 +243,7 @@ class TextDelegate(QStyledItemDelegate): # {{{
|
|||||||
ct = unicode(index.data(Qt.DisplayRole) or '')
|
ct = unicode(index.data(Qt.DisplayRole) or '')
|
||||||
editor.setText(ct)
|
editor.setText(ct)
|
||||||
editor.selectAll()
|
editor.selectAll()
|
||||||
resize_line_edit_to_contents(self.parent, editor)
|
resize_line_edit_to_contents(self.table_widget, editor)
|
||||||
|
|
||||||
def setModelData(self, editor, model, index):
|
def setModelData(self, editor, model, index):
|
||||||
if isinstance(editor, EditWithComplete):
|
if isinstance(editor, EditWithComplete):
|
||||||
@ -256,7 +261,7 @@ class CompleteDelegate(QStyledItemDelegate): # {{{
|
|||||||
self.sep = sep
|
self.sep = sep
|
||||||
self.items_func_name = items_func_name
|
self.items_func_name = items_func_name
|
||||||
self.space_before_sep = space_before_sep
|
self.space_before_sep = space_before_sep
|
||||||
self.parent = parent
|
self.table_widget = parent
|
||||||
|
|
||||||
def set_database(self, db):
|
def set_database(self, db):
|
||||||
self.db = db
|
self.db = db
|
||||||
@ -283,7 +288,7 @@ class CompleteDelegate(QStyledItemDelegate): # {{{
|
|||||||
ct = unicode(index.data(Qt.DisplayRole) or '')
|
ct = unicode(index.data(Qt.DisplayRole) or '')
|
||||||
editor.setText(ct)
|
editor.setText(ct)
|
||||||
editor.selectAll()
|
editor.selectAll()
|
||||||
resize_line_edit_to_contents(self.parent, editor)
|
resize_line_edit_to_contents(self.table_widget, editor)
|
||||||
|
|
||||||
def setModelData(self, editor, model, index):
|
def setModelData(self, editor, model, index):
|
||||||
if isinstance(editor, EditWithComplete):
|
if isinstance(editor, EditWithComplete):
|
||||||
@ -297,7 +302,7 @@ class LanguagesDelegate(QStyledItemDelegate): # {{{
|
|||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
QStyledItemDelegate.__init__(self, parent)
|
QStyledItemDelegate.__init__(self, parent)
|
||||||
self.parent = parent
|
self.table_widget = parent
|
||||||
|
|
||||||
def createEditor(self, parent, option, index):
|
def createEditor(self, parent, option, index):
|
||||||
editor = LanguagesEdit(parent=parent)
|
editor = LanguagesEdit(parent=parent)
|
||||||
@ -307,7 +312,7 @@ class LanguagesDelegate(QStyledItemDelegate): # {{{
|
|||||||
def setEditorData(self, editor, index):
|
def setEditorData(self, editor, index):
|
||||||
ct = unicode(index.data(Qt.DisplayRole) or '')
|
ct = unicode(index.data(Qt.DisplayRole) or '')
|
||||||
editor.show_initial_value(ct)
|
editor.show_initial_value(ct)
|
||||||
resize_line_edit_to_contents(self.parent, editor)
|
resize_line_edit_to_contents(self.table_widget, editor)
|
||||||
|
|
||||||
def setModelData(self, editor, model, index):
|
def setModelData(self, editor, model, index):
|
||||||
val = ','.join(editor.lang_codes)
|
val = ','.join(editor.lang_codes)
|
||||||
@ -324,7 +329,7 @@ class CcDateDelegate(QStyledItemDelegate): # {{{
|
|||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
QStyledItemDelegate.__init__(self, parent)
|
QStyledItemDelegate.__init__(self, parent)
|
||||||
self.parent = parent
|
self.table_widget = parent
|
||||||
|
|
||||||
def set_format(self, format):
|
def set_format(self, format):
|
||||||
if not format:
|
if not format:
|
||||||
@ -349,7 +354,7 @@ class CcDateDelegate(QStyledItemDelegate): # {{{
|
|||||||
if val is None:
|
if val is None:
|
||||||
val = now()
|
val = now()
|
||||||
editor.setDateTime(val)
|
editor.setDateTime(val)
|
||||||
resize_line_edit_to_contents(self.parent, editor)
|
resize_line_edit_to_contents(self.table_widget, editor)
|
||||||
|
|
||||||
def setModelData(self, editor, model, index):
|
def setModelData(self, editor, model, index):
|
||||||
val = editor.dateTime()
|
val = editor.dateTime()
|
||||||
@ -367,7 +372,7 @@ class CcTextDelegate(QStyledItemDelegate): # {{{
|
|||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
QStyledItemDelegate.__init__(self, parent)
|
QStyledItemDelegate.__init__(self, parent)
|
||||||
self.parent = parent
|
self.table_widget = parent
|
||||||
|
|
||||||
def createEditor(self, parent, option, index):
|
def createEditor(self, parent, option, index):
|
||||||
m = index.model()
|
m = index.model()
|
||||||
@ -382,7 +387,7 @@ class CcTextDelegate(QStyledItemDelegate): # {{{
|
|||||||
def setEditorData(self, editor, index):
|
def setEditorData(self, editor, index):
|
||||||
ct = unicode(index.data(Qt.DisplayRole) or '')
|
ct = unicode(index.data(Qt.DisplayRole) or '')
|
||||||
editor.setText(ct)
|
editor.setText(ct)
|
||||||
resize_line_edit_to_contents(self.parent, editor)
|
resize_line_edit_to_contents(self.table_widget, editor)
|
||||||
editor.selectAll()
|
editor.selectAll()
|
||||||
|
|
||||||
def setModelData(self, editor, model, index):
|
def setModelData(self, editor, model, index):
|
||||||
@ -398,7 +403,7 @@ class CcNumberDelegate(QStyledItemDelegate): # {{{
|
|||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
QStyledItemDelegate.__init__(self, parent)
|
QStyledItemDelegate.__init__(self, parent)
|
||||||
self.parent = parent
|
self.table_widget = parent
|
||||||
|
|
||||||
def createEditor(self, parent, option, index):
|
def createEditor(self, parent, option, index):
|
||||||
m = index.model()
|
m = index.model()
|
||||||
@ -428,7 +433,7 @@ class CcNumberDelegate(QStyledItemDelegate): # {{{
|
|||||||
if val is None:
|
if val is None:
|
||||||
val = 0
|
val = 0
|
||||||
editor.setValue(val)
|
editor.setValue(val)
|
||||||
resize_line_edit_to_contents(self.parent, editor)
|
resize_line_edit_to_contents(self.table_widget, editor)
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
@ -440,7 +445,7 @@ class CcEnumDelegate(QStyledItemDelegate): # {{{
|
|||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
QStyledItemDelegate.__init__(self, parent)
|
QStyledItemDelegate.__init__(self, parent)
|
||||||
self.parent = parent
|
self.table_widget = parent
|
||||||
|
|
||||||
def createEditor(self, parent, option, index):
|
def createEditor(self, parent, option, index):
|
||||||
m = index.model()
|
m = index.model()
|
||||||
@ -467,7 +472,7 @@ class CcEnumDelegate(QStyledItemDelegate): # {{{
|
|||||||
editor.setCurrentIndex(0)
|
editor.setCurrentIndex(0)
|
||||||
else:
|
else:
|
||||||
editor.setCurrentIndex(idx)
|
editor.setCurrentIndex(idx)
|
||||||
resize_line_edit_to_contents(self.parent, editor)
|
resize_line_edit_to_contents(self.table_widget, editor)
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
class CcCommentsDelegate(QStyledItemDelegate): # {{{
|
class CcCommentsDelegate(QStyledItemDelegate): # {{{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user