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 | |
---|
21 | class CorfuncWindow(QtGui.QDialog, Ui_CorfuncDialog): |
---|
22 | # The controller which is responsible for managing signal slots connections |
---|
23 | # for the gui and providing an interface to the data model. |
---|
24 | name = "Corfunc" # For displaying in the combo box |
---|
25 | #def __init__(self, manager=None, parent=None): |
---|
26 | def __init__(self, parent=None): |
---|
27 | #super(InvariantWindow, self).__init__(parent) |
---|
28 | super(CorfuncWindow, self).__init__() |
---|
29 | self.setupUi(self) |
---|
30 | |
---|
31 | self.setWindowTitle("Corfunc Perspective") |
---|
32 | |
---|
33 | self.model = QtGui.QStandardItemModel(self) |
---|
34 | self.communicate = GuiUtils.Communicate() |
---|
35 | self._calculator = CorfuncCalculator() |
---|
36 | |
---|
37 | # Connect buttons to slots. |
---|
38 | # Needs to be done early so default values propagate properly. |
---|
39 | self.setupSlots() |
---|
40 | |
---|
41 | # Set up the model. |
---|
42 | self.setupModel() |
---|
43 | |
---|
44 | # Set up the mapper |
---|
45 | self.setupMapper() |
---|
46 | |
---|
47 | def setupSlots(self): |
---|
48 | self.extractBtn.clicked.connect(self.action) |
---|
49 | self.extrapolateBtn.clicked.connect(self.action) |
---|
50 | self.transformBtn.clicked.connect(self.action) |
---|
51 | |
---|
52 | self.calculateBgBtn.clicked.connect(self.calculateBackground) |
---|
53 | |
---|
54 | self.hilbertBtn.clicked.connect(self.action) |
---|
55 | self.fourierBtn.clicked.connect(self.action) |
---|
56 | |
---|
57 | self.model.itemChanged.connect(self.modelChanged) |
---|
58 | |
---|
59 | def setupModel(self): |
---|
60 | self.model.setItem(W.W_QMIN, |
---|
61 | QtGui.QStandardItem("0")) |
---|
62 | self.model.setItem(W.W_QMAX, |
---|
63 | QtGui.QStandardItem("0")) |
---|
64 | self.model.setItem(W.W_QCUTOFF, |
---|
65 | QtGui.QStandardItem("0")) |
---|
66 | self.model.setItem(W.W_BACKGROUND, |
---|
67 | QtGui.QStandardItem("0")) |
---|
68 | self.model.setItem(W.W_TRANSFORM, |
---|
69 | QtGui.QStandardItem("Fourier")) |
---|
70 | |
---|
71 | def modelChanged(self, item): |
---|
72 | if item.row() == W.W_QMIN: |
---|
73 | value = float(self.model.item(W.W_QMIN).text()) |
---|
74 | self.qMin.setValue(value) |
---|
75 | self._calculator.lowerq = value |
---|
76 | elif item.row() == W.W_QMAX: |
---|
77 | value = float(self.model.item(W.W_QMAX).text()) |
---|
78 | self.qMax1.setValue(value) |
---|
79 | self._calculator.upperq = (value, self._calculator.upperq[1]) |
---|
80 | elif item.row() == W.W_QCUTOFF: |
---|
81 | value = float(self.model.item(W.W_QCUTOFF).text()) |
---|
82 | self.qMax2.setValue(value) |
---|
83 | self._calculator.upperq = (self._calculator.upperq[0], value) |
---|
84 | elif item.row() == W.W_BACKGROUND: |
---|
85 | value = float(self.model.item(W.W_BACKGROUND).text()) |
---|
86 | self.bg.setValue(float(value)) |
---|
87 | else: |
---|
88 | print("{} Changed".format(item)) |
---|
89 | |
---|
90 | |
---|
91 | def setupMapper(self): |
---|
92 | self.mapper = QtGui.QDataWidgetMapper(self) |
---|
93 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
94 | self.mapper.setModel(self.model) |
---|
95 | |
---|
96 | self.mapper.addMapping(self.qMin, W.W_QMIN) |
---|
97 | self.mapper.addMapping(self.qMax1, W.W_QMAX) |
---|
98 | self.mapper.addMapping(self.qMax2, W.W_QCUTOFF) |
---|
99 | self.mapper.addMapping(self.bg, W.W_BACKGROUND) |
---|
100 | |
---|
101 | self.mapper.toFirst() |
---|
102 | |
---|
103 | def calculateBackground(self): |
---|
104 | bg = self._calculator.compute_background() |
---|
105 | print(bg) |
---|
106 | self.model.setItem(W.W_BACKGROUND, QtGui.QStandardItem(str(bg))) |
---|
107 | |
---|
108 | def action(self): |
---|
109 | print("Called an action!") |
---|
110 | print(self.model) |
---|
111 | print(self.mapper) |
---|
112 | |
---|
113 | def allowBatch(self): |
---|
114 | """ |
---|
115 | We cannot perform corfunc analysis in batch at this time. |
---|
116 | """ |
---|
117 | return False |
---|
118 | |
---|
119 | def setData(self, data_item, is_batch=False): |
---|
120 | """ |
---|
121 | Obtain a QStandardItem object and dissect it to get Data1D/2D |
---|
122 | Pass it over to the calculator |
---|
123 | """ |
---|
124 | if not isinstance(data_item, list): |
---|
125 | msg = "Incorrect type passed to the Corfunc Perpsective" |
---|
126 | raise AttributeError(msg) |
---|
127 | |
---|
128 | if not isinstance(data_item[0], QtGui.QStandardItem): |
---|
129 | msg = "Incorrect type passed to the Corfunc Perspective" |
---|
130 | raise AttributeError(msg) |
---|
131 | |
---|
132 | self._model_item = data_item[0] |
---|
133 | data = GuiUtils.dataFromItem(self._model_item) |
---|
134 | self._calculator.lowerq = 1e-3 |
---|
135 | self._calculator.upperq = (2e-1, 3e-1) |
---|
136 | self._calculator.set_data(data) |
---|
137 | |
---|
138 | # self.model.item(WIDGETS.W_FILENAME).setData(QtCoreQVariant(self._model_item.text())) |
---|
139 | |
---|
140 | def setClosable(self, value=True): |
---|
141 | """ |
---|
142 | Allow outsiders close this widget |
---|
143 | """ |
---|
144 | assert isinstance(value, bool) |
---|
145 | |
---|
146 | self._allow_close = value |
---|
147 | |
---|
148 | |
---|
149 | if __name__ == "__main__": |
---|
150 | app = QtGui.QApplication([]) |
---|
151 | import qt4reactor |
---|
152 | # qt4reactor.install() |
---|
153 | # DO NOT move the following import to the top! |
---|
154 | # (unless you know what you're doing) |
---|
155 | from twisted.internet import reactor |
---|
156 | dlg = CorfuncWindow(reactor) |
---|
157 | print(dlg) |
---|
158 | dlg.show() |
---|
159 | # print(reactor) |
---|
160 | # reactor.run() |
---|
161 | sys.exit(app.exec_()) |
---|