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

ESS_GUIESS_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 c5e0d84 was c5e0d84, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Minor modifications in response to requests during demo session

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