mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add format strings to int and float custom columns
This commit is contained in:
parent
f9b1305807
commit
f209a7b079
@ -628,6 +628,12 @@ class Metadata(object):
|
|||||||
res = _('Yes') if res else _('No')
|
res = _('Yes') if res else _('No')
|
||||||
elif datatype == 'rating':
|
elif datatype == 'rating':
|
||||||
res = res/2.0
|
res = res/2.0
|
||||||
|
elif datatype in ['int', 'float']:
|
||||||
|
try:
|
||||||
|
fmt = cmeta['display'].get('number_format', None)
|
||||||
|
res = fmt.format(res)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
return (name, unicode(res), orig_res, cmeta)
|
return (name, unicode(res), orig_res, cmeta)
|
||||||
|
|
||||||
# convert top-level ids into their value
|
# convert top-level ids into their value
|
||||||
|
@ -623,7 +623,12 @@ class BooksModel(QAbstractTableModel): # {{{
|
|||||||
return None
|
return None
|
||||||
return QVariant(text)
|
return QVariant(text)
|
||||||
|
|
||||||
def number_type(r, idx=-1):
|
def number_type(r, idx=-1, fmt=None):
|
||||||
|
if fmt is not None:
|
||||||
|
try:
|
||||||
|
return QVariant(fmt.format(self.db.data[r][idx]))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
return QVariant(self.db.data[r][idx])
|
return QVariant(self.db.data[r][idx])
|
||||||
|
|
||||||
self.dc = {
|
self.dc = {
|
||||||
@ -674,7 +679,8 @@ class BooksModel(QAbstractTableModel): # {{{
|
|||||||
bool_cols_are_tristate=
|
bool_cols_are_tristate=
|
||||||
self.db.prefs.get('bools_are_tristate'))
|
self.db.prefs.get('bools_are_tristate'))
|
||||||
elif datatype in ('int', 'float'):
|
elif datatype in ('int', 'float'):
|
||||||
self.dc[col] = functools.partial(number_type, idx=idx)
|
fmt = self.custom_columns[col]['display'].get('number_format', None)
|
||||||
|
self.dc[col] = functools.partial(number_type, idx=idx, fmt=fmt)
|
||||||
elif datatype == 'datetime':
|
elif datatype == 'datetime':
|
||||||
self.dc[col] = functools.partial(datetime_type, idx=idx)
|
self.dc[col] = functools.partial(datetime_type, idx=idx)
|
||||||
elif datatype == 'bool':
|
elif datatype == 'bool':
|
||||||
|
@ -127,6 +127,9 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
|
|||||||
elif ct == 'enumeration':
|
elif ct == 'enumeration':
|
||||||
self.enum_box.setText(','.join(c['display'].get('enum_values', [])))
|
self.enum_box.setText(','.join(c['display'].get('enum_values', [])))
|
||||||
self.enum_colors.setText(','.join(c['display'].get('enum_colors', [])))
|
self.enum_colors.setText(','.join(c['display'].get('enum_colors', [])))
|
||||||
|
elif ct in ['int', 'float']:
|
||||||
|
if c['display'].get('number_format', None):
|
||||||
|
self.number_format_box.setText(c['display'].get('number_format', ''))
|
||||||
self.datatype_changed()
|
self.datatype_changed()
|
||||||
if ct in ['text', 'composite', 'enumeration']:
|
if ct in ['text', 'composite', 'enumeration']:
|
||||||
self.use_decorations.setChecked(c['display'].get('use_decorations', False))
|
self.use_decorations.setChecked(c['display'].get('use_decorations', False))
|
||||||
@ -171,6 +174,7 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
|
|||||||
col_type = None
|
col_type = None
|
||||||
for x in ('box', 'default_label', 'label'):
|
for x in ('box', 'default_label', 'label'):
|
||||||
getattr(self, 'date_format_'+x).setVisible(col_type == 'datetime')
|
getattr(self, 'date_format_'+x).setVisible(col_type == 'datetime')
|
||||||
|
getattr(self, 'number_format_'+x).setVisible(col_type in ['int', 'float'])
|
||||||
for x in ('box', 'default_label', 'label', 'sort_by', 'sort_by_label',
|
for x in ('box', 'default_label', 'label', 'sort_by', 'sort_by_label',
|
||||||
'make_category'):
|
'make_category'):
|
||||||
getattr(self, 'composite_'+x).setVisible(col_type in ['composite', '*composite'])
|
getattr(self, 'composite_'+x).setVisible(col_type in ['composite', '*composite'])
|
||||||
@ -267,6 +271,11 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
|
|||||||
display_dict = {'enum_values': l, 'enum_colors': c}
|
display_dict = {'enum_values': l, 'enum_colors': c}
|
||||||
elif col_type == 'text' and is_multiple:
|
elif col_type == 'text' and is_multiple:
|
||||||
display_dict = {'is_names': self.is_names.isChecked()}
|
display_dict = {'is_names': self.is_names.isChecked()}
|
||||||
|
elif col_type in ['int', 'float']:
|
||||||
|
if unicode(self.number_format_box.text()).strip():
|
||||||
|
display_dict = {'number_format':unicode(self.number_format_box.text()).strip()}
|
||||||
|
else:
|
||||||
|
display_dict = {'number_format': None}
|
||||||
|
|
||||||
if col_type in ['text', 'composite', 'enumeration'] and not is_multiple:
|
if col_type in ['text', 'composite', 'enumeration'] and not is_multiple:
|
||||||
display_dict['use_decorations'] = self.use_decorations.checkState()
|
display_dict['use_decorations'] = self.use_decorations.checkState()
|
||||||
|
@ -171,6 +171,20 @@ Everything else will show nothing.</string>
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLineEdit" name="number_format_box">
|
||||||
|
<property name="sizePolicy">
|
||||||
|
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
|
||||||
|
<horstretch>0</horstretch>
|
||||||
|
<verstretch>0</verstretch>
|
||||||
|
</sizepolicy>
|
||||||
|
</property>
|
||||||
|
<property name="toolTip">
|
||||||
|
<string><p>Use 0 (a zero) for the field name. Example: {0:0>5.2f} gives a 5-digit floating point number, 2 digits after the decimal point, with leading zeros
|
||||||
|
</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLabel" name="date_format_default_label">
|
<widget class="QLabel" name="date_format_default_label">
|
||||||
<property name="toolTip">
|
<property name="toolTip">
|
||||||
@ -181,6 +195,19 @@ Everything else will show nothing.</string>
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="number_format_default_label">
|
||||||
|
<property name="toolTip">
|
||||||
|
<string><p>Example: ${0:,.2f} gives floating point number prefixed by a dollar sign, 2 digits after the decimal point, with thousands separated by commas</string>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string><p>Default: Not formatted. For format language details see <a href="http://docs.python.org/library/string.html#format-string-syntax">the python documentation</a></string>
|
||||||
|
</property>
|
||||||
|
<property name="openExternalLinks">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item row="4" column="0">
|
<item row="4" column="0">
|
||||||
@ -193,6 +220,16 @@ Everything else will show nothing.</string>
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item row="4" column="0">
|
||||||
|
<widget class="QLabel" name="number_format_label">
|
||||||
|
<property name="text">
|
||||||
|
<string>Format for &numbers</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>number_format_box</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item row="5" column="0">
|
<item row="5" column="0">
|
||||||
<widget class="QLabel" name="composite_label">
|
<widget class="QLabel" name="composite_label">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user