1 | # global |
---|
2 | import sys |
---|
3 | from PyQt4 import QtCore |
---|
4 | from PyQt4 import QtGui |
---|
5 | from PyQt4 import QtWebKit |
---|
6 | |
---|
7 | from twisted.internet import threads |
---|
8 | from twisted.internet import reactor |
---|
9 | |
---|
10 | # sas-global |
---|
11 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
12 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
13 | from sas.sascalc.corfunc.corfunc_calculator import CorfuncCalculator |
---|
14 | |
---|
15 | # local |
---|
16 | from UI.CorfuncPanel import Ui_CorfuncDialog |
---|
17 | # from InvariantDetails import DetailsDialog |
---|
18 | from CorfuncUtils import WIDGETS as W |
---|
19 | |
---|
20 | from matplotlib.backends import qt_compat |
---|
21 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
22 | from matplotlib.figure import Figure |
---|
23 | |
---|
24 | |
---|
25 | class MyMplCanvas(FigureCanvas): |
---|
26 | """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.).""" |
---|
27 | def __init__(self, parent=None, width=5, height=4, dpi=100): |
---|
28 | self.fig = Figure(figsize=(width, height), dpi=dpi) |
---|
29 | self.axes = self.fig.add_subplot(111) |
---|
30 | # # We want the axes cleared every time plot() is called |
---|
31 | # self.axes.hold(False) |
---|
32 | self.axes.set_xscale("log") |
---|
33 | self.axes.set_yscale("log") |
---|
34 | |
---|
35 | # self.compute_initial_figure() |
---|
36 | |
---|
37 | FigureCanvas.__init__(self, self.fig) |
---|
38 | # self.reparent(parent, QPoint(0, 0)) |
---|
39 | |
---|
40 | # FigureCanvas.setSizePolicy(self, |
---|
41 | # QSizePolicy.Expanding, |
---|
42 | # QSizePolicy.Expanding) |
---|
43 | # FigureCanvas.updateGeometry(self) |
---|
44 | |
---|
45 | def sizeHint(self): |
---|
46 | w, h = self.get_width_height() |
---|
47 | return QSize(w, h) |
---|
48 | |
---|
49 | def minimumSizeHint(self): |
---|
50 | return QSize(10, 10) |
---|
51 | |
---|
52 | |
---|
53 | class CorfuncWindow(QtGui.QDialog, Ui_CorfuncDialog): |
---|
54 | # The controller which is responsible for managing signal slots connections |
---|
55 | # for the gui and providing an interface to the data model. |
---|
56 | name = "Corfunc" # For displaying in the combo box |
---|
57 | #def __init__(self, manager=None, parent=None): |
---|
58 | def __init__(self, parent=None): |
---|
59 | #super(InvariantWindow, self).__init__(parent) |
---|
60 | super(CorfuncWindow, self).__init__() |
---|
61 | self.setupUi(self) |
---|
62 | |
---|
63 | self.setWindowTitle("Corfunc Perspective") |
---|
64 | |
---|
65 | self.model = QtGui.QStandardItemModel(self) |
---|
66 | self.communicate = GuiUtils.Communicate() |
---|
67 | self._calculator = CorfuncCalculator() |
---|
68 | |
---|
69 | self._canvas = MyMplCanvas(self) |
---|
70 | self.verticalLayout_7.addWidget(self._canvas) |
---|
71 | |
---|
72 | # Connect buttons to slots. |
---|
73 | # Needs to be done early so default values propagate properly. |
---|
74 | self.setupSlots() |
---|
75 | |
---|
76 | # Set up the model. |
---|
77 | self.setupModel() |
---|
78 | |
---|
79 | # Set up the mapper |
---|
80 | self.setupMapper() |
---|
81 | |
---|
82 | def setupSlots(self): |
---|
83 | self.extractBtn.clicked.connect(self.action) |
---|
84 | self.extrapolateBtn.clicked.connect(self.extrapolate) |
---|
85 | self.transformBtn.clicked.connect(self.action) |
---|
86 | |
---|
87 | self.calculateBgBtn.clicked.connect(self.calculateBackground) |
---|
88 | |
---|
89 | self.hilbertBtn.clicked.connect(self.action) |
---|
90 | self.fourierBtn.clicked.connect(self.action) |
---|
91 | |
---|
92 | self.model.itemChanged.connect(self.modelChanged) |
---|
93 | |
---|
94 | def setupModel(self): |
---|
95 | self.model.setItem(W.W_QMIN, |
---|
96 | QtGui.QStandardItem("0")) |
---|
97 | self.model.setItem(W.W_QMAX, |
---|
98 | QtGui.QStandardItem("0")) |
---|
99 | self.model.setItem(W.W_QCUTOFF, |
---|
100 | QtGui.QStandardItem("0")) |
---|
101 | self.model.setItem(W.W_BACKGROUND, |
---|
102 | QtGui.QStandardItem("0")) |
---|
103 | self.model.setItem(W.W_TRANSFORM, |
---|
104 | QtGui.QStandardItem("Fourier")) |
---|
105 | |
---|
106 | def modelChanged(self, item): |
---|
107 | if item.row() == W.W_QMIN: |
---|
108 | value = float(self.model.item(W.W_QMIN).text()) |
---|
109 | self.qMin.setValue(value) |
---|
110 | self._calculator.lowerq = value |
---|
111 | elif item.row() == W.W_QMAX: |
---|
112 | value = float(self.model.item(W.W_QMAX).text()) |
---|
113 | self.qMax1.setValue(value) |
---|
114 | self._calculator.upperq = (value, self._calculator.upperq[1]) |
---|
115 | elif item.row() == W.W_QCUTOFF: |
---|
116 | value = float(self.model.item(W.W_QCUTOFF).text()) |
---|
117 | self.qMax2.setValue(value) |
---|
118 | self._calculator.upperq = (self._calculator.upperq[0], value) |
---|
119 | elif item.row() == W.W_BACKGROUND: |
---|
120 | value = float(self.model.item(W.W_BACKGROUND).text()) |
---|
121 | self.bg.setValue(value) |
---|
122 | self._calculator.background = value |
---|
123 | else: |
---|
124 | print("{} Changed".format(item)) |
---|
125 | |
---|
126 | |
---|
127 | def extrapolate(self): |
---|
128 | params, extrapolation = self._calculator.compute_extrapolation() |
---|
129 | self.guinierA.setValue(params['A']) |
---|
130 | self.guinierB.setValue(params['B']) |
---|
131 | self.porodK.setValue(params['K']) |
---|
132 | self.porodSigma.setValue(params['sigma']) |
---|
133 | print(params) |
---|
134 | |
---|
135 | |
---|
136 | |
---|
137 | def setupMapper(self): |
---|
138 | self.mapper = QtGui.QDataWidgetMapper(self) |
---|
139 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
140 | self.mapper.setModel(self.model) |
---|
141 | |
---|
142 | self.mapper.addMapping(self.qMin, W.W_QMIN) |
---|
143 | self.mapper.addMapping(self.qMax1, W.W_QMAX) |
---|
144 | self.mapper.addMapping(self.qMax2, W.W_QCUTOFF) |
---|
145 | self.mapper.addMapping(self.bg, W.W_BACKGROUND) |
---|
146 | |
---|
147 | self.mapper.toFirst() |
---|
148 | |
---|
149 | def calculateBackground(self): |
---|
150 | bg = self._calculator.compute_background() |
---|
151 | print(bg) |
---|
152 | self.model.setItem(W.W_BACKGROUND, QtGui.QStandardItem(str(bg))) |
---|
153 | |
---|
154 | def action(self): |
---|
155 | print("Called an action!") |
---|
156 | print(self.model) |
---|
157 | print(self.mapper) |
---|
158 | |
---|
159 | def allowBatch(self): |
---|
160 | """ |
---|
161 | We cannot perform corfunc analysis in batch at this time. |
---|
162 | """ |
---|
163 | return False |
---|
164 | |
---|
165 | def setData(self, data_item, is_batch=False): |
---|
166 | """ |
---|
167 | Obtain a QStandardItem object and dissect it to get Data1D/2D |
---|
168 | Pass it over to the calculator |
---|
169 | """ |
---|
170 | if not isinstance(data_item, list): |
---|
171 | msg = "Incorrect type passed to the Corfunc Perpsective" |
---|
172 | raise AttributeError(msg) |
---|
173 | |
---|
174 | if not isinstance(data_item[0], QtGui.QStandardItem): |
---|
175 | msg = "Incorrect type passed to the Corfunc Perspective" |
---|
176 | raise AttributeError(msg) |
---|
177 | |
---|
178 | self._model_item = data_item[0] |
---|
179 | data = GuiUtils.dataFromItem(self._model_item) |
---|
180 | self._calculator.lowerq = 1e-3 |
---|
181 | self._calculator.upperq = (2e-1, 3e-1) |
---|
182 | self._calculator.set_data(data) |
---|
183 | |
---|
184 | self._canvas.axes.plot(data.x, data.y) |
---|
185 | |
---|
186 | # self.model.item(WIDGETS.W_FILENAME).setData(QtCoreQVariant(self._model_item.text())) |
---|
187 | |
---|
188 | def setClosable(self, value=True): |
---|
189 | """ |
---|
190 | Allow outsiders close this widget |
---|
191 | """ |
---|
192 | assert isinstance(value, bool) |
---|
193 | |
---|
194 | self._allow_close = value |
---|
195 | |
---|
196 | |
---|
197 | if __name__ == "__main__": |
---|
198 | app = QtGui.QApplication([]) |
---|
199 | import qt4reactor |
---|
200 | # qt4reactor.install() |
---|
201 | # DO NOT move the following import to the top! |
---|
202 | # (unless you know what you're doing) |
---|
203 | from twisted.internet import reactor |
---|
204 | dlg = CorfuncWindow(reactor) |
---|
205 | print(dlg) |
---|
206 | dlg.show() |
---|
207 | # print(reactor) |
---|
208 | # reactor.run() |
---|
209 | sys.exit(app.exec_()) |
---|