From c3a2c3f458304abaef826e08f61265a235df5d50 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sat, 21 May 2011 19:24:04 +0100 Subject: [PATCH] Add option to color custom enumeration values in the library view --- src/calibre/gui2/library/delegates.py | 25 ++++++++++++++++ .../gui2/preferences/create_custom_column.py | 21 ++++++++++++-- .../gui2/preferences/create_custom_column.ui | 29 ++++++++++++++++--- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py index e2234f6df5..ae01081736 100644 --- a/src/calibre/gui2/library/delegates.py +++ b/src/calibre/gui2/library/delegates.py @@ -274,6 +274,31 @@ class CcEnumDelegate(QStyledItemDelegate): # {{{ Delegate for text/int/float data. ''' + def __init__(self, parent): + QStyledItemDelegate.__init__(self, parent) + self.document = QTextDocument() + + def paint(self, painter, option, index): + style = self.parent().style() + txt = unicode(index.data(Qt.DisplayRole).toString()) + self.document.setPlainText(txt) + painter.save() + if hasattr(QStyle, 'CE_ItemViewItem'): + style.drawControl(QStyle.CE_ItemViewItem, option, + painter, self.parent()) + elif option.state & QStyle.State_Selected: + painter.fillRect(option.rect, option.palette.highlight()) + m = index.model() + col = m.column_map[index.column()] + colors = m.custom_columns[col]['display'].get('enum_colors', []) + values = m.custom_columns[col]['display']['enum_values'] + if len(colors) > 0 and txt in values: + painter.fillRect(option.rect, QColor(colors[values.index(txt)])) + painter.setClipRect(option.rect) + painter.translate(option.rect.topLeft()) + self.document.drawContents(painter) + painter.restore() + def createEditor(self, parent, option, index): m = index.model() col = m.column_map[index.column()] diff --git a/src/calibre/gui2/preferences/create_custom_column.py b/src/calibre/gui2/preferences/create_custom_column.py index 7b891b782c..180c3aed7a 100644 --- a/src/calibre/gui2/preferences/create_custom_column.py +++ b/src/calibre/gui2/preferences/create_custom_column.py @@ -6,7 +6,7 @@ __copyright__ = '2010, Kovid Goyal ' import re from functools import partial -from PyQt4.Qt import QDialog, Qt, QListWidgetItem, QVariant +from PyQt4.Qt import QDialog, Qt, QListWidgetItem, QVariant, QColor from calibre.gui2.preferences.create_custom_column_ui import Ui_QCreateCustomColumn from calibre.gui2 import error_dialog @@ -126,6 +126,7 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn): c['display'].get('make_category', False)) elif ct == 'enumeration': self.enum_box.setText(','.join(c['display'].get('enum_values', []))) + self.enum_colors.setText(','.join(c['display'].get('enum_colors', []))) self.datatype_changed() if ct in ['text', 'composite', 'enumeration']: self.use_decorations.setChecked(c['display'].get('use_decorations', False)) @@ -170,7 +171,7 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn): for x in ('box', 'default_label', 'label', 'sort_by', 'sort_by_label', 'make_category'): getattr(self, 'composite_'+x).setVisible(col_type in ['composite', '*composite']) - for x in ('box', 'default_label', 'label'): + for x in ('box', 'default_label', 'label', 'colors', 'colors_label'): 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') @@ -247,7 +248,21 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn): if l[i] in l[i+1:]: return self.simple_error('', _('The value "{0}" is in the ' 'list more than once').format(l[i])) - display_dict = {'enum_values': l} + c = unicode(self.enum_colors.text()) + if c: + c = [v.strip() for v in unicode(self.enum_colors.text()).split(',')] + else: + c = [] + print c, len(c) + if len(c) != 0 and len(c) != len(l): + return self.simple_error('', _('The colors box must be empty or ' + 'contain the same number of items as the value box')) + for tc in c: + if tc not in QColor.colorNames(): + return self.simple_error('', + _('The color {0} is unknown').format(tc)) + + display_dict = {'enum_values': l, 'enum_colors': c} elif col_type == 'text' and is_multiple: display_dict = {'is_names': self.is_names.isChecked()} diff --git a/src/calibre/gui2/preferences/create_custom_column.ui b/src/calibre/gui2/preferences/create_custom_column.ui index 619b0c6212..2bdadd4b9d 100644 --- a/src/calibre/gui2/preferences/create_custom_column.ui +++ b/src/calibre/gui2/preferences/create_custom_column.ui @@ -304,8 +304,8 @@ Everything else will show nothing. - - + + @@ -320,13 +320,34 @@ four values, the first of them being the empty value. - + The empty string is always the first value - Default: (nothing) + Values + + + + + + + + 0 + 0 + + + + A list of color names to use when displaying an item. The +list must be empty or contain a color for each value. + + + + + + + Colors