1 | from PyQt4 import QtGui |
---|
2 | from PyQt4 import QtCore |
---|
3 | |
---|
4 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
5 | |
---|
6 | class ModelViewDelegate(QtGui.QStyledItemDelegate): |
---|
7 | """ |
---|
8 | Custom delegate for appearance and behavior control of the model view |
---|
9 | """ |
---|
10 | # Main parameter table view columns |
---|
11 | PARAM_PROPERTY=0 |
---|
12 | PARAM_VALUE=1 |
---|
13 | PARAM_MIN=2 |
---|
14 | PARAM_MAX=3 |
---|
15 | PARAM_UNIT=4 |
---|
16 | |
---|
17 | def paint(self, painter, option, index): |
---|
18 | """ |
---|
19 | Overwrite generic painter for certain columns |
---|
20 | """ |
---|
21 | if index.column() in (self.PARAM_UNIT, self.PARAM_MIN, self.PARAM_MAX): |
---|
22 | # Units - present in nice HTML |
---|
23 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
24 | self.initStyleOption(options,index) |
---|
25 | |
---|
26 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
27 | |
---|
28 | # Prepare document for inserting into cell |
---|
29 | doc = QtGui.QTextDocument() |
---|
30 | |
---|
31 | # Convert the unit description into HTML |
---|
32 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
33 | doc.setHtml(text_html) |
---|
34 | |
---|
35 | # delete the original content |
---|
36 | options.text = "" |
---|
37 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
38 | |
---|
39 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
40 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
41 | |
---|
42 | painter.save() |
---|
43 | painter.translate(textRect.topLeft()) |
---|
44 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
45 | # Draw the QTextDocument in the cell |
---|
46 | doc.documentLayout().draw(painter, context) |
---|
47 | painter.restore() |
---|
48 | else: |
---|
49 | # Just the default paint |
---|
50 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|
51 | |
---|
52 | def createEditor(self, widget, option, index): |
---|
53 | """ |
---|
54 | Overwrite generic editor for certain columns |
---|
55 | """ |
---|
56 | if not index.isValid(): |
---|
57 | return 0 |
---|
58 | if index.column() == self.PARAM_VALUE: #only in the value column |
---|
59 | editor = QtGui.QLineEdit(widget) |
---|
60 | validator = QtGui.QDoubleValidator() |
---|
61 | editor.setValidator(validator) |
---|
62 | return editor |
---|
63 | |
---|
64 | return super(ModelViewDelegate, self).createEditor(widget, option, index) |
---|
65 | |
---|
66 | def setModelData(self, editor, model, index): |
---|
67 | """ |
---|
68 | Overwrite generic model update method for certain columns |
---|
69 | """ |
---|
70 | if index.column() in (self.PARAM_MIN, self.PARAM_MAX): |
---|
71 | try: |
---|
72 | value_float = float(editor.text()) |
---|
73 | except ValueError: |
---|
74 | # TODO: present the failure to the user |
---|
75 | # balloon popup? tooltip? cell background colour flash? |
---|
76 | return |
---|
77 | QtGui.QStyledItemDelegate.setModelData(self, editor, model, index) |
---|
78 | |
---|
79 | |
---|
80 | class PolyViewDelegate(QtGui.QStyledItemDelegate): |
---|
81 | """ |
---|
82 | Custom delegate for appearance and behavior control of the polydispersity view |
---|
83 | """ |
---|
84 | # polydispersity functions |
---|
85 | POLYDISPERSE_FUNCTIONS=['rectangle', 'array', 'lognormal', 'gaussian', 'schulz'] |
---|
86 | # polydispersity columns |
---|
87 | POLY_PARAMETER=0 |
---|
88 | POLY_PD=1 |
---|
89 | POLY_MIN=2 |
---|
90 | POLY_MAX=3 |
---|
91 | POLY_NPTS=4 |
---|
92 | POLY_NSIGS=5 |
---|
93 | POLY_FUNCTION=6 |
---|
94 | POLY_EDITABLE_PARAMS = [POLY_MIN, POLY_MAX, POLY_NPTS, POLY_NSIGS] |
---|
95 | POLY_COLUMN_DICT = { |
---|
96 | POLY_PD: 'width', |
---|
97 | POLY_MIN: 'min', |
---|
98 | POLY_MAX: 'max', |
---|
99 | POLY_NPTS: 'npts', |
---|
100 | POLY_NSIGS: 'nsigmas'} |
---|
101 | |
---|
102 | combo_updated = QtCore.pyqtSignal(str, int) |
---|
103 | |
---|
104 | def createEditor(self, widget, option, index): |
---|
105 | # Remember the current choice |
---|
106 | current_text = index.data().toString() |
---|
107 | if not index.isValid(): |
---|
108 | return 0 |
---|
109 | if index.column() == self.POLY_FUNCTION: |
---|
110 | editor = QtGui.QComboBox(widget) |
---|
111 | for function in self.POLYDISPERSE_FUNCTIONS: |
---|
112 | editor.addItem(function) |
---|
113 | current_index = editor.findText(current_text) |
---|
114 | editor.setCurrentIndex(current_index if current_index>-1 else 3) |
---|
115 | editor.currentIndexChanged.connect(lambda: self.combo_updated.emit(str(editor.currentText()), index.row())) |
---|
116 | return editor |
---|
117 | elif index.column() in self.POLY_EDITABLE_PARAMS: |
---|
118 | editor = QtGui.QLineEdit(widget) |
---|
119 | validator = QtGui.QDoubleValidator() |
---|
120 | editor.setValidator(validator) |
---|
121 | return editor |
---|
122 | else: |
---|
123 | QtGui.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|
124 | |
---|
125 | def paint(self, painter, option, index): |
---|
126 | """ |
---|
127 | Overwrite generic painter for certain columns |
---|
128 | """ |
---|
129 | if index.column() in (self.POLY_MIN, self.POLY_MAX): |
---|
130 | # Units - present in nice HTML |
---|
131 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
132 | self.initStyleOption(options,index) |
---|
133 | |
---|
134 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
135 | |
---|
136 | # Prepare document for inserting into cell |
---|
137 | doc = QtGui.QTextDocument() |
---|
138 | |
---|
139 | # Convert the unit description into HTML |
---|
140 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
141 | doc.setHtml(text_html) |
---|
142 | |
---|
143 | # delete the original content |
---|
144 | options.text = "" |
---|
145 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
146 | |
---|
147 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
148 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
149 | |
---|
150 | painter.save() |
---|
151 | painter.translate(textRect.topLeft()) |
---|
152 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
153 | # Draw the QTextDocument in the cell |
---|
154 | doc.documentLayout().draw(painter, context) |
---|
155 | painter.restore() |
---|
156 | else: |
---|
157 | # Just the default paint |
---|
158 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|