Permit customization of date display. The format string can be anything accepted by QDate, with a default of dd MMM yyyy (set in library.py):

d	the day as number without a leading zero (1 to 31)
dd	the day as number with a leading zero (01 to 31)
ddd	the abbreviated localized day name (e.g. 'Mon' to 'Sun').
dddd	the long localized day name (e.g. 'Monday' to 'Sunday'). 
M	the month as number without a leading zero (1 to 12)
MM	the month as number with a leading zero (01 to 12)
MMM	the abbreviated localized month name (e.g. 'Jan' to 'Dec').
MMMM	the long localized month name (e.g. 'January' to 'December').
yy	the year as two digit number (00 to 99)
yyyy	the year as four digit number. If the year is negative, a minus sign is prepended in addition.
This commit is contained in:
Charles Haley 2010-05-07 17:36:40 +01:00
parent a7a20a5e1c
commit 1c1e3bf9a1
4 changed files with 90 additions and 8 deletions

View File

@ -776,14 +776,17 @@ class ConfigDialog(ResizableDialog, Ui_Dialog):
label=c,
name=self.custcols[c]['name'],
datatype=self.custcols[c]['datatype'],
is_multiple=self.custcols[c]['is_multiple'])
is_multiple=self.custcols[c]['is_multiple'],
display = self.custcols[c]['display'])
must_restart = True
elif '*deleteme' in self.custcols[c]:
self.db.delete_custom_column(label=c)
must_restart = True
elif '*edited' in self.custcols[c]:
cc = self.custcols[c]
self.db.set_custom_column_metadata(cc['num'], name=cc['name'], label=cc['label'])
self.db.set_custom_column_metadata(cc['num'], name=cc['name'],
label=cc['label'],
display = self.custcols[c]['display'])
if '*must_restart' in self.custcols[c]:
must_restart = True

View File

@ -48,9 +48,9 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
self.editing_col = editing
self.standard_colheads = standard_colheads
self.standard_colnames = standard_colnames
for t in self.column_types:
self.column_type_box.addItem(self.column_types[t]['text'])
if not self.editing_col:
for t in self.column_types:
self.column_type_box.addItem(self.column_types[t]['text'])
self.exec_()
return
idx = parent.columns.currentRow()
@ -68,7 +68,10 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
self.orig_column_number = c['num']
self.orig_column_name = col
column_numbers = dict(map(lambda x:(self.column_types[x]['datatype'], x), self.column_types))
self.column_type_box.addItem(self.column_types[column_numbers[ct]]['text'])
self.column_type_box.setCurrentIndex(column_numbers[ct])
self.column_type_box.setEnabled(False)
if ct == 'datetime':
self.date_format_box.setText(c['display'].get('date_format', ''))
self.exec_()
def accept(self):
@ -105,13 +108,18 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
if ':' in col or ' ' in col or col.lower() != col:
return self.simple_error('', _('The lookup name must be lower case and cannot contain ":"s or spaces'))
date_format = None
if col_type == 'datetime':
if self.date_format_box.text():
date_format = {'date_format':unicode(self.date_format_box.text())}
if not self.editing_col:
self.parent.custcols[col] = {
'label':col,
'name':col_heading,
'datatype':col_type,
'editable':True,
'display':None,
'display':date_format,
'normalized':None,
'num':None,
'is_multiple':is_multiple,
@ -127,6 +135,7 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
item.setText(col_heading)
self.parent.custcols[self.orig_column_name]['label'] = col
self.parent.custcols[self.orig_column_name]['name'] = col_heading
self.parent.custcols[self.orig_column_name]['display'] = date_format
self.parent.custcols[self.orig_column_name]['*edited'] = True
self.parent.custcols[self.orig_column_name]['*must_restart'] = True
QDialog.accept(self)

View File

@ -10,7 +10,7 @@
<x>0</x>
<y>0</y>
<width>528</width>
<height>165</height>
<height>194</height>
</rect>
</property>
<property name="sizePolicy">
@ -33,6 +33,9 @@
</property>
<item row="2" column="0">
<layout class="QGridLayout" name="gridLayout">
<property name="margin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
@ -102,6 +105,43 @@
</property>
</widget>
</item>
<item row="4" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLineEdit" name="date_format_box">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Date format. Use 1-4 'd's for day, 1-4 'M's for month, and 1-2 'Y's for year.</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_5">
<property name="toolTip">
<string>Use MMM yyyy for month + year, yyyy for year only</string>
</property>
<property name="text">
<string>Default: dd MMM yyyy.</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Format for dates</string>
</property>
<property name="buddy">
<cstring>date_format_box</cstring>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0">
@ -138,6 +178,7 @@
<tabstop>column_name_box</tabstop>
<tabstop>column_heading_box</tabstop>
<tabstop>column_type_box</tabstop>
<tabstop>date_format_box</tabstop>
<tabstop>button_box</tabstop>
</tabstops>
<resources/>

View File

@ -177,6 +177,33 @@ class TagsDelegate(QStyledItemDelegate):
editor = EnLineEdit(parent)
return editor
class CcDateDelegate(QStyledItemDelegate):
'''
Delegate for custom columns dates. Because this delegate stores the
format as an instance variable, a new instance must be created for each
column. This differs from all the other delegates.
'''
def set_format(self, format):
if not format:
self.format = 'dd MMM yyyy'
else:
self.format = format
def displayText(self, val, locale):
d = val.toDate()
if d == UNDEFINED_DATE:
return ''
return d.toString(self.format)
def createEditor(self, parent, option, index):
qde = QStyledItemDelegate.createEditor(self, parent, option, index)
qde.setDisplayFormat(self.format)
qde.setMinimumDate(UNDEFINED_DATE)
qde.setSpecialValueText(_('Undefined'))
qde.setCalendarPopup(True)
return qde
class CcTextDelegate(QStyledItemDelegate):
'''
Delegate for text/int/float data.
@ -989,7 +1016,9 @@ class BooksView(TableView):
continue
cc = self._model.custom_columns[colhead]
if cc['datatype'] == 'datetime':
self.setItemDelegateForColumn(cm.index(colhead), self.timestamp_delegate)
delegate = CcDateDelegate(self)
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)
elif cc['datatype'] == 'text':