-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportWindow.py
442 lines (381 loc) · 21.2 KB
/
ExportWindow.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QComboBox, QCheckBox, QSpinBox, QFileDialog
import ResultWindow
import matplotlib
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from mpl_toolkits.axes_grid1 import make_axes_locatable
class ExportWindow(QMainWindow):
FILETYPES_TOPO = ['png', 'jpg', 'svg', 'eps']
COLORMAPS = ['afmhot', 'hot', 'gist_heat', 'gist_gray']
COLORS = ['black', 'white', 'blue', 'green']
DATA_TYPES = ['topo', 'spectro']
def __init__(self, parent):
super(ExportWindow, self).__init__()
self.parent = parent
self.setWindowModality(QtCore.Qt.ApplicationModal)
self.active_window = None
# Data selected for combined results:
self.data_selected = 'topo'
self.filetype = 'png'
self.colorbar = False
self.rulers = False
self.colormap = 'afmhot'
self.scalebar = True
self.scalebar_color = 'black'
self.scalebar_fontsize = 13
self.dpi = 300
self.fontsize_labels = 11
self.fontsize = 11
self.profiles = False
self.profiles_color = 'black'
self._setup()
self._createActions()
self._connectActions()
def _setup(self):
self.setObjectName("Export Data")
self.setFixedSize(710, 640)
self.setWindowTitle("Export")
self.central_widget = QtWidgets.QWidget(self)
self.layout_main = QtWidgets.QVBoxLayout(self.central_widget)
self.layout_2 = QtWidgets.QHBoxLayout(self.central_widget)
self.layout_settings = QtWidgets.QVBoxLayout(self.central_widget)
self.layout_preview = QtWidgets.QVBoxLayout(self.central_widget)
self.layout_buttons = QtWidgets.QHBoxLayout(self.central_widget)
self.layout_2.addLayout(self.layout_preview)
self.layout_2.addLayout(self.layout_settings)
self.layout_main.addLayout(self.layout_2)
self.layout_main.addLayout(self.layout_buttons)
self.setCentralWidget(self.central_widget)
self.canvas = FigureCanvas(self)
self.canvas.setMinimumSize(400, 400)
self.canvas.setMaximumSize(400, 400)
self.layout_preview.addWidget(self.canvas)
self._create_topography_filetypes()
self._create_data_selector()
self._create_image_settings()
def _create_topography_filetypes(self):
self.combobox_filetypes = QComboBox(self)
self.combobox_filetypes.addItems(self.FILETYPES_TOPO)
self.layout_settings.addWidget(self.combobox_filetypes)
self.combobox_filetypes.currentIndexChanged.connect(self._filetype_text_changed)
def _create_data_selector(self):
self.combobox_data_selector = QComboBox(self)
self.combobox_data_selector.addItems(self.DATA_TYPES)
self.layout_settings.addWidget(self.combobox_data_selector)
self.combobox_data_selector.currentIndexChanged.connect(self._data_selector_text_changed)
def _create_image_settings(self):
self.setting_colorbar = QCheckBox('Colorbar', self)
self.setting_colorbar.setChecked(self.colorbar)
self.setting_colorbar.toggled.connect(self._colorbar_changed)
self.layout_settings.addWidget(self.setting_colorbar)
self.setting_profiles_layout = QtWidgets.QHBoxLayout(self.central_widget)
self.setting_profiles = QCheckBox('Profile Lines', self)
self.setting_profiles.setChecked(self.profiles)
self.setting_profiles.toggled.connect(self._profiles_changed)
self.setting_profiles_layout.addWidget(self.setting_profiles)
self.label_profiles_color = QtWidgets.QLabel('Color:', self.central_widget)
self.setting_profiles_layout.addWidget(self.label_profiles_color)
self.setting_profiles_color = QtWidgets.QComboBox(self.central_widget)
self.setting_profiles_color.addItems(self.COLORS)
self.setting_profiles_color.currentTextChanged.connect(self._profiles_color_changed)
self.setting_profiles_layout.addWidget(self.setting_profiles_color)
self.layout_settings.addLayout(self.setting_profiles_layout)
self.setting_colormap_layout = QtWidgets.QHBoxLayout(self.central_widget)
self.label_colormap = QtWidgets.QLabel('Colormap:', self.central_widget)
self.setting_colormap_layout.addWidget(self.label_colormap)
self.setting_colormap = QComboBox(self.central_widget)
self.setting_colormap.addItems(self.COLORMAPS)
self.setting_colormap_layout.addWidget(self.setting_colormap)
self.setting_colormap.currentTextChanged.connect(self._colormap_changed)
self.layout_settings.addLayout(self.setting_colormap_layout)
self.setting_scalebar_layout = QtWidgets.QHBoxLayout(self.central_widget)
self.setting_scalebar = QCheckBox('Scalebar', self)
self.setting_scalebar.setChecked(self.scalebar)
self.setting_scalebar.toggled.connect(self._scalebar_changed)
self.setting_scalebar_layout.addWidget(self.setting_scalebar)
self.label_scalebar_color = QtWidgets.QLabel('Color:', self.central_widget)
self.setting_scalebar_layout.addWidget(self.label_scalebar_color)
self.setting_scalebar_color = QtWidgets.QComboBox(self.central_widget)
self.setting_scalebar_color.addItems(self.COLORS)
self.setting_scalebar_color.currentTextChanged.connect(self._scalebar_color_changed)
self.setting_scalebar_layout.addWidget(self.setting_scalebar_color)
self.label_scalebar_fontsize = QtWidgets.QLabel('Font size:', self.central_widget)
self.setting_scalebar_layout.addWidget(self.label_scalebar_fontsize)
self.setting_scalebar_fontsize = QtWidgets.QSpinBox(self.central_widget)
self.setting_scalebar_fontsize.setRange(1, 60)
self.setting_scalebar_fontsize.setValue(self.scalebar_fontsize)
self.setting_scalebar_fontsize.valueChanged.connect(self._scalebar_fontsize_changed)
self.setting_scalebar_layout.addWidget(self.setting_scalebar_fontsize)
self.layout_settings.addLayout(self.setting_scalebar_layout)
self.setting_ruler_layout = QtWidgets.QHBoxLayout(self.central_widget)
self.setting_ruler = QCheckBox('Axes', self)
self.setting_ruler.setChecked(self.rulers)
self.setting_ruler.toggled.connect(self._ruler_changed)
self.setting_ruler_layout.addWidget(self.setting_ruler)
self.label_fontsize = QtWidgets.QLabel('Font size:', self.central_widget)
self.setting_ruler_layout.addWidget(self.label_fontsize)
self.setting_fontsize = QtWidgets.QSpinBox(self.central_widget)
self.setting_fontsize.setRange(1, 60)
self.setting_fontsize.setValue(self.fontsize)
self.setting_fontsize.valueChanged.connect(self._fontsize_changed)
self.setting_ruler_layout.addWidget(self.setting_fontsize)
self.label_fontsize_labels = QtWidgets.QLabel('Lables font size:', self.central_widget)
self.setting_ruler_layout.addWidget(self.label_fontsize_labels)
self.setting_fontsize_labels = QtWidgets.QSpinBox(self.central_widget)
self.setting_fontsize_labels.setRange(1, 60)
self.setting_fontsize_labels.setValue(self.fontsize)
self.setting_fontsize_labels.valueChanged.connect(self._fontsize_labels_changed)
self.setting_ruler_layout.addWidget(self.setting_fontsize_labels)
self.layout_settings.addLayout(self.setting_ruler_layout)
self.setting_dpi_layout = QtWidgets.QHBoxLayout(self.central_widget)
self.label_dpi = QtWidgets.QLabel('DPI: ', self.central_widget)
self.setting_dpi = QtWidgets.QSpinBox(self.central_widget)
self.setting_dpi.setRange(60, 1200)
self.setting_dpi.setValue(self.dpi)
self.setting_dpi.valueChanged.connect(self._dpi_changed)
self.setting_dpi_layout.addWidget(self.label_dpi)
self.setting_dpi_layout.addWidget(self.setting_dpi)
self.layout_settings.addLayout(self.setting_dpi_layout)
def _filetype_text_changed(self, i):
self.filetype = self.combobox_filetypes.currentText()
def _data_selector_text_changed(self, i):
self.data_selected = self.combobox_data_selector.currentText()
self.update()
def _colorbar_changed(self):
self.colorbar = self.setting_colorbar.isChecked()
self.draw()
def _profiles_changed(self):
self.profiles = self.setting_profiles.isChecked()
self.draw()
def _profiles_color_changed(self, color):
self.profiles_color = color
self.draw()
def _colormap_changed(self, colormap):
self.colormap = colormap
self.draw()
def _scalebar_changed(self):
self.scalebar = self.setting_scalebar.isChecked()
self.draw()
def _scalebar_color_changed(self, color):
self.scalebar_color = color
self.draw()
def _ruler_changed(self):
self.rulers = self.setting_ruler.isChecked()
self.draw()
def _dpi_changed(self, value):
self.dpi = value
def _fontsize_changed(self, value):
self.fontsize = value
self.draw()
def _fontsize_labels_changed(self, value):
self.fontsize_labels = value
self.draw()
def _scalebar_fontsize_changed(self, value):
self.scalebar_fontsize = value
self.draw()
def _createActions(self):
# Create apply button
self.applyButton = QtWidgets.QPushButton("Apply", self)
self.layout_buttons.addWidget(self.applyButton)
self.applyButton.setObjectName("applyButton")
# Create cancel button
self.cancelButton = QtWidgets.QPushButton("Cancel", self)
self.layout_buttons.addWidget(self.cancelButton)
self.cancelButton.setObjectName("cancelButton")
def _connectActions(self):
self.cancelButton.clicked.connect(self.cancel)
self.applyButton.clicked.connect(self.apply)
def _hide_from_layout(self, layout):
for i in reversed(range(layout.count())):
if layout.itemAt(i).widget() is None:
self._hide_from_layout(layout.itemAt(i).layout())
else:
layout.itemAt(i).widget().hide()
def _show_topography_filetypes(self):
self.combobox_filetypes.show()
def _show_image_settings(self):
self.setting_colorbar.show()
self.setting_profiles.show()
self.label_profiles_color.show()
self.setting_profiles_color.show()
self.label_colormap.show()
self.setting_colormap.show()
self.setting_scalebar.show()
self.setting_scalebar_color.show()
self.label_scalebar_fontsize.show()
self.setting_scalebar_fontsize.show()
self.setting_ruler.show()
self.label_fontsize.show()
self.setting_fontsize.show()
self.label_fontsize_labels.show()
self.setting_fontsize_labels.show()
self.label_dpi.show()
self.setting_dpi.show()
def _show_plot_settings(self):
pass
def _show_data_selector(self):
self.combobox_data_selector.show()
def update(self):
self._hide_from_layout(self.layout_settings)
if self.parent.active_result_window is not None:
self.active_window = self.parent.results_windows[self.parent.active_result_window]
if isinstance(self.active_window, ResultWindow.TopographyWindow):
self._show_topography_filetypes()
self._show_image_settings()
elif isinstance(self.active_window, (ResultWindow.SpectroscopyWindow, ResultWindow.ProfileResultWindow)):
self._show_topography_filetypes()
self._show_plot_settings()
elif isinstance(self.active_window, ResultWindow.CombinedSpectroscopyMapWindow):
self._show_topography_filetypes()
self._show_data_selector()
if self.data_selected == 'topo':
self._show_topography_filetypes()
self._show_image_settings()
if self.data_selected == 'spectro':
self._show_topography_filetypes()
self._show_image_settings()
self.draw()
def _delete_from_layout(self, layout):
for i in reversed(range(layout.count())):
if layout.itemAt(i).widget() is None:
self._delete_from_layout(layout.itemAt(i).layout())
else:
layout.itemAt(i).widget().setParent(None)
def draw(self):
self.canvas.fig.clf()
self.canvas.axes = self.canvas.fig.add_subplot(111)
if isinstance(self.active_window, (ResultWindow.TopographyWindow)):
im = self.canvas.axes.pcolormesh(self.active_window.data.X, self.active_window.data.Y,
self.active_window.data.Z, cmap=self.colormap)
elif isinstance(self.active_window, ResultWindow.CombinedSpectroscopyMapWindow) and self.data_selected =='topo':
im = self.canvas.axes.pcolormesh(self.active_window.data[1].X, self.active_window.data[1].Y,
self.active_window.data[1].Z, cmap=self.colormap)
elif isinstance(self.active_window, ResultWindow.CombinedSpectroscopyMapWindow) and self.data_selected =='spectro':
im = self.canvas.axes.pcolormesh(self.active_window.data[0].x, self.active_window.data[0].y, self.active_window.data[0].z_forward[:, :, self.active_window.active_plane], cmap=self.colormap)
self.canvas.axes.set_aspect('equal')
if self.profiles:
self._draw_profile_lines()
if self.colorbar:
divider = make_axes_locatable(self.canvas.axes)
self.canvas.caxes = divider.append_axes("right", size="3%", pad=0.02)
self.cb = self.canvas.fig.colorbar(im, cax=self.canvas.caxes)
if isinstance(self.active_window, (ResultWindow.TopographyWindow)):
self.cb.ax.set_title(self.active_window.data.unit)
elif isinstance(self.active_window,
ResultWindow.CombinedSpectroscopyMapWindow) and self.data_selected == 'topo':
self.cb.ax.set_title(self.active_window.data[1].unit)
elif isinstance(self.active_window,
ResultWindow.CombinedSpectroscopyMapWindow) and self.data_selected == 'spectro':
self.cb.ax.set_title(self.active_window.data[0].unit)
if not self.rulers:
self.canvas.axes.get_xaxis().set_visible(False)
self.canvas.axes.get_yaxis().set_visible(False)
else:
if isinstance(self.active_window, (ResultWindow.TopographyWindow)):
self.canvas.axes.set_xlabel(f'x [{self.active_window.data.xunit}]', fontsize=self.fontsize_labels)
self.canvas.axes.set_ylabel(f'y [{self.active_window.data.xunit}]', fontsize=self.fontsize_labels)
elif isinstance(self.active_window,
ResultWindow.CombinedSpectroscopyMapWindow) and self.data_selected == 'topo':
self.canvas.axes.set_xlabel(f'x [{self.active_window.data[1].xunit}]', fontsize=self.fontsize_labels)
self.canvas.axes.set_ylabel(f'y [{self.active_window.data[1].xunit}]', fontsize=self.fontsize_labels)
elif isinstance(self.active_window,
ResultWindow.CombinedSpectroscopyMapWindow) and self.data_selected == 'spectro':
self.canvas.axes.set_xlabel(f'x [{self.active_window.data[0].xunit}]', fontsize=self.fontsize_labels)
self.canvas.axes.set_ylabel(f'y [{self.active_window.data[0].xunit}]', fontsize=self.fontsize_labels)
self.canvas.axes.tick_params(axis='both', which='major', labelsize=self.fontsize)
if self.scalebar:
self._draw_scale_bar()
if not self.colorbar and not self.rulers:
self.canvas.axes.set_frame_on(False)
self.canvas.fig.tight_layout(pad=0)
else:
self.canvas.fig.tight_layout(rect=[0, 0.02, 1, 0.97])
self.canvas.draw()
def _draw_scale_bar(self):
if isinstance(self.active_window, ResultWindow.TopographyWindow):
shape = self.active_window.data.X.shape
xrange = self.active_window.data.get_x_range()
length = round(0.2 * xrange)
text = str(length) + ' ' + self.active_window.data.xunit
x1 = self.active_window.data.X[1, round(0.1 * shape[0])]
x2 = self.active_window.data.X[1, round(0.3 * shape[0])]
y1 = self.active_window.data.Y[round(0.12 * shape[0]), 1]
y2 = self.active_window.data.Y[round(0.12 * shape[0]), 1]
line = matplotlib.lines.Line2D([x1, x2],
[y1, y2], linewidth=5,
color=self.scalebar_color, solid_capstyle='butt')
text = matplotlib.text.Text(x=self.active_window.data.X[1, round(0.2 * shape[0])],
y=self.active_window.data.Y[round(0.15 * shape[1]), 1],
text=text,
horizontalalignment='center', fontsize=self.scalebar_fontsize, color=self.scalebar_color)
if isinstance(self.active_window, ResultWindow.CombinedSpectroscopyMapWindow) and self.data_selected == 'topo':
shape = self.active_window.data[1].X.shape
xrange = self.active_window.data[1].get_x_range()
length = round(0.2 * xrange)
text = str(length) + ' ' + self.active_window.data[1].xunit
x1 = self.active_window.data[1].X[1, round(0.1 * shape[0])]
x2 = self.active_window.data[1].X[1, round(0.3 * shape[0])]
y1 = self.active_window.data[1].Y[round(0.12 * shape[0]), 1]
y2 = self.active_window.data[1].Y[round(0.12 * shape[0]), 1]
line = matplotlib.lines.Line2D([x1, x2],
[y1, y2], linewidth=5,
color=self.scalebar_color, solid_capstyle='butt')
text = matplotlib.text.Text(x=self.active_window.data[1].X[1, round(0.2 * shape[0])],
y=self.active_window.data[1].Y[round(0.15 * shape[1]), 1],
text=text,
horizontalalignment='center', fontsize=self.scalebar_fontsize,
color=self.scalebar_color)
if isinstance(self.active_window,
ResultWindow.CombinedSpectroscopyMapWindow) and self.data_selected == 'spectro':
shape = self.active_window.data[0].x.shape
xrange = self.active_window.data[0].get_x_range()
length = round(0.2 * xrange)
text = str(length) + ' ' + self.active_window.data[0].xunit
x1 = self.active_window.data[0].x[1, round(0.1 * shape[0])]
x2 = self.active_window.data[0].x[1, round(0.3 * shape[0])]
y1 = self.active_window.data[0].y[round(0.12 * shape[0]), 1]
y2 = self.active_window.data[0].y[round(0.12 * shape[0]), 1]
line = matplotlib.lines.Line2D([x1, x2],
[y1, y2], linewidth=5,
color=self.scalebar_color, solid_capstyle='butt')
text = matplotlib.text.Text(x=self.active_window.data[0].x[1, round(0.2 * shape[0])],
y=self.active_window.data[0].y[round(0.15 * shape[1]), 1],
text=text,
horizontalalignment='center', fontsize=self.scalebar_fontsize,
color=self.scalebar_color)
self.canvas.axes.add_line(line)
self.canvas.axes.add_artist(text)
def _draw_profile_lines(self):
if self.active_window.profile_lines[self.active_window.active_ax] != []:
for profile in self.active_window.profile_lines[self.active_window.active_ax]:
x1 = profile.first_point.x
x2 = profile.second_point.x
y1 = profile.first_point.y
y2 = profile.second_point.y
line = matplotlib.lines.Line2D([x1, x2],
[y1, y2],
color=self.profiles_color, solid_capstyle='butt')
self.canvas.axes.add_line(line)
def disable(self):
self.applyButton.setDisabled(True)
def enable(self):
self.applyButton.setDisabled(False)
def apply(self):
file, _ = QFileDialog.getSaveFileName(self, caption=f'Save .{self.filetype}', filter=f'Images (*.{self.filetype})')
self.canvas.fig.savefig(file, format=self.filetype, dpi=self.dpi)
def cancel(self):
self.hide()
# Event handling
def eventFilter(self, source, event):
# Calling parent closing function on close
if event.type() == QtCore.QEvent.Hide:
self.parent.export_win_active = False
return False
class FigureCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
self.fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = self.fig.add_subplot(111)
self.caxes = None
# self.axes.set_position([0.1, 0.2, 0.85, 0.78])
super(FigureCanvas, self).__init__(self.fig)