1 | # global |
---|
2 | import sys |
---|
3 | import os |
---|
4 | import time |
---|
5 | import logging |
---|
6 | |
---|
7 | from PyQt4 import QtCore |
---|
8 | from PyQt4 import QtGui |
---|
9 | from PyQt4 import QtWebKit |
---|
10 | from PyQt4.Qt import QMutex |
---|
11 | |
---|
12 | from twisted.internet import threads |
---|
13 | |
---|
14 | # SAS |
---|
15 | from sas.sascalc.dataloader.loader import Loader |
---|
16 | from sas.sasgui.guiframe.data_manager import DataManager |
---|
17 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
18 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
19 | |
---|
20 | import GuiUtils |
---|
21 | import PlotHelper |
---|
22 | from Plotter import Plotter |
---|
23 | from DroppableDataLoadWidget import DroppableDataLoadWidget |
---|
24 | |
---|
25 | # This is how to get data1/2D from the model item |
---|
26 | # data = [selected_items[0].child(0).data().toPyObject()] |
---|
27 | |
---|
28 | class DataExplorerWindow(DroppableDataLoadWidget): |
---|
29 | # The controller which is responsible for managing signal slots connections |
---|
30 | # for the gui and providing an interface to the data model. |
---|
31 | |
---|
32 | def __init__(self, parent=None, guimanager=None): |
---|
33 | super(DataExplorerWindow, self).__init__(parent, guimanager) |
---|
34 | |
---|
35 | # Main model for keeping loaded data |
---|
36 | self.model = QtGui.QStandardItemModel(self) |
---|
37 | |
---|
38 | # Secondary model for keeping frozen data sets |
---|
39 | self.theory_model = QtGui.QStandardItemModel(self) |
---|
40 | |
---|
41 | # GuiManager is the actual parent, but we needed to also pass the QMainWindow |
---|
42 | # in order to set the widget parentage properly. |
---|
43 | self.parent = guimanager |
---|
44 | self.loader = Loader() |
---|
45 | self.manager = DataManager() |
---|
46 | self.txt_widget = QtGui.QTextEdit(None) |
---|
47 | # self.txt_widget = GuiUtils.DisplayWindow() |
---|
48 | |
---|
49 | |
---|
50 | # Be careful with twisted threads. |
---|
51 | self.mutex = QMutex() |
---|
52 | |
---|
53 | # Active plots |
---|
54 | self.active_plots = [] |
---|
55 | |
---|
56 | # Connect the buttons |
---|
57 | self.cmdLoad.clicked.connect(self.loadFile) |
---|
58 | self.cmdDeleteData.clicked.connect(self.deleteFile) |
---|
59 | self.cmdDeleteTheory.clicked.connect(self.deleteTheory) |
---|
60 | self.cmdFreeze.clicked.connect(self.freezeTheory) |
---|
61 | self.cmdSendTo.clicked.connect(self.sendData) |
---|
62 | self.cmdNew.clicked.connect(self.newPlot) |
---|
63 | self.cmdAppend.clicked.connect(self.appendPlot) |
---|
64 | self.cmdHelp.clicked.connect(self.displayHelp) |
---|
65 | self.cmdHelp_2.clicked.connect(self.displayHelp) |
---|
66 | |
---|
67 | # Display HTML content |
---|
68 | self._helpView = QtWebKit.QWebView() |
---|
69 | |
---|
70 | # Custom context menu |
---|
71 | self.treeView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) |
---|
72 | self.treeView.customContextMenuRequested.connect(self.onCustomContextMenu) |
---|
73 | self.contextMenu() |
---|
74 | |
---|
75 | # Connect the comboboxes |
---|
76 | self.cbSelect.currentIndexChanged.connect(self.selectData) |
---|
77 | |
---|
78 | #self.closeEvent.connect(self.closeEvent) |
---|
79 | # self.aboutToQuit.connect(self.closeEvent) |
---|
80 | self.communicator = self.parent.communicator() |
---|
81 | self.communicator.fileReadSignal.connect(self.loadFromURL) |
---|
82 | self.communicator.activeGraphsSignal.connect(self.updateGraphCombo) |
---|
83 | self.cbgraph.editTextChanged.connect(self.enableGraphCombo) |
---|
84 | self.cbgraph.currentIndexChanged.connect(self.enableGraphCombo) |
---|
85 | |
---|
86 | # Proxy model for showing a subset of Data1D/Data2D content |
---|
87 | self.data_proxy = QtGui.QSortFilterProxyModel(self) |
---|
88 | self.data_proxy.setSourceModel(self.model) |
---|
89 | |
---|
90 | # Don't show "empty" rows with data objects |
---|
91 | self.data_proxy.setFilterRegExp(r"[^()]") |
---|
92 | |
---|
93 | # The Data viewer is QTreeView showing the proxy model |
---|
94 | self.treeView.setModel(self.data_proxy) |
---|
95 | |
---|
96 | # Proxy model for showing a subset of Theory content |
---|
97 | self.theory_proxy = QtGui.QSortFilterProxyModel(self) |
---|
98 | self.theory_proxy.setSourceModel(self.theory_model) |
---|
99 | |
---|
100 | # Don't show "empty" rows with data objects |
---|
101 | self.theory_proxy.setFilterRegExp(r"[^()]") |
---|
102 | |
---|
103 | # Theory model view |
---|
104 | self.freezeView.setModel(self.theory_proxy) |
---|
105 | |
---|
106 | self.enableGraphCombo(None) |
---|
107 | |
---|
108 | def closeEvent(self, event): |
---|
109 | """ |
---|
110 | Overwrite the close event - no close! |
---|
111 | """ |
---|
112 | event.ignore() |
---|
113 | |
---|
114 | def displayHelp(self): |
---|
115 | """ |
---|
116 | Show the "Loading data" section of help |
---|
117 | """ |
---|
118 | _TreeLocation = "html/user/sasgui/guiframe/data_explorer_help.html" |
---|
119 | self._helpView.load(QtCore.QUrl(_TreeLocation)) |
---|
120 | self._helpView.show() |
---|
121 | |
---|
122 | def enableGraphCombo(self, combo_text): |
---|
123 | """ |
---|
124 | Enables/disables "Assign Plot" elements |
---|
125 | """ |
---|
126 | self.cbgraph.setEnabled(len(PlotHelper.currentPlots()) > 0) |
---|
127 | self.cmdAppend.setEnabled(len(PlotHelper.currentPlots()) > 0) |
---|
128 | |
---|
129 | def loadFromURL(self, url): |
---|
130 | """ |
---|
131 | Threaded file load |
---|
132 | """ |
---|
133 | load_thread = threads.deferToThread(self.readData, url) |
---|
134 | load_thread.addCallback(self.loadComplete) |
---|
135 | |
---|
136 | def loadFile(self, event=None): |
---|
137 | """ |
---|
138 | Called when the "Load" button pressed. |
---|
139 | Opens the Qt "Open File..." dialog |
---|
140 | """ |
---|
141 | path_str = self.chooseFiles() |
---|
142 | if not path_str: |
---|
143 | return |
---|
144 | self.loadFromURL(path_str) |
---|
145 | |
---|
146 | def loadFolder(self, event=None): |
---|
147 | """ |
---|
148 | Called when the "File/Load Folder" menu item chosen. |
---|
149 | Opens the Qt "Open Folder..." dialog |
---|
150 | """ |
---|
151 | folder = QtGui.QFileDialog.getExistingDirectory(self, "Choose a directory", "", |
---|
152 | QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontUseNativeDialog) |
---|
153 | if folder is None: |
---|
154 | return |
---|
155 | |
---|
156 | folder = str(folder) |
---|
157 | |
---|
158 | if not os.path.isdir(folder): |
---|
159 | return |
---|
160 | |
---|
161 | # get content of dir into a list |
---|
162 | path_str = [os.path.join(os.path.abspath(folder), filename) |
---|
163 | for filename in os.listdir(folder)] |
---|
164 | |
---|
165 | self.loadFromURL(path_str) |
---|
166 | |
---|
167 | def deleteFile(self, event): |
---|
168 | """ |
---|
169 | Delete selected rows from the model |
---|
170 | """ |
---|
171 | # Assure this is indeed wanted |
---|
172 | delete_msg = "This operation will delete the checked data sets and all the dependents." +\ |
---|
173 | "\nDo you want to continue?" |
---|
174 | reply = QtGui.QMessageBox.question(self, |
---|
175 | 'Warning', |
---|
176 | delete_msg, |
---|
177 | QtGui.QMessageBox.Yes, |
---|
178 | QtGui.QMessageBox.No) |
---|
179 | |
---|
180 | if reply == QtGui.QMessageBox.No: |
---|
181 | return |
---|
182 | |
---|
183 | # Figure out which rows are checked |
---|
184 | ind = -1 |
---|
185 | # Use 'while' so the row count is forced at every iteration |
---|
186 | while ind < self.model.rowCount(): |
---|
187 | ind += 1 |
---|
188 | item = self.model.item(ind) |
---|
189 | if item and item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
190 | # Delete these rows from the model |
---|
191 | self.model.removeRow(ind) |
---|
192 | # Decrement index since we just deleted it |
---|
193 | ind -= 1 |
---|
194 | |
---|
195 | # pass temporarily kept as a breakpoint anchor |
---|
196 | pass |
---|
197 | |
---|
198 | def deleteTheory(self, event): |
---|
199 | """ |
---|
200 | Delete selected rows from the theory model |
---|
201 | """ |
---|
202 | # Assure this is indeed wanted |
---|
203 | delete_msg = "This operation will delete the checked data sets and all the dependents." +\ |
---|
204 | "\nDo you want to continue?" |
---|
205 | reply = QtGui.QMessageBox.question(self, |
---|
206 | 'Warning', |
---|
207 | delete_msg, |
---|
208 | QtGui.QMessageBox.Yes, |
---|
209 | QtGui.QMessageBox.No) |
---|
210 | |
---|
211 | if reply == QtGui.QMessageBox.No: |
---|
212 | return |
---|
213 | |
---|
214 | # Figure out which rows are checked |
---|
215 | ind = -1 |
---|
216 | # Use 'while' so the row count is forced at every iteration |
---|
217 | while ind < self.theory_model.rowCount(): |
---|
218 | ind += 1 |
---|
219 | item = self.theory_model.item(ind) |
---|
220 | if item and item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
221 | # Delete these rows from the model |
---|
222 | self.theory_model.removeRow(ind) |
---|
223 | # Decrement index since we just deleted it |
---|
224 | ind -= 1 |
---|
225 | |
---|
226 | # pass temporarily kept as a breakpoint anchor |
---|
227 | pass |
---|
228 | |
---|
229 | def sendData(self, event): |
---|
230 | """ |
---|
231 | Send selected item data to the current perspective and set the relevant notifiers |
---|
232 | """ |
---|
233 | # should this reside on GuiManager or here? |
---|
234 | self._perspective = self.parent.perspective() |
---|
235 | |
---|
236 | # Set the signal handlers |
---|
237 | self.communicator.updateModelFromPerspectiveSignal.connect(self.updateModelFromPerspective) |
---|
238 | |
---|
239 | # Figure out which rows are checked |
---|
240 | selected_items = [] |
---|
241 | for index in range(self.model.rowCount()): |
---|
242 | item = self.model.item(index) |
---|
243 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
244 | selected_items.append(item) |
---|
245 | |
---|
246 | if len(selected_items) < 1: |
---|
247 | return |
---|
248 | |
---|
249 | # Which perspective has been selected? |
---|
250 | if len(selected_items) > 1 and not self._perspective.allowBatch(): |
---|
251 | msg = self._perspective.title() + " does not allow multiple data." |
---|
252 | msgbox = QtGui.QMessageBox() |
---|
253 | msgbox.setIcon(QtGui.QMessageBox.Critical) |
---|
254 | msgbox.setText(msg) |
---|
255 | msgbox.setStandardButtons(QtGui.QMessageBox.Ok) |
---|
256 | retval = msgbox.exec_() |
---|
257 | return |
---|
258 | |
---|
259 | # Dig up the item |
---|
260 | data = selected_items |
---|
261 | |
---|
262 | # TODO |
---|
263 | # New plot or appended? |
---|
264 | |
---|
265 | # Notify the GuiManager about the send request |
---|
266 | self._perspective.setData(data_item=data) |
---|
267 | |
---|
268 | def freezeTheory(self, event): |
---|
269 | """ |
---|
270 | Freeze selected theory rows. |
---|
271 | |
---|
272 | "Freezing" means taking the plottable data from the filename item |
---|
273 | and copying it to a separate top-level item. |
---|
274 | """ |
---|
275 | # Figure out which _inner_ rows are checked |
---|
276 | # Use 'while' so the row count is forced at every iteration |
---|
277 | outer_index = -1 |
---|
278 | theories_copied = 0 |
---|
279 | while outer_index < self.model.rowCount(): |
---|
280 | outer_index += 1 |
---|
281 | outer_item = self.model.item(outer_index) |
---|
282 | if not outer_item: |
---|
283 | continue |
---|
284 | # Should be just two rows: data and Info |
---|
285 | for inner_index in xrange(outer_item.rowCount()): |
---|
286 | subitem = outer_item.child(inner_index) |
---|
287 | if subitem and \ |
---|
288 | subitem.isCheckable() and \ |
---|
289 | subitem.checkState() == QtCore.Qt.Checked: |
---|
290 | theories_copied += 1 |
---|
291 | new_item = self.recursivelyCloneItem(subitem) |
---|
292 | # Append a "unique" descriptor to the name |
---|
293 | time_bit = str(time.time())[7:-1].replace('.', '') |
---|
294 | new_name = new_item.text() + '_@' + time_bit |
---|
295 | new_item.setText(new_name) |
---|
296 | self.theory_model.appendRow(new_item) |
---|
297 | self.theory_model.reset() |
---|
298 | |
---|
299 | freeze_msg = "" |
---|
300 | if theories_copied == 0: |
---|
301 | return |
---|
302 | elif theories_copied == 1: |
---|
303 | freeze_msg = "1 theory copied to the Theory tab as a data set" |
---|
304 | elif theories_copied > 1: |
---|
305 | freeze_msg = "%i theories copied to the Theory tab as data sets" % theories_copied |
---|
306 | else: |
---|
307 | freeze_msg = "Unexpected number of theories copied: %i" % theories_copied |
---|
308 | raise AttributeError, freeze_msg |
---|
309 | self.communicator.statusBarUpdateSignal.emit(freeze_msg) |
---|
310 | # Actively switch tabs |
---|
311 | self.setCurrentIndex(1) |
---|
312 | |
---|
313 | def recursivelyCloneItem(self, item): |
---|
314 | """ |
---|
315 | Clone QStandardItem() object |
---|
316 | """ |
---|
317 | new_item = item.clone() |
---|
318 | # clone doesn't do deepcopy :( |
---|
319 | for child_index in xrange(item.rowCount()): |
---|
320 | child_item = self.recursivelyCloneItem(item.child(child_index)) |
---|
321 | new_item.setChild(child_index, child_item) |
---|
322 | return new_item |
---|
323 | |
---|
324 | def updateGraphCombo(self, graph_list): |
---|
325 | """ |
---|
326 | Modify Graph combo box on graph add/delete |
---|
327 | """ |
---|
328 | orig_text = self.cbgraph.currentText() |
---|
329 | self.cbgraph.clear() |
---|
330 | graph_titles = [] |
---|
331 | for graph in graph_list: |
---|
332 | graph_titles.append("Graph"+str(graph)) |
---|
333 | self.cbgraph.insertItems(0, graph_titles) |
---|
334 | ind = self.cbgraph.findText(orig_text) |
---|
335 | if ind > 0: |
---|
336 | self.cbgraph.setCurrentIndex(ind) |
---|
337 | pass |
---|
338 | |
---|
339 | def newPlot(self): |
---|
340 | """ |
---|
341 | Create a new matplotlib chart from selected data |
---|
342 | |
---|
343 | TODO: Add 2D-functionality |
---|
344 | """ |
---|
345 | plots = GuiUtils.plotsFromCheckedItems(self.model) |
---|
346 | |
---|
347 | # Call show on requested plots |
---|
348 | new_plot = Plotter(self) |
---|
349 | for plot_set in plots: |
---|
350 | new_plot.data(plot_set) |
---|
351 | new_plot.plot() |
---|
352 | |
---|
353 | # Update the global plot counter |
---|
354 | title = "Graph"+str(PlotHelper.idOfPlot(new_plot)) |
---|
355 | new_plot.setWindowTitle(title) |
---|
356 | |
---|
357 | # Add the plot to the workspace |
---|
358 | self.parent.workspace().addWindow(new_plot) |
---|
359 | |
---|
360 | # Show the plot |
---|
361 | new_plot.show() |
---|
362 | |
---|
363 | # Update the active chart list |
---|
364 | self.active_plots.append(title) |
---|
365 | |
---|
366 | def appendPlot(self): |
---|
367 | """ |
---|
368 | Add data set(s) to the existing matplotlib chart |
---|
369 | |
---|
370 | TODO: Add 2D-functionality |
---|
371 | """ |
---|
372 | # new plot data |
---|
373 | new_plots = GuiUtils.plotsFromCheckedItems(self.model) |
---|
374 | |
---|
375 | # old plot data |
---|
376 | plot_id = self.cbgraph.currentText() |
---|
377 | plot_id = int(plot_id[5:]) |
---|
378 | |
---|
379 | assert plot_id in PlotHelper.currentPlots(), "No such plot: Graph%s"%str(plot_id) |
---|
380 | |
---|
381 | old_plot = PlotHelper.plotById(plot_id) |
---|
382 | |
---|
383 | # Add new data to the old plot |
---|
384 | for plot_set in new_plots: |
---|
385 | old_plot.data(plot_set) |
---|
386 | old_plot.plot() |
---|
387 | |
---|
388 | def chooseFiles(self): |
---|
389 | """ |
---|
390 | Shows the Open file dialog and returns the chosen path(s) |
---|
391 | """ |
---|
392 | # List of known extensions |
---|
393 | wlist = self.getWlist() |
---|
394 | |
---|
395 | # Location is automatically saved - no need to keep track of the last dir |
---|
396 | # But only with Qt built-in dialog (non-platform native) |
---|
397 | paths = QtGui.QFileDialog.getOpenFileNames(self, "Choose a file", "", |
---|
398 | wlist, None, QtGui.QFileDialog.DontUseNativeDialog) |
---|
399 | if paths is None: |
---|
400 | return |
---|
401 | |
---|
402 | if isinstance(paths, QtCore.QStringList): |
---|
403 | paths = [str(f) for f in paths] |
---|
404 | |
---|
405 | if not isinstance(paths, list): |
---|
406 | paths = [paths] |
---|
407 | |
---|
408 | return paths |
---|
409 | |
---|
410 | def readData(self, path): |
---|
411 | """ |
---|
412 | verbatim copy-paste from |
---|
413 | sasgui.guiframe.local_perspectives.data_loader.data_loader.py |
---|
414 | slightly modified for clarity |
---|
415 | """ |
---|
416 | message = "" |
---|
417 | log_msg = '' |
---|
418 | output = {} |
---|
419 | any_error = False |
---|
420 | data_error = False |
---|
421 | error_message = "" |
---|
422 | |
---|
423 | number_of_files = len(path) |
---|
424 | self.communicator.progressBarUpdateSignal.emit(0.0) |
---|
425 | |
---|
426 | for index, p_file in enumerate(path): |
---|
427 | basename = os.path.basename(p_file) |
---|
428 | _, extension = os.path.splitext(basename) |
---|
429 | if extension.lower() in GuiUtils.EXTENSIONS: |
---|
430 | any_error = True |
---|
431 | log_msg = "Data Loader cannot " |
---|
432 | log_msg += "load: %s\n" % str(p_file) |
---|
433 | log_msg += """Please try to open that file from "open project" """ |
---|
434 | log_msg += """or "open analysis" menu\n""" |
---|
435 | error_message = log_msg + "\n" |
---|
436 | logging.info(log_msg) |
---|
437 | continue |
---|
438 | |
---|
439 | try: |
---|
440 | message = "Loading Data... " + str(basename) + "\n" |
---|
441 | |
---|
442 | # change this to signal notification in GuiManager |
---|
443 | self.communicator.statusBarUpdateSignal.emit(message) |
---|
444 | |
---|
445 | output_objects = self.loader.load(p_file) |
---|
446 | |
---|
447 | # Some loaders return a list and some just a single Data1D object. |
---|
448 | # Standardize. |
---|
449 | if not isinstance(output_objects, list): |
---|
450 | output_objects = [output_objects] |
---|
451 | |
---|
452 | for item in output_objects: |
---|
453 | # cast sascalc.dataloader.data_info.Data1D into |
---|
454 | # sasgui.guiframe.dataFitting.Data1D |
---|
455 | # TODO : Fix it |
---|
456 | new_data = self.manager.create_gui_data(item, p_file) |
---|
457 | output[new_data.id] = new_data |
---|
458 | |
---|
459 | # Model update should be protected |
---|
460 | self.mutex.lock() |
---|
461 | self.updateModel(new_data, p_file) |
---|
462 | self.model.reset() |
---|
463 | QtGui.qApp.processEvents() |
---|
464 | self.mutex.unlock() |
---|
465 | |
---|
466 | if hasattr(item, 'errors'): |
---|
467 | for error_data in item.errors: |
---|
468 | data_error = True |
---|
469 | message += "\tError: {0}\n".format(error_data) |
---|
470 | else: |
---|
471 | |
---|
472 | logging.error("Loader returned an invalid object:\n %s" % str(item)) |
---|
473 | data_error = True |
---|
474 | |
---|
475 | except Exception as ex: |
---|
476 | logging.error(sys.exc_value) |
---|
477 | |
---|
478 | any_error = True |
---|
479 | if any_error or error_message != "": |
---|
480 | if error_message == "": |
---|
481 | error = "Error: " + str(sys.exc_info()[1]) + "\n" |
---|
482 | error += "while loading Data: \n%s\n" % str(basename) |
---|
483 | error_message += "The data file you selected could not be loaded.\n" |
---|
484 | error_message += "Make sure the content of your file" |
---|
485 | error_message += " is properly formatted.\n\n" |
---|
486 | error_message += "When contacting the SasView team, mention the" |
---|
487 | error_message += " following:\n%s" % str(error) |
---|
488 | elif data_error: |
---|
489 | base_message = "Errors occurred while loading " |
---|
490 | base_message += "{0}\n".format(basename) |
---|
491 | base_message += "The data file loaded but with errors.\n" |
---|
492 | error_message = base_message + error_message |
---|
493 | else: |
---|
494 | error_message += "%s\n" % str(p_file) |
---|
495 | |
---|
496 | current_percentage = int(100.0* index/number_of_files) |
---|
497 | self.communicator.progressBarUpdateSignal.emit(current_percentage) |
---|
498 | |
---|
499 | if any_error or error_message: |
---|
500 | logging.error(error_message) |
---|
501 | status_bar_message = "Errors occurred while loading %s" % format(basename) |
---|
502 | self.communicator.statusBarUpdateSignal.emit(status_bar_message) |
---|
503 | |
---|
504 | else: |
---|
505 | message = "Loading Data Complete! " |
---|
506 | message += log_msg |
---|
507 | # Notify the progress bar that the updates are over. |
---|
508 | self.communicator.progressBarUpdateSignal.emit(-1) |
---|
509 | |
---|
510 | return output, message |
---|
511 | |
---|
512 | def getWlist(self): |
---|
513 | """ |
---|
514 | Wildcards of files we know the format of. |
---|
515 | """ |
---|
516 | # Display the Qt Load File module |
---|
517 | cards = self.loader.get_wildcards() |
---|
518 | |
---|
519 | # get rid of the wx remnant in wildcards |
---|
520 | # TODO: modify sasview loader get_wildcards method, after merge, |
---|
521 | # so this kludge can be avoided |
---|
522 | new_cards = [] |
---|
523 | for item in cards: |
---|
524 | new_cards.append(item[:item.find("|")]) |
---|
525 | wlist = ';;'.join(new_cards) |
---|
526 | |
---|
527 | return wlist |
---|
528 | |
---|
529 | def selectData(self, index): |
---|
530 | """ |
---|
531 | Callback method for modifying the TreeView on Selection Options change |
---|
532 | """ |
---|
533 | if not isinstance(index, int): |
---|
534 | msg = "Incorrect type passed to DataExplorer.selectData()" |
---|
535 | raise AttributeError, msg |
---|
536 | |
---|
537 | # Respond appropriately |
---|
538 | if index == 0: |
---|
539 | # Select All |
---|
540 | for index in range(self.model.rowCount()): |
---|
541 | item = self.model.item(index) |
---|
542 | if item.isCheckable() and item.checkState() == QtCore.Qt.Unchecked: |
---|
543 | item.setCheckState(QtCore.Qt.Checked) |
---|
544 | elif index == 1: |
---|
545 | # De-select All |
---|
546 | for index in range(self.model.rowCount()): |
---|
547 | item = self.model.item(index) |
---|
548 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
549 | item.setCheckState(QtCore.Qt.Unchecked) |
---|
550 | |
---|
551 | elif index == 2: |
---|
552 | # Select All 1-D |
---|
553 | for index in range(self.model.rowCount()): |
---|
554 | item = self.model.item(index) |
---|
555 | item.setCheckState(QtCore.Qt.Unchecked) |
---|
556 | |
---|
557 | try: |
---|
558 | is1D = isinstance(item.child(0).data().toPyObject(), Data1D) |
---|
559 | except AttributeError: |
---|
560 | msg = "Bad structure of the data model." |
---|
561 | raise RuntimeError, msg |
---|
562 | |
---|
563 | if is1D: |
---|
564 | item.setCheckState(QtCore.Qt.Checked) |
---|
565 | |
---|
566 | elif index == 3: |
---|
567 | # Unselect All 1-D |
---|
568 | for index in range(self.model.rowCount()): |
---|
569 | item = self.model.item(index) |
---|
570 | |
---|
571 | try: |
---|
572 | is1D = isinstance(item.child(0).data().toPyObject(), Data1D) |
---|
573 | except AttributeError: |
---|
574 | msg = "Bad structure of the data model." |
---|
575 | raise RuntimeError, msg |
---|
576 | |
---|
577 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked and is1D: |
---|
578 | item.setCheckState(QtCore.Qt.Unchecked) |
---|
579 | |
---|
580 | elif index == 4: |
---|
581 | # Select All 2-D |
---|
582 | for index in range(self.model.rowCount()): |
---|
583 | item = self.model.item(index) |
---|
584 | item.setCheckState(QtCore.Qt.Unchecked) |
---|
585 | try: |
---|
586 | is2D = isinstance(item.child(0).data().toPyObject(), Data2D) |
---|
587 | except AttributeError: |
---|
588 | msg = "Bad structure of the data model." |
---|
589 | raise RuntimeError, msg |
---|
590 | |
---|
591 | if is2D: |
---|
592 | item.setCheckState(QtCore.Qt.Checked) |
---|
593 | |
---|
594 | elif index == 5: |
---|
595 | # Unselect All 2-D |
---|
596 | for index in range(self.model.rowCount()): |
---|
597 | item = self.model.item(index) |
---|
598 | |
---|
599 | try: |
---|
600 | is2D = isinstance(item.child(0).data().toPyObject(), Data2D) |
---|
601 | except AttributeError: |
---|
602 | msg = "Bad structure of the data model." |
---|
603 | raise RuntimeError, msg |
---|
604 | |
---|
605 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked and is2D: |
---|
606 | item.setCheckState(QtCore.Qt.Unchecked) |
---|
607 | |
---|
608 | else: |
---|
609 | msg = "Incorrect value in the Selection Option" |
---|
610 | # Change this to a proper logging action |
---|
611 | raise Exception, msg |
---|
612 | |
---|
613 | def contextMenu(self): |
---|
614 | """ |
---|
615 | Define actions and layout of the right click context menu |
---|
616 | """ |
---|
617 | # Create a custom menu based on actions defined in the UI file |
---|
618 | self.context_menu = QtGui.QMenu(self) |
---|
619 | self.context_menu.addAction(self.actionDataInfo) |
---|
620 | self.context_menu.addAction(self.actionSaveAs) |
---|
621 | self.context_menu.addAction(self.actionQuickPlot) |
---|
622 | self.context_menu.addSeparator() |
---|
623 | self.context_menu.addAction(self.actionQuick3DPlot) |
---|
624 | self.context_menu.addAction(self.actionEditMask) |
---|
625 | |
---|
626 | # Define the callbacks |
---|
627 | self.actionDataInfo.triggered.connect(self.showDataInfo) |
---|
628 | self.actionSaveAs.triggered.connect(self.saveDataAs) |
---|
629 | self.actionQuickPlot.triggered.connect(self.quickDataPlot) |
---|
630 | self.actionQuick3DPlot.triggered.connect(self.quickData3DPlot) |
---|
631 | self.actionEditMask.triggered.connect(self.showEditDataMask) |
---|
632 | |
---|
633 | def onCustomContextMenu(self, position): |
---|
634 | """ |
---|
635 | Show the right-click context menu in the data treeview |
---|
636 | """ |
---|
637 | index = self.treeView.indexAt(position) |
---|
638 | if index.isValid(): |
---|
639 | model_item = self.model.itemFromIndex(self.data_proxy.mapToSource(index)) |
---|
640 | # Find the mapped index |
---|
641 | orig_index = model_item.isCheckable() |
---|
642 | if orig_index: |
---|
643 | # Check the data to enable/disable actions |
---|
644 | is_2D = isinstance(model_item.child(0).data().toPyObject(), Data2D) |
---|
645 | self.actionQuick3DPlot.setEnabled(is_2D) |
---|
646 | self.actionEditMask.setEnabled(is_2D) |
---|
647 | # Fire up the menu |
---|
648 | self.context_menu.exec_(self.treeView.mapToGlobal(position)) |
---|
649 | |
---|
650 | def showDataInfo(self): |
---|
651 | """ |
---|
652 | Show a simple read-only text edit with data information. |
---|
653 | """ |
---|
654 | index = self.treeView.selectedIndexes()[0] |
---|
655 | model_item = self.model.itemFromIndex(self.data_proxy.mapToSource(index)) |
---|
656 | data = model_item.child(0).data().toPyObject() |
---|
657 | if isinstance(data, Data1D): |
---|
658 | text_to_show = GuiUtils.retrieveData1d(data) |
---|
659 | # Hardcoded sizes to enable full width rendering with default font |
---|
660 | self.txt_widget.resize(420,600) |
---|
661 | else: |
---|
662 | text_to_show = GuiUtils.retrieveData2d(data) |
---|
663 | # Hardcoded sizes to enable full width rendering with default font |
---|
664 | self.txt_widget.resize(700,600) |
---|
665 | |
---|
666 | self.txt_widget.setReadOnly(True) |
---|
667 | self.txt_widget.setWindowFlags(QtCore.Qt.Window) |
---|
668 | self.txt_widget.setWindowIcon(QtGui.QIcon(":/res/ball.ico")) |
---|
669 | self.txt_widget.setWindowTitle("Data Info: %s" % data.filename) |
---|
670 | self.txt_widget.insertPlainText(text_to_show) |
---|
671 | |
---|
672 | self.txt_widget.show() |
---|
673 | # Move the slider all the way up, if present |
---|
674 | vertical_scroll_bar = self.txt_widget.verticalScrollBar() |
---|
675 | vertical_scroll_bar.triggerAction(QtGui.QScrollBar.SliderToMinimum) |
---|
676 | |
---|
677 | def saveDataAs(self): |
---|
678 | """ |
---|
679 | Save the data points as either txt or xml |
---|
680 | """ |
---|
681 | index = self.treeView.selectedIndexes()[0] |
---|
682 | model_item = self.model.itemFromIndex(self.data_proxy.mapToSource(index)) |
---|
683 | data = model_item.child(0).data().toPyObject() |
---|
684 | if isinstance(data, Data1D): |
---|
685 | GuiUtils.saveData1D(data) |
---|
686 | else: |
---|
687 | GuiUtils.saveData2D(data) |
---|
688 | |
---|
689 | def quickDataPlot(self): |
---|
690 | """ |
---|
691 | Frozen plot - display an image of the plot |
---|
692 | """ |
---|
693 | index = self.treeView.selectedIndexes()[0] |
---|
694 | model_item = self.model.itemFromIndex(self.data_proxy.mapToSource(index)) |
---|
695 | data = model_item.child(0).data().toPyObject() |
---|
696 | |
---|
697 | dimension = 1 if isinstance(data, Data1D) else 2 |
---|
698 | |
---|
699 | # TODO: Replace this with the proper MaskPlotPanel from plottools |
---|
700 | new_plot = Plotter(self) |
---|
701 | new_plot.data(data) |
---|
702 | new_plot.plot(marker='o', linestyle='') |
---|
703 | |
---|
704 | # Update the global plot counter |
---|
705 | title = "Plot " + data.name |
---|
706 | new_plot.setWindowTitle(title) |
---|
707 | |
---|
708 | # Show the plot |
---|
709 | new_plot.show() |
---|
710 | |
---|
711 | def quickData3DPlot(self): |
---|
712 | """ |
---|
713 | """ |
---|
714 | print "quickData3DPlot" |
---|
715 | pass |
---|
716 | |
---|
717 | def showEditDataMask(self): |
---|
718 | """ |
---|
719 | """ |
---|
720 | print "showEditDataMask" |
---|
721 | pass |
---|
722 | |
---|
723 | def loadComplete(self, output): |
---|
724 | """ |
---|
725 | Post message to status bar and update the data manager |
---|
726 | """ |
---|
727 | assert isinstance(output, tuple) |
---|
728 | |
---|
729 | # Reset the model so the view gets updated. |
---|
730 | self.model.reset() |
---|
731 | self.communicator.progressBarUpdateSignal.emit(-1) |
---|
732 | |
---|
733 | output_data = output[0] |
---|
734 | message = output[1] |
---|
735 | # Notify the manager of the new data available |
---|
736 | self.communicator.statusBarUpdateSignal.emit(message) |
---|
737 | self.communicator.fileDataReceivedSignal.emit(output_data) |
---|
738 | self.manager.add_data(data_list=output_data) |
---|
739 | |
---|
740 | def updateModel(self, data, p_file): |
---|
741 | """ |
---|
742 | Add data and Info fields to the model item |
---|
743 | """ |
---|
744 | # Structure of the model |
---|
745 | # checkbox + basename |
---|
746 | # |-------> Data.D object |
---|
747 | # |-------> Info |
---|
748 | # |----> Title: |
---|
749 | # |----> Run: |
---|
750 | # |----> Type: |
---|
751 | # |----> Path: |
---|
752 | # |----> Process |
---|
753 | # |-----> process[0].name |
---|
754 | # |-------> THEORIES |
---|
755 | |
---|
756 | # Top-level item: checkbox with label |
---|
757 | checkbox_item = QtGui.QStandardItem(True) |
---|
758 | checkbox_item.setCheckable(True) |
---|
759 | checkbox_item.setCheckState(QtCore.Qt.Checked) |
---|
760 | checkbox_item.setText(os.path.basename(p_file)) |
---|
761 | |
---|
762 | # Add the actual Data1D/Data2D object |
---|
763 | object_item = QtGui.QStandardItem() |
---|
764 | object_item.setData(QtCore.QVariant(data)) |
---|
765 | |
---|
766 | checkbox_item.setChild(0, object_item) |
---|
767 | |
---|
768 | # Add rows for display in the view |
---|
769 | info_item = GuiUtils.infoFromData(data) |
---|
770 | |
---|
771 | # Set info_item as the first child |
---|
772 | checkbox_item.setChild(1, info_item) |
---|
773 | |
---|
774 | # Caption for the theories |
---|
775 | checkbox_item.setChild(2, QtGui.QStandardItem("THEORIES")) |
---|
776 | |
---|
777 | # New row in the model |
---|
778 | self.model.appendRow(checkbox_item) |
---|
779 | |
---|
780 | |
---|
781 | def updateModelFromPerspective(self, model_item): |
---|
782 | """ |
---|
783 | Receive an update model item from a perspective |
---|
784 | Make sure it is valid and if so, replace it in the model |
---|
785 | """ |
---|
786 | # Assert the correct type |
---|
787 | if not isinstance(model_item, QtGui.QStandardItem): |
---|
788 | msg = "Wrong data type returned from calculations." |
---|
789 | raise AttributeError, msg |
---|
790 | |
---|
791 | # TODO: Assert other properties |
---|
792 | |
---|
793 | # Reset the view |
---|
794 | self.model.reset() |
---|
795 | |
---|
796 | # Pass acting as a debugger anchor |
---|
797 | pass |
---|
798 | |
---|
799 | |
---|
800 | if __name__ == "__main__": |
---|
801 | app = QtGui.QApplication([]) |
---|
802 | dlg = DataExplorerWindow() |
---|
803 | dlg.show() |
---|
804 | sys.exit(app.exec_()) |
---|