diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py index 73d722e485..bb73a55fc9 100644 --- a/src/calibre/gui2/library/delegates.py +++ b/src/calibre/gui2/library/delegates.py @@ -269,10 +269,11 @@ class CcEnumDelegate(QStyledItemDelegate): # {{{ def setModelData(self, editor, model, index): val = unicode(editor.currentText()) - if val == '': + m = index.model() + col = m.column_map[index.column()] + if val not in m.custom_columns[col]['display']['enum_values']: # This shouldn't happen ... - m = index.model() - col = m.column_map[index.column()] + print 'shouldnt happen' val = m.custom_columns[col]['display']['enum_values'][0] model.setData(index, QVariant(val), Qt.EditRole) diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 99639ed0a7..bee90fc44c 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -719,10 +719,12 @@ class BooksModel(QAbstractTableModel): # {{{ typ = cc['datatype'] label=self.db.field_metadata.key_to_label(colhead) s_index = None - if typ in ('text', 'comments', 'enumeration'): + if typ in ('text', 'comments'): val = unicode(value.toString()).strip() val = val if val else None - if typ == 'bool': + elif typ == 'enumeration': + val = unicode(value.toString()).strip() + elif typ == 'bool': val = value.toPyObject() elif typ == 'rating': val = value.toInt()[0] diff --git a/src/calibre/gui2/preferences/create_custom_column.py b/src/calibre/gui2/preferences/create_custom_column.py index 47fcb58afb..419fed046d 100644 --- a/src/calibre/gui2/preferences/create_custom_column.py +++ b/src/calibre/gui2/preferences/create_custom_column.py @@ -166,8 +166,12 @@ class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn): if not self.enum_box.text(): return self.simple_error('', _('You must enter at least one' ' value for enumeration columns')) - display_dict = {'enum_values': - [v.strip() for v in unicode(self.enum_box.text()).split(',')]} + l = [v.strip() for v in unicode(self.enum_box.text()).split(',')] + for i in range(0, len(l)-1): + 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} db = self.parent.gui.library_view.model().db key = db.field_metadata.custom_field_prefix+col diff --git a/src/calibre/gui2/preferences/create_custom_column.ui b/src/calibre/gui2/preferences/create_custom_column.ui index 54003cd770..360c1a4345 100644 --- a/src/calibre/gui2/preferences/create_custom_column.ui +++ b/src/calibre/gui2/preferences/create_custom_column.ui @@ -212,7 +212,10 @@ - A comma-separated list of permitted values. The first value is the default + A comma-separated list of permitted values. You can specify +empty values by entering only the comma. For example, the list +',one,two,three' has 4 valid values, one of them empty. The first +value in the list is the default. diff --git a/src/calibre/library/cli.py b/src/calibre/library/cli.py index 5049476226..01e8ad449b 100644 --- a/src/calibre/library/cli.py +++ b/src/calibre/library/cli.py @@ -580,7 +580,6 @@ def command_add_custom_column(args, dbpath): print print >>sys.stderr, _('You must specify label, name and datatype') return 1 - print opts.display do_add_custom_column(get_db(dbpath, opts), args[0], args[1], args[2], opts.is_multiple, json.loads(opts.display)) # Re-open the DB so that field_metadata is reflects the column changes