More work on coloring columns. Refactor the template editor, add some documentation for the new template function first_non_empty, add help text to the configuration dialog.

This commit is contained in:
Charles Haley 2011-05-22 14:14:23 +01:00
parent ad6af10670
commit 3c92c4a988
7 changed files with 85 additions and 57 deletions

View File

@ -8,7 +8,7 @@ __docformat__ = 'restructuredtext en'
from PyQt4.Qt import (SIGNAL, QLineEdit) from PyQt4.Qt import (SIGNAL, QLineEdit)
from calibre.gui2.dialogs.template_dialog import TemplateDialog from calibre.gui2.dialogs.template_dialog import TemplateDialog
class LineEditWithTextBox(QLineEdit): class TemplateLineEditor(QLineEdit):
''' '''
Extend the context menu of a QLineEdit to include more actions. Extend the context menu of a QLineEdit to include more actions.

View File

@ -535,12 +535,10 @@ class BooksModel(QAbstractTableModel): # {{{
return img return img
def set_color_templates(self): def set_color_templates(self):
print 'here'
self.column_color_map = {} self.column_color_map = {}
for i in range(1,self.db.column_color_count+1): for i in range(1,self.db.column_color_count+1):
name = self.db.prefs.get('column_color_name_'+str(i)) name = self.db.prefs.get('column_color_name_'+str(i))
if name: if name:
print name, self.db.prefs.get('column_color_template_'+str(i))
self.column_color_map[name] = \ self.column_color_map[name] = \
self.db.prefs.get('column_color_template_'+str(i)) self.db.prefs.get('column_color_template_'+str(i))
self.refresh() self.refresh()

View File

@ -6,7 +6,7 @@ __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
from PyQt4.Qt import (QApplication, QFont, QFontInfo, QFontDialog, from PyQt4.Qt import (QApplication, QFont, QFontInfo, QFontDialog,
QAbstractListModel, Qt) QAbstractListModel, Qt, QColor)
from calibre.gui2.preferences import ConfigWidgetBase, test_widget, CommaSeparatedList from calibre.gui2.preferences import ConfigWidgetBase, test_widget, CommaSeparatedList
from calibre.gui2.preferences.look_feel_ui import Ui_Form from calibre.gui2.preferences.look_feel_ui import Ui_Form
@ -159,12 +159,36 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.df_up_button.clicked.connect(self.move_df_up) self.df_up_button.clicked.connect(self.move_df_up)
self.df_down_button.clicked.connect(self.move_df_down) self.df_down_button.clicked.connect(self.move_df_down)
self.color_help_text.setWordWrap(True)
self.color_help_text.setText('<p>' +
_('Here you can specify coloring rules for fields shown in the '
'library view. Choose the field you wish to color, then '
'supply a template that specifies the color to use.') +
'</p><p>' +
_('The template must evaluate to one of the color names shown '
'below. You can use any legal template expression. '
'For example, you can set the title to always display in '
'green using the template "green" (without the quotes). '
'To show the title in blue if the book has the tag "Science '
'Fiction", red if the book has the tag "Mystery", or black if '
'the book has neither tag, use '
'"{tags:switch(Science Fiction,blue,Mystery,red,)}" '
'To show the title in green if it has one format, blue if it '
'two formats, and red if more, use '
"\"program:cmp(count(field('formats'),','), 2, 'green', 'blue', 'red')\"") +
'</p><p>' +
_('Note: if you want to color a "custom column with a fixed set '
'of values", it is possible and often easier to specify the '
'colors in the column definition dialog. There you can '
'provide a color for each value without using a template.')+ '</p>')
choices = db.field_metadata.displayable_field_keys() choices = db.field_metadata.displayable_field_keys()
choices.sort(key=sort_key) choices.sort(key=sort_key)
choices.insert(0, '') choices.insert(0, '')
for i in range(1, db.column_color_count+1): for i in range(1, db.column_color_count+1):
r('column_color_name_'+str(i), db.prefs, choices=choices) r('column_color_name_'+str(i), db.prefs, choices=choices)
r('column_color_template_'+str(i), db.prefs) r('column_color_template_'+str(i), db.prefs)
all_colors = [unicode(s) for s in list(QColor.colorNames())]
self.colors_box.setText(', '.join(all_colors))
def initialize(self): def initialize(self):
ConfigWidgetBase.initialize(self) ConfigWidgetBase.initialize(self)

View File

