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

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 dda8f16 was 3933ee9, checked in by Torin Cooper-Bennun <torin.cooper-bennun@…>, 6 years ago

standardise monospace font for code editors, QT console

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