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

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

Code cleanup and minor fixes

  • Property mode set to 100644
File size: 5.3 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(r'^[\w,\s-]+$')
61        #rx = QtCore.QRegExp("[a-z-A-Z_]+")
62        rx = QtCore.QRegExp("^[A-Za-z0-9_]*$")
63
64        txt_validator = QtGui.QRegExpValidator(rx)
65        self.txtName.setValidator(txt_validator)
66        self.highlight = PythonHighlighter(self.txtFunction.document())
67
68    def initializeModel(self):
69        """
70        Define the dictionary for internal data representation
71        """
72        # Define the keys
73        self.model = {
74            'filename':'',
75            'overwrite':False,
76            'description':'',
77            'parameters':{},
78            'pd_parameters':{},
79            'text':''}
80
81    def addSignals(self):
82        """
83        Define slots for widget signals
84        """
85        self.txtName.editingFinished.connect(self.onPluginNameChanged)
86        self.txtDescription.editingFinished.connect(self.onDescriptionChanged)
87        self.tblParams.cellChanged.connect(self.onParamsChanged)
88        self.tblParamsPD.cellChanged.connect(self.onParamsPDChanged)
89        # QTextEdit doesn't have a signal for edit finish, so we respond to text changed.
90        # Possibly a slight overkill.
91        self.txtFunction.textChanged.connect(self.onFunctionChanged)
92        self.chkOverwrite.toggled.connect(self.onOverwrite)
93
94    def onPluginNameChanged(self):
95        """
96        Respond to changes in plugin name
97        """
98        self.model['filename'] = self.txtName.text()
99        self.modelModified.emit()
100
101    def onDescriptionChanged(self):
102        """
103        Respond to changes in plugin description
104        """
105        self.model['description'] = self.txtDescription.text()
106        self.modelModified.emit()
107
108    def onParamsChanged(self, row, column):
109        """
110        Respond to changes in non-polydisperse parameter table
111        """
112        param = value = None
113        if self.tblParams.item(row, 0):
114            param = self.tblParams.item(row, 0).data(0)
115        if self.tblParams.item(row, 1):
116            value = self.tblParams.item(row, 1).data(0)
117
118        # If modified, just update the dict
119        self.parameter_dict[row] = (param, value)
120        self.model['parameters'] = self.parameter_dict
121
122        # Check if the update was Value for last row. If so, add a new row
123        if column == 1 and row == self.tblParams.rowCount()-1:
124            # Add a row
125            self.tblParams.insertRow(self.tblParams.rowCount())
126        self.modelModified.emit()
127
128    def onParamsPDChanged(self, row, column):
129        """
130        Respond to changes in non-polydisperse parameter table
131        """
132        param = value = None
133        if self.tblParamsPD.item(row, 0):
134            param = self.tblParamsPD.item(row, 0).data(0)
135        if self.tblParamsPD.item(row, 1):
136            value = self.tblParamsPD.item(row, 1).data(0)
137
138        # If modified, just update the dict
139        self.pd_parameter_dict[row] = (param, value)
140        self.model['pd_parameters'] = self.pd_parameter_dict
141
142        # Check if the update was Value for last row. If so, add a new row
143        if column == 1 and row == self.tblParamsPD.rowCount()-1:
144            # Add a row
145            self.tblParamsPD.insertRow(self.tblParamsPD.rowCount())
146        self.modelModified.emit()
147
148
149    def onFunctionChanged(self):
150        """
151        Respond to changes in function body
152        """
153        # keep in mind that this is called every time the text changes.
154        # mind the performance!
155        self.model['text'] = self.txtFunction.toPlainText().lstrip().rstrip()
156        self.modelModified.emit()
157
158    def onOverwrite(self):
159        """
160        Respond to change in file overwrite checkbox
161        """
162        self.model['overwrite'] = self.chkOverwrite.isChecked()
163        self.modelModified.emit()
164
165    def getModel(self):
166        """
167        Return the current plugin model
168        """
169        return self.model
Note: See TracBrowser for help on using the repository browser.