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