[4d457df] | 1 | from PyQt4 import QtGui |
---|
| 2 | from PyQt4 import QtCore |
---|
| 3 | |
---|
| 4 | def replaceShellName(param_name, value): |
---|
| 5 | """ |
---|
| 6 | Updates parameter name from <param_name>[n_shell] to <param_name>value |
---|
| 7 | """ |
---|
| 8 | assert '[' in param_name |
---|
| 9 | return param_name[:param_name.index('[')]+str(value) |
---|
| 10 | |
---|
| 11 | def getIterParams(model): |
---|
| 12 | """ |
---|
| 13 | Returns a list of all multi-shell parameters in 'model' |
---|
| 14 | """ |
---|
| 15 | return list(filter(lambda par: "[" in par.name, model.iq_parameters)) |
---|
| 16 | |
---|
| 17 | def getMultiplicity(model): |
---|
| 18 | """ |
---|
| 19 | Finds out if 'model' has multishell parameters. |
---|
| 20 | If so, returns the name of the counter parameter and the number of shells |
---|
| 21 | """ |
---|
| 22 | iter_params = getIterParams(model) |
---|
| 23 | # return the iterator parameter name and length |
---|
| 24 | return (iter_params[0].length_control if iter_params else "", |
---|
| 25 | iter_params[0].length if iter_params else 0) |
---|
| 26 | |
---|
| 27 | def addParametersToModel(parameters, model): |
---|
| 28 | """ |
---|
| 29 | Update local ModelModel with sasmodel parameters |
---|
| 30 | """ |
---|
| 31 | multishell_parameters = getIterParams(parameters) |
---|
| 32 | multishell_param_name, _ = getMultiplicity(parameters) |
---|
| 33 | |
---|
| 34 | for param in parameters.iq_parameters: |
---|
| 35 | # don't include shell parameters |
---|
| 36 | if param.name == multishell_param_name: |
---|
| 37 | continue |
---|
| 38 | # Modify parameter name from <param>[n] to <param>1 |
---|
| 39 | item_name = param.name |
---|
| 40 | if param in multishell_parameters: |
---|
| 41 | item_name = replaceShellName(param.name, 1) |
---|
| 42 | |
---|
| 43 | item1 = QtGui.QStandardItem(item_name) |
---|
| 44 | item1.setCheckable(True) |
---|
| 45 | # check for polydisp params |
---|
| 46 | if param.polydisperse: |
---|
| 47 | poly_item = QtGui.QStandardItem("Polydispersity") |
---|
| 48 | item1_1 = QtGui.QStandardItem("Distribution") |
---|
| 49 | # Find param in volume_params |
---|
| 50 | for p in parameters.form_volume_parameters: |
---|
| 51 | if p.name != param.name: |
---|
| 52 | continue |
---|
| 53 | item1_2 = QtGui.QStandardItem(str(p.default)) |
---|
| 54 | item1_3 = QtGui.QStandardItem(str(p.limits[0])) |
---|
| 55 | item1_4 = QtGui.QStandardItem(str(p.limits[1])) |
---|
| 56 | item1_5 = QtGui.QStandardItem(p.units) |
---|
| 57 | poly_item.appendRow([item1_1, item1_2, item1_3, item1_4, item1_5]) |
---|
| 58 | break |
---|
| 59 | # Add the polydisp item as a child |
---|
| 60 | item1.appendRow([poly_item]) |
---|
| 61 | # Param values |
---|
| 62 | item2 = QtGui.QStandardItem(str(param.default)) |
---|
| 63 | # TODO: the error column. |
---|
| 64 | # Either add a proxy model or a custom view delegate |
---|
| 65 | #item_err = QtGui.QStandardItem() |
---|
| 66 | item3 = QtGui.QStandardItem(str(param.limits[0])) |
---|
| 67 | item4 = QtGui.QStandardItem(str(param.limits[1])) |
---|
| 68 | item5 = QtGui.QStandardItem(param.units) |
---|
| 69 | model.appendRow([item1, item2, item3, item4, item5]) |
---|
| 70 | |
---|
| 71 | def addSimpleParametersToModel(parameters, model): |
---|
| 72 | """ |
---|
| 73 | Update local ModelModel with sasmodel parameters |
---|
| 74 | """ |
---|
| 75 | for param in parameters.iq_parameters: |
---|
[7248d75d] | 76 | # Create the top level, checkable item |
---|
[4d457df] | 77 | item_name = param.name |
---|
| 78 | item1 = QtGui.QStandardItem(item_name) |
---|
| 79 | item1.setCheckable(True) |
---|
| 80 | # Param values |
---|
| 81 | item2 = QtGui.QStandardItem(str(param.default)) |
---|
| 82 | # TODO: the error column. |
---|
| 83 | # Either add a proxy model or a custom view delegate |
---|
| 84 | #item_err = QtGui.QStandardItem() |
---|
| 85 | item3 = QtGui.QStandardItem(str(param.limits[0])) |
---|
| 86 | item4 = QtGui.QStandardItem(str(param.limits[1])) |
---|
| 87 | item5 = QtGui.QStandardItem(param.units) |
---|
| 88 | model.appendRow([item1, item2, item3, item4, item5]) |
---|
| 89 | |
---|
| 90 | def addCheckedListToModel(model, param_list): |
---|
| 91 | """ |
---|
| 92 | Add a QItem to model. Makes the QItem checkable |
---|
| 93 | """ |
---|
| 94 | assert isinstance(model, QtGui.QStandardItemModel) |
---|
| 95 | item_list = [QtGui.QStandardItem(item) for item in param_list] |
---|
| 96 | item_list[0].setCheckable(True) |
---|
| 97 | model.appendRow(item_list) |
---|
| 98 | |
---|
| 99 | def addHeadersToModel(model): |
---|
| 100 | """ |
---|
| 101 | Adds predefined headers to the model |
---|
| 102 | """ |
---|
| 103 | model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant("Parameter")) |
---|
| 104 | model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant("Value")) |
---|
| 105 | model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant("Min")) |
---|
| 106 | model.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant("Max")) |
---|
| 107 | model.setHeaderData(4, QtCore.Qt.Horizontal, QtCore.QVariant("[Units]")) |
---|
| 108 | |
---|
| 109 | def addPolyHeadersToModel(model): |
---|
| 110 | """ |
---|
| 111 | Adds predefined headers to the model |
---|
| 112 | """ |
---|
| 113 | model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant("Parameter")) |
---|
| 114 | model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant("PD[ratio]")) |
---|
| 115 | model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant("Min")) |
---|
| 116 | model.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant("Max")) |
---|
| 117 | model.setHeaderData(4, QtCore.Qt.Horizontal, QtCore.QVariant("Npts")) |
---|
| 118 | model.setHeaderData(5, QtCore.Qt.Horizontal, QtCore.QVariant("Nsigs")) |
---|
| 119 | model.setHeaderData(6, QtCore.Qt.Horizontal, QtCore.QVariant("Function")) |
---|
| 120 | |
---|
| 121 | def addShellsToModel(parameters, model, index): |
---|
| 122 | """ |
---|
| 123 | Find out multishell parameters and update the model with the requested number of them |
---|
| 124 | """ |
---|
| 125 | multishell_parameters = getIterParams(parameters) |
---|
| 126 | |
---|
| 127 | for i in xrange(index): |
---|
| 128 | for par in multishell_parameters: |
---|
| 129 | param_name = replaceShellName(par.name, i+2) |
---|
| 130 | item1 = QtGui.QStandardItem(param_name) |
---|
| 131 | item1.setCheckable(True) |
---|
| 132 | # check for polydisp params |
---|
| 133 | if par.polydisperse: |
---|
| 134 | poly_item = QtGui.QStandardItem("Polydispersity") |
---|
| 135 | item1_1 = QtGui.QStandardItem("Distribution") |
---|
| 136 | # Find param in volume_params |
---|
| 137 | for p in parameters.form_volume_parameters: |
---|
| 138 | if p.name != par.name: |
---|
| 139 | continue |
---|
| 140 | item1_2 = QtGui.QStandardItem(str(p.default)) |
---|
| 141 | item1_3 = QtGui.QStandardItem(str(p.limits[0])) |
---|
| 142 | item1_4 = QtGui.QStandardItem(str(p.limits[1])) |
---|
| 143 | item1_5 = QtGui.QStandardItem(p.units) |
---|
| 144 | poly_item.appendRow([item1_1, item1_2, item1_3, item1_4, item1_5]) |
---|
| 145 | break |
---|
| 146 | item1.appendRow([poly_item]) |
---|
| 147 | |
---|
| 148 | item2 = QtGui.QStandardItem(str(par.default)) |
---|
| 149 | item3 = QtGui.QStandardItem(str(par.limits[0])) |
---|
| 150 | item4 = QtGui.QStandardItem(str(par.limits[1])) |
---|
| 151 | item5 = QtGui.QStandardItem(par.units) |
---|
| 152 | model.appendRow([item1, item2, item3, item4, item5]) |
---|
| 153 | |
---|