-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHussariX.py
321 lines (303 loc) · 14.7 KB
/
HussariX.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
import sys
import os
from datetime import datetime
from PyQt5.QtCore import Qt, QSettings, QDir
from PyQt5 import QtGui, QtWidgets
from pyqtgraph.dockarea import Dock, DockArea
from pyqtgraph.console import Console
from lib.ui.CamecaQtModels import CamecaWDSTreeModel
from lib.ui import SpectrumWidgets as sw
from lib.parsers import cameca
from lib.icons.icons import IconProvider
setting = QSettings('setting.ini', QSettings.IniFormat)
# check if there is peaksight installed
CAMSOFT_IS_PRESENT = False
if os.name == 'nt':
import winreg
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r"SOFTWARE\Cameca\SX\Configuration") as cam_con:
cam_data_path = winreg.QueryValueEx(cam_con, "DataPath")[0]
CAMSOFT_IS_PRESENT = True
except EnvironmentError:
pass
class DockMenu(QtWidgets.QMenu):
def __init__(self, dock_widget, parent=None):
self.dock_widget = dock_widget
name = dock_widget.title()
QtWidgets.QMenu.__init__(self, name, parent=parent)
self.rename_action = QtWidgets.QAction('rename')
self.rename_action.setIcon(
QtGui.QIcon(self.parent().icon_provider.get_icon_path(
'rename.svg'
)))
self.del_action = QtWidgets.QAction('discard plot')
self.del_action.setIcon(
QtGui.QIcon(self.parent().icon_provider.get_icon_path(
'plot-window-close.svg'
)))
self.addAction(self.rename_action)
self.addAction(self.del_action)
self.rename_action.triggered.connect(self.rename_dock)
self.del_action.triggered.connect(self.remove_dock)
def rename_dock(self):
dlg = QtWidgets.QInputDialog()
dlg.setTextValue(self.dock_widget.title())
dlg.setLabelText("Give a new name for Plot Widget")
renamed = dlg.exec()
if renamed:
new_text = dlg.textValue()
self.dock_widget.setTitle(new_text)
self.dock_widget.widgets[0].name = new_text
self.setTitle(new_text)
def remove_dock(self):
main_window = self.parent()
main_window.destroy_wds_plotWidget(self.dock_widget)
class HussariX(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self, parent=None)
self.setGeometry(20, 20, 800, 600)
placeholder_text = QtWidgets.QLabel(
"Content will appear here after opening file")
placeholder_text.setWhatsThis(
"""<p>After opening file this is transformed into Docking Area,
where any newly opened window can be dragged and placed
or stacked independently, or even poped-out to separate
independent window (useful in multi-monitor setup)</p>
""")
placeholder_text.setAlignment(Qt.AlignCenter)
self.setCentralWidget(placeholder_text)
self.docking_area = DockArea()
self.icon_provider = IconProvider(self)
self.plot_n = 0
menu = self.menuBar()
file_m = menu.addMenu("File")
self.plotting_m = menu.addMenu("Plotting")
self.help_m = menu.addMenu("Help")
self.plotting_m_entries = []
self.setWindowTitle("HussariX")
icon = QtGui.QIcon()
for i in [16, 22, 32, 48, 64]:
icon.addFile(
self.icon_provider.get_icon_path(
'hussarix_{}_icon.svg'.format(i),
neutral=True))
self.setWindowIcon(icon)
self.action_whats_this = QtWidgets.QWhatsThis.createAction(self)
self.help_m.addAction(self.action_whats_this)
self.help_m.addAction("About Qt", QtWidgets.qApp.aboutQt)
self.action_console = QtWidgets.QAction('python console')
self.console = None
self.action_console.setCheckable(True)
self.action_console.setChecked(False)
self.help_m.addAction(self.action_console)
self.action_console.toggled.connect(self.show_console)
self.action_open_wdsDat = QtWidgets.QAction('Open wdsDat')
self.action_open_wdsDat.setIcon(
QtGui.QIcon(self.icon_provider.get_icon_path(
'wdsDat_open.svg')))
self.action_open_qtiDat = QtWidgets.QAction('Open qtiDat')
self.action_open_qtiDat.setIcon(
QtGui.QIcon(self.icon_provider.get_icon_path(
'qtiDat_open.svg')))
self.action_open_impDat = QtWidgets.QAction('Open impDat')
self.action_open_impDat.setIcon(
QtGui.QIcon(self.icon_provider.get_icon_path(
'impDat_open.svg')))
self.action_open_qtiDat.setDisabled(True)
self.action_open_impDat.setDisabled(True)
self.action_open_spx = QtWidgets.QAction('Open spx')
self.action_open_spx.setIcon(
QtGui.QIcon(self.icon_provider.get_icon_path('spx_open.svg')))
self.action_open_spx.setToolTip('Bruker Esprit spectra .spx')
self.action_open_rtx = QtWidgets.QAction('Open rtx')
self.action_open_rtx.setIcon(
QtGui.QIcon(self.icon_provider.get_icon_path('rtx_open.svg')))
self.action_open_rtx.setToolTip('Bruker Esprit project .rtx')
self.action_open_rtx.setDisabled(True)
file_m.addSection('Cameca Peaksight files')
file_m.addAction(self.action_open_wdsDat)
file_m.addAction(self.action_open_qtiDat)
file_m.addAction(self.action_open_impDat)
file_m.addSection('Bruker Esprit files')
file_m.addAction(self.action_open_spx)
file_m.addAction(self.action_open_rtx)
self.any_file_opened = False
self.any_wds_opened = False
file_m.addSeparator()
self.plotting_m.addSeparator()
self.action_quit = QtWidgets.QAction('Quit')
self.action_quit.setShortcut('Ctrl+Q')
self.action_quit.triggered.connect(QtWidgets.qApp.quit)
file_m.addAction(self.action_quit)
self.action_mkPlotItem = QtWidgets.QAction('New Plotting Widget')
self.action_mkPlotItem.setIcon(
QtGui.QIcon(self.icon_provider.get_icon_path(
'plot-window-new.svg')))
self.plotting_m.addAction(self.action_mkPlotItem)
self.action_mkPlotItem.setEnabled(False)
self.plot_widgets = []
self.action_open_wdsDat.triggered.connect(self.open_wds_files)
def open_wds_files(self):
dialog = QtWidgets.QFileDialog()
# agg_checkbox = QtWidgets.QCheckBox(
# 'Sum the items with duplicate comment')
# dl = dialog.layout()
# dl.addWidget(agg_checkbox)
dialog.setNameFilter("Cameca Wds Data (*.wdsDat);;All Files (*)")
dialog.setWindowTitle("select WdsDat file(s)")
dialog.setFileMode(dialog.ExistingFiles)
if CAMSOFT_IS_PRESENT:
with winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r"SOFTWARE\Cameca\SX\Configuration") as conf:
current_project = winreg.QueryValueEx(conf,
"CurrentProject")[0]
current_sample = winreg.QueryValueEx(conf,
"CurrentSample")[0]
wds_data_path = os.path.join(cam_data_path, current_project,
current_sample, "WDS Spectra")
dialog.setDirectory(wds_data_path)
else:
setting.sync()
last_dir = setting.value('last_wds_dir')
if last_dir is not None and QDir(last_dir).exists():
dialog.setDirectory(last_dir)
dialog.exec()
files = dialog.selectedFiles()
if len(files) > 0 and 'wdsDat' in files[0]:
if not CAMSOFT_IS_PRESENT:
setting.setValue('last_wds_dir',
dialog.directory().absolutePath())
setting.sync()
if not self.any_file_opened:
self.setCentralWidget(self.docking_area)
self.any_file_opened = True
self.docking_widgets = []
if not self.any_wds_opened:
self.wds_files_model = CamecaWDSTreeModel([])
self.wds_tree_view = QtWidgets.QTreeView()
self.wds_tree_view.setWhatsThis(
"""<h4> WDS dataset/file tree View</h4>
<p> This is where visibility of given dataset can
be controlled by checking/unchecking the dataset;
When cheking/unchecking filename it will check/uncheck
all datasets under that filename in the tree;
Visibility is changed for generated curves in all opened
Plotting Widgets simultaniously.</p>
<p>Datasets have capability of multi selection
(with a help of "Shift" or/and "Ctrl" key modifiers);
Selected datasets will result in highlighting
the coresponding curves in the Plotting Widgets
(if dataset is checked in the tree, and checked Xtal/spect
combinations is present in the dataset)</p>
<p>Current item (the last item clicked or navigated with
keyboard) will change background model in Plotting Widgets
for simple Peak-Background markers</p>
<p>Mouse right click will open color dialog.</p>"""
)
self.wds_tree_view.setModel(self.wds_files_model)
self.wds_tree_view.setStyleSheet(
'show-decoration-selected: 0')
self.wds_files_selection_model = self.wds_tree_view.selectionModel()
self.wds_files_selection_model.selectionChanged.connect(
self.wds_files_model.highlight_spectra)
self.wds_tree_view.setHeaderHidden(True)
self.wds_tree_view.setSelectionMode(
QtWidgets.QTreeView.ExtendedSelection)
self.wds_tree_view.setContextMenuPolicy(Qt.CustomContextMenu)
self.wds_tree_view.customContextMenuRequested.connect(
self.wds_color_dialog)
self.wds_items_dw = Dock('WDS Files/Datasets',
widget=self.wds_tree_view,
autoOrientation=False,
size=(100, 100))
self.wds_items_dw.setStretch(x=1, y=2)
alpha_container = QtWidgets.QWidget()
qgb_layout = QtWidgets.QVBoxLayout(alpha_container)
alpha_container.setLayout(qgb_layout)
self.alpha_spin_box = QtWidgets.QSpinBox()
alpha_container.setWhatsThis(
"""<h4>Global alpha/transparency of plotted curves</h4>
<p>with many WDS spectra opened the Plotting Canvas/-es
get/-s easily cluttered. For better clarity curve alpha
(transparency) comes to help. Alpha can be changed from
0 (Fully transparent) to 255 (Fully opaque). This value
is forced for all plotted curves globaly, unless curve is
from slected dataset (is highlighted)</p>"""
)
self.alpha_spin_box.setMinimum(0)
self.alpha_spin_box.setMaximum(255)
self.alpha_spin_box.setValue(200)
qgb_layout.addWidget(QtWidgets.QLabel('alpha of curves:'))
qgb_layout.addWidget(self.alpha_spin_box)
self.wds_items_dw.addWidget(alpha_container)
self.docking_area.addDock(self.wds_items_dw, position="left")
self.wds_selection_model = self.wds_tree_view.selectionModel()
self.any_wds_opened = True
self.action_mkPlotItem.setEnabled(True)
self.action_mkPlotItem.trigger()
self.alpha_spin_box.valueChanged.connect(
self.wds_files_model.change_global_alpha)
self.action_mkPlotItem.triggered.connect(
self.make_wds_plotWidget)
self.action_mkPlotItem.trigger() # make at least one
wds_files = [cameca.CamecaWDS(i) for i in files]
self.wds_files_model.append_wds_files(wds_files)
def wds_color_dialog(self, qpoint):
index = self.wds_tree_view.indexAt(qpoint)
node = index.internalPointer()
initial_color = node.q_custom_color
new_color = QtWidgets.QColorDialog.getColor(initial_color)
if new_color.isValid():
self.wds_files_model.setData(index, new_color, Qt.DecorationRole)
def make_wds_plotWidget(self):
self.plot_n += 1
widget = sw.WDSSpectraGUI(self.wds_files_model,
self.wds_files_selection_model)
plotting_dw = Dock('WDS Plot {}'.format(self.plot_n), widget=widget,
autoOrientation=False)
widget.widgetFullscreened.connect(plotting_dw.setHidden)
widget.name = plotting_dw.title()
self.plot_widgets.append(widget)
self.docking_widgets.append(plotting_dw)
plotting_dw.setStretch(x=5, y=1)
if len(self.docking_widgets) == 1:
self.docking_area.addDock(plotting_dw, position="right",
size=(100, 100))
else:
self.docking_area.addDock(plotting_dw, "bottom",
self.docking_widgets[-2],
size=(100, 100))
plotting_dw.menu_entry = DockMenu(plotting_dw,
parent=self)
plotting_dw.menu_action = self.plotting_m.addMenu(
plotting_dw.menu_entry)
self.plotting_m.update()
def destroy_wds_plotWidget(self, dock_widget):
dock_widget.widgets[0].prepare_to_destroy()
self.plot_widgets.remove(dock_widget.widgets[0])
self.docking_widgets.remove(dock_widget)
self.plotting_m.removeAction(dock_widget.menu_action)
dock_widget.close()
def show_console(self, state):
if state:
if self.console is None:
self.console = Console.ConsoleWidget(self, namespace={
'hussarix': main_window,
'cameca': cameca})
self.console_dw = Dock("Python Console", widget=self.console,
autoOrientation=False)
self.docking_area.addDock(self.console_dw, position='bottom')
else:
self.console_dw.show()
else:
self.console_dw.hide()
def main():
global main_window
app = QtWidgets.QApplication(sys.argv)
main_window = HussariX()
main_window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()