[f721030] | 1 | # global |
---|
| 2 | import sys |
---|
| 3 | import os |
---|
[e540cd2] | 4 | import time |
---|
[f721030] | 5 | import logging |
---|
| 6 | |
---|
[4992ff2] | 7 | from PyQt5 import QtCore |
---|
| 8 | from PyQt5 import QtGui |
---|
| 9 | from PyQt5 import QtWidgets |
---|
[481ff26] | 10 | |
---|
[f721030] | 11 | from twisted.internet import threads |
---|
| 12 | |
---|
[dc5ef15] | 13 | # SASCALC |
---|
[f721030] | 14 | from sas.sascalc.dataloader.loader import Loader |
---|
| 15 | |
---|
[dc5ef15] | 16 | # QTGUI |
---|
[83eb5208] | 17 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
| 18 | import sas.qtgui.Plotting.PlotHelper as PlotHelper |
---|
[dc5ef15] | 19 | |
---|
| 20 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
| 21 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
[83eb5208] | 22 | from sas.qtgui.Plotting.Plotter import Plotter |
---|
| 23 | from sas.qtgui.Plotting.Plotter2D import Plotter2D |
---|
| 24 | from sas.qtgui.Plotting.MaskEditor import MaskEditor |
---|
| 25 | |
---|
[dc5ef15] | 26 | from sas.qtgui.MainWindow.DataManager import DataManager |
---|
| 27 | from sas.qtgui.MainWindow.DroppableDataLoadWidget import DroppableDataLoadWidget |
---|
| 28 | |
---|
[83eb5208] | 29 | import sas.qtgui.Perspectives as Perspectives |
---|
[1970780] | 30 | |
---|
[d4881f6a] | 31 | DEFAULT_PERSPECTIVE = "Fitting" |
---|
| 32 | |
---|
[f82ab8c] | 33 | class DataExplorerWindow(DroppableDataLoadWidget): |
---|
[f721030] | 34 | # The controller which is responsible for managing signal slots connections |
---|
| 35 | # for the gui and providing an interface to the data model. |
---|
| 36 | |
---|
[630155bd] | 37 | def __init__(self, parent=None, guimanager=None, manager=None): |
---|
[f82ab8c] | 38 | super(DataExplorerWindow, self).__init__(parent, guimanager) |
---|
[f721030] | 39 | |
---|
| 40 | # Main model for keeping loaded data |
---|
| 41 | self.model = QtGui.QStandardItemModel(self) |
---|
[f82ab8c] | 42 | |
---|
| 43 | # Secondary model for keeping frozen data sets |
---|
| 44 | self.theory_model = QtGui.QStandardItemModel(self) |
---|
[f721030] | 45 | |
---|
| 46 | # GuiManager is the actual parent, but we needed to also pass the QMainWindow |
---|
| 47 | # in order to set the widget parentage properly. |
---|
| 48 | self.parent = guimanager |
---|
| 49 | self.loader = Loader() |
---|
[630155bd] | 50 | self.manager = manager if manager is not None else DataManager() |
---|
[4992ff2] | 51 | self.txt_widget = QtWidgets.QTextEdit(None) |
---|
[f721030] | 52 | |
---|
[481ff26] | 53 | # Be careful with twisted threads. |
---|
[4992ff2] | 54 | self.mutex = QtCore.QMutex() |
---|
[481ff26] | 55 | |
---|
[8cb6cd6] | 56 | # Active plots |
---|
[7d077d1] | 57 | self.active_plots = {} |
---|
[8cb6cd6] | 58 | |
---|
[f721030] | 59 | # Connect the buttons |
---|
| 60 | self.cmdLoad.clicked.connect(self.loadFile) |
---|
[f82ab8c] | 61 | self.cmdDeleteData.clicked.connect(self.deleteFile) |
---|
| 62 | self.cmdDeleteTheory.clicked.connect(self.deleteTheory) |
---|
| 63 | self.cmdFreeze.clicked.connect(self.freezeTheory) |
---|
[f721030] | 64 | self.cmdSendTo.clicked.connect(self.sendData) |
---|
[1042dba] | 65 | self.cmdNew.clicked.connect(self.newPlot) |
---|
[0268aed] | 66 | self.cmdNew_2.clicked.connect(self.newPlot) |
---|
[8cb6cd6] | 67 | self.cmdAppend.clicked.connect(self.appendPlot) |
---|
[481ff26] | 68 | self.cmdHelp.clicked.connect(self.displayHelp) |
---|
| 69 | self.cmdHelp_2.clicked.connect(self.displayHelp) |
---|
| 70 | |
---|
[83d6249] | 71 | # Fill in the perspectives combo |
---|
| 72 | self.initPerspectives() |
---|
| 73 | |
---|
[e540cd2] | 74 | # Custom context menu |
---|
| 75 | self.treeView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) |
---|
| 76 | self.treeView.customContextMenuRequested.connect(self.onCustomContextMenu) |
---|
[4b71e91] | 77 | self.contextMenu() |
---|
[e540cd2] | 78 | |
---|
[cbcdd2c] | 79 | # Same menus for the theory view |
---|
| 80 | self.freezeView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) |
---|
| 81 | self.freezeView.customContextMenuRequested.connect(self.onCustomContextMenu) |
---|
| 82 | |
---|
[488c49d] | 83 | # Connect the comboboxes |
---|
| 84 | self.cbSelect.currentIndexChanged.connect(self.selectData) |
---|
| 85 | |
---|
[f82ab8c] | 86 | #self.closeEvent.connect(self.closeEvent) |
---|
[cbcdd2c] | 87 | self.currentChanged.connect(self.onTabSwitch) |
---|
[e540cd2] | 88 | self.communicator = self.parent.communicator() |
---|
[f82ab8c] | 89 | self.communicator.fileReadSignal.connect(self.loadFromURL) |
---|
[7d8bebf] | 90 | self.communicator.activeGraphsSignal.connect(self.updateGraphCount) |
---|
[27313b7] | 91 | self.communicator.activeGraphName.connect(self.updatePlotName) |
---|
[7d077d1] | 92 | self.communicator.plotUpdateSignal.connect(self.updatePlot) |
---|
[d5c5d3d] | 93 | |
---|
[8cb6cd6] | 94 | self.cbgraph.editTextChanged.connect(self.enableGraphCombo) |
---|
| 95 | self.cbgraph.currentIndexChanged.connect(self.enableGraphCombo) |
---|
[f721030] | 96 | |
---|
| 97 | # Proxy model for showing a subset of Data1D/Data2D content |
---|
[4992ff2] | 98 | self.data_proxy = QtCore.QSortFilterProxyModel(self) |
---|
[481ff26] | 99 | self.data_proxy.setSourceModel(self.model) |
---|
| 100 | |
---|
| 101 | # Don't show "empty" rows with data objects |
---|
| 102 | self.data_proxy.setFilterRegExp(r"[^()]") |
---|
[f721030] | 103 | |
---|
| 104 | # The Data viewer is QTreeView showing the proxy model |
---|
[481ff26] | 105 | self.treeView.setModel(self.data_proxy) |
---|
| 106 | |
---|
| 107 | # Proxy model for showing a subset of Theory content |
---|
[4992ff2] | 108 | self.theory_proxy = QtCore.QSortFilterProxyModel(self) |
---|
[481ff26] | 109 | self.theory_proxy.setSourceModel(self.theory_model) |
---|
| 110 | |
---|
| 111 | # Don't show "empty" rows with data objects |
---|
| 112 | self.theory_proxy.setFilterRegExp(r"[^()]") |
---|
[f721030] | 113 | |
---|
[f82ab8c] | 114 | # Theory model view |
---|
[481ff26] | 115 | self.freezeView.setModel(self.theory_proxy) |
---|
[f82ab8c] | 116 | |
---|
[8cb6cd6] | 117 | self.enableGraphCombo(None) |
---|
| 118 | |
---|
[cbcdd2c] | 119 | # Current view on model |
---|
| 120 | self.current_view = self.treeView |
---|
| 121 | |
---|
[f82ab8c] | 122 | def closeEvent(self, event): |
---|
| 123 | """ |
---|
| 124 | Overwrite the close event - no close! |
---|
| 125 | """ |
---|
| 126 | event.ignore() |
---|
[1042dba] | 127 | |
---|
[cbcdd2c] | 128 | def onTabSwitch(self, index): |
---|
| 129 | """ Callback for tab switching signal """ |
---|
| 130 | if index == 0: |
---|
| 131 | self.current_view = self.treeView |
---|
| 132 | else: |
---|
| 133 | self.current_view = self.freezeView |
---|
| 134 | |
---|
[481ff26] | 135 | def displayHelp(self): |
---|
| 136 | """ |
---|
| 137 | Show the "Loading data" section of help |
---|
| 138 | """ |
---|
[e90988c] | 139 | tree_location = "/user/sasgui/guiframe/data_explorer_help.html" |
---|
| 140 | self.parent.showHelp(tree_location) |
---|
[481ff26] | 141 | |
---|
[8cb6cd6] | 142 | def enableGraphCombo(self, combo_text): |
---|
| 143 | """ |
---|
| 144 | Enables/disables "Assign Plot" elements |
---|
| 145 | """ |
---|
| 146 | self.cbgraph.setEnabled(len(PlotHelper.currentPlots()) > 0) |
---|
| 147 | self.cmdAppend.setEnabled(len(PlotHelper.currentPlots()) > 0) |
---|
| 148 | |
---|
[83d6249] | 149 | def initPerspectives(self): |
---|
| 150 | """ |
---|
| 151 | Populate the Perspective combobox and define callbacks |
---|
| 152 | """ |
---|
[b3e8629] | 153 | available_perspectives = sorted([p for p in list(Perspectives.PERSPECTIVES.keys())]) |
---|
[1970780] | 154 | if available_perspectives: |
---|
| 155 | self.cbFitting.clear() |
---|
| 156 | self.cbFitting.addItems(available_perspectives) |
---|
[83d6249] | 157 | self.cbFitting.currentIndexChanged.connect(self.updatePerspectiveCombo) |
---|
| 158 | # Set the index so we see the default (Fitting) |
---|
[d4881f6a] | 159 | self.cbFitting.setCurrentIndex(self.cbFitting.findText(DEFAULT_PERSPECTIVE)) |
---|
[83d6249] | 160 | |
---|
[1970780] | 161 | def _perspective(self): |
---|
| 162 | """ |
---|
| 163 | Returns the current perspective |
---|
| 164 | """ |
---|
| 165 | return self.parent.perspective() |
---|
| 166 | |
---|
[f82ab8c] | 167 | def loadFromURL(self, url): |
---|
| 168 | """ |
---|
| 169 | Threaded file load |
---|
| 170 | """ |
---|
| 171 | load_thread = threads.deferToThread(self.readData, url) |
---|
| 172 | load_thread.addCallback(self.loadComplete) |
---|
[7fb471d] | 173 | load_thread.addErrback(self.loadFailed) |
---|
[5032ea68] | 174 | |
---|
| 175 | def loadFile(self, event=None): |
---|
[f721030] | 176 | """ |
---|
| 177 | Called when the "Load" button pressed. |
---|
[481ff26] | 178 | Opens the Qt "Open File..." dialog |
---|
[f721030] | 179 | """ |
---|
| 180 | path_str = self.chooseFiles() |
---|
| 181 | if not path_str: |
---|
| 182 | return |
---|
[f82ab8c] | 183 | self.loadFromURL(path_str) |
---|
[f721030] | 184 | |
---|
[5032ea68] | 185 | def loadFolder(self, event=None): |
---|
[f721030] | 186 | """ |
---|
[5032ea68] | 187 | Called when the "File/Load Folder" menu item chosen. |
---|
[481ff26] | 188 | Opens the Qt "Open Folder..." dialog |
---|
[f721030] | 189 | """ |
---|
[7969b9c] | 190 | folder = QtWidgets.QFileDialog.getExistingDirectory(self, "Choose a directory", "", |
---|
[4992ff2] | 191 | QtWidgets.QFileDialog.ShowDirsOnly | QtWidgets.QFileDialog.DontUseNativeDialog) |
---|
[481ff26] | 192 | if folder is None: |
---|
[5032ea68] | 193 | return |
---|
| 194 | |
---|
[481ff26] | 195 | folder = str(folder) |
---|
[f721030] | 196 | |
---|
[481ff26] | 197 | if not os.path.isdir(folder): |
---|
[5032ea68] | 198 | return |
---|
[f721030] | 199 | |
---|
[5032ea68] | 200 | # get content of dir into a list |
---|
[481ff26] | 201 | path_str = [os.path.join(os.path.abspath(folder), filename) |
---|
[e540cd2] | 202 | for filename in os.listdir(folder)] |
---|
[f721030] | 203 | |
---|
[f82ab8c] | 204 | self.loadFromURL(path_str) |
---|
[5032ea68] | 205 | |
---|
[630155bd] | 206 | def loadProject(self): |
---|
| 207 | """ |
---|
| 208 | Called when the "Open Project" menu item chosen. |
---|
| 209 | """ |
---|
| 210 | kwargs = { |
---|
| 211 | 'parent' : self, |
---|
| 212 | 'caption' : 'Open Project', |
---|
| 213 | 'filter' : 'Project (*.json);;All files (*.*)', |
---|
[4992ff2] | 214 | 'options' : QtWidgets.QFileDialog.DontUseNativeDialog |
---|
[630155bd] | 215 | } |
---|
[fbfc488] | 216 | filename = QtWidgets.QFileDialog.getOpenFileName(**kwargs)[0] |
---|
[630155bd] | 217 | if filename: |
---|
| 218 | load_thread = threads.deferToThread(self.readProject, filename) |
---|
| 219 | load_thread.addCallback(self.readProjectComplete) |
---|
[7fb471d] | 220 | load_thread.addErrback(self.readProjectFailed) |
---|
| 221 | |
---|
[4992ff2] | 222 | def loadFailed(self, reason): |
---|
| 223 | """ |
---|
| 224 | """ |
---|
| 225 | print("file load FAILED: ", reason) |
---|
| 226 | pass |
---|
| 227 | |
---|
[7fb471d] | 228 | def readProjectFailed(self, reason): |
---|
| 229 | """ |
---|
| 230 | """ |
---|
| 231 | print("readProjectFailed FAILED: ", reason) |
---|
| 232 | pass |
---|
[630155bd] | 233 | |
---|
| 234 | def readProject(self, filename): |
---|
| 235 | self.communicator.statusBarUpdateSignal.emit("Loading Project... %s" % os.path.basename(filename)) |
---|
| 236 | try: |
---|
| 237 | manager = DataManager() |
---|
| 238 | with open(filename, 'r') as infile: |
---|
| 239 | manager.load_from_readable(infile) |
---|
| 240 | |
---|
| 241 | self.communicator.statusBarUpdateSignal.emit("Loaded Project: %s" % os.path.basename(filename)) |
---|
| 242 | return manager |
---|
| 243 | |
---|
| 244 | except: |
---|
| 245 | self.communicator.statusBarUpdateSignal.emit("Failed: %s" % os.path.basename(filename)) |
---|
| 246 | raise |
---|
| 247 | |
---|
| 248 | def readProjectComplete(self, manager): |
---|
| 249 | self.model.clear() |
---|
| 250 | |
---|
| 251 | self.manager.assign(manager) |
---|
[4992ff2] | 252 | self.model.beginResetModel() |
---|
[b3e8629] | 253 | for id, item in self.manager.get_all_data().items(): |
---|
[630155bd] | 254 | self.updateModel(item.data, item.path) |
---|
| 255 | |
---|
[4992ff2] | 256 | self.model.endResetModel() |
---|
[630155bd] | 257 | |
---|
| 258 | def saveProject(self): |
---|
| 259 | """ |
---|
| 260 | Called when the "Save Project" menu item chosen. |
---|
| 261 | """ |
---|
| 262 | kwargs = { |
---|
| 263 | 'parent' : self, |
---|
| 264 | 'caption' : 'Save Project', |
---|
| 265 | 'filter' : 'Project (*.json)', |
---|
[4992ff2] | 266 | 'options' : QtWidgets.QFileDialog.DontUseNativeDialog |
---|
[630155bd] | 267 | } |
---|
[d6b8a1d] | 268 | name_tuple = QtWidgets.QFileDialog.getSaveFileName(**kwargs) |
---|
| 269 | filename = name_tuple[0] |
---|
[630155bd] | 270 | if filename: |
---|
[d6b8a1d] | 271 | _, extension = os.path.splitext(filename) |
---|
| 272 | if not extension: |
---|
| 273 | filename = '.'.join((filename, 'json')) |
---|
[630155bd] | 274 | self.communicator.statusBarUpdateSignal.emit("Saving Project... %s\n" % os.path.basename(filename)) |
---|
| 275 | with open(filename, 'w') as outfile: |
---|
| 276 | self.manager.save_to_writable(outfile) |
---|
| 277 | |
---|
[5032ea68] | 278 | def deleteFile(self, event): |
---|
| 279 | """ |
---|
| 280 | Delete selected rows from the model |
---|
| 281 | """ |
---|
| 282 | # Assure this is indeed wanted |
---|
| 283 | delete_msg = "This operation will delete the checked data sets and all the dependents." +\ |
---|
| 284 | "\nDo you want to continue?" |
---|
[53c771e] | 285 | reply = QtWidgets.QMessageBox.question(self, |
---|
[e540cd2] | 286 | 'Warning', |
---|
| 287 | delete_msg, |
---|
[53c771e] | 288 | QtWidgets.QMessageBox.Yes, |
---|
| 289 | QtWidgets.QMessageBox.No) |
---|
[5032ea68] | 290 | |
---|
[53c771e] | 291 | if reply == QtWidgets.QMessageBox.No: |
---|
[5032ea68] | 292 | return |
---|
| 293 | |
---|
| 294 | # Figure out which rows are checked |
---|
| 295 | ind = -1 |
---|
| 296 | # Use 'while' so the row count is forced at every iteration |
---|
[38eb433] | 297 | deleted_indices = [] |
---|
[1420066] | 298 | deleted_names = [] |
---|
[5032ea68] | 299 | while ind < self.model.rowCount(): |
---|
| 300 | ind += 1 |
---|
| 301 | item = self.model.item(ind) |
---|
[f0bb711] | 302 | |
---|
[5032ea68] | 303 | if item and item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
| 304 | # Delete these rows from the model |
---|
[1420066] | 305 | deleted_names.append(str(self.model.item(ind).text())) |
---|
[38eb433] | 306 | deleted_indices.append(item) |
---|
[f0bb711] | 307 | |
---|
[5032ea68] | 308 | self.model.removeRow(ind) |
---|
| 309 | # Decrement index since we just deleted it |
---|
| 310 | ind -= 1 |
---|
| 311 | |
---|
[38eb433] | 312 | # Let others know we deleted data |
---|
| 313 | self.communicator.dataDeletedSignal.emit(deleted_indices) |
---|
[f721030] | 314 | |
---|
[f0bb711] | 315 | # update stored_data |
---|
[1420066] | 316 | self.manager.update_stored_data(deleted_names) |
---|
[f0bb711] | 317 | |
---|
[f82ab8c] | 318 | def deleteTheory(self, event): |
---|
| 319 | """ |
---|
| 320 | Delete selected rows from the theory model |
---|
| 321 | """ |
---|
| 322 | # Assure this is indeed wanted |
---|
| 323 | delete_msg = "This operation will delete the checked data sets and all the dependents." +\ |
---|
| 324 | "\nDo you want to continue?" |
---|
[53c771e] | 325 | reply = QtWidgets.QMessageBox.question(self, |
---|
[8cb6cd6] | 326 | 'Warning', |
---|
| 327 | delete_msg, |
---|
[53c771e] | 328 | QtWidgets.QMessageBox.Yes, |
---|
| 329 | QtWidgets.QMessageBox.No) |
---|
[f82ab8c] | 330 | |
---|
[53c771e] | 331 | if reply == QtWidgets.QMessageBox.No: |
---|
[f82ab8c] | 332 | return |
---|
| 333 | |
---|
| 334 | # Figure out which rows are checked |
---|
| 335 | ind = -1 |
---|
| 336 | # Use 'while' so the row count is forced at every iteration |
---|
| 337 | while ind < self.theory_model.rowCount(): |
---|
| 338 | ind += 1 |
---|
| 339 | item = self.theory_model.item(ind) |
---|
| 340 | if item and item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
| 341 | # Delete these rows from the model |
---|
| 342 | self.theory_model.removeRow(ind) |
---|
| 343 | # Decrement index since we just deleted it |
---|
| 344 | ind -= 1 |
---|
| 345 | |
---|
| 346 | # pass temporarily kept as a breakpoint anchor |
---|
| 347 | pass |
---|
| 348 | |
---|
[f721030] | 349 | def sendData(self, event): |
---|
| 350 | """ |
---|
[5032ea68] | 351 | Send selected item data to the current perspective and set the relevant notifiers |
---|
[f721030] | 352 | """ |
---|
[5032ea68] | 353 | # Set the signal handlers |
---|
| 354 | self.communicator.updateModelFromPerspectiveSignal.connect(self.updateModelFromPerspective) |
---|
[f721030] | 355 | |
---|
[adf81b8] | 356 | def isItemReady(index): |
---|
[5032ea68] | 357 | item = self.model.item(index) |
---|
[adf81b8] | 358 | return item.isCheckable() and item.checkState() == QtCore.Qt.Checked |
---|
| 359 | |
---|
| 360 | # Figure out which rows are checked |
---|
| 361 | selected_items = [self.model.item(index) |
---|
[b3e8629] | 362 | for index in range(self.model.rowCount()) |
---|
[adf81b8] | 363 | if isItemReady(index)] |
---|
[f721030] | 364 | |
---|
[f82ab8c] | 365 | if len(selected_items) < 1: |
---|
| 366 | return |
---|
| 367 | |
---|
[f721030] | 368 | # Which perspective has been selected? |
---|
[1970780] | 369 | if len(selected_items) > 1 and not self._perspective().allowBatch(): |
---|
| 370 | msg = self._perspective().title() + " does not allow multiple data." |
---|
[53c771e] | 371 | msgbox = QtWidgets.QMessageBox() |
---|
| 372 | msgbox.setIcon(QtWidgets.QMessageBox.Critical) |
---|
[5032ea68] | 373 | msgbox.setText(msg) |
---|
[53c771e] | 374 | msgbox.setStandardButtons(QtWidgets.QMessageBox.Ok) |
---|
[5032ea68] | 375 | retval = msgbox.exec_() |
---|
| 376 | return |
---|
[a281ab8] | 377 | |
---|
[f721030] | 378 | # Notify the GuiManager about the send request |
---|
[ee18d33] | 379 | self._perspective().setData(data_item=selected_items, is_batch=self.chkBatch.isChecked()) |
---|
[5032ea68] | 380 | |
---|
[f82ab8c] | 381 | def freezeTheory(self, event): |
---|
| 382 | """ |
---|
| 383 | Freeze selected theory rows. |
---|
| 384 | |
---|
[ca8b853] | 385 | "Freezing" means taking the plottable data from the Theory item |
---|
| 386 | and copying it to a separate top-level item in Data. |
---|
[f82ab8c] | 387 | """ |
---|
[ca8b853] | 388 | # Figure out which rows are checked |
---|
[f82ab8c] | 389 | # Use 'while' so the row count is forced at every iteration |
---|
| 390 | outer_index = -1 |
---|
[481ff26] | 391 | theories_copied = 0 |
---|
[ca8b853] | 392 | while outer_index < self.theory_model.rowCount(): |
---|
[f82ab8c] | 393 | outer_index += 1 |
---|
[ca8b853] | 394 | outer_item = self.theory_model.item(outer_index) |
---|
[f82ab8c] | 395 | if not outer_item: |
---|
| 396 | continue |
---|
[ca8b853] | 397 | if outer_item.isCheckable() and \ |
---|
| 398 | outer_item.checkState() == QtCore.Qt.Checked: |
---|
[7969b9c] | 399 | self.model.beginResetModel() |
---|
[ca8b853] | 400 | theories_copied += 1 |
---|
| 401 | new_item = self.recursivelyCloneItem(outer_item) |
---|
| 402 | # Append a "unique" descriptor to the name |
---|
| 403 | time_bit = str(time.time())[7:-1].replace('.', '') |
---|
| 404 | new_name = new_item.text() + '_@' + time_bit |
---|
| 405 | new_item.setText(new_name) |
---|
| 406 | self.model.appendRow(new_item) |
---|
[7969b9c] | 407 | self.model.endResetModel() |
---|
| 408 | #self.model.reset() |
---|
[f82ab8c] | 409 | |
---|
[481ff26] | 410 | freeze_msg = "" |
---|
| 411 | if theories_copied == 0: |
---|
| 412 | return |
---|
| 413 | elif theories_copied == 1: |
---|
[ca8b853] | 414 | freeze_msg = "1 theory copied from the Theory tab as a data set" |
---|
[481ff26] | 415 | elif theories_copied > 1: |
---|
[ca8b853] | 416 | freeze_msg = "%i theories copied from the Theory tab as data sets" % theories_copied |
---|
[481ff26] | 417 | else: |
---|
| 418 | freeze_msg = "Unexpected number of theories copied: %i" % theories_copied |
---|
[b3e8629] | 419 | raise AttributeError(freeze_msg) |
---|
[481ff26] | 420 | self.communicator.statusBarUpdateSignal.emit(freeze_msg) |
---|
| 421 | # Actively switch tabs |
---|
| 422 | self.setCurrentIndex(1) |
---|
| 423 | |
---|
| 424 | def recursivelyCloneItem(self, item): |
---|
| 425 | """ |
---|
| 426 | Clone QStandardItem() object |
---|
| 427 | """ |
---|
| 428 | new_item = item.clone() |
---|
| 429 | # clone doesn't do deepcopy :( |
---|
[b3e8629] | 430 | for child_index in range(item.rowCount()): |
---|
[481ff26] | 431 | child_item = self.recursivelyCloneItem(item.child(child_index)) |
---|
| 432 | new_item.setChild(child_index, child_item) |
---|
| 433 | return new_item |
---|
[f82ab8c] | 434 | |
---|
[27313b7] | 435 | def updatePlotName(self, name_tuple): |
---|
| 436 | """ |
---|
| 437 | Modify the name of the current plot |
---|
| 438 | """ |
---|
| 439 | old_name, current_name = name_tuple |
---|
| 440 | ind = self.cbgraph.findText(old_name) |
---|
| 441 | self.cbgraph.setCurrentIndex(ind) |
---|
| 442 | self.cbgraph.setItemText(ind, current_name) |
---|
| 443 | |
---|
[7d8bebf] | 444 | def updateGraphCount(self, graph_list): |
---|
| 445 | """ |
---|
| 446 | Modify the graph name combo and potentially remove |
---|
| 447 | deleted graphs |
---|
| 448 | """ |
---|
| 449 | self.updateGraphCombo(graph_list) |
---|
| 450 | |
---|
| 451 | if not self.active_plots: |
---|
| 452 | return |
---|
| 453 | new_plots = [PlotHelper.plotById(plot) for plot in graph_list] |
---|
[b3e8629] | 454 | active_plots_copy = list(self.active_plots.keys()) |
---|
[7d8bebf] | 455 | for plot in active_plots_copy: |
---|
| 456 | if self.active_plots[plot] in new_plots: |
---|
| 457 | continue |
---|
| 458 | self.active_plots.pop(plot) |
---|
| 459 | |
---|
[8cb6cd6] | 460 | def updateGraphCombo(self, graph_list): |
---|
| 461 | """ |
---|
| 462 | Modify Graph combo box on graph add/delete |
---|
| 463 | """ |
---|
| 464 | orig_text = self.cbgraph.currentText() |
---|
| 465 | self.cbgraph.clear() |
---|
[0268aed] | 466 | self.cbgraph.insertItems(0, graph_list) |
---|
[8cb6cd6] | 467 | ind = self.cbgraph.findText(orig_text) |
---|
| 468 | if ind > 0: |
---|
| 469 | self.cbgraph.setCurrentIndex(ind) |
---|
| 470 | |
---|
[83d6249] | 471 | def updatePerspectiveCombo(self, index): |
---|
| 472 | """ |
---|
| 473 | Notify the gui manager about the new perspective chosen. |
---|
| 474 | """ |
---|
[8ac3551] | 475 | self.communicator.perspectiveChangedSignal.emit(self.cbFitting.itemText(index)) |
---|
[1970780] | 476 | self.chkBatch.setEnabled(self.parent.perspective().allowBatch()) |
---|
[83d6249] | 477 | |
---|
[3b3b40b] | 478 | def displayFile(self, filename=None, is_data=True): |
---|
[d48cc19] | 479 | """ |
---|
| 480 | Forces display of charts for the given filename |
---|
| 481 | """ |
---|
[3b3b40b] | 482 | model = self.model if is_data else self.theory_model |
---|
[88e1f57] | 483 | # Now query the model item for available plots |
---|
[d48cc19] | 484 | plots = GuiUtils.plotsFromFilename(filename, model) |
---|
[88e1f57] | 485 | item = GuiUtils.itemFromFilename(filename, model) |
---|
| 486 | |
---|
| 487 | new_plots = [] |
---|
[fef38e8] | 488 | for plot in plots: |
---|
| 489 | plot_id = plot.id |
---|
[b3e8629] | 490 | if plot_id in list(self.active_plots.keys()): |
---|
[7d8bebf] | 491 | self.active_plots[plot_id].replacePlot(plot_id, plot) |
---|
[fef38e8] | 492 | else: |
---|
[88e1f57] | 493 | # 'sophisticated' test to generate standalone plot for residuals |
---|
| 494 | if 'esiduals' in plot.title: |
---|
| 495 | self.plotData([(item, plot)]) |
---|
| 496 | else: |
---|
| 497 | new_plots.append((item, plot)) |
---|
| 498 | |
---|
| 499 | if new_plots: |
---|
| 500 | self.plotData(new_plots) |
---|
[56b22f9] | 501 | |
---|
[3b3b40b] | 502 | def displayData(self, data_list): |
---|
| 503 | """ |
---|
| 504 | Forces display of charts for the given data set |
---|
| 505 | """ |
---|
| 506 | plot_to_show = data_list[0] |
---|
| 507 | # passed plot is used ONLY to figure out its title, |
---|
| 508 | # so all the charts related by it can be pulled from |
---|
| 509 | # the data explorer indices. |
---|
| 510 | filename = plot_to_show.filename |
---|
| 511 | self.displayFile(filename=filename, is_data=plot_to_show.is_data) |
---|
| 512 | |
---|
[56b22f9] | 513 | def addDataPlot2D(self, plot_set, item): |
---|
[672b8ab] | 514 | """ |
---|
| 515 | Create a new 2D plot and add it to the workspace |
---|
| 516 | """ |
---|
[56b22f9] | 517 | plot2D = Plotter2D(self) |
---|
| 518 | plot2D.item = item |
---|
| 519 | plot2D.plot(plot_set) |
---|
| 520 | self.addPlot(plot2D) |
---|
[88e1f57] | 521 | self.active_plots[plot2D.data.id] = plot2D |
---|
[56b22f9] | 522 | #============================================ |
---|
[672b8ab] | 523 | # Experimental hook for silx charts |
---|
| 524 | #============================================ |
---|
[56b22f9] | 525 | ## Attach silx |
---|
| 526 | #from silx.gui import qt |
---|
| 527 | #from silx.gui.plot import StackView |
---|
| 528 | #sv = StackView() |
---|
| 529 | #sv.setColormap("jet", autoscale=True) |
---|
| 530 | #sv.setStack(plot_set.data.reshape(1,100,100)) |
---|
| 531 | ##sv.setLabels(["x: -10 to 10 (200 samples)", |
---|
| 532 | ## "y: -10 to 5 (150 samples)"]) |
---|
| 533 | #sv.show() |
---|
| 534 | #============================================ |
---|
| 535 | |
---|
| 536 | def plotData(self, plots): |
---|
| 537 | """ |
---|
| 538 | Takes 1D/2D data and generates a single plot (1D) or multiple plots (2D) |
---|
[1042dba] | 539 | """ |
---|
| 540 | # Call show on requested plots |
---|
[31c5b58] | 541 | # All same-type charts in one plot |
---|
[3bdbfcc] | 542 | for item, plot_set in plots: |
---|
[49e124c] | 543 | if isinstance(plot_set, Data1D): |
---|
[7d8bebf] | 544 | if not 'new_plot' in locals(): |
---|
| 545 | new_plot = Plotter(self) |
---|
[9290b1a] | 546 | new_plot.plot(plot_set) |
---|
[88e1f57] | 547 | # active_plots may contain multiple charts |
---|
| 548 | self.active_plots[plot_set.id] = new_plot |
---|
[49e124c] | 549 | elif isinstance(plot_set, Data2D): |
---|
[56b22f9] | 550 | self.addDataPlot2D(plot_set, item) |
---|
[49e124c] | 551 | else: |
---|
| 552 | msg = "Incorrect data type passed to Plotting" |
---|
[b3e8629] | 553 | raise AttributeError(msg) |
---|
[49e124c] | 554 | |
---|
[7d8bebf] | 555 | if 'new_plot' in locals() and \ |
---|
[b4b8589] | 556 | hasattr(new_plot, 'data') and \ |
---|
| 557 | isinstance(new_plot.data, Data1D): |
---|
[56b22f9] | 558 | self.addPlot(new_plot) |
---|
| 559 | |
---|
| 560 | def newPlot(self): |
---|
| 561 | """ |
---|
| 562 | Select checked data and plot it |
---|
| 563 | """ |
---|
| 564 | # Check which tab is currently active |
---|
| 565 | if self.current_view == self.treeView: |
---|
| 566 | plots = GuiUtils.plotsFromCheckedItems(self.model) |
---|
| 567 | else: |
---|
| 568 | plots = GuiUtils.plotsFromCheckedItems(self.theory_model) |
---|
| 569 | |
---|
| 570 | self.plotData(plots) |
---|
[1042dba] | 571 | |
---|
[56b22f9] | 572 | def addPlot(self, new_plot): |
---|
[31c5b58] | 573 | """ |
---|
| 574 | Helper method for plot bookkeeping |
---|
| 575 | """ |
---|
| 576 | # Update the global plot counter |
---|
[0268aed] | 577 | title = str(PlotHelper.idOfPlot(new_plot)) |
---|
[31c5b58] | 578 | new_plot.setWindowTitle(title) |
---|
[8cb6cd6] | 579 | |
---|
[7d8bebf] | 580 | # Set the object name to satisfy the Squish object picker |
---|
| 581 | new_plot.setObjectName(title) |
---|
| 582 | |
---|
[31c5b58] | 583 | # Add the plot to the workspace |
---|
[7969b9c] | 584 | self.parent.workspace().addSubWindow(new_plot) |
---|
[8cb6cd6] | 585 | |
---|
[31c5b58] | 586 | # Show the plot |
---|
| 587 | new_plot.show() |
---|
[fbfc488] | 588 | new_plot.canvas.draw() |
---|
[f721030] | 589 | |
---|
[31c5b58] | 590 | # Update the active chart list |
---|
[88e1f57] | 591 | #self.active_plots[new_plot.data.id] = new_plot |
---|
[8cb6cd6] | 592 | |
---|
| 593 | def appendPlot(self): |
---|
| 594 | """ |
---|
| 595 | Add data set(s) to the existing matplotlib chart |
---|
| 596 | """ |
---|
| 597 | # new plot data |
---|
| 598 | new_plots = GuiUtils.plotsFromCheckedItems(self.model) |
---|
| 599 | |
---|
| 600 | # old plot data |
---|
[0268aed] | 601 | plot_id = str(self.cbgraph.currentText()) |
---|
[8cb6cd6] | 602 | |
---|
[0268aed] | 603 | assert plot_id in PlotHelper.currentPlots(), "No such plot: %s"%(plot_id) |
---|
[8cb6cd6] | 604 | |
---|
| 605 | old_plot = PlotHelper.plotById(plot_id) |
---|
| 606 | |
---|
[14d9c7b] | 607 | # Add new data to the old plot, if data type is the same. |
---|
[3bdbfcc] | 608 | for _, plot_set in new_plots: |
---|
[14d9c7b] | 609 | if type(plot_set) is type(old_plot._data): |
---|
[31c5b58] | 610 | old_plot.data = plot_set |
---|
[14d9c7b] | 611 | old_plot.plot() |
---|
[8cb6cd6] | 612 | |
---|
[7d077d1] | 613 | def updatePlot(self, new_data): |
---|
| 614 | """ |
---|
| 615 | Modify existing plot for immediate response |
---|
| 616 | """ |
---|
| 617 | data = new_data[0] |
---|
| 618 | assert type(data).__name__ in ['Data1D', 'Data2D'] |
---|
| 619 | |
---|
| 620 | id = data.id |
---|
[b3e8629] | 621 | if data.id in list(self.active_plots.keys()): |
---|
[7d077d1] | 622 | self.active_plots[id].replacePlot(id, data) |
---|
| 623 | |
---|
[f721030] | 624 | def chooseFiles(self): |
---|
| 625 | """ |
---|
[5032ea68] | 626 | Shows the Open file dialog and returns the chosen path(s) |
---|
[f721030] | 627 | """ |
---|
| 628 | # List of known extensions |
---|
| 629 | wlist = self.getWlist() |
---|
| 630 | |
---|
| 631 | # Location is automatically saved - no need to keep track of the last dir |
---|
[5032ea68] | 632 | # But only with Qt built-in dialog (non-platform native) |
---|
[4992ff2] | 633 | paths = QtWidgets.QFileDialog.getOpenFileNames(self, "Choose a file", "", |
---|
[7969b9c] | 634 | wlist, None, QtWidgets.QFileDialog.DontUseNativeDialog)[0] |
---|
[fbfc488] | 635 | if not paths: |
---|
[f721030] | 636 | return |
---|
| 637 | |
---|
[0cd8612] | 638 | if not isinstance(paths, list): |
---|
[f721030] | 639 | paths = [paths] |
---|
| 640 | |
---|
[9e426c1] | 641 | return paths |
---|
[f721030] | 642 | |
---|
| 643 | def readData(self, path): |
---|
| 644 | """ |
---|
[481ff26] | 645 | verbatim copy-paste from |
---|
| 646 | sasgui.guiframe.local_perspectives.data_loader.data_loader.py |
---|
[f721030] | 647 | slightly modified for clarity |
---|
| 648 | """ |
---|
| 649 | message = "" |
---|
| 650 | log_msg = '' |
---|
| 651 | output = {} |
---|
| 652 | any_error = False |
---|
| 653 | data_error = False |
---|
| 654 | error_message = "" |
---|
[e540cd2] | 655 | number_of_files = len(path) |
---|
| 656 | self.communicator.progressBarUpdateSignal.emit(0.0) |
---|
| 657 | |
---|
| 658 | for index, p_file in enumerate(path): |
---|
[f721030] | 659 | basename = os.path.basename(p_file) |
---|
| 660 | _, extension = os.path.splitext(basename) |
---|
[0cd8612] | 661 | if extension.lower() in GuiUtils.EXTENSIONS: |
---|
[f721030] | 662 | any_error = True |
---|
| 663 | log_msg = "Data Loader cannot " |
---|
| 664 | log_msg += "load: %s\n" % str(p_file) |
---|
| 665 | log_msg += """Please try to open that file from "open project" """ |
---|
| 666 | log_msg += """or "open analysis" menu\n""" |
---|
| 667 | error_message = log_msg + "\n" |
---|
| 668 | logging.info(log_msg) |
---|
| 669 | continue |
---|
| 670 | |
---|
| 671 | try: |
---|
[5032ea68] | 672 | message = "Loading Data... " + str(basename) + "\n" |
---|
[f721030] | 673 | |
---|
| 674 | # change this to signal notification in GuiManager |
---|
[f82ab8c] | 675 | self.communicator.statusBarUpdateSignal.emit(message) |
---|
[f721030] | 676 | |
---|
| 677 | output_objects = self.loader.load(p_file) |
---|
| 678 | |
---|
| 679 | # Some loaders return a list and some just a single Data1D object. |
---|
| 680 | # Standardize. |
---|
| 681 | if not isinstance(output_objects, list): |
---|
| 682 | output_objects = [output_objects] |
---|
| 683 | |
---|
| 684 | for item in output_objects: |
---|
[481ff26] | 685 | # cast sascalc.dataloader.data_info.Data1D into |
---|
| 686 | # sasgui.guiframe.dataFitting.Data1D |
---|
[f721030] | 687 | # TODO : Fix it |
---|
| 688 | new_data = self.manager.create_gui_data(item, p_file) |
---|
| 689 | output[new_data.id] = new_data |
---|
[481ff26] | 690 | |
---|
| 691 | # Model update should be protected |
---|
| 692 | self.mutex.lock() |
---|
[f721030] | 693 | self.updateModel(new_data, p_file) |
---|
[7969b9c] | 694 | #self.model.reset() |
---|
| 695 | QtWidgets.QApplication.processEvents() |
---|
[481ff26] | 696 | self.mutex.unlock() |
---|
[f721030] | 697 | |
---|
| 698 | if hasattr(item, 'errors'): |
---|
| 699 | for error_data in item.errors: |
---|
| 700 | data_error = True |
---|
| 701 | message += "\tError: {0}\n".format(error_data) |
---|
| 702 | else: |
---|
[5032ea68] | 703 | |
---|
[f721030] | 704 | logging.error("Loader returned an invalid object:\n %s" % str(item)) |
---|
| 705 | data_error = True |
---|
| 706 | |
---|
[5032ea68] | 707 | except Exception as ex: |
---|
[b3e8629] | 708 | logging.error(sys.exc_info()[1]) |
---|
[5032ea68] | 709 | |
---|
[f721030] | 710 | any_error = True |
---|
| 711 | if any_error or error_message != "": |
---|
| 712 | if error_message == "": |
---|
| 713 | error = "Error: " + str(sys.exc_info()[1]) + "\n" |
---|
| 714 | error += "while loading Data: \n%s\n" % str(basename) |
---|
| 715 | error_message += "The data file you selected could not be loaded.\n" |
---|
| 716 | error_message += "Make sure the content of your file" |
---|
| 717 | error_message += " is properly formatted.\n\n" |
---|
| 718 | error_message += "When contacting the SasView team, mention the" |
---|
| 719 | error_message += " following:\n%s" % str(error) |
---|
| 720 | elif data_error: |
---|
| 721 | base_message = "Errors occurred while loading " |
---|
| 722 | base_message += "{0}\n".format(basename) |
---|
| 723 | base_message += "The data file loaded but with errors.\n" |
---|
| 724 | error_message = base_message + error_message |
---|
| 725 | else: |
---|
| 726 | error_message += "%s\n" % str(p_file) |
---|
[481ff26] | 727 | |
---|
[e540cd2] | 728 | current_percentage = int(100.0* index/number_of_files) |
---|
| 729 | self.communicator.progressBarUpdateSignal.emit(current_percentage) |
---|
| 730 | |
---|
[f721030] | 731 | if any_error or error_message: |
---|
[0cd8612] | 732 | logging.error(error_message) |
---|
| 733 | status_bar_message = "Errors occurred while loading %s" % format(basename) |
---|
| 734 | self.communicator.statusBarUpdateSignal.emit(status_bar_message) |
---|
[f721030] | 735 | |
---|
| 736 | else: |
---|
| 737 | message = "Loading Data Complete! " |
---|
| 738 | message += log_msg |
---|
[0cd8612] | 739 | # Notify the progress bar that the updates are over. |
---|
[e540cd2] | 740 | self.communicator.progressBarUpdateSignal.emit(-1) |
---|
[454670d] | 741 | self.communicator.statusBarUpdateSignal.emit(message) |
---|
[481ff26] | 742 | |
---|
[a281ab8] | 743 | return output, message |
---|
[f721030] | 744 | |
---|
| 745 | def getWlist(self): |
---|
| 746 | """ |
---|
[f82ab8c] | 747 | Wildcards of files we know the format of. |
---|
[f721030] | 748 | """ |
---|
| 749 | # Display the Qt Load File module |
---|
| 750 | cards = self.loader.get_wildcards() |
---|
| 751 | |
---|
| 752 | # get rid of the wx remnant in wildcards |
---|
| 753 | # TODO: modify sasview loader get_wildcards method, after merge, |
---|
| 754 | # so this kludge can be avoided |
---|
| 755 | new_cards = [] |
---|
| 756 | for item in cards: |
---|
| 757 | new_cards.append(item[:item.find("|")]) |
---|
| 758 | wlist = ';;'.join(new_cards) |
---|
| 759 | |
---|
| 760 | return wlist |
---|
[488c49d] | 761 | |
---|
| 762 | def selectData(self, index): |
---|
| 763 | """ |
---|
| 764 | Callback method for modifying the TreeView on Selection Options change |
---|
| 765 | """ |
---|
| 766 | if not isinstance(index, int): |
---|
| 767 | msg = "Incorrect type passed to DataExplorer.selectData()" |
---|
[b3e8629] | 768 | raise AttributeError(msg) |
---|
[488c49d] | 769 | |
---|
| 770 | # Respond appropriately |
---|
| 771 | if index == 0: |
---|
| 772 | # Select All |
---|
[5032ea68] | 773 | for index in range(self.model.rowCount()): |
---|
| 774 | item = self.model.item(index) |
---|
| 775 | if item.isCheckable() and item.checkState() == QtCore.Qt.Unchecked: |
---|
[488c49d] | 776 | item.setCheckState(QtCore.Qt.Checked) |
---|
| 777 | elif index == 1: |
---|
| 778 | # De-select All |
---|
[5032ea68] | 779 | for index in range(self.model.rowCount()): |
---|
| 780 | item = self.model.item(index) |
---|
| 781 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
[488c49d] | 782 | item.setCheckState(QtCore.Qt.Unchecked) |
---|
| 783 | |
---|
| 784 | elif index == 2: |
---|
| 785 | # Select All 1-D |
---|
[5032ea68] | 786 | for index in range(self.model.rowCount()): |
---|
| 787 | item = self.model.item(index) |
---|
[488c49d] | 788 | item.setCheckState(QtCore.Qt.Unchecked) |
---|
[5032ea68] | 789 | |
---|
| 790 | try: |
---|
[8548d739] | 791 | is1D = isinstance(GuiUtils.dataFromItem(item), Data1D) |
---|
[5032ea68] | 792 | except AttributeError: |
---|
| 793 | msg = "Bad structure of the data model." |
---|
[b3e8629] | 794 | raise RuntimeError(msg) |
---|
[5032ea68] | 795 | |
---|
| 796 | if is1D: |
---|
[488c49d] | 797 | item.setCheckState(QtCore.Qt.Checked) |
---|
| 798 | |
---|
| 799 | elif index == 3: |
---|
| 800 | # Unselect All 1-D |
---|
[5032ea68] | 801 | for index in range(self.model.rowCount()): |
---|
| 802 | item = self.model.item(index) |
---|
| 803 | |
---|
| 804 | try: |
---|
[8548d739] | 805 | is1D = isinstance(GuiUtils.dataFromItem(item), Data1D) |
---|
[5032ea68] | 806 | except AttributeError: |
---|
| 807 | msg = "Bad structure of the data model." |
---|
[b3e8629] | 808 | raise RuntimeError(msg) |
---|
[5032ea68] | 809 | |
---|
| 810 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked and is1D: |
---|
[488c49d] | 811 | item.setCheckState(QtCore.Qt.Unchecked) |
---|
| 812 | |
---|
| 813 | elif index == 4: |
---|
| 814 | # Select All 2-D |
---|
[5032ea68] | 815 | for index in range(self.model.rowCount()): |
---|
| 816 | item = self.model.item(index) |
---|
| 817 | item.setCheckState(QtCore.Qt.Unchecked) |
---|
| 818 | try: |
---|
[8548d739] | 819 | is2D = isinstance(GuiUtils.dataFromItem(item), Data2D) |
---|
[5032ea68] | 820 | except AttributeError: |
---|
| 821 | msg = "Bad structure of the data model." |
---|
[b3e8629] | 822 | raise RuntimeError(msg) |
---|
[5032ea68] | 823 | |
---|
| 824 | if is2D: |
---|
[488c49d] | 825 | item.setCheckState(QtCore.Qt.Checked) |
---|
| 826 | |
---|
| 827 | elif index == 5: |
---|
| 828 | # Unselect All 2-D |
---|
[5032ea68] | 829 | for index in range(self.model.rowCount()): |
---|
| 830 | item = self.model.item(index) |
---|
| 831 | |
---|
| 832 | try: |
---|
[8548d739] | 833 | is2D = isinstance(GuiUtils.dataFromItem(item), Data2D) |
---|
[5032ea68] | 834 | except AttributeError: |
---|
| 835 | msg = "Bad structure of the data model." |
---|
[b3e8629] | 836 | raise RuntimeError(msg) |
---|
[5032ea68] | 837 | |
---|
| 838 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked and is2D: |
---|
[488c49d] | 839 | item.setCheckState(QtCore.Qt.Unchecked) |
---|
| 840 | |
---|
| 841 | else: |
---|
| 842 | msg = "Incorrect value in the Selection Option" |
---|
| 843 | # Change this to a proper logging action |
---|
[b3e8629] | 844 | raise Exception(msg) |
---|
[488c49d] | 845 | |
---|
[4b71e91] | 846 | def contextMenu(self): |
---|
[e540cd2] | 847 | """ |
---|
[4b71e91] | 848 | Define actions and layout of the right click context menu |
---|
[e540cd2] | 849 | """ |
---|
[4b71e91] | 850 | # Create a custom menu based on actions defined in the UI file |
---|
[4992ff2] | 851 | self.context_menu = QtWidgets.QMenu(self) |
---|
[4b71e91] | 852 | self.context_menu.addAction(self.actionDataInfo) |
---|
| 853 | self.context_menu.addAction(self.actionSaveAs) |
---|
| 854 | self.context_menu.addAction(self.actionQuickPlot) |
---|
| 855 | self.context_menu.addSeparator() |
---|
| 856 | self.context_menu.addAction(self.actionQuick3DPlot) |
---|
| 857 | self.context_menu.addAction(self.actionEditMask) |
---|
[c6fb57c] | 858 | self.context_menu.addSeparator() |
---|
| 859 | self.context_menu.addAction(self.actionDelete) |
---|
| 860 | |
---|
[4b71e91] | 861 | |
---|
| 862 | # Define the callbacks |
---|
| 863 | self.actionDataInfo.triggered.connect(self.showDataInfo) |
---|
| 864 | self.actionSaveAs.triggered.connect(self.saveDataAs) |
---|
| 865 | self.actionQuickPlot.triggered.connect(self.quickDataPlot) |
---|
| 866 | self.actionQuick3DPlot.triggered.connect(self.quickData3DPlot) |
---|
| 867 | self.actionEditMask.triggered.connect(self.showEditDataMask) |
---|
[c6fb57c] | 868 | self.actionDelete.triggered.connect(self.deleteItem) |
---|
[e540cd2] | 869 | |
---|
| 870 | def onCustomContextMenu(self, position): |
---|
| 871 | """ |
---|
[4b71e91] | 872 | Show the right-click context menu in the data treeview |
---|
[e540cd2] | 873 | """ |
---|
[cbcdd2c] | 874 | index = self.current_view.indexAt(position) |
---|
| 875 | proxy = self.current_view.model() |
---|
| 876 | model = proxy.sourceModel() |
---|
| 877 | |
---|
[e540cd2] | 878 | if index.isValid(): |
---|
[cbcdd2c] | 879 | model_item = model.itemFromIndex(proxy.mapToSource(index)) |
---|
[4b71e91] | 880 | # Find the mapped index |
---|
| 881 | orig_index = model_item.isCheckable() |
---|
| 882 | if orig_index: |
---|
| 883 | # Check the data to enable/disable actions |
---|
[8548d739] | 884 | is_2D = isinstance(GuiUtils.dataFromItem(model_item), Data2D) |
---|
[4b71e91] | 885 | self.actionQuick3DPlot.setEnabled(is_2D) |
---|
| 886 | self.actionEditMask.setEnabled(is_2D) |
---|
| 887 | # Fire up the menu |
---|
[cbcdd2c] | 888 | self.context_menu.exec_(self.current_view.mapToGlobal(position)) |
---|
[4b71e91] | 889 | |
---|
| 890 | def showDataInfo(self): |
---|
| 891 | """ |
---|
| 892 | Show a simple read-only text edit with data information. |
---|
| 893 | """ |
---|
[cbcdd2c] | 894 | index = self.current_view.selectedIndexes()[0] |
---|
| 895 | proxy = self.current_view.model() |
---|
| 896 | model = proxy.sourceModel() |
---|
| 897 | model_item = model.itemFromIndex(proxy.mapToSource(index)) |
---|
| 898 | |
---|
[8548d739] | 899 | data = GuiUtils.dataFromItem(model_item) |
---|
[28a84e9] | 900 | if isinstance(data, Data1D): |
---|
[4b71e91] | 901 | text_to_show = GuiUtils.retrieveData1d(data) |
---|
[28a84e9] | 902 | # Hardcoded sizes to enable full width rendering with default font |
---|
[4b71e91] | 903 | self.txt_widget.resize(420,600) |
---|
| 904 | else: |
---|
| 905 | text_to_show = GuiUtils.retrieveData2d(data) |
---|
[28a84e9] | 906 | # Hardcoded sizes to enable full width rendering with default font |
---|
[4b71e91] | 907 | self.txt_widget.resize(700,600) |
---|
| 908 | |
---|
| 909 | self.txt_widget.setReadOnly(True) |
---|
| 910 | self.txt_widget.setWindowFlags(QtCore.Qt.Window) |
---|
| 911 | self.txt_widget.setWindowIcon(QtGui.QIcon(":/res/ball.ico")) |
---|
| 912 | self.txt_widget.setWindowTitle("Data Info: %s" % data.filename) |
---|
[d5c5d3d] | 913 | self.txt_widget.clear() |
---|
[4b71e91] | 914 | self.txt_widget.insertPlainText(text_to_show) |
---|
| 915 | |
---|
| 916 | self.txt_widget.show() |
---|
[28a84e9] | 917 | # Move the slider all the way up, if present |
---|
[4b71e91] | 918 | vertical_scroll_bar = self.txt_widget.verticalScrollBar() |
---|
[7969b9c] | 919 | vertical_scroll_bar.triggerAction(QtWidgets.QScrollBar.SliderToMinimum) |
---|
[4b71e91] | 920 | |
---|
| 921 | def saveDataAs(self): |
---|
| 922 | """ |
---|
[28a84e9] | 923 | Save the data points as either txt or xml |
---|
[4b71e91] | 924 | """ |
---|
[cbcdd2c] | 925 | index = self.current_view.selectedIndexes()[0] |
---|
| 926 | proxy = self.current_view.model() |
---|
| 927 | model = proxy.sourceModel() |
---|
| 928 | model_item = model.itemFromIndex(proxy.mapToSource(index)) |
---|
| 929 | |
---|
[8548d739] | 930 | data = GuiUtils.dataFromItem(model_item) |
---|
[28a84e9] | 931 | if isinstance(data, Data1D): |
---|
| 932 | GuiUtils.saveData1D(data) |
---|
| 933 | else: |
---|
| 934 | GuiUtils.saveData2D(data) |
---|
[4b71e91] | 935 | |
---|
| 936 | def quickDataPlot(self): |
---|
[1af348e] | 937 | """ |
---|
| 938 | Frozen plot - display an image of the plot |
---|
| 939 | """ |
---|
[cbcdd2c] | 940 | index = self.current_view.selectedIndexes()[0] |
---|
| 941 | proxy = self.current_view.model() |
---|
| 942 | model = proxy.sourceModel() |
---|
| 943 | model_item = model.itemFromIndex(proxy.mapToSource(index)) |
---|
| 944 | |
---|
[8548d739] | 945 | data = GuiUtils.dataFromItem(model_item) |
---|
[39551a68] | 946 | |
---|
[ef01be4] | 947 | method_name = 'Plotter' |
---|
| 948 | if isinstance(data, Data2D): |
---|
| 949 | method_name='Plotter2D' |
---|
[1af348e] | 950 | |
---|
[ef01be4] | 951 | new_plot = globals()[method_name](self, quickplot=True) |
---|
| 952 | new_plot.data = data |
---|
[5236449] | 953 | #new_plot.plot(marker='o') |
---|
| 954 | new_plot.plot() |
---|
[39551a68] | 955 | |
---|
| 956 | # Update the global plot counter |
---|
| 957 | title = "Plot " + data.name |
---|
| 958 | new_plot.setWindowTitle(title) |
---|
| 959 | |
---|
| 960 | # Show the plot |
---|
| 961 | new_plot.show() |
---|
[4b71e91] | 962 | |
---|
| 963 | def quickData3DPlot(self): |
---|
| 964 | """ |
---|
[55d89f8] | 965 | Slowish 3D plot |
---|
[4b71e91] | 966 | """ |
---|
[cbcdd2c] | 967 | index = self.current_view.selectedIndexes()[0] |
---|
| 968 | proxy = self.current_view.model() |
---|
| 969 | model = proxy.sourceModel() |
---|
| 970 | model_item = model.itemFromIndex(proxy.mapToSource(index)) |
---|
| 971 | |
---|
[55d89f8] | 972 | data = GuiUtils.dataFromItem(model_item) |
---|
| 973 | |
---|
| 974 | new_plot = Plotter2D(self, quickplot=True, dimension=3) |
---|
| 975 | new_plot.data = data |
---|
[965fbd8] | 976 | new_plot.plot() |
---|
[55d89f8] | 977 | |
---|
| 978 | # Update the global plot counter |
---|
| 979 | title = "Plot " + data.name |
---|
| 980 | new_plot.setWindowTitle(title) |
---|
| 981 | |
---|
| 982 | # Show the plot |
---|
| 983 | new_plot.show() |
---|
[4b71e91] | 984 | |
---|
| 985 | def showEditDataMask(self): |
---|
| 986 | """ |
---|
[cad617b] | 987 | Mask Editor for 2D plots |
---|
[4b71e91] | 988 | """ |
---|
[cbcdd2c] | 989 | index = self.current_view.selectedIndexes()[0] |
---|
| 990 | proxy = self.current_view.model() |
---|
| 991 | model = proxy.sourceModel() |
---|
| 992 | model_item = model.itemFromIndex(proxy.mapToSource(index)) |
---|
| 993 | |
---|
[416fa8f] | 994 | data = GuiUtils.dataFromItem(model_item) |
---|
| 995 | |
---|
| 996 | mask_editor = MaskEditor(self, data) |
---|
[cad617b] | 997 | # Modal dialog here. |
---|
[416fa8f] | 998 | mask_editor.exec_() |
---|
[f721030] | 999 | |
---|
[c6fb57c] | 1000 | def deleteItem(self): |
---|
| 1001 | """ |
---|
| 1002 | Delete the current item |
---|
| 1003 | """ |
---|
| 1004 | # Assure this is indeed wanted |
---|
| 1005 | delete_msg = "This operation will delete the selected data sets." +\ |
---|
| 1006 | "\nDo you want to continue?" |
---|
| 1007 | reply = QtWidgets.QMessageBox.question(self, |
---|
| 1008 | 'Warning', |
---|
| 1009 | delete_msg, |
---|
| 1010 | QtWidgets.QMessageBox.Yes, |
---|
| 1011 | QtWidgets.QMessageBox.No) |
---|
| 1012 | |
---|
| 1013 | if reply == QtWidgets.QMessageBox.No: |
---|
| 1014 | return |
---|
| 1015 | |
---|
| 1016 | indices = self.current_view.selectedIndexes() |
---|
| 1017 | proxy = self.current_view.model() |
---|
| 1018 | model = proxy.sourceModel() |
---|
| 1019 | |
---|
| 1020 | for index in indices: |
---|
| 1021 | row_index = proxy.mapToSource(index) |
---|
| 1022 | item_to_delete = model.itemFromIndex(row_index) |
---|
[cb4d219] | 1023 | if item_to_delete and item_to_delete.isCheckable(): |
---|
[c6fb57c] | 1024 | row = row_index.row() |
---|
| 1025 | if item_to_delete.parent(): |
---|
| 1026 | # We have a child item - delete from it |
---|
| 1027 | item_to_delete.parent().removeRow(row) |
---|
| 1028 | else: |
---|
| 1029 | # delete directly from model |
---|
| 1030 | model.removeRow(row) |
---|
| 1031 | pass |
---|
| 1032 | |
---|
[8ac3551] | 1033 | def onAnalysisUpdate(self, new_perspective=""): |
---|
| 1034 | """ |
---|
| 1035 | Update the perspective combo index based on passed string |
---|
| 1036 | """ |
---|
| 1037 | assert new_perspective in Perspectives.PERSPECTIVES.keys() |
---|
| 1038 | self.cbFitting.blockSignals(True) |
---|
| 1039 | self.cbFitting.setCurrentIndex(self.cbFitting.findText(new_perspective)) |
---|
| 1040 | self.cbFitting.blockSignals(False) |
---|
| 1041 | pass |
---|
| 1042 | |
---|
[a281ab8] | 1043 | def loadComplete(self, output): |
---|
[f721030] | 1044 | """ |
---|
| 1045 | Post message to status bar and update the data manager |
---|
| 1046 | """ |
---|
[8cb6cd6] | 1047 | assert isinstance(output, tuple) |
---|
[e540cd2] | 1048 | |
---|
[9e426c1] | 1049 | # Reset the model so the view gets updated. |
---|
[7969b9c] | 1050 | #self.model.reset() |
---|
[e540cd2] | 1051 | self.communicator.progressBarUpdateSignal.emit(-1) |
---|
[a281ab8] | 1052 | |
---|
| 1053 | output_data = output[0] |
---|
| 1054 | message = output[1] |
---|
[f721030] | 1055 | # Notify the manager of the new data available |
---|
[f82ab8c] | 1056 | self.communicator.statusBarUpdateSignal.emit(message) |
---|
| 1057 | self.communicator.fileDataReceivedSignal.emit(output_data) |
---|
[a281ab8] | 1058 | self.manager.add_data(data_list=output_data) |
---|
[f721030] | 1059 | |
---|
[7969b9c] | 1060 | def loadFailed(self, reason): |
---|
[7fb471d] | 1061 | print("File Load Failed with:\n", reason) |
---|
| 1062 | pass |
---|
| 1063 | |
---|
[f721030] | 1064 | def updateModel(self, data, p_file): |
---|
| 1065 | """ |
---|
[481ff26] | 1066 | Add data and Info fields to the model item |
---|
[f721030] | 1067 | """ |
---|
| 1068 | # Structure of the model |
---|
| 1069 | # checkbox + basename |
---|
[481ff26] | 1070 | # |-------> Data.D object |
---|
[f721030] | 1071 | # |-------> Info |
---|
| 1072 | # |----> Title: |
---|
| 1073 | # |----> Run: |
---|
| 1074 | # |----> Type: |
---|
| 1075 | # |----> Path: |
---|
| 1076 | # |----> Process |
---|
| 1077 | # |-----> process[0].name |
---|
[28a84e9] | 1078 | # |-------> THEORIES |
---|
[f721030] | 1079 | |
---|
| 1080 | # Top-level item: checkbox with label |
---|
[6a3e1fe] | 1081 | checkbox_item = GuiUtils.HashableStandardItem() |
---|
[f721030] | 1082 | checkbox_item.setCheckable(True) |
---|
| 1083 | checkbox_item.setCheckState(QtCore.Qt.Checked) |
---|
| 1084 | checkbox_item.setText(os.path.basename(p_file)) |
---|
| 1085 | |
---|
| 1086 | # Add the actual Data1D/Data2D object |
---|
[6a3e1fe] | 1087 | object_item = GuiUtils.HashableStandardItem() |
---|
[b3e8629] | 1088 | object_item.setData(data) |
---|
[f721030] | 1089 | |
---|
[488c49d] | 1090 | checkbox_item.setChild(0, object_item) |
---|
| 1091 | |
---|
[f721030] | 1092 | # Add rows for display in the view |
---|
[0cd8612] | 1093 | info_item = GuiUtils.infoFromData(data) |
---|
[f721030] | 1094 | |
---|
[28a84e9] | 1095 | # Set info_item as the first child |
---|
[488c49d] | 1096 | checkbox_item.setChild(1, info_item) |
---|
[f721030] | 1097 | |
---|
[28a84e9] | 1098 | # Caption for the theories |
---|
| 1099 | checkbox_item.setChild(2, QtGui.QStandardItem("THEORIES")) |
---|
| 1100 | |
---|
[f721030] | 1101 | # New row in the model |
---|
[7969b9c] | 1102 | self.model.beginResetModel() |
---|
[f721030] | 1103 | self.model.appendRow(checkbox_item) |
---|
[7969b9c] | 1104 | self.model.endResetModel() |
---|
[481ff26] | 1105 | |
---|
[5032ea68] | 1106 | def updateModelFromPerspective(self, model_item): |
---|
| 1107 | """ |
---|
[a281ab8] | 1108 | Receive an update model item from a perspective |
---|
| 1109 | Make sure it is valid and if so, replace it in the model |
---|
[5032ea68] | 1110 | """ |
---|
[a281ab8] | 1111 | # Assert the correct type |
---|
[0cd8612] | 1112 | if not isinstance(model_item, QtGui.QStandardItem): |
---|
[5032ea68] | 1113 | msg = "Wrong data type returned from calculations." |
---|
[b3e8629] | 1114 | raise AttributeError(msg) |
---|
[a281ab8] | 1115 | |
---|
[1042dba] | 1116 | # TODO: Assert other properties |
---|
[a281ab8] | 1117 | |
---|
[5032ea68] | 1118 | # Reset the view |
---|
[7969b9c] | 1119 | ##self.model.reset() |
---|
[5032ea68] | 1120 | # Pass acting as a debugger anchor |
---|
| 1121 | pass |
---|
[481ff26] | 1122 | |
---|
[5236449] | 1123 | def updateTheoryFromPerspective(self, model_item): |
---|
| 1124 | """ |
---|
| 1125 | Receive an update theory item from a perspective |
---|
| 1126 | Make sure it is valid and if so, replace/add in the model |
---|
| 1127 | """ |
---|
| 1128 | # Assert the correct type |
---|
| 1129 | if not isinstance(model_item, QtGui.QStandardItem): |
---|
| 1130 | msg = "Wrong data type returned from calculations." |
---|
[b3e8629] | 1131 | raise AttributeError(msg) |
---|
[5236449] | 1132 | |
---|
| 1133 | # Check if there are any other items for this tab |
---|
| 1134 | # If so, delete them |
---|
[d6e38661] | 1135 | current_tab_name = model_item.text() |
---|
| 1136 | for current_index in range(self.theory_model.rowCount()): |
---|
[d6b8a1d] | 1137 | #if current_tab_name in self.theory_model.item(current_index).text(): |
---|
[d6e38661] | 1138 | if current_tab_name == self.theory_model.item(current_index).text(): |
---|
| 1139 | return |
---|
| 1140 | self.theory_model.removeRow(current_index) |
---|
| 1141 | break |
---|
[d6b8a1d] | 1142 | |
---|
[d6e38661] | 1143 | # send in the new item |
---|
[5236449] | 1144 | self.theory_model.appendRow(model_item) |
---|
| 1145 | |
---|
[f721030] | 1146 | |
---|
| 1147 | if __name__ == "__main__": |
---|
[7969b9c] | 1148 | app = QtWidgets.QApplication([]) |
---|
[f721030] | 1149 | dlg = DataExplorerWindow() |
---|
| 1150 | dlg.show() |
---|
[481ff26] | 1151 | sys.exit(app.exec_()) |
---|