[4992ff2] | 1 | from PyQt5 import QtCore |
---|
| 2 | from PyQt5 import QtGui |
---|
| 3 | from PyQt5 import QtWidgets |
---|
[ad6b4e2] | 4 | |
---|
| 5 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[6011788] | 6 | |
---|
[4992ff2] | 7 | class ModelViewDelegate(QtWidgets.QStyledItemDelegate): |
---|
[6011788] | 8 | """ |
---|
| 9 | Custom delegate for appearance and behavior control of the model view |
---|
| 10 | """ |
---|
[8f2548c] | 11 | def __init__(self, parent=None): |
---|
| 12 | """ |
---|
| 13 | Overwrite generic constructor to allow for some globals |
---|
| 14 | """ |
---|
[722b7d6] | 15 | super(ModelViewDelegate, self).__init__() |
---|
[8f2548c] | 16 | |
---|
| 17 | # Main parameter table view columns |
---|
| 18 | self.param_error=-1 |
---|
| 19 | self.param_property=0 |
---|
| 20 | self.param_value=1 |
---|
| 21 | self.param_min=2 |
---|
[16afe01] | 22 | self.param_max=3 |
---|
| 23 | self.param_unit=4 |
---|
[8f2548c] | 24 | |
---|
[16afe01] | 25 | def fancyColumns(self): |
---|
| 26 | return [self.param_value, self.param_min, self.param_max, self.param_unit] |
---|
[8f2548c] | 27 | |
---|
| 28 | def addErrorColumn(self): |
---|
| 29 | """ |
---|
| 30 | Modify local column pointers |
---|
| 31 | Note: the reverse is never required! |
---|
| 32 | """ |
---|
| 33 | self.param_property=0 |
---|
| 34 | self.param_value=1 |
---|
| 35 | self.param_error=2 |
---|
| 36 | self.param_min=3 |
---|
| 37 | self.param_max=4 |
---|
| 38 | self.param_unit=5 |
---|
[06b0138] | 39 | |
---|
[6011788] | 40 | def paint(self, painter, option, index): |
---|
| 41 | """ |
---|
| 42 | Overwrite generic painter for certain columns |
---|
| 43 | """ |
---|
[16afe01] | 44 | if index.column() in self.fancyColumns(): |
---|
[6011788] | 45 | # Units - present in nice HTML |
---|
[4992ff2] | 46 | options = QtWidgets.QStyleOptionViewItem(option) |
---|
[6011788] | 47 | self.initStyleOption(options,index) |
---|
[2a432e7] | 48 | |
---|
[7969b9c] | 49 | style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style() |
---|
[6011788] | 50 | |
---|
| 51 | # Prepare document for inserting into cell |
---|
| 52 | doc = QtGui.QTextDocument() |
---|
| 53 | |
---|
| 54 | # Convert the unit description into HTML |
---|
| 55 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
| 56 | doc.setHtml(text_html) |
---|
[ee7e423] | 57 | doc.setDocumentMargin(1) |
---|
[6011788] | 58 | |
---|
| 59 | # delete the original content |
---|
| 60 | options.text = "" |
---|
[4992ff2] | 61 | style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
[6011788] | 62 | |
---|
| 63 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
[4992ff2] | 64 | textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options) |
---|
[6011788] | 65 | |
---|
| 66 | painter.save() |
---|
[cf8d6c9] | 67 | rect = textRect.topLeft() |
---|
| 68 | x = rect.x() |
---|
| 69 | y = rect.y() |
---|
| 70 | x += 3.0 # magic value for rendering nice display in the table |
---|
| 71 | y += 2.0 # magic value for rendering nice display in the table |
---|
| 72 | rect.setX(x) |
---|
| 73 | rect.setY(y) |
---|
| 74 | painter.translate(rect) |
---|
| 75 | painter.setClipRect(textRect.translated(-rect)) |
---|
[6011788] | 76 | # Draw the QTextDocument in the cell |
---|
| 77 | doc.documentLayout().draw(painter, context) |
---|
| 78 | painter.restore() |
---|
| 79 | else: |
---|
| 80 | # Just the default paint |
---|
[4992ff2] | 81 | QtWidgets.QStyledItemDelegate.paint(self, painter, option, index) |
---|
[6011788] | 82 | |
---|
| 83 | def createEditor(self, widget, option, index): |
---|
| 84 | """ |
---|
| 85 | Overwrite generic editor for certain columns |
---|
| 86 | """ |
---|
| 87 | if not index.isValid(): |
---|
| 88 | return 0 |
---|
[8f2548c] | 89 | if index.column() == self.param_value: #only in the value column |
---|
[4992ff2] | 90 | editor = QtWidgets.QLineEdit(widget) |
---|
[d6b8a1d] | 91 | validator = GuiUtils.DoubleValidator() |
---|
[6011788] | 92 | editor.setValidator(validator) |
---|
| 93 | return editor |
---|
[fde5bcd] | 94 | if index.column() in [self.param_property, self.param_error, self.param_unit]: |
---|
[0d13814] | 95 | # Set some columns uneditable |
---|
| 96 | return None |
---|
[7f41584] | 97 | if index.column() in (self.param_min, self.param_max): |
---|
| 98 | # Check if the edit role is set |
---|
| 99 | if not (index.flags() & QtCore.Qt.ItemIsEditable): |
---|
| 100 | return None |
---|
[6011788] | 101 | |
---|
| 102 | return super(ModelViewDelegate, self).createEditor(widget, option, index) |
---|
| 103 | |
---|
[00b3b40] | 104 | def setModelData(self, editor, model, index): |
---|
| 105 | """ |
---|
| 106 | Overwrite generic model update method for certain columns |
---|
| 107 | """ |
---|
[8f2548c] | 108 | if index.column() in (self.param_min, self.param_max): |
---|
[6011788] | 109 | try: |
---|
[00b3b40] | 110 | value_float = float(editor.text()) |
---|
[6011788] | 111 | except ValueError: |
---|
[00b3b40] | 112 | # TODO: present the failure to the user |
---|
| 113 | # balloon popup? tooltip? cell background colour flash? |
---|
[6011788] | 114 | return |
---|
[4992ff2] | 115 | QtWidgets.QStyledItemDelegate.setModelData(self, editor, model, index) |
---|
[6011788] | 116 | |
---|
| 117 | |
---|
[4992ff2] | 118 | class PolyViewDelegate(QtWidgets.QStyledItemDelegate): |
---|
[6011788] | 119 | """ |
---|
[00b3b40] | 120 | Custom delegate for appearance and behavior control of the polydispersity view |
---|
[6011788] | 121 | """ |
---|
[e43fc91] | 122 | POLYDISPERSE_FUNCTIONS = ['rectangle', 'array', 'lognormal', 'gaussian', 'schulz'] |
---|
[06b0138] | 123 | |
---|
[aca8418] | 124 | combo_updated = QtCore.pyqtSignal(str, int) |
---|
[e43fc91] | 125 | filename_updated = QtCore.pyqtSignal(int) |
---|
[06b0138] | 126 | |
---|
[8eaa101] | 127 | def __init__(self, parent=None): |
---|
| 128 | """ |
---|
| 129 | Overwrite generic constructor to allow for some globals |
---|
| 130 | """ |
---|
[722b7d6] | 131 | super(PolyViewDelegate, self).__init__() |
---|
[8eaa101] | 132 | |
---|
| 133 | self.poly_parameter = 0 |
---|
| 134 | self.poly_pd = 1 |
---|
[906e0c7] | 135 | self.poly_error = None |
---|
[8eaa101] | 136 | self.poly_min = 2 |
---|
| 137 | self.poly_max = 3 |
---|
| 138 | self.poly_npts = 4 |
---|
| 139 | self.poly_nsigs = 5 |
---|
| 140 | self.poly_function = 6 |
---|
[e43fc91] | 141 | self.poly_filename = 7 |
---|
[8eaa101] | 142 | |
---|
| 143 | def editableParameters(self): |
---|
[7ffa5ee9] | 144 | return [self.poly_pd, self.poly_min, self.poly_max, self.poly_npts, self.poly_nsigs] |
---|
[8eaa101] | 145 | |
---|
| 146 | def columnDict(self): |
---|
| 147 | return {self.poly_pd: 'width', |
---|
| 148 | self.poly_min: 'min', |
---|
| 149 | self.poly_max: 'max', |
---|
| 150 | self.poly_npts: 'npts', |
---|
| 151 | self.poly_nsigs: 'nsigmas'} |
---|
| 152 | |
---|
| 153 | def addErrorColumn(self): |
---|
| 154 | """ |
---|
| 155 | Modify local column pointers |
---|
| 156 | Note: the reverse is never required! |
---|
| 157 | """ |
---|
| 158 | self.poly_parameter = 0 |
---|
| 159 | self.poly_pd = 1 |
---|
[906e0c7] | 160 | self.poly_error = 2 |
---|
[8eaa101] | 161 | self.poly_min = 3 |
---|
| 162 | self.poly_max = 4 |
---|
| 163 | self.poly_npts = 5 |
---|
| 164 | self.poly_nsigs = 6 |
---|
| 165 | self.poly_function = 7 |
---|
[e43fc91] | 166 | self.poly_filename = 8 |
---|
[8eaa101] | 167 | |
---|
[00b3b40] | 168 | def createEditor(self, widget, option, index): |
---|
| 169 | # Remember the current choice |
---|
[aca8418] | 170 | if not index.isValid(): |
---|
| 171 | return 0 |
---|
[e43fc91] | 172 | elif index.column() == self.poly_filename: |
---|
| 173 | # Notify the widget that we want to change the filename |
---|
| 174 | self.filename_updated.emit(index.row()) |
---|
| 175 | return None |
---|
[8eaa101] | 176 | elif index.column() in self.editableParameters(): |
---|
[4992ff2] | 177 | self.editor = QtWidgets.QLineEdit(widget) |
---|
[d6b8a1d] | 178 | validator = GuiUtils.DoubleValidator() |
---|
[e43fc91] | 179 | self.editor.setValidator(validator) |
---|
| 180 | return self.editor |
---|
[6011788] | 181 | else: |
---|
[4992ff2] | 182 | QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|
[aca8418] | 183 | |
---|
| 184 | def paint(self, painter, option, index): |
---|
| 185 | """ |
---|
| 186 | Overwrite generic painter for certain columns |
---|
| 187 | """ |
---|
[97df8a9] | 188 | if index.column() in (self.poly_pd, self.poly_min, self.poly_max): |
---|
[aca8418] | 189 | # Units - present in nice HTML |
---|
[4992ff2] | 190 | options = QtWidgets.QStyleOptionViewItem(option) |
---|
[aca8418] | 191 | self.initStyleOption(options,index) |
---|
| 192 | |
---|
[7969b9c] | 193 | style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style() |
---|
[b00414d] | 194 | |
---|
| 195 | # Prepare document for inserting into cell |
---|
| 196 | doc = QtGui.QTextDocument() |
---|
[cf8d6c9] | 197 | current_font = painter.font() |
---|
| 198 | doc.setDefaultFont(current_font) |
---|
[b00414d] | 199 | # Convert the unit description into HTML |
---|
| 200 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
| 201 | doc.setHtml(text_html) |
---|
| 202 | |
---|
| 203 | # delete the original content |
---|
| 204 | options.text = "" |
---|
[4992ff2] | 205 | style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
[b00414d] | 206 | |
---|
| 207 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
[4992ff2] | 208 | textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options) |
---|
[b00414d] | 209 | painter.save() |
---|
[cf8d6c9] | 210 | |
---|
| 211 | rect = textRect.topLeft() |
---|
| 212 | y = rect.y() |
---|
| 213 | y += 5.0 # magic value for rendering nice display in the table |
---|
| 214 | rect.setY(y) |
---|
| 215 | painter.translate(rect) |
---|
| 216 | painter.setClipRect(textRect.translated(-rect)) |
---|
[b00414d] | 217 | # Draw the QTextDocument in the cell |
---|
| 218 | doc.documentLayout().draw(painter, context) |
---|
| 219 | painter.restore() |
---|
| 220 | else: |
---|
| 221 | # Just the default paint |
---|
[4992ff2] | 222 | QtWidgets.QStyledItemDelegate.paint(self, painter, option, index) |
---|
[b00414d] | 223 | |
---|
[4992ff2] | 224 | class MagnetismViewDelegate(QtWidgets.QStyledItemDelegate): |
---|
[b00414d] | 225 | """ |
---|
| 226 | Custom delegate for appearance and behavior control of the magnetism view |
---|
| 227 | """ |
---|
| 228 | def __init__(self, parent=None): |
---|
| 229 | """ |
---|
| 230 | Overwrite generic constructor to allow for some globals |
---|
| 231 | """ |
---|
[722b7d6] | 232 | super(MagnetismViewDelegate, self).__init__() |
---|
[b00414d] | 233 | |
---|
| 234 | self.mag_parameter = 0 |
---|
| 235 | self.mag_value = 1 |
---|
| 236 | self.mag_min = 2 |
---|
| 237 | self.mag_max = 3 |
---|
| 238 | self.mag_unit = 4 |
---|
| 239 | |
---|
| 240 | def editableParameters(self): |
---|
[02f1d12] | 241 | return [self.mag_value, self.mag_min, self.mag_max] |
---|
[b00414d] | 242 | |
---|
| 243 | def addErrorColumn(self): |
---|
| 244 | """ |
---|
| 245 | Modify local column pointers |
---|
| 246 | Note: the reverse is never required! |
---|
| 247 | """ |
---|
| 248 | self.mag_parameter = 0 |
---|
| 249 | self.mag_value = 1 |
---|
| 250 | self.mag_min = 3 |
---|
| 251 | self.mag_max = 4 |
---|
| 252 | self.mag_unit = 5 |
---|
| 253 | |
---|
| 254 | def createEditor(self, widget, option, index): |
---|
| 255 | # Remember the current choice |
---|
[fbfc488] | 256 | current_text = index.data() |
---|
[b00414d] | 257 | if not index.isValid(): |
---|
| 258 | return 0 |
---|
| 259 | if index.column() in self.editableParameters(): |
---|
[4992ff2] | 260 | editor = QtWidgets.QLineEdit(widget) |
---|
[d6b8a1d] | 261 | validator = GuiUtils.DoubleValidator() |
---|
[b00414d] | 262 | editor.setValidator(validator) |
---|
| 263 | return editor |
---|
| 264 | else: |
---|
[4992ff2] | 265 | QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|
[b00414d] | 266 | |
---|
| 267 | def paint(self, painter, option, index): |
---|
| 268 | """ |
---|
| 269 | Overwrite generic painter for certain columns |
---|
| 270 | """ |
---|
[97df8a9] | 271 | if index.column() in (self.mag_value, self.mag_min, self.mag_max, self.mag_unit): |
---|
[b00414d] | 272 | # Units - present in nice HTML |
---|
[4992ff2] | 273 | options = QtWidgets.QStyleOptionViewItem(option) |
---|
[b00414d] | 274 | self.initStyleOption(options,index) |
---|
| 275 | |
---|
[7969b9c] | 276 | style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style() |
---|
[aca8418] | 277 | |
---|
| 278 | # Prepare document for inserting into cell |
---|
| 279 | doc = QtGui.QTextDocument() |
---|
[cf8d6c9] | 280 | current_font = painter.font() |
---|
| 281 | doc.setDefaultFont(current_font) |
---|
[aca8418] | 282 | # Convert the unit description into HTML |
---|
| 283 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
| 284 | doc.setHtml(text_html) |
---|
| 285 | |
---|
| 286 | # delete the original content |
---|
| 287 | options.text = "" |
---|
[4992ff2] | 288 | style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
[aca8418] | 289 | |
---|
| 290 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
[4992ff2] | 291 | textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options) |
---|
[aca8418] | 292 | |
---|
| 293 | painter.save() |
---|
[cf8d6c9] | 294 | rect = textRect.topLeft() |
---|
| 295 | y = rect.y() |
---|
[8e2cd79] | 296 | y += 6.0 # magic value for rendering nice display in the table |
---|
[cf8d6c9] | 297 | rect.setY(y) |
---|
| 298 | painter.translate(rect) |
---|
| 299 | painter.setClipRect(textRect.translated(-rect)) |
---|
[aca8418] | 300 | # Draw the QTextDocument in the cell |
---|
| 301 | doc.documentLayout().draw(painter, context) |
---|
| 302 | painter.restore() |
---|
| 303 | else: |
---|
| 304 | # Just the default paint |
---|
[4992ff2] | 305 | QtWidgets.QStyledItemDelegate.paint(self, painter, option, index) |
---|