[ad6b4e2] | 1 | from PyQt4 import QtGui |
---|
| 2 | from PyQt4 import QtCore |
---|
| 3 | |
---|
| 4 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[6011788] | 5 | |
---|
| 6 | # Main parameter table view columns |
---|
| 7 | PARAM_PROPERTY=0 |
---|
| 8 | PARAM_VALUE=1 |
---|
| 9 | PARAM_MIN=2 |
---|
| 10 | PARAM_MAX=3 |
---|
| 11 | PARAM_UNIT=4 |
---|
| 12 | |
---|
| 13 | # polydispersity functions |
---|
| 14 | POLYDISPERSE_FUNCTIONS=['rectangle', 'array', 'lognormal', 'gaussian', 'schulz'] |
---|
| 15 | # polydispersity columns |
---|
| 16 | POLY_PARAMETER=0 |
---|
| 17 | POLY_PD=1 |
---|
| 18 | POLY_MIN=2 |
---|
| 19 | POLY_MAX=3 |
---|
| 20 | POLY_NPTS=4 |
---|
| 21 | POLY_NSIGS=5 |
---|
| 22 | POLY_FUNCTION=6 |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | class ModelViewDelegate(QtGui.QStyledItemDelegate): |
---|
| 26 | """ |
---|
| 27 | Custom delegate for appearance and behavior control of the model view |
---|
| 28 | """ |
---|
| 29 | def paint(self, painter, option, index): |
---|
| 30 | """ |
---|
| 31 | Overwrite generic painter for certain columns |
---|
| 32 | """ |
---|
| 33 | if index.column() in (PARAM_UNIT, PARAM_MIN, PARAM_MAX): |
---|
| 34 | # Units - present in nice HTML |
---|
| 35 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
| 36 | self.initStyleOption(options,index) |
---|
[2a432e7] | 37 | |
---|
[6011788] | 38 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
| 39 | |
---|
| 40 | # Prepare document for inserting into cell |
---|
| 41 | doc = QtGui.QTextDocument() |
---|
| 42 | |
---|
| 43 | # Convert the unit description into HTML |
---|
| 44 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
| 45 | doc.setHtml(text_html) |
---|
| 46 | |
---|
| 47 | # delete the original content |
---|
| 48 | options.text = "" |
---|
[2a432e7] | 49 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
[6011788] | 50 | |
---|
| 51 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
| 52 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
| 53 | |
---|
| 54 | painter.save() |
---|
| 55 | painter.translate(textRect.topLeft()) |
---|
| 56 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
| 57 | # Draw the QTextDocument in the cell |
---|
| 58 | doc.documentLayout().draw(painter, context) |
---|
| 59 | painter.restore() |
---|
| 60 | else: |
---|
| 61 | # Just the default paint |
---|
| 62 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|
| 63 | |
---|
| 64 | def createEditor(self, widget, option, index): |
---|
| 65 | """ |
---|
| 66 | Overwrite generic editor for certain columns |
---|
| 67 | """ |
---|
| 68 | if not index.isValid(): |
---|
| 69 | return 0 |
---|
| 70 | if index.column() == PARAM_VALUE: #only in the value column |
---|
| 71 | editor = QtGui.QLineEdit(widget) |
---|
| 72 | validator = QtGui.QDoubleValidator() |
---|
| 73 | editor.setValidator(validator) |
---|
| 74 | return editor |
---|
| 75 | |
---|
| 76 | return super(ModelViewDelegate, self).createEditor(widget, option, index) |
---|
| 77 | |
---|
[00b3b40] | 78 | def setModelData(self, editor, model, index): |
---|
| 79 | """ |
---|
| 80 | Overwrite generic model update method for certain columns |
---|
| 81 | """ |
---|
[dc5ef15] | 82 | if index.column() in (PARAM_MIN, PARAM_MAX): |
---|
[6011788] | 83 | try: |
---|
[00b3b40] | 84 | value_float = float(editor.text()) |
---|
[6011788] | 85 | except ValueError: |
---|
[00b3b40] | 86 | # TODO: present the failure to the user |
---|
| 87 | # balloon popup? tooltip? cell background colour flash? |
---|
[6011788] | 88 | return |
---|
[00b3b40] | 89 | QtGui.QStyledItemDelegate.setModelData(self, editor, model, index) |
---|
[6011788] | 90 | |
---|
| 91 | |
---|
| 92 | class PolyViewDelegate(QtGui.QStyledItemDelegate): |
---|
| 93 | """ |
---|
[00b3b40] | 94 | Custom delegate for appearance and behavior control of the polydispersity view |
---|
[6011788] | 95 | """ |
---|
[00b3b40] | 96 | def createEditor(self, widget, option, index): |
---|
| 97 | # Remember the current choice |
---|
| 98 | current_text = index.data().toString() |
---|
[6011788] | 99 | if index.column() == POLY_FUNCTION: |
---|
[00b3b40] | 100 | editor = QtGui.QComboBox(widget) |
---|
[6011788] | 101 | for function in POLYDISPERSE_FUNCTIONS: |
---|
| 102 | editor.addItem(function) |
---|
[00b3b40] | 103 | current_index = editor.findText(current_text) |
---|
| 104 | editor.setCurrentIndex(current_index if current_index>-1 else 0) |
---|
[6011788] | 105 | return editor |
---|
| 106 | else: |
---|
[00b3b40] | 107 | QtGui.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|