@ -416,72 +416,80 @@ then the tags will be displayed each on their own line.</string>
<string>Column Coloring</string> <string>Column Coloring</string>
</attribute> </attribute>
<layout class="QGridLayout" name="column_color_layout"> <layout class="QGridLayout" name="column_color_layout">
<item row="0" column="0"> <item row="1" column="0">
<widget class="QLabel"> <widget class="QLabel">
<property name="text"> <property name="text">
<string>Column name</string> <string>Column name</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="0" colspan="2">
<widget class="QLabel" name="color_help_text">
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel"> <widget class="QLabel">
<property name="text"> <property name="text">
<string>Selection template</string> <string>Selection template</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item row="2" column="0">
<widget class="QComboBox" name="opt_column_color_name_1"> <widget class="QComboBox" name="opt_column_color_name_1">
</widget> </widget>
</item> </item>
<item row="1" column="1">
<widget class="LineEditWithTextBox" name="opt_column_color_template_1">
</widget>
</item>
<item row="2" column="0">
<widget class="QComboBox" name="opt_column_color_name_2">
</widget>
</item>
<item row="2" column="1"> <item row="2" column="1">
<widget class="LineEditWithTextBox" name="opt_column_color_template_2"> <widget class="TemplateLineEditor" name="opt_column_color_template_1">
</widget> </widget>
</item> </item>
<item row="3" column="0"> <item row="3" column="0">
<widget class="QComboBox" name="opt_column_color_name_3"> <widget class="QComboBox" name="opt_column_color_name_2">
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="3" column="1">
<widget class="LineEditWithTextBox" name="opt_column_color_template_3"> <widget class="TemplateLineEditor" name="opt_column_color_template_2">
</widget> </widget>
</item> </item>
<item row="4" column="0"> <item row="4" column="0">
<widget class="QComboBox" name="opt_column_color_name_4"> <widget class="QComboBox" name="opt_column_color_name_3">
</widget> </widget>
</item> </item>
<item row="4" column="1"> <item row="4" column="1">
<widget class="LineEditWithTextBox" name="opt_column_color_template_4"> <widget class="TemplateLineEditor" name="opt_column_color_template_3">
</widget> </widget>
</item> </item>
<item row="5" column="0"> <item row="5" column="0">
<widget class="QComboBox" name="opt_column_color_name_5"> <widget class="QComboBox" name="opt_column_color_name_4">
</widget> </widget>
</item> </item>
<item row="5" column="1"> <item row="5" column="1">
<widget class="LineEditWithTextBox" name="opt_column_color_template_5"> <widget class="TemplateLineEditor" name="opt_column_color_template_4">
</widget> </widget>
</item> </item>
<item row="10" column="0" colspan="2"> <item row="6" column="0">
<spacer name="verticalSpacer_4"> <widget class="QComboBox" name="opt_column_color_name_5">
<property name="orientation"> </widget>
<enum>Qt::Vertical</enum> </item>
<item row="6" column="1">
<widget class="TemplateLineEditor" name="opt_column_color_template_5">
</widget>
</item>
<item row="20" column="0">
<widget class="QLabel">
<property name="text">
<string>Color names</string>
</property> </property>
<property name="sizeHint" stdset="0"> </widget>
<size> </item>
<width>690</width> <item row="21" column="0" colspan="2">
<height>283</height> <widget class="QTextEdit" name="colors_box">
</size> <property name="sizePolicy">
<sizepolicy vsizetype="Expanding" hsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property> </property>
</spacer> </widget>
</item> </item>
</layout> </layout>
</widget> </widget>
@ -496,7 +504,7 @@ then the tags will be displayed each on their own line.</string>
<header>calibre/gui2/complete.h</header> <header>calibre/gui2/complete.h</header>
</customwidget> </customwidget>
<customwidget> <customwidget>
<class>LineEditWithTextBox</class> <class>TemplateLineEditor</class>
<extends>QLineEdit</extends> <extends>QLineEdit</extends>
<header>calibre/gui2/dialogs/template_line_editor.h</header> <header>calibre/gui2/dialogs/template_line_editor.h</header>
</customwidget> </customwidget>

View File

