Add option to color custom enumeration values in the library view

This commit is contained in:
Charles Haley 2011-05-21 19:24:04 +01:00
parent 22a3a7c60a
commit c3a2c3f458
3 changed files with 68 additions and 7 deletions

View File

@ -274,6 +274,31 @@ class CcEnumDelegate(QStyledItemDelegate): # {{{
Delegate for text/int/float data. 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): def createEditor(self, parent, option, index):
m = index.model() m = index.model()
col = m.column_map[index.column()] col = m.column_map[index.column()]

View File

@ -6,7 +6,7 @@ __copyright__ = '2010, Kovid Goyal <kovid at kovidgoyal.net>'
import re import re
from functools import partial 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.preferences.create_custom_column_ui import Ui_QCreateCustomColumn
from calibre.gui2 import error_dialog from calibre.gui2 import error_dialog
@ -126,6 +126,7 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
c['display'].get('make_category', False)) c['display'].get('make_category', False))
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.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))
@ -170,7 +171,7 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
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'])
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') getattr(self, 'enum_'+x).setVisible(col_type == 'enumeration')
self.use_decorations.setVisible(col_type in ['text', 'composite', 'enumeration']) self.use_decorations.setVisible(col_type in ['text', 'composite', 'enumeration'])
self.is_names.setVisible(col_type == '*text') self.is_names.setVisible(col_type == '*text')
@ -247,7 +248,21 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
if l[i] in l[i+1:]: if l[i] in l[i+1:]:
return self.simple_error('', _('The value "{0}" is in the ' return self.simple_error('', _('The value "{0}" is in the '
'list more than once').format(l[i])) '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: elif col_type == 'text' and is_multiple:
display_dict = {'is_names': self.is_names.isChecked()} display_dict = {'is_names': self.is_names.isChecked()}

View File

@ -304,8 +304,8 @@ Everything else will show nothing.</string>
</widget> </widget>
</item> </item>
<item row="6" column="2"> <item row="6" column="2">
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QGridLayout" name="horizontalLayout_2">
<item> <item row="0" column="0">
<widget class="QLineEdit" name="enum_box"> <widget class="QLineEdit" name="enum_box">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed"> <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@ -320,13 +320,34 @@ four values, the first of them being the empty value.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item row="0" column="1">
<widget class="QLabel" name="enum_default_label"> <widget class="QLabel" name="enum_default_label">
<property name="toolTip"> <property name="toolTip">
<string>The empty string is always the first value</string> <string>The empty string is always the first value</string>
</property> </property>
<property name="text"> <property name="text">
<string>Default: (nothing)</string> <string>Values</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLineEdit" name="enum_colors">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>A list of color names to use when displaying an item. The
list must be empty or contain a color for each value.</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="enum_colors_label">
<property name="text">
<string>Colors</string>
</property> </property>
</widget> </widget>
</item> </item>