source: sasview/src/sas/qtgui/Utilities/PluginDefinition.py @ fa05c6c1

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since fa05c6c1 was 3b8cc00, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Code review changes

  • Property mode set to 100644
File size: 5.2 KB
Line 
1from PyQt5 import QtCore
2from PyQt5 import QtGui
3from PyQt5 import QtWidgets
4
5from sas.qtgui.Utilities.UI.PluginDefinitionUI import Ui_PluginDefinition
6from sas.qtgui.Utilities.PythonSyntax import PythonHighlighter
7
8# txtName
9# txtDescription
10# chkOverwrite
11# tblParams
12# tblParamsPD
13# txtFunction
14
15class PluginDefinition(QtWidgets.QDialog, Ui_PluginDefinition):
16    """
17    Class describing the "simple" plugin editor.
18    This is a simple series of widgets allowing for specifying
19    model form and parameters.
20    """
21    modelModified = QtCore.pyqtSignal()
22    def __init__(self, parent=None):
23        super(PluginDefinition, self).__init__(parent)
24        self.setupUi(self)
25
26        # globals
27        self.initializeModel()
28        # internal representation of the parameter list
29        # {<row>: (<parameter>, <value>)}
30        self.parameter_dict = {}
31        self.pd_parameter_dict = {}
32
33        # Initialize signals
34        self.addSignals()
35
36        # Initialize widgets
37        self.addWidgets()
38
39    def addWidgets(self):
40        """
41        Initialize various widgets in the dialog
42        """
43        # Set the tooltip
44        hint_function = "#Example:\n\n"
45        hint_function += "if x <= 0:\n"
46        hint_function += "    y = A + B\n"
47        hint_function += "else:\n"
48        hint_function += "    y = A + B * cos(2 * pi * x)\n"
49        hint_function += "return y\n"
50        self.txtFunction.setToolTip(hint_function)
51        # Initial text in the function table
52        text = \
53"""y = x
54
55return y
56"""
57        self.txtFunction.insertPlainText(text)
58
59        # Validators
60        rx = QtCore.QRegExp("^[A-Za-z0-9_]*$")
61
62        txt_validator = QtGui.QRegExpValidator(rx)
63        self.txtName.setValidator(txt_validator)
64        self.highlight = PythonHighlighter(self.txtFunction.document())
65
66    def initializeModel(self):
67        """
68        Define the dictionary for internal data representation
69        """
70        # Define the keys
71        self.model = {
72            'filename':'',
73            'overwrite':False,
74            'description':'',
75            'parameters':{},
76            'pd_parameters':{},
77            'text':''}
78
79    def addSignals(self):
80        """
81        Define slots for widget signals
82        """
83        self.txtName.editingFinished.connect(self.onPluginNameChanged)
84        self.txtDescription.editingFinished.connect(self.onDescriptionChanged)
85        self.tblParams.cellChanged.connect(self.onParamsChanged)
86        self.tblParamsPD.cellChanged.connect(self.onParamsPDChanged)
87        # QTextEdit doesn't have a signal for edit finish, so we respond to text changed.
88        # Possibly a slight overkill.
89        self.txtFunction.textChanged.connect(self.onFunctionChanged)
90        self.chkOverwrite.toggled.connect(self.onOverwrite)
91
92    def onPluginNameChanged(self):
93        """
94        Respond to changes in plugin name
95        """
96        self.model['filename'] = self.txtName.text()
97        self.modelModified.emit()
98
99    def onDescriptionChanged(self):
100        """
101        Respond to changes in plugin description
102        """
103        self.model['description'] = self.txtDescription.text()
104        self.modelModified.emit()
105
106    def onParamsChanged(self, row, column):
107        """
108        Respond to changes in non-polydisperse parameter table
109        """
110        param = value = None
111        if self.tblParams.item(row, 0):
112            param = self.tblParams.item(row, 0).data(0)
113        if self.tblParams.item(row, 1):
114            value = self.tblParams.item(row, 1).data(0)
115
116        # If modified, just update the dict
117        self.parameter_dict[row] = (param, value)
118        self.model['parameters'] = self.parameter_dict
119
120        # Check if the update was Value for last row. If so, add a new row
121        if column == 1 and row == self.tblParams.rowCount()-1:
122            # Add a row
123            self.tblParams.insertRow(self.tblParams.rowCount())
124        self.modelModified.emit()
125
126    def onParamsPDChanged(self, row, column):
127        """
128        Respond to changes in non-polydisperse parameter table
129        """
130        param = value = None
131        if self.tblParamsPD.item(row, 0):
132            param = self.tblParamsPD.item(row, 0).data(0)
133        if self.tblParamsPD.item(row, 1):
134            value = self.tblParamsPD.item(row, 1).data(0)
135
136        # If modified, just update the dict
137        self.pd_parameter_dict[row] = (param, value)
138        self.model['pd_parameters'] = self.pd_parameter_dict
139
140        # Check if the update was Value for last row. If so, add a new row
141        if column == 1 and row == self.tblParamsPD.rowCount()-1:
142            # Add a row
143            self.tblParamsPD.insertRow(self.tblParamsPD.rowCount())
144        self.modelModified.emit()
145
146
147    def onFunctionChanged(self):
148        """
149        Respond to changes in function body
150        """
151        # keep in mind that this is called every time the text changes.
152        # mind the performance!
153        self.model['text'] = self.txtFunction.toPlainText().lstrip().rstrip()
154        self.modelModified.emit()
155
156    def onOverwrite(self):
157        """
158        Respond to change in file overwrite checkbox
159        """
160        self.model['overwrite'] = self.chkOverwrite.isChecked()
161        self.modelModified.emit()
162
163    def getModel(self):
164        """
165        Return the current plugin model
166        """
167        return self.model
Note: See TracBrowser for help on using the repository browser.