From db78e9c45ef2d6cd98eaa02ee6b01a8a0f69e9d2 Mon Sep 17 00:00:00 2001
From: Charles Haley <>
Date: Sat, 28 May 2011 12:27:49 +0100
Subject: [PATCH 2/5] Fix delegates to deal with formatted numbers
---
src/calibre/gui2/library/delegates.py | 33 +++++++++++++++++++--------
src/calibre/gui2/library/views.py | 7 ++++--
2 files changed, 29 insertions(+), 11 deletions(-)
diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py
index 50c411aaa4..6990a76b21 100644
--- a/src/calibre/gui2/library/delegates.py
+++ b/src/calibre/gui2/library/delegates.py
@@ -249,6 +249,23 @@ class CcDateDelegate(QStyledItemDelegate): # {{{
# }}}
class CcTextDelegate(QStyledItemDelegate): # {{{
+ '''
+ Delegate for text data.
+ '''
+
+ def createEditor(self, parent, option, index):
+ m = index.model()
+ col = m.column_map[index.column()]
+ editor = MultiCompleteLineEdit(parent)
+ editor.set_separator(None)
+ complete_items = sorted(list(m.db.all_custom(label=m.db.field_metadata.key_to_label(col))),
+ key=sort_key)
+ editor.update_items_cache(complete_items)
+ return editor
+
+# }}}
+
+class CcNumberDelegate(QStyledItemDelegate): # {{{
'''
Delegate for text/int/float data.
'''
@@ -256,25 +273,23 @@ class CcTextDelegate(QStyledItemDelegate): # {{{
def createEditor(self, parent, option, index):
m = index.model()
col = m.column_map[index.column()]
- typ = m.custom_columns[col]['datatype']
- if typ == 'int':
+ if m.custom_columns[col]['datatype'] == 'int':
editor = QSpinBox(parent)
editor.setRange(-100, 100000000)
editor.setSpecialValueText(_('Undefined'))
editor.setSingleStep(1)
- elif typ == 'float':
+ else:
editor = QDoubleSpinBox(parent)
editor.setSpecialValueText(_('Undefined'))
editor.setRange(-100., 100000000)
editor.setDecimals(2)
- else:
- editor = MultiCompleteLineEdit(parent)
- editor.set_separator(None)
- complete_items = sorted(list(m.db.all_custom(label=m.db.field_metadata.key_to_label(col))),
- key=sort_key)
- editor.update_items_cache(complete_items)
return editor
+ def setEditorData(self, editor, index):
+ m = index.model()
+ val = m.db.data[index.row()][m.custom_columns[m.column_map[index.column()]]['rec_index']]
+ editor.setValue(val)
+
# }}}
class CcEnumDelegate(QStyledItemDelegate): # {{{
diff --git a/src/calibre/gui2/library/views.py b/src/calibre/gui2/library/views.py
index 1cfb04921d..f59473851f 100644
--- a/src/calibre/gui2/library/views.py
+++ b/src/calibre/gui2/library/views.py
@@ -15,7 +15,7 @@ from PyQt4.Qt import QTableView, Qt, QAbstractItemView, QMenu, pyqtSignal, \
from calibre.gui2.library.delegates import RatingDelegate, PubDateDelegate, \
TextDelegate, DateDelegate, CompleteDelegate, CcTextDelegate, \
CcBoolDelegate, CcCommentsDelegate, CcDateDelegate, CcTemplateDelegate, \
- CcEnumDelegate
+ CcEnumDelegate, CcNumberDelegate
from calibre.gui2.library.models import BooksModel, DeviceBooksModel
from calibre.utils.config import tweaks, prefs
from calibre.gui2 import error_dialog, gprefs
@@ -89,6 +89,7 @@ class BooksView(QTableView): # {{{
self.cc_bool_delegate = CcBoolDelegate(self)
self.cc_comments_delegate = CcCommentsDelegate(self)
self.cc_template_delegate = CcTemplateDelegate(self)
+ self.cc_number_delegate = CcNumberDelegate(self)
self.display_parent = parent
self._model = modelcls(self)
self.setModel(self._model)
@@ -501,8 +502,10 @@ class BooksView(QTableView): # {{{
self.tags_delegate)
else:
self.setItemDelegateForColumn(cm.index(colhead), self.cc_text_delegate)
- elif cc['datatype'] in ('series', 'int', 'float'):
+ elif cc['datatype'] == 'series':
self.setItemDelegateForColumn(cm.index(colhead), self.cc_text_delegate)
+ elif cc['datatype'] in ('int', 'float'):
+ self.setItemDelegateForColumn(cm.index(colhead), self.cc_number_delegate)
elif cc['datatype'] == 'bool':
self.setItemDelegateForColumn(cm.index(colhead), self.cc_bool_delegate)
elif cc['datatype'] == 'rating':
From 0f0efc1ca17d94bf2540d6a792270a742e0ecaf6 Mon Sep 17 00:00:00 2001
From: Charles Haley <>
Date: Sat, 28 May 2011 19:42:36 +0100
Subject: [PATCH 3/5] Some improvements to the number format tooltips.
---
src/calibre/gui2/preferences/create_custom_column.py | 12 ++++++++++++
src/calibre/gui2/preferences/create_custom_column.ui | 8 +++-----
2 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/src/calibre/gui2/preferences/create_custom_column.py b/src/calibre/gui2/preferences/create_custom_column.py
index f3fe8f03a3..8eaa2dd7d9 100644
--- a/src/calibre/gui2/preferences/create_custom_column.py
+++ b/src/calibre/gui2/preferences/create_custom_column.py
@@ -182,6 +182,18 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
getattr(self, 'enum_'+x).setVisible(col_type == 'enumeration')
self.use_decorations.setVisible(col_type in ['text', 'composite', 'enumeration'])
self.is_names.setVisible(col_type == '*text')
+ if col_type == 'int':
+ self.number_format_box.setToolTip('' +
+ _('Examples: The format {0:0>4d}
'
+ 'gives a 4-digit number with leading zeros. The format '
+ '{0:d} days
prints the number then the word "days"')+ '
')
+ elif col_type == 'float':
+ self.number_format_box.setToolTip('' +
+ _('Examples: The format {0:.1f}
gives a floating '
+ 'point number with 1 digit after the decimal point. The format '
+ 'Price: $ {0:,.2f}
prints '
+ '"Price $ " then displays the number with 2 digits '
+ 'after the decimal point and thousands separated by commas.') + '
')
def accept(self):
col = unicode(self.column_name_box.text()).strip()
diff --git a/src/calibre/gui2/preferences/create_custom_column.ui b/src/calibre/gui2/preferences/create_custom_column.ui
index 02daae988f..cedbfd72b8 100644
--- a/src/calibre/gui2/preferences/create_custom_column.ui
+++ b/src/calibre/gui2/preferences/create_custom_column.ui
@@ -179,10 +179,6 @@ Everything else will show nothing.
0
-