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

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

Merging feature branches

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