@ -7,12 +7,12 @@ __docformat__ = 'restructuredtext en'
import copy import copy
from PyQt4.Qt import Qt, QLineEdit, QComboBox, SIGNAL, QListWidgetItem from PyQt4.Qt import Qt, QComboBox, QListWidgetItem
from calibre.customize.ui import is_disabled from calibre.customize.ui import is_disabled
from calibre.gui2 import error_dialog, question_dialog from calibre.gui2 import error_dialog, question_dialog
from calibre.gui2.device import device_name_for_plugboards from calibre.gui2.device import device_name_for_plugboards
from calibre.gui2.dialogs.template_dialog import TemplateDialog from calibre.gui2.dialogs.template_line_editor import TemplateLineEditor
from calibre.gui2.preferences import ConfigWidgetBase, test_widget from calibre.gui2.preferences import ConfigWidgetBase, test_widget
from calibre.gui2.preferences.plugboard_ui import Ui_Form from calibre.gui2.preferences.plugboard_ui import Ui_Form
from calibre.customize.ui import metadata_writers, device_plugins from calibre.customize.ui import metadata_writers, device_plugins
@ -24,26 +24,6 @@ from calibre.library.server.content import plugboard_content_server_value, \
from calibre.utils.formatter import validation_formatter from calibre.utils.formatter import validation_formatter
class LineEditWithTextBox(QLineEdit):
'''
Extend the context menu of a QLineEdit to include more actions.
'''
def contextMenuEvent(self, event):
menu = self.createStandardContextMenu()
menu.addSeparator()
action_open_editor = menu.addAction(_('Open Editor'))
self.connect(action_open_editor, SIGNAL('triggered()'), self.open_editor)
menu.exec_(event.globalPos())
def open_editor(self):
t = TemplateDialog(self, self.text())
if t.exec_():
self.setText(t.textbox.toPlainText())
class ConfigWidget(ConfigWidgetBase, Ui_Form): class ConfigWidget(ConfigWidgetBase, Ui_Form):
def genesis(self, gui): def genesis(self, gui):
@ -107,7 +87,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.source_widgets = [] self.source_widgets = []
self.dest_widgets = [] self.dest_widgets = []
for i in range(0, len(self.dest_fields)-1): for i in range(0, len(self.dest_fields)-1):
w = LineEditWithTextBox(self) w = TemplateLineEditor(self)
self.source_widgets.append(w) self.source_widgets.append(w)
self.fields_layout.addWidget(w, 5+i, 0, 1, 1) self.fields_layout.addWidget(w, 5+i, 0, 1, 1)
w = QComboBox(self) w = QComboBox(self)

View File

@ -234,6 +234,7 @@ The following functions are available in addition to those described in single-f
* ``cmp(x, y, lt, eq, gt)`` -- compares x and y after converting both to numbers. Returns ``lt`` if x < y. Returns ``eq`` if x == y. Otherwise returns ``gt``. * ``cmp(x, y, lt, eq, gt)`` -- compares x and y after converting both to numbers. Returns ``lt`` if x < y. Returns ``eq`` if x == y. Otherwise returns ``gt``.
* ``divide(x, y)`` -- returns x / y. Throws an exception if either x or y are not numbers. * ``divide(x, y)`` -- returns x / y. Throws an exception if either x or y are not numbers.
* ``field(name)`` -- returns the metadata field named by ``name``. * ``field(name)`` -- returns the metadata field named by ``name``.
* ``first_non_empty(value, value, ...) -- returns the first value that is not empty. If all values are empty, then the empty value is returned. You can have as many values as you want.
* ``format_date(x, date_format)`` -- format_date(val, format_string) -- format the value, which must be a date field, using the format_string, returning a string. The formatting codes are:: * ``format_date(x, date_format)`` -- format_date(val, format_string) -- format the value, which must be a date field, using the format_string, returning a string. The formatting codes are::
d : the day as number without a leading zero (1 to 31) d : the day as number without a leading zero (1 to 31)

View File

@ -562,6 +562,22 @@ class BuiltinBooksize(BuiltinFormatterFunction):
pass pass
return '' return ''
class BuiltinFirstNonEmpty(BuiltinFormatterFunction):
name = 'first_non_empty'
arg_count = -1
doc = _('first_non_empty(value, value, ...) -- '
'returns the first value that is not empty. If all values are '
'empty, then the empty value is returned.'
'You can have as many values as you want.')
def evaluate(self, formatter, kwargs, mi, locals, *args):
i = 0
while i < len(args):
if args[i]:
return args[i]
i += 1
return ''
builtin_add = BuiltinAdd() builtin_add = BuiltinAdd()
builtin_assign = BuiltinAssign() builtin_assign = BuiltinAssign()
builtin_booksize = BuiltinBooksize() builtin_booksize = BuiltinBooksize()
@ -571,8 +587,9 @@ builtin_contains = BuiltinContains()
builtin_count = BuiltinCount() builtin_count = BuiltinCount()
builtin_divide = BuiltinDivide() builtin_divide = BuiltinDivide()
builtin_eval = BuiltinEval() builtin_eval = BuiltinEval()
builtin_format_date = BuiltinFormat_date() builtin_first_non_empty = BuiltinFirstNonEmpty()
builtin_field = BuiltinField() builtin_field = BuiltinField()
builtin_format_date = BuiltinFormat_date()
builtin_ifempty = BuiltinIfempty() builtin_ifempty = BuiltinIfempty()
builtin_list_item = BuiltinListitem() builtin_list_item = BuiltinListitem()
builtin_lookup = BuiltinLookup() builtin_lookup = BuiltinLookup()