home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1339 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  7.2 KB  |  185 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid at kovidgoyal.net>'
  6. import re
  7. from functools import partial
  8. from PyQt4.QtCore import SIGNAL
  9. from PyQt4.Qt import QDialog, Qt, QListWidgetItem, QVariant
  10. from calibre.gui2.preferences.create_custom_column_ui import Ui_QCreateCustomColumn
  11. from calibre.gui2 import error_dialog
  12.  
  13. class CreateCustomColumn(QDialog, Ui_QCreateCustomColumn):
  14.     column_types = {
  15.         0: {
  16.             'datatype': 'text',
  17.             'text': _('Text, column shown in the tag browser'),
  18.             'is_multiple': False },
  19.         1: {
  20.             'datatype': '*text',
  21.             'text': _('Comma separated text, like tags, shown in the tag browser'),
  22.             'is_multiple': True },
  23.         2: {
  24.             'datatype': 'comments',
  25.             'text': _('Long text, like comments, not shown in the tag browser'),
  26.             'is_multiple': False },
  27.         3: {
  28.             'datatype': 'series',
  29.             'text': _('Text column for keeping series-like information'),
  30.             'is_multiple': False },
  31.         4: {
  32.             'datatype': 'datetime',
  33.             'text': _('Date'),
  34.             'is_multiple': False },
  35.         5: {
  36.             'datatype': 'float',
  37.             'text': _('Floating point numbers'),
  38.             'is_multiple': False },
  39.         6: {
  40.             'datatype': 'int',
  41.             'text': _('Integers'),
  42.             'is_multiple': False },
  43.         7: {
  44.             'datatype': 'rating',
  45.             'text': _('Ratings, shown with stars'),
  46.             'is_multiple': False },
  47.         8: {
  48.             'datatype': 'bool',
  49.             'text': _('Yes/No'),
  50.             'is_multiple': False },
  51.         9: {
  52.             'datatype': 'composite',
  53.             'text': _('Column built from other columns'),
  54.             'is_multiple': False } }
  55.     
  56.     def __init__(self, parent, editing, standard_colheads, standard_colnames):
  57.         QDialog.__init__(self, parent)
  58.         Ui_QCreateCustomColumn.__init__(self)
  59.         self.setupUi(self)
  60.         icon = self.windowIcon()
  61.         self.setWindowFlags(self.windowFlags() & ~(Qt.WindowContextHelpButtonHint))
  62.         self.setWindowIcon(icon)
  63.         self.simple_error = partial(error_dialog, self, show = True, show_copy_button = False)
  64.         self.connect(self.button_box, SIGNAL('accepted()'), self.accept)
  65.         self.connect(self.button_box, SIGNAL('rejected()'), self.reject)
  66.         self.parent = parent
  67.         self.editing_col = editing
  68.         self.standard_colheads = standard_colheads
  69.         self.standard_colnames = standard_colnames
  70.         for t in self.column_types:
  71.             self.column_type_box.addItem(self.column_types[t]['text'])
  72.         
  73.         self.column_type_box.currentIndexChanged.connect(self.datatype_changed)
  74.         if not self.editing_col:
  75.             self.datatype_changed()
  76.             self.exec_()
  77.             return None
  78.         idx = parent.opt_columns.currentRow()
  79.         if idx < 0:
  80.             self.simple_error(_('No column selected'), _('No column has been selected'))
  81.             return None
  82.         col = unicode(parent.opt_columns.item(idx).data(Qt.UserRole).toString())
  83.         if col not in parent.custcols:
  84.             self.simple_error('', _('Selected column is not a user-defined column'))
  85.             return None
  86.         c = parent.custcols[col]
  87.         self.column_name_box.setText(c['label'])
  88.         self.column_heading_box.setText(c['name'])
  89.         ct = col not in parent.custcols if not c['is_multiple'] else '*text'
  90.         self.orig_column_number = c['colnum']
  91.         self.orig_column_name = col
  92.         column_numbers = dict((map,)((lambda x: (self.column_types[x]['datatype'], x)), self.column_types))
  93.         self.column_type_box.setCurrentIndex(column_numbers[ct])
  94.         self.column_type_box.setEnabled(False)
  95.         if ct == 'datetime':
  96.             if c['display'].get('date_format', None):
  97.                 self.date_format_box.setText(c['display'].get('date_format', ''))
  98.             
  99.         elif ct == 'composite':
  100.             self.composite_box.setText(c['display'].get('composite_template', ''))
  101.         
  102.         self.datatype_changed()
  103.         self.exec_()
  104.  
  105.     
  106.     def datatype_changed(self, *args):
  107.         
  108.         try:
  109.             col_type = self.column_types[self.column_type_box.currentIndex()]['datatype']
  110.         except:
  111.             col_type = None
  112.  
  113.         for x in ('box', 'default_label', 'label'):
  114.             getattr(self, 'date_format_' + x).setVisible(col_type == 'datetime')
  115.         
  116.         for x in ('box', 'default_label', 'label'):
  117.             getattr(self, 'composite_' + x).setVisible(col_type == 'composite')
  118.         
  119.  
  120.     
  121.     def accept(self):
  122.         col = unicode(self.column_name_box.text())
  123.         if not col:
  124.             return self.simple_error('', _('No lookup name was provided'))
  125.         if re.match('^\\w*$', col) is None and not col[0].isalpha() or col.lower() != col:
  126.             return self.simple_error('', _('The lookup name must contain only lower case letters, digits and underscores, and start with a letter'))
  127.         if col.endswith('_index'):
  128.             return self.simple_error('', _('Lookup names cannot end with _index, because these names are reserved for the index of a series column.'))
  129.         col_heading = unicode(self.column_heading_box.text())
  130.         col_type = self.column_types[self.column_type_box.currentIndex()]['datatype']
  131.         if not col_heading:
  132.             return self.simple_error('', _('No column heading was provided'))
  133.         bad_col = False
  134.         if bad_col:
  135.             return self.simple_error('', _('The lookup name %s is already used') % col)
  136.         bad_head = False
  137.         for t in self.parent.custcols:
  138.             if self.parent.custcols[t]['name'] == col_heading:
  139.                 if not (self.editing_col) or self.parent.custcols[t]['colnum'] != self.orig_column_number:
  140.                     bad_head = True
  141.                 
  142.             self.parent.custcols[t]['colnum'] != self.orig_column_number
  143.         
  144.         for t in self.standard_colheads:
  145.             if self.standard_colheads[t] == col_heading:
  146.                 bad_head = True
  147.                 continue
  148.             bad_col
  149.         
  150.         if bad_head:
  151.             return self.simple_error('', _('The heading %s is already used') % col_heading)
  152.         display_dict = { }
  153.         db = self.parent.gui.library_view.model().db
  154.         key = db.field_metadata.custom_field_prefix + col
  155.         if not self.editing_col:
  156.             db.field_metadata
  157.             self.parent.custcols[key] = {
  158.                 'label': col,
  159.                 'name': col_heading,
  160.                 'datatype': col_type,
  161.                 'display': display_dict,
  162.                 'normalized': None,
  163.                 'colnum': None,
  164.                 'is_multiple': is_multiple }
  165.             item = QListWidgetItem(col_heading, self.parent.opt_columns)
  166.             item.setData(Qt.UserRole, QVariant(key))
  167.             item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsSelectable)
  168.             item.setCheckState(Qt.Checked)
  169.         else:
  170.             idx = self.parent.opt_columns.currentRow()
  171.             item = self.parent.opt_columns.item(idx)
  172.             item.setText(col_heading)
  173.             self.parent.custcols[self.orig_column_name]['label'] = col
  174.             self.parent.custcols[self.orig_column_name]['name'] = col_heading
  175.             self.parent.custcols[self.orig_column_name]['display'].update(display_dict)
  176.             self.parent.custcols[self.orig_column_name]['*edited'] = True
  177.             self.parent.custcols[self.orig_column_name]['*must_restart'] = True
  178.         QDialog.accept(self)
  179.  
  180.     
  181.     def reject(self):
  182.         QDialog.reject(self)
  183.  
  184.  
  185.