[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 | class CustomLineEdit(QtGui.QLineEdit): |
---|
| 25 | editingFinished = QtCore.pyqtSignal() |
---|
| 26 | def __init__(self, parent=None, old_value=None): |
---|
| 27 | super(CustomLineEdit, self).__init__(parent) |
---|
| 28 | self.setAutoFillBackground(True) |
---|
| 29 | self.old_value = old_value |
---|
| 30 | def focusOutEvent(self, event): |
---|
| 31 | self.editingFinished.emit() |
---|
| 32 | |
---|
| 33 | class ModelViewDelegate(QtGui.QStyledItemDelegate): |
---|
| 34 | """ |
---|
| 35 | Custom delegate for appearance and behavior control of the model view |
---|
| 36 | """ |
---|
| 37 | def paint(self, painter, option, index): |
---|
| 38 | """ |
---|
| 39 | Overwrite generic painter for certain columns |
---|
| 40 | """ |
---|
| 41 | if index.column() in (PARAM_UNIT, PARAM_MIN, PARAM_MAX): |
---|
| 42 | # Units - present in nice HTML |
---|
| 43 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
| 44 | self.initStyleOption(options,index) |
---|
| 45 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
| 46 | |
---|
| 47 | # Prepare document for inserting into cell |
---|
| 48 | doc = QtGui.QTextDocument() |
---|
| 49 | |
---|
| 50 | # Convert the unit description into HTML |
---|
| 51 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
| 52 | doc.setHtml(text_html) |
---|
| 53 | |
---|
| 54 | # delete the original content |
---|
| 55 | options.text = "" |
---|
| 56 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter); |
---|
| 57 | |
---|
| 58 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
| 59 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
| 60 | |
---|
| 61 | painter.save() |
---|
| 62 | painter.translate(textRect.topLeft()) |
---|
| 63 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
| 64 | # Draw the QTextDocument in the cell |
---|
| 65 | doc.documentLayout().draw(painter, context) |
---|
| 66 | |
---|
| 67 | painter.restore() |
---|
| 68 | else: |
---|
| 69 | # Just the default paint |
---|
| 70 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|
| 71 | |
---|
| 72 | def createEditor(self, widget, option, index): |
---|
| 73 | """ |
---|
| 74 | Overwrite generic editor for certain columns |
---|
| 75 | """ |
---|
| 76 | if not index.isValid(): |
---|
| 77 | return 0 |
---|
| 78 | if index.column() == PARAM_VALUE: #only in the value column |
---|
| 79 | editor = QtGui.QLineEdit(widget) |
---|
| 80 | validator = QtGui.QDoubleValidator() |
---|
| 81 | editor.setValidator(validator) |
---|
| 82 | return editor |
---|
| 83 | elif index.column() in (PARAM_MIN, PARAM_MAX): |
---|
| 84 | # Save current value in case we need to revert |
---|
| 85 | #self._old_value = index.data().toFloat()[0] |
---|
| 86 | self._old_value = index.data().toString() |
---|
| 87 | editor = CustomLineEdit(widget, old_value=self._old_value) |
---|
| 88 | editor.editingFinished.connect(self.commitAndCloseEditor) |
---|
| 89 | return editor |
---|
| 90 | |
---|
| 91 | return super(ModelViewDelegate, self).createEditor(widget, option, index) |
---|
| 92 | |
---|
| 93 | #def setEditorData(self, editor, index): |
---|
| 94 | # if index.column() == MIN: |
---|
| 95 | # #value = index.data().toString()[0] |
---|
| 96 | # value = index.model().data(index, QtCore.Qt.DisplayRole).toString() |
---|
| 97 | # print "VALUE = ", value |
---|
| 98 | # editor.setText('['+value+']') |
---|
| 99 | # return editor |
---|
| 100 | |
---|
| 101 | # return super(ModelViewDelegate, self).setEditorData(editor, index) |
---|
| 102 | |
---|
| 103 | def commitAndCloseEditor(self): |
---|
| 104 | editor = self.sender() |
---|
| 105 | content = editor.text() |
---|
| 106 | try: |
---|
| 107 | value_float = float(content) |
---|
| 108 | except ValueError: |
---|
| 109 | # TODO: Notify the user |
---|
| 110 | # <scary popup> |
---|
| 111 | # Do nothing |
---|
| 112 | return |
---|
| 113 | self.commitData.emit(editor) |
---|
| 114 | self.closeEditor.emit(editor, QtGui.QAbstractItemDelegate.NoHint) |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | class PolyViewDelegate(QtGui.QStyledItemDelegate): |
---|
| 118 | """ |
---|
| 119 | Custom delegate for appearance and behavior control of the polydisperisty view |
---|
| 120 | """ |
---|
| 121 | def createEditor(self, parent, option, index): |
---|
| 122 | # TODO: set it to correct index on creation |
---|
| 123 | if index.column() == POLY_FUNCTION: |
---|
| 124 | editor = QtGui.QComboBox(parent) |
---|
| 125 | for function in POLYDISPERSE_FUNCTIONS: |
---|
| 126 | editor.addItem(function) |
---|
| 127 | return editor |
---|
| 128 | else: |
---|
| 129 | QtGui.QStyledItemDelegate.createEditor(self, parent, option, index) |
---|