When editing on the spreadsheet, grow the edit box to the size of the contents if possible.

This commit is contained in:
Charles Haley 2015-03-15 15:17:26 +01:00
parent ea9d1918c1
commit 1ff334698f

View File

@ -126,6 +126,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.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:
@ -140,6 +141,10 @@ class DateDelegate(QStyledItemDelegate): # {{{
def createEditor(self, parent, option, index): def createEditor(self, parent, option, index):
return DateTimeEdit(parent, self.format) 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): # {{{ class PubDateDelegate(QStyledItemDelegate): # {{{
@ -147,6 +152,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]
if self.format is None: if self.format is None:
self.format = 'MMM yyyy' self.format = 'MMM yyyy'
@ -166,9 +172,43 @@ 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)
# }}} # }}}
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): # {{{ class TextDelegate(QStyledItemDelegate): # {{{
def __init__(self, parent): def __init__(self, parent):
@ -178,6 +218,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.auto_complete_function = None self.auto_complete_function = None
def set_auto_complete_function(self, f): def set_auto_complete_function(self, f):
@ -197,6 +238,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)
def setModelData(self, editor, model, index): def setModelData(self, editor, model, index):
if isinstance(editor, EditWithComplete): if isinstance(editor, EditWithComplete):
@ -214,6 +256,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
def set_database(self, db): def set_database(self, db):
self.db = db self.db = db
@ -240,6 +283,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)
def setModelData(self, editor, model, index): def setModelData(self, editor, model, index):
if isinstance(editor, EditWithComplete): if isinstance(editor, EditWithComplete):
@ -251,6 +295,10 @@ class CompleteDelegate(QStyledItemDelegate): # {{{
class LanguagesDelegate(QStyledItemDelegate): # {{{ class LanguagesDelegate(QStyledItemDelegate): # {{{
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
self.parent = parent
def createEditor(self, parent, option, index): def createEditor(self, parent, option, index):
editor = LanguagesEdit(parent=parent) editor = LanguagesEdit(parent=parent)
editor.init_langs(index.model().db) editor.init_langs(index.model().db)
@ -259,6 +307,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)
def setModelData(self, editor, model, index): def setModelData(self, editor, model, index):
val = ','.join(editor.lang_codes) val = ','.join(editor.lang_codes)
@ -273,6 +322,10 @@ class CcDateDelegate(QStyledItemDelegate): # {{{
column. This differs from all the other delegates. column. This differs from all the other delegates.
''' '''
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
self.parent = parent
def set_format(self, format): def set_format(self, format):
if not format: if not format:
self.format = 'dd MMM yyyy' self.format = 'dd MMM yyyy'
@ -296,6 +349,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)
def setModelData(self, editor, model, index): def setModelData(self, editor, model, index):
val = editor.dateTime() val = editor.dateTime()
@ -311,6 +365,10 @@ class CcTextDelegate(QStyledItemDelegate): # {{{
Delegate for text data. Delegate for text data.
''' '''
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
self.parent = parent
def createEditor(self, parent, option, index): def createEditor(self, parent, option, index):
m = index.model() m = index.model()
col = m.column_map[index.column()] col = m.column_map[index.column()]
@ -324,6 +382,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)
editor.selectAll() editor.selectAll()
def setModelData(self, editor, model, index): def setModelData(self, editor, model, index):
@ -337,6 +396,10 @@ class CcNumberDelegate(QStyledItemDelegate): # {{{
Delegate for text/int/float data. Delegate for text/int/float data.
''' '''
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
self.parent = parent
def createEditor(self, parent, option, index): def createEditor(self, parent, option, index):
m = index.model() m = index.model()
col = m.column_map[index.column()] col = m.column_map[index.column()]
@ -357,6 +420,7 @@ class CcNumberDelegate(QStyledItemDelegate): # {{{
if val == editor.minimum(): if val == editor.minimum():
val = None val = None
model.setData(index, (val), Qt.EditRole) model.setData(index, (val), Qt.EditRole)
editor.adjustSize()
def setEditorData(self, editor, index): def setEditorData(self, editor, index):
m = index.model() m = index.model()
@ -364,6 +428,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)
# }}} # }}}
@ -373,6 +438,10 @@ class CcEnumDelegate(QStyledItemDelegate): # {{{
Delegate for text/int/float data. Delegate for text/int/float data.
''' '''
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
self.parent = parent
def createEditor(self, parent, option, index): def createEditor(self, parent, option, index):
m = index.model() m = index.model()
col = m.column_map[index.column()] col = m.column_map[index.column()]
@ -398,6 +467,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)
# }}} # }}}
class CcCommentsDelegate(QStyledItemDelegate): # {{{ class CcCommentsDelegate(QStyledItemDelegate): # {{{
@ -464,6 +534,7 @@ class CcBoolDelegate(QStyledItemDelegate): # {{{
Delegate for custom_column bool data. Delegate for custom_column bool data.
''' '''
QStyledItemDelegate.__init__(self, parent) QStyledItemDelegate.__init__(self, parent)
self.parent = parent
def createEditor(self, parent, option, index): def createEditor(self, parent, option, index):
editor = DelegateCB(parent) editor = DelegateCB(parent)