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

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 060413c was 060413c, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

More CR and merge fixes.

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