diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py index 4ce8fff84d..1315e22c15 100644 --- a/src/calibre/gui2/library/delegates.py +++ b/src/calibre/gui2/library/delegates.py @@ -126,6 +126,7 @@ class DateDelegate(QStyledItemDelegate): # {{{ def __init__(self, parent, tweak_name='gui_timestamp_display_format', default_format='dd MMM yyyy'): QStyledItemDelegate.__init__(self, parent) + self.parent = parent self.tweak_name = tweak_name self.format = tweaks[self.tweak_name] if self.format is None: @@ -140,6 +141,10 @@ class DateDelegate(QStyledItemDelegate): # {{{ def createEditor(self, parent, option, index): return DateTimeEdit(parent, self.format) + def setEditorData(self, editor, index): + QStyledItemDelegate.setEditorData(self, editor, index) + resize_line_edit_to_contents(self.parent, editor) + # }}} class PubDateDelegate(QStyledItemDelegate): # {{{ @@ -147,6 +152,7 @@ class PubDateDelegate(QStyledItemDelegate): # {{{ def __init__(self, *args, **kwargs): QStyledItemDelegate.__init__(self, *args, **kwargs) self.format = tweaks['gui_pubdate_display_format'] + self.parent = args[0] if self.format is None: self.format = 'MMM yyyy' @@ -166,9 +172,43 @@ class PubDateDelegate(QStyledItemDelegate): # {{{ if isinstance(val, QDateTime): val = val.date() editor.setDate(val) + resize_line_edit_to_contents(self.parent, editor) # }}} +def resize_line_edit_to_contents(parent, line_edit): + if isinstance(line_edit, DelegateCB): + text = line_edit.currentText() + else: + text = line_edit.text(); + + fm = line_edit.fontMetrics(); + style = QApplication.style() + orig_width = line_edit.width() + + # I can't figure out how to find the size of the area around where the text + # goes. A constant of 10 seems to work. + 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() + srect = style.itemTextRect(fm, line_edit.geometry(), Qt.AlignLeft, True, text) + + # Compute the space available from the left edge of the widget to the + # right edge of the table + max_width = (parent.horizontalScrollBar().geometry().width() - + parent.verticalHeader().width() - + line_edit.pos().x()) + new_width = new_width if new_width < max_width else max_width + + if new_width > orig_width: + line_edit.resize(new_width, line_edit.height()); + class TextDelegate(QStyledItemDelegate): # {{{ def __init__(self, parent): @@ -178,6 +218,7 @@ class TextDelegate(QStyledItemDelegate): # {{{ auto-complete will be used. ''' QStyledItemDelegate.__init__(self, parent) + self.parent = parent self.auto_complete_function = None def set_auto_complete_function(self, f): @@ -197,6 +238,7 @@ class TextDelegate(QStyledItemDelegate): # {{{ ct = unicode(index.data(Qt.DisplayRole) or '') editor.setText(ct) editor.selectAll() + resize_line_edit_to_contents(self.parent, editor) def setModelData(self, editor, model, index): if isinstance(editor, EditWithComplete): @@ -214,6 +256,7 @@ class CompleteDelegate(QStyledItemDelegate): # {{{ self.sep = sep self.items_func_name = items_func_name self.space_before_sep = space_before_sep + self.parent = parent def set_database(self, db): self.db = db @@ -240,6 +283,7 @@ class CompleteDelegate(QStyledItemDelegate): # {{{ ct = unicode(index.data(Qt.DisplayRole) or '') editor.setText(ct) editor.selectAll() + resize_line_edit_to_contents(self.parent, editor) def setModelData(self, editor, model, index): if isinstance(editor, EditWithComplete): @@ -251,6 +295,10 @@ class CompleteDelegate(QStyledItemDelegate): # {{{ class LanguagesDelegate(QStyledItemDelegate): # {{{ + def __init__(self, parent): + QStyledItemDelegate.__init__(self, parent) + self.parent = parent + def createEditor(self, parent, option, index): editor = LanguagesEdit(parent=parent) editor.init_langs(index.model().db) @@ -259,6 +307,7 @@ class LanguagesDelegate(QStyledItemDelegate): # {{{ def setEditorData(self, editor, index): ct = unicode(index.data(Qt.DisplayRole) or '') editor.show_initial_value(ct) + resize_line_edit_to_contents(self.parent, editor) def setModelData(self, editor, model, index): val = ','.join(editor.lang_codes) @@ -273,6 +322,10 @@ class CcDateDelegate(QStyledItemDelegate): # {{{ column. This differs from all the other delegates. ''' + def __init__(self, parent): + QStyledItemDelegate.__init__(self, parent) + self.parent = parent + def set_format(self, format): if not format: self.format = 'dd MMM yyyy' @@ -296,6 +349,7 @@ class CcDateDelegate(QStyledItemDelegate): # {{{ if val is None: val = now() editor.setDateTime(val) + resize_line_edit_to_contents(self.parent, editor) def setModelData(self, editor, model, index): val = editor.dateTime() @@ -311,6 +365,10 @@ class CcTextDelegate(QStyledItemDelegate): # {{{ Delegate for text data. ''' + def __init__(self, parent): + QStyledItemDelegate.__init__(self, parent) + self.parent = parent + def createEditor(self, parent, option, index): m = index.model() col = m.column_map[index.column()] @@ -324,6 +382,7 @@ class CcTextDelegate(QStyledItemDelegate): # {{{ def setEditorData(self, editor, index): ct = unicode(index.data(Qt.DisplayRole) or '') editor.setText(ct) + resize_line_edit_to_contents(self.parent, editor) editor.selectAll() def setModelData(self, editor, model, index): @@ -337,6 +396,10 @@ class CcNumberDelegate(QStyledItemDelegate): # {{{ Delegate for text/int/float data. ''' + def __init__(self, parent): + QStyledItemDelegate.__init__(self, parent) + self.parent = parent + def createEditor(self, parent, option, index): m = index.model() col = m.column_map[index.column()] @@ -357,6 +420,7 @@ class CcNumberDelegate(QStyledItemDelegate): # {{{ if val == editor.minimum(): val = None model.setData(index, (val), Qt.EditRole) + editor.adjustSize() def setEditorData(self, editor, index): m = index.model() @@ -364,6 +428,7 @@ class CcNumberDelegate(QStyledItemDelegate): # {{{ if val is None: val = 0 editor.setValue(val) + resize_line_edit_to_contents(self.parent, editor) # }}} @@ -373,6 +438,10 @@ class CcEnumDelegate(QStyledItemDelegate): # {{{ Delegate for text/int/float data. ''' + def __init__(self, parent): + QStyledItemDelegate.__init__(self, parent) + self.parent = parent + def createEditor(self, parent, option, index): m = index.model() col = m.column_map[index.column()] @@ -398,6 +467,7 @@ class CcEnumDelegate(QStyledItemDelegate): # {{{ editor.setCurrentIndex(0) else: editor.setCurrentIndex(idx) + resize_line_edit_to_contents(self.parent, editor) # }}} class CcCommentsDelegate(QStyledItemDelegate): # {{{ @@ -464,6 +534,7 @@ class CcBoolDelegate(QStyledItemDelegate): # {{{ Delegate for custom_column bool data. ''' QStyledItemDelegate.__init__(self, parent) + self.parent = parent def createEditor(self, parent, option, index): editor = DelegateCB(parent)