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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. import sys
  8. from math import cos, sin, pi
  9. from PyQt4.Qt import QColor, Qt, QModelIndex, QSize, QPainterPath, QLinearGradient, QBrush, QPen, QStyle, QPainter, QStyleOptionViewItemV4, QIcon, QDoubleSpinBox, QVariant, QSpinBox, QStyledItemDelegate, QCompleter, QComboBox
  10. from calibre.gui2 import UNDEFINED_QDATE, error_dialog
  11. from calibre.gui2.widgets import EnLineEdit, TagsLineEdit
  12. from calibre.utils.date import now, format_date
  13. from calibre.utils.config import tweaks
  14. from calibre.utils.formatter import validation_formatter
  15. from calibre.gui2.dialogs.comments_dialog import CommentsDialog
  16.  
  17. class RatingDelegate(QStyledItemDelegate):
  18.     COLOR = QColor('blue')
  19.     SIZE = 16
  20.     PEN = QPen(COLOR, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
  21.     
  22.     def __init__(self, parent):
  23.         QStyledItemDelegate.__init__(self, parent)
  24.         self._parent = parent
  25.         self.dummy = QModelIndex()
  26.         self.star_path = QPainterPath()
  27.         self.star_path.moveTo(90, 50)
  28.         for i in range(1, 5):
  29.             self.star_path.lineTo(50 + 40 * cos(0.8 * i * pi), 50 + 40 * sin(0.8 * i * pi))
  30.         
  31.         self.star_path.closeSubpath()
  32.         self.star_path.setFillRule(Qt.WindingFill)
  33.         gradient = QLinearGradient(0, 0, 0, 100)
  34.         gradient.setColorAt(0, self.COLOR)
  35.         gradient.setColorAt(1, self.COLOR)
  36.         self.brush = QBrush(gradient)
  37.         self.factor = self.SIZE / 100
  38.  
  39.     
  40.     def sizeHint(self, option, index):
  41.         return QSize(5 * self.SIZE, self.SIZE + 4)
  42.  
  43.     
  44.     def paint(self, painter, option, index):
  45.         style = self._parent.style()
  46.         option = QStyleOptionViewItemV4(option)
  47.         self.initStyleOption(option, self.dummy)
  48.         num = index.model().data(index, Qt.DisplayRole).toInt()[0]
  49.         
  50.         def draw_star():
  51.             painter.save()
  52.             painter.scale(self.factor, self.factor)
  53.             painter.translate(50, 50)
  54.             painter.rotate(-20)
  55.             painter.translate(-50, -50)
  56.             painter.drawPath(self.star_path)
  57.             painter.restore()
  58.  
  59.         painter.save()
  60.         if hasattr(QStyle, 'CE_ItemViewItem'):
  61.             style.drawControl(QStyle.CE_ItemViewItem, option, painter, self._parent)
  62.         elif option.state & QStyle.State_Selected:
  63.             painter.fillRect(option.rect, option.palette.highlight())
  64.         
  65.         
  66.         try:
  67.             painter.setRenderHint(QPainter.Antialiasing)
  68.             painter.setClipRect(option.rect)
  69.             y = option.rect.center().y() - self.SIZE / 2
  70.             x = option.rect.left()
  71.             painter.setPen(self.PEN)
  72.             painter.setBrush(self.brush)
  73.             painter.translate(x, y)
  74.             i = 0
  75.             while i < num:
  76.                 draw_star()
  77.                 painter.translate(self.SIZE, 0)
  78.                 i += 1
  79.         except:
  80.             import traceback
  81.             traceback.print_exc()
  82.  
  83.         painter.restore()
  84.  
  85.     
  86.     def createEditor(self, parent, option, index):
  87.         sb = QStyledItemDelegate.createEditor(self, parent, option, index)
  88.         sb.setMinimum(0)
  89.         sb.setMaximum(5)
  90.         return sb
  91.  
  92.  
  93.  
  94. class DateDelegate(QStyledItemDelegate):
  95.     
  96.     def displayText(self, val, locale):
  97.         d = val.toDate()
  98.         if d <= UNDEFINED_QDATE:
  99.             return ''
  100.         format = tweaks['gui_timestamp_display_format']
  101.         if format is None:
  102.             format = 'dd MMM yyyy'
  103.         
  104.         return format_date(d.toPyDate(), format)
  105.  
  106.     
  107.     def createEditor(self, parent, option, index):
  108.         qde = QStyledItemDelegate.createEditor(self, parent, option, index)
  109.         qde.setDisplayFormat('dd MMM yyyy')
  110.         qde.setMinimumDate(UNDEFINED_QDATE)
  111.         qde.setSpecialValueText(_('Undefined'))
  112.         qde.setCalendarPopup(True)
  113.         return qde
  114.  
  115.  
  116.  
  117. class PubDateDelegate(QStyledItemDelegate):
  118.     
  119.     def displayText(self, val, locale):
  120.         d = val.toDate()
  121.         if d <= UNDEFINED_QDATE:
  122.             return ''
  123.         format = tweaks['gui_pubdate_display_format']
  124.         if format is None:
  125.             format = 'MMM yyyy'
  126.         
  127.         return format_date(d.toPyDate(), format)
  128.  
  129.     
  130.     def createEditor(self, parent, option, index):
  131.         qde = QStyledItemDelegate.createEditor(self, parent, option, index)
  132.         qde.setDisplayFormat('MM yyyy')
  133.         qde.setMinimumDate(UNDEFINED_QDATE)
  134.         qde.setSpecialValueText(_('Undefined'))
  135.         qde.setCalendarPopup(True)
  136.         return qde
  137.  
  138.  
  139.  
  140. class TextDelegate(QStyledItemDelegate):
  141.     
  142.     def __init__(self, parent):
  143.         QStyledItemDelegate.__init__(self, parent)
  144.         self.auto_complete_function = None
  145.  
  146.     
  147.     def set_auto_complete_function(self, f):
  148.         self.auto_complete_function = f
  149.  
  150.     
  151.     def createEditor(self, parent, option, index):
  152.         editor = EnLineEdit(parent)
  153.         return editor
  154.  
  155.  
  156.  
  157. class TagsDelegate(QStyledItemDelegate):
  158.     
  159.     def __init__(self, parent):
  160.         QStyledItemDelegate.__init__(self, parent)
  161.         self.db = None
  162.  
  163.     
  164.     def set_database(self, db):
  165.         self.db = db
  166.  
  167.     
  168.     def createEditor(self, parent, option, index):
  169.         if self.db:
  170.             col = index.model().column_map[index.column()]
  171.             if not index.model().is_custom_column(col):
  172.                 editor = TagsLineEdit(parent, self.db.all_tags())
  173.             else:
  174.                 editor = TagsLineEdit(parent, sorted(list(self.db.all_custom(label = self.db.field_metadata.key_to_label(col)))))
  175.                 return editor
  176.         index.model().is_custom_column(col)
  177.         editor = EnLineEdit(parent)
  178.         return editor
  179.  
  180.  
  181.  
  182. class CcDateDelegate(QStyledItemDelegate):
  183.     
  184.     def set_format(self, format):
  185.         if not format:
  186.             self.format = 'dd MMM yyyy'
  187.         else:
  188.             self.format = format
  189.  
  190.     
  191.     def displayText(self, val, locale):
  192.         d = val.toDate()
  193.         if d <= UNDEFINED_QDATE:
  194.             return ''
  195.         return format_date(d.toPyDate(), self.format)
  196.  
  197.     
  198.     def createEditor(self, parent, option, index):
  199.         qde = QStyledItemDelegate.createEditor(self, parent, option, index)
  200.         qde.setDisplayFormat(self.format)
  201.         qde.setMinimumDate(UNDEFINED_QDATE)
  202.         qde.setSpecialValueText(_('Undefined'))
  203.         qde.setCalendarPopup(True)
  204.         return qde
  205.  
  206.     
  207.     def setEditorData(self, editor, index):
  208.         m = index.model()
  209.         val = m.db.data[index.row()][m.custom_columns[m.column_map[index.column()]]['rec_index']]
  210.         if val is None:
  211.             val = now()
  212.         
  213.         editor.setDate(val)
  214.  
  215.     
  216.     def setModelData(self, editor, model, index):
  217.         val = editor.date()
  218.         if val <= UNDEFINED_QDATE:
  219.             val = None
  220.         
  221.         model.setData(index, QVariant(val), Qt.EditRole)
  222.  
  223.  
  224.  
  225. class CcTextDelegate(QStyledItemDelegate):
  226.     
  227.     def createEditor(self, parent, option, index):
  228.         m = index.model()
  229.         col = m.column_map[index.column()]
  230.         typ = m.custom_columns[col]['datatype']
  231.         if typ == 'int':
  232.             editor = QSpinBox(parent)
  233.             editor.setRange(-100, sys.maxint)
  234.             editor.setSpecialValueText(_('Undefined'))
  235.             editor.setSingleStep(1)
  236.         elif typ == 'float':
  237.             editor = QDoubleSpinBox(parent)
  238.             editor.setSpecialValueText(_('Undefined'))
  239.             editor.setRange(-100, float(sys.maxint))
  240.             editor.setDecimals(2)
  241.         else:
  242.             editor = EnLineEdit(parent)
  243.             complete_items = sorted(list(m.db.all_custom(label = m.db.field_metadata.key_to_label(col))))
  244.             completer = QCompleter(complete_items, self)
  245.             completer.setCaseSensitivity(Qt.CaseInsensitive)
  246.             completer.setCompletionMode(QCompleter.PopupCompletion)
  247.             editor.setCompleter(completer)
  248.         return editor
  249.  
  250.  
  251.  
  252. class CcCommentsDelegate(QStyledItemDelegate):
  253.     
  254.     def createEditor(self, parent, option, index):
  255.         m = index.model()
  256.         col = m.column_map[index.column()]
  257.         text = m.db.data[index.row()][m.custom_columns[col]['rec_index']]
  258.         editor = CommentsDialog(parent, text)
  259.         d = editor.exec_()
  260.         if d:
  261.             m.setData(index, QVariant(editor.textbox.toPlainText()), Qt.EditRole)
  262.         
  263.  
  264.     
  265.     def setModelData(self, editor, model, index):
  266.         model.setData(index, QVariant(editor.textbox.toPlainText()), Qt.EditRole)
  267.  
  268.  
  269.  
  270. class CcBoolDelegate(QStyledItemDelegate):
  271.     
  272.     def __init__(self, parent):
  273.         QStyledItemDelegate.__init__(self, parent)
  274.  
  275.     
  276.     def createEditor(self, parent, option, index):
  277.         editor = QComboBox(parent)
  278.         items = [
  279.             _('Y'),
  280.             _('N'),
  281.             ' ']
  282.         icons = [
  283.             I('ok.png'),
  284.             I('list_remove.png'),
  285.             I('blank.png')]
  286.         if tweaks['bool_custom_columns_are_tristate'] == 'no':
  287.             items = items[:-1]
  288.             icons = icons[:-1]
  289.         
  290.         for icon, text in zip(icons, items):
  291.             editor.addItem(QIcon(icon), text)
  292.         
  293.         return editor
  294.  
  295.     
  296.     def setModelData(self, editor, model, index):
  297.         val = {
  298.             0: True,
  299.             1: False,
  300.             2: None }[editor.currentIndex()]
  301.         model.setData(index, QVariant(val), Qt.EditRole)
  302.  
  303.     
  304.     def setEditorData(self, editor, index):
  305.         m = index.model()
  306.         val = m.db.data[index.row()][m.custom_columns[m.column_map[index.column()]]['rec_index']]
  307.         if tweaks['bool_custom_columns_are_tristate'] == 'no':
  308.             val = None if not val else 0
  309.         elif val is None:
  310.             pass
  311.         elif not val:
  312.             pass
  313.         
  314.         val = 0
  315.         editor.setCurrentIndex(val)
  316.  
  317.  
  318.  
  319. class CcTemplateDelegate(QStyledItemDelegate):
  320.     
  321.     def __init__(self, parent):
  322.         QStyledItemDelegate.__init__(self, parent)
  323.  
  324.     
  325.     def createEditor(self, parent, option, index):
  326.         return EnLineEdit(parent)
  327.  
  328.     
  329.     def setModelData(self, editor, model, index):
  330.         val = unicode(editor.text())
  331.         
  332.         try:
  333.             validation_formatter.validate(val)
  334.         except Exception:
  335.             err = None
  336.             error_dialog(self.parent(), _('Invalid template'), '<p>' + _('The template %s is invalid:') % val + '<br>' + str(err), show = True)
  337.  
  338.         model.setData(index, QVariant(val), Qt.EditRole)
  339.  
  340.     
  341.     def setEditorData(self, editor, index):
  342.         m = index.model()
  343.         val = m.custom_columns[m.column_map[index.column()]]['display']['composite_template']
  344.         editor.setText(val)
  345.  
  346.  
  347.