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

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 44753d6 was 26970b3, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Fix for Sphinx crashing on importing QSyntaxHighlighter

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