| 1 | # ===========================================================================
|
|---|
| 2 | #
|
|---|
| 3 | # Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved.
|
|---|
| 4 | # www.conceptive.be / project-camelot@conceptive.be
|
|---|
| 5 | #
|
|---|
| 6 | # This file is part of the Camelot Library.
|
|---|
| 7 | #
|
|---|
| 8 | # This file may be used under the terms of the GNU General Public
|
|---|
| 9 | # License version 2.0 as published by the Free Software Foundation
|
|---|
| 10 | # and appearing in the file LICENSE.GPL included in the packaging of
|
|---|
| 11 | # this file. Please review the following information to ensure GNU
|
|---|
| 12 | # General Public Licensing requirements will be met:
|
|---|
| 13 | # http://www.trolltech.com/products/qt/opensource.html
|
|---|
| 14 | #
|
|---|
| 15 | # If you are unsure which license is appropriate for your use, please
|
|---|
| 16 | # review the following information:
|
|---|
| 17 | # http://www.trolltech.com/products/qt/licensing.html or contact
|
|---|
| 18 | # project-camelot@conceptive.be.
|
|---|
| 19 | #
|
|---|
| 20 | # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 21 | # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 22 | #
|
|---|
| 23 | # For use of this library in commercial applications, please contact
|
|---|
| 24 | # project-camelot@conceptive.be
|
|---|
| 25 | #
|
|---|
| 26 | # ===========================================================================
|
|---|
| 27 |
|
|---|
| 28 | from PyQt4 import QtGui, QtCore
|
|---|
| 29 | from PyQt4.QtCore import Qt, SIGNAL
|
|---|
| 30 | from PyQt4.QtGui import QItemDelegate
|
|---|
| 31 | from camelot.core.utils import variant_to_pyobject
|
|---|
| 32 |
|
|---|
| 33 | from camelot.view.controls import editors
|
|---|
| 34 | from camelot.core.utils import create_constant_function
|
|---|
| 35 | from camelot.view.proxy import ValueLoading
|
|---|
| 36 |
|
|---|
| 37 | # custom color
|
|---|
| 38 | not_editable_background = QtGui.QColor(235, 233, 237)
|
|---|
| 39 | # darkgray
|
|---|
| 40 | not_editable_foreground = QtGui.QColor(Qt.darkGray)
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | def DocumentationMetaclass(name, bases, dct):
|
|---|
| 44 | dct['__doc__'] = dct.get('__doc__','') + """
|
|---|
| 45 |
|
|---|
| 46 | .. image:: ../_static/delegates/%s_unselected_disabled.png
|
|---|
| 47 | .. image:: ../_static/delegates/%s_unselected_editable.png
|
|---|
| 48 |
|
|---|
| 49 | .. image:: ../_static/delegates/%s_selected_disabled.png
|
|---|
| 50 | .. image:: ../_static/delegates/%s_selected_editable.png
|
|---|
| 51 |
|
|---|
| 52 | """%(name, name, name, name,)
|
|---|
| 53 |
|
|---|
| 54 | if 'editor' in dct:
|
|---|
| 55 | dct['__doc__'] = dct['__doc__'] + '.. image:: ../_static/editors/%s_editable.png'%dct['editor'].__name__ + '\n'
|
|---|
| 56 |
|
|---|
| 57 | if '__init__' in dct:
|
|---|
| 58 | dct['__doc__'] = dct['__doc__'] + 'Field attributes supported by this delegate : \n'
|
|---|
| 59 | import inspect
|
|---|
| 60 | args, _varargs, _varkw, _defaults = inspect.getargspec(dct['__init__'])
|
|---|
| 61 | for arg in args:
|
|---|
| 62 | if arg not in ['self', 'parent']:
|
|---|
| 63 | dct['__doc__'] = dct['__doc__'] + '\n * %s'%arg
|
|---|
| 64 | return type(name, bases, dct)
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 | class CustomDelegate(QItemDelegate):
|
|---|
| 68 | """Base class for implementing custom delegates.
|
|---|
| 69 |
|
|---|
| 70 | .. attribute:: editor
|
|---|
| 71 |
|
|---|
| 72 | class attribute specifies the editor class that should be used
|
|---|
| 73 | """
|
|---|
| 74 |
|
|---|
| 75 | editor = None
|
|---|
| 76 |
|
|---|
| 77 | def __init__(self, parent=None, editable=True, **kwargs):
|
|---|
| 78 | """:param parent: the parent object for the delegate
|
|---|
| 79 | :param editable: a boolean indicating if the field associated to the delegate
|
|---|
| 80 | is editable"""
|
|---|
| 81 | QItemDelegate.__init__(self, parent)
|
|---|
| 82 | self.editable = editable
|
|---|
| 83 | self.kwargs = kwargs
|
|---|
| 84 | self._font_metrics = QtGui.QFontMetrics(QtGui.QApplication.font())
|
|---|
| 85 | self._height = self._font_metrics.lineSpacing() + 10
|
|---|
| 86 | self._width = self._font_metrics.averageCharWidth() * 20
|
|---|
| 87 |
|
|---|
| 88 | def createEditor(self, parent, option, index):
|
|---|
| 89 | """:param option: use an option with version 5 to indicate the widget
|
|---|
| 90 | will be put onto a form"""
|
|---|
| 91 | editor = self.editor(parent, editable=self.editable, **self.kwargs)
|
|---|
| 92 | assert editor
|
|---|
| 93 | assert isinstance(editor, (QtGui.QWidget,))
|
|---|
| 94 | if option.version != 5:
|
|---|
| 95 | editor.setAutoFillBackground(True)
|
|---|
| 96 | self.connect(editor,
|
|---|
| 97 | editors.editingFinished,
|
|---|
| 98 | self.commitAndCloseEditor)
|
|---|
| 99 | return editor
|
|---|
| 100 |
|
|---|
| 101 | def sizeHint(self, option, index):
|
|---|
| 102 | return QtCore.QSize(self._width, self._height)
|
|---|
| 103 |
|
|---|
| 104 | def commitAndCloseEditor(self):
|
|---|
| 105 | editor = self.sender()
|
|---|
| 106 | assert editor
|
|---|
| 107 | assert isinstance(editor, (QtGui.QWidget,))
|
|---|
| 108 | self.emit(SIGNAL('commitData(QWidget*)'), editor)
|
|---|
| 109 | sig = SIGNAL('closeEditor(QWidget*, \
|
|---|
| 110 | QAbstractItemDelegate::EndEditHint)')
|
|---|
| 111 | self.emit(sig, editor, QtGui.QAbstractItemDelegate.NoHint)
|
|---|
| 112 |
|
|---|
| 113 | def setEditorData(self, editor, index):
|
|---|
| 114 | value = variant_to_pyobject(index.model().data(index, Qt.EditRole))
|
|---|
| 115 | editor.set_value(value)
|
|---|
| 116 | index.model().data(index, Qt.ToolTipRole)
|
|---|
| 117 | tip = variant_to_pyobject(index.model().data(index, Qt.ToolTipRole))
|
|---|
| 118 | if tip not in (None, ValueLoading):
|
|---|
| 119 | editor.setToolTip(unicode(tip))
|
|---|
| 120 | else:
|
|---|
| 121 | editor.setToolTip('')
|
|---|
| 122 | background_color = variant_to_pyobject(index.model().data(index, Qt.BackgroundRole))
|
|---|
| 123 | if background_color not in (None, ValueLoading):
|
|---|
| 124 | editor.set_background_color(background_color)
|
|---|
| 125 |
|
|---|
| 126 | def setModelData(self, editor, model, index):
|
|---|
| 127 | if isinstance(model, QtGui.QStandardItemModel):
|
|---|
| 128 | val = QtCore.QVariant(editor.get_value())
|
|---|
| 129 | else:
|
|---|
| 130 | val = create_constant_function(editor.get_value())
|
|---|
| 131 | model.setData(index, val)
|
|---|
| 132 |
|
|---|
| 133 | def paint_text(self, painter, option, index, text, margin_left=0, margin_right=0):
|
|---|
| 134 | """Paint unicode text into the given rect defined by option, and fill the rect with
|
|---|
| 135 | the background color
|
|---|
| 136 | :arg margin_left: additional margin to the left, to be used for icons or others
|
|---|
| 137 | :arg margin_right: additional margin to the right, to be used for icons or others"""
|
|---|
| 138 |
|
|---|
| 139 | background_color = QtGui.QColor(index.model().data(index, Qt.BackgroundRole))
|
|---|
| 140 | rect = option.rect
|
|---|
| 141 |
|
|---|
| 142 | if( option.state & QtGui.QStyle.State_Selected ):
|
|---|
| 143 | painter.fillRect(option.rect, option.palette.highlight())
|
|---|
| 144 | fontColor = QtGui.QColor()
|
|---|
| 145 | if self.editable:
|
|---|
| 146 | Color = option.palette.highlightedText().color()
|
|---|
| 147 | fontColor.setRgb(Color.red(), Color.green(), Color.blue())
|
|---|
| 148 | else:
|
|---|
| 149 | fontColor.setRgb(130,130,130)
|
|---|
| 150 | else:
|
|---|
| 151 | if self.editable:
|
|---|
| 152 | painter.fillRect(rect, background_color)
|
|---|
| 153 | fontColor = QtGui.QColor()
|
|---|
| 154 | fontColor.setRgb(0,0,0)
|
|---|
| 155 | else:
|
|---|
| 156 | painter.fillRect(rect, option.palette.window())
|
|---|
| 157 | fontColor = QtGui.QColor()
|
|---|
| 158 | fontColor.setRgb(130,130,130)
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | painter.setPen(fontColor.toRgb())
|
|---|
| 162 | painter.drawText(rect.x() + 2 + margin_left,
|
|---|
| 163 | rect.y(),
|
|---|
| 164 | rect.width() - 4 - (margin_left + margin_right),
|
|---|
| 165 | rect.height(),
|
|---|
| 166 | Qt.AlignVCenter | Qt.AlignLeft,
|
|---|
| 167 | text)
|
|---|