home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1242 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.8 KB  |  173 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.dialogs.config.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.     
  52.     def __init__(self, parent, editing, standard_colheads, standard_colnames):
  53.         QDialog.__init__(self, parent)
  54.         Ui_QCreateCustomColumn.__init__(self)
  55.         self.setupUi(self)
  56.         icon = self.windowIcon()
  57.         self.setWindowFlags(self.windowFlags() & ~(Qt.WindowContextHelpButtonHint))
  58.         self.setWindowIcon(icon)
  59.         self.simple_error = partial(error_dialog, self, show = True, show_copy_button = False)
  60.         self.connect(self.button_box, SIGNAL('accepted()'), self.accept)
  61.         self.connect(self.button_box, SIGNAL('rejected()'), self.reject)
  62.         self.parent = parent
  63.         self.editing_col = editing
  64.         self.standard_colheads = standard_colheads
  65.         self.standard_colnames = standard_colnames
  66.         for t in self.column_types:
  67.             self.column_type_box.addItem(self.column_types[t]['text'])
  68.         
  69.         self.column_type_box.currentIndexChanged.connect(self.datatype_changed)
  70.         if not self.editing_col:
  71.             self.datatype_changed()
  72.             self.exec_()
  73.             return None
  74.         idx = parent.columns.currentRow()
  75.         if idx < 0:
  76.             self.simple_error(_('No column selected'), _('No column has been selected'))
  77.             return None
  78.         col = unicode(parent.columns.item(idx).data(Qt.UserRole).toString())
  79.         if col not in parent.custcols:
  80.             self.simple_error('', _('Selected column is not a user-defined column'))
  81.             return None
  82.         c = parent.custcols[col]
  83.         self.column_name_box.setText(c['label'])
  84.         self.column_heading_box.setText(c['name'])
  85.         ct = col not in parent.custcols if not c['is_multiple'] else '*text'
  86.         self.orig_column_number = c['colnum']
  87.         self.orig_column_name = col
  88.         column_numbers = dict((map,)((lambda x: (self.column_types[x]['datatype'], x)), self.column_types))
  89.         self.column_type_box.setCurrentIndex(column_numbers[ct])
  90.         self.column_type_box.setEnabled(False)
  91.         self.datatype_changed()
  92.         self.exec_()
  93.  
  94.     
  95.     def datatype_changed(self, *args):
  96.         
  97.         try:
  98.             col_type = self.column_types[self.column_type_box.currentIndex()]['datatype']
  99.         except:
  100.             col_type = None
  101.  
  102.         df_visible = col_type == 'datetime'
  103.         for x in ('box', 'default_label', 'label'):
  104.             getattr(self, 'date_format_' + x).setVisible(df_visible)
  105.         
  106.  
  107.     
  108.     def accept(self):
  109.         col = unicode(self.column_name_box.text()).lower()
  110.         if not col:
  111.             return self.simple_error('', _('No lookup name was provided'))
  112.         if re.match('^\\w*$', col) is None or not col[0].isalpha():
  113.             return self.simple_error('', _('The label must contain only letters, digits and underscores, and start with a letter'))
  114.         col_heading = unicode(self.column_heading_box.text())
  115.         col_type = self.column_types[self.column_type_box.currentIndex()]['datatype']
  116.         if not col_heading:
  117.             return self.simple_error('', _('No column heading was provided'))
  118.         bad_col = False
  119.         if bad_col:
  120.             return self.simple_error('', _('The lookup name %s is already used') % col)
  121.         bad_head = False
  122.         for t in self.parent.custcols:
  123.             if self.parent.custcols[t]['name'] == col_heading:
  124.                 if not (self.editing_col) or self.parent.custcols[t]['colnum'] != self.orig_column_number:
  125.                     bad_head = True
  126.                 
  127.             self.parent.custcols[t]['colnum'] != self.orig_column_number
  128.         
  129.         for t in self.standard_colheads:
  130.             if self.standard_colheads[t] == col_heading:
  131.                 bad_head = True
  132.                 continue
  133.             bad_col
  134.         
  135.         if bad_head:
  136.             return self.simple_error('', _('The heading %s is already used') % col_heading)
  137.         if ':' in col and ' ' in col or col.lower() != col:
  138.             return self.simple_error('', _('The lookup name must be lower case and cannot contain ":"s or spaces'))
  139.         date_format = { }
  140.         key = self.parent.db.field_metadata.custom_field_prefix + col
  141.         if not self.editing_col:
  142.             self.parent.db.field_metadata
  143.             self.parent.custcols[key] = {
  144.                 'label': col,
  145.                 'name': col_heading,
  146.                 'datatype': col_type,
  147.                 'editable': True,
  148.                 'display': date_format,
  149.                 'normalized': None,
  150.                 'colnum': None,
  151.                 'is_multiple': is_multiple }
  152.             item = QListWidgetItem(col_heading, self.parent.columns)
  153.             item.setData(Qt.UserRole, QVariant(key))
  154.             item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsSelectable)
  155.             item.setCheckState(Qt.Checked)
  156.         else:
  157.             idx = self.parent.columns.currentRow()
  158.             item = self.parent.columns.item(idx)
  159.             item.setData(Qt.UserRole, QVariant(key))
  160.             item.setText(col_heading)
  161.             self.parent.custcols[self.orig_column_name]['label'] = col
  162.             self.parent.custcols[self.orig_column_name]['name'] = col_heading
  163.             self.parent.custcols[self.orig_column_name]['display'].update(date_format)
  164.             self.parent.custcols[self.orig_column_name]['*edited'] = True
  165.             self.parent.custcols[self.orig_column_name]['*must_restart'] = True
  166.         QDialog.accept(self)
  167.  
  168.     
  169.     def reject(self):
  170.         QDialog.reject(self)
  171.  
  172.  
  173.