/
GuiqwtScreen.py
336 lines (236 loc) · 8.65 KB
/
GuiqwtScreen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 22 17:05:06 2015
@author: Alexandre DAREAU
"""
from guidata.qt.QtGui import (QWidget, QVBoxLayout, QHBoxLayout, QPushButton,
QMainWindow, QGridLayout,QSizePolicy)
from guidata.qt.QtCore import QRect
import numpy as np
import weakref
#---Import plot widget base class
import guiqwt.tools as tools
from guiqwt.config import _
from guiqwt.curve import CurvePlot
from guiqwt.image import ImagePlot
from guiqwt.plot import PlotManager
from guiqwt.builder import make
from guiqwt.shapes import RectangleShape
from guiqwt.annotations import AnnotatedRectangle
from guiqwt.styles import LineStyleParam
from guiqwt.styles import ShapeParam
from guidata.configtools import get_icon
class DefaultToolbarID:
pass
#---
class ImageScreen(QWidget):
"""
Hum hum hum
"""
def __init__(self, parent, x, y, data):
QWidget.__init__(self, parent)
#self.setMinimumSize(sizex, sizey)
self.x = x
self.y = y
self.data = data
self.level_xy = [ [[0,0],[0,0]] for i in range(8) ]
#---guiqwt related attributes:
self.plot = None
self.image = None
self.ROI = None
self.background = None
self.levels = []
#---
self.setup_widget()
def setup_widget(self):
self.plot = ImagePlot(self)
self.image = make.xyimage(self.x,self.y,self.data,colormap='jet')
self.plot.add_item(self.image)
self.plot.set_antialiasing(True)
#--- levels ?
for i in range(8):
x = self.level_xy[i][0]
y = self.level_xy[i][1]
self.levels.append(make.curve(x,y, color="w"))
self.plot.add_item(self.levels[-1])
#----------------------
vlayout = QVBoxLayout()
vlayout.addWidget(self.plot)
self.setLayout(vlayout)
self.plot.adjustSize()
def update_image(self):
self.image.x = self.x
self.image.y = self.y
self.image.data = self.data
for i in range(8):
x = self.level_xy[i][0]
y = self.level_xy[i][1]
self.levels[i].x = x
self.levels[i].y = y
self.plot.replot()
pass
def update_contour(self):
for i in range(8):
x = self.level_xy[i][0]
y = self.level_xy[i][1]
self.levels[i].set_data(x,y)
self.plot.replot()
pass
def reset_contour(self):
self.level_xy = [ [[0,0],[0,0]] for i in range(8) ]
for i in range(8):
x = self.level_xy[i][0]
y = self.level_xy[i][1]
self.levels[i].set_data(x,y)
self.plot.replot()
pass
class CutScreen(QWidget):
def __init__(self, parent, xdata=[], ydata=[], xfit=[], yfit=[],
sizex=100, sizey=100):
QWidget.__init__(self,parent)
self.sizex = sizex
self.sizey = sizey
#self.setMinimumSize(self.sizex,self.sizey)
self.xdata = xdata
self.ydata = ydata
self.xfit = xfit
self.yfit = yfit
self.plot = None
self.data_cut = None
self.fit_cut = None
self.setup_widget()
def setup_widget(self):
self.plot = CurvePlot(self)
self.data_cut = make.curve(self.xdata,self.ydata, color="b")
self.data_fit = make.curve(self.xfit, self.yfit, color="r")
self.plot.add_item(self.data_cut)
self.plot.add_item(self.data_fit)
vlayout = QVBoxLayout()
vlayout.addWidget(self.plot)
self.setLayout(vlayout)
def update_plot(self):
self.data_cut.set_data(self.xdata, self.ydata)
self.data_fit.set_data(self.xfit, self.yfit)
self.plot.set_plot_limits(self.xdata.min(), self.xdata.max(),
self.ydata.min(), self.ydata.max())
self.plot.replot()
# New class : LabelledRectangle = same as AnnotatedRectangle but with fixed text label
class LabelledRectangle(AnnotatedRectangle):
def __init__(self, label_txt=''):
super(AnnotatedRectangle,self).__init__()
self.label_txt = label_txt
# Override get_text to return always the same text
def get_text(self):
return self.label_txt
class RegionSelectTool(tools.RectangleTool):
def __init__(self, *args, **kwargs):
super(RegionSelectTool,self).__init__(*args, **kwargs)
self.name = "test"
self.rect = LabelledRectangle(label_txt=self.name)
self.set_shape_style(self.rect)
self.p0=(0,0)
self.p1=(0,0)
def add_shape_to_plot(self, plot, p0, p1):
"""
Method called when shape's rectangular area
has just been drawn on screen.
Adding the final shape to plot and returning it.
"""
self.p0 = p0
self.p1 = p1
self.rect.move_local_point_to(0,p0)
self.rect.move_local_point_to(2,p1)
#self.set_shape_style(self.rect)
plot.replot()
class ROISelectTool(RegionSelectTool):
def __init__(self, *args, **kwargs):
super(ROISelectTool,self).__init__(*args, **kwargs)
self.name = "ROI"
self.rect.label_txt = self.name
self.TIP = _("Select ROI")
self.TITLE = _("ROI")
'''
line = LineStyleParam()
line.set_style_from_matlab(':')
line.color="#ff0000"
self.rect.shape.shapeparam.line.update_param(line.build_pen())
'''
class BKGNDSelectTool(RegionSelectTool):
def __init__(self, *args, **kwargs):
super(BKGNDSelectTool,self).__init__(*args, **kwargs)
self.name = "BKGND"
self.rect.label_txt = self.name
class HOLESelectTool(RegionSelectTool):
def __init__(self, *args, **kwargs):
super(HOLESelectTool,self).__init__(*args, **kwargs)
self.name = "HOLE"
self.rect.label_txt = self.name
class GuiqwtScreen(QWidget):
"""
Hum hum hum
"""
def __init__(self, parent=None, x=[], y=[], data=[]):
QWidget.__init__(self, parent)
if len(x)<2:
x = np.linspace(0,1392,1392)
y = np.linspace(0,1040,1040)
data = np.zeros((1040,1392))
self.x = x
self.y = y
self.data = data
sx = 600
sy = int(sx/1.338)
stretch_x = 3
stretch_y = 3
#self.setGeometry(QRect(0,0,sx,sy))
layout = QGridLayout(self)
layout.setMargin(0)
self.screen = ImageScreen(self,x,y,data)
self.cutX = CutScreen(self)
self.cutY = CutScreen(self)
layout.addWidget(self.screen,1,0)
layout.addWidget(self.cutX,0,0)
layout.addWidget(self.cutY,1,1)
layout.setColumnStretch(0, stretch_x)
layout.setColumnStretch(1, 1)
layout.setRowStretch(0, 1)
layout.setRowStretch(1, stretch_y)
self.setLayout(layout)
self.updateGeometry()
self.manager = PlotManager(self)
self.manager.add_plot(self.screen.plot)
self.manager.add_plot(self.cutX.plot)
self.manager.add_plot(self.cutY.plot)
#roi = ROISelectTool
#bkgnd = BKGNDSelectTool
self.tools=[tools.SelectTool,tools.RectZoomTool,
tools.ColormapTool]
self.manager.register_all_curve_tools()
### Test function
class TestWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("Test")
a = 600
self.resize(int(a),int(a/1.338))
hlayout = QHBoxLayout()
central_widget = QWidget(self)
central_widget.setLayout(hlayout)
self.setCentralWidget(central_widget)
def setup_window(self):
x = np.linspace(0,1392,1392)
y = np.linspace(0,1040,1040)
xm,ym = np.meshgrid(x,y)
data = np.exp(-(xm-700.)**2/50.0-(ym-500.)**2/100.0)
x=[]
wid = GuiqwtScreen(self,x,y,data)
self.centralWidget().layout().addWidget(wid)
def test():
from guidata.qt.QtGui import QApplication
app = QApplication([])
win = TestWindow()
win.setup_window()
win.show()
app.exec_()
if __name__ == '__main__':
test()