Changeset d9150d8 in sasview for src/sas/qtgui/MainWindow


Ignore:
Timestamp:
Jul 10, 2018 6:03:11 AM (6 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
4dd5766
Parents:
515c23df
git-author:
Piotr Rozyczko <rozyczko@…> (07/10/18 05:57:36)
git-committer:
Piotr Rozyczko <rozyczko@…> (07/10/18 06:03:11)
Message:

Delete open plots on data removal SASVIEW-958

Location:
src/sas/qtgui/MainWindow
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/MainWindow/DataExplorer.py

    r515c23df rd9150d8  
    5656        self.mutex = QtCore.QMutex() 
    5757 
    58         # Active plots 
     58        # Plot widgets {name:widget}, required to keep track of plots shown as MDI subwindows 
     59        self.plot_widgets = {} 
     60 
     61        # Active plots {id:Plotter1D/2D}, required to keep track of currently displayed plots 
    5962        self.active_plots = {} 
    6063 
     
    554557                if not 'new_plot' in locals(): 
    555558                    new_plot = Plotter(self) 
     559                    new_plot.item = item 
    556560                new_plot.plot(plot_set) 
    557561                # active_plots may contain multiple charts 
     
    592596 
    593597        # Add the plot to the workspace 
    594         self.parent.workspace().addSubWindow(new_plot) 
     598        plot_widget = self.parent.workspace().addSubWindow(new_plot) 
    595599 
    596600        # Show the plot 
    597601        new_plot.show() 
    598602        new_plot.canvas.draw() 
     603 
     604        # Update the plot widgets dict 
     605        self.plot_widgets[title]=plot_widget 
    599606 
    600607        # Update the active chart list 
     
    10261033            return 
    10271034 
    1028         proxy = self.current_view.model() 
    1029         model = proxy.sourceModel() 
    1030  
    1031         deleted_items = [] 
    1032         deleted_names = [] 
    1033  
    10341035        # Every time a row is removed, the indices change, so we'll just remove 
    10351036        # rows and keep calling selectedIndexes until it returns an empty list. 
    10361037        indices = self.current_view.selectedIndexes() 
     1038 
     1039        proxy = self.current_view.model() 
     1040        model = proxy.sourceModel() 
     1041 
     1042        deleted_items = [] 
     1043        deleted_names = [] 
    10371044 
    10381045        while len(indices) > 0: 
     
    10461053                deleted_names.append(item_to_delete.text()) 
    10471054                deleted_items.append(item_to_delete) 
     1055 
     1056                # Delete corresponding open plots 
     1057                self.closePlotsForItem(item_to_delete) 
    10481058 
    10491059                if item_to_delete.parent(): 
     
    10601070        # update stored_data 
    10611071        self.manager.update_stored_data(deleted_names) 
     1072 
     1073    def closePlotsForItem(self, item): 
     1074        """ 
     1075        Given standard item, close all its currently displayed plots 
     1076        """ 
     1077        # item - HashableStandardItems of active plots 
     1078 
     1079        # {} -> 'Graph1' : HashableStandardItem() 
     1080        current_plot_items = {} 
     1081        for plot_name in PlotHelper.currentPlots(): 
     1082            current_plot_items[plot_name] = PlotHelper.plotById(plot_name).item 
     1083 
     1084        # item and its hashable children 
     1085        items_being_deleted = [] 
     1086        if item.rowCount() > 0: 
     1087            items_being_deleted = [item.child(n) for n in range(item.rowCount()) 
     1088                                   if isinstance(item.child(n), GuiUtils.HashableStandardItem)] 
     1089        items_being_deleted.append(item) 
     1090        # Add the parent in case a child is selected 
     1091        if isinstance(item.parent(), GuiUtils.HashableStandardItem): 
     1092            items_being_deleted.append(item.parent()) 
     1093 
     1094        # Compare plot items and items to delete 
     1095        plots_to_close = set(current_plot_items.values()) & set(items_being_deleted) 
     1096 
     1097        for plot_item in plots_to_close: 
     1098            for plot_name in current_plot_items.keys(): 
     1099                if plot_item == current_plot_items[plot_name]: 
     1100                    plotter = PlotHelper.plotById(plot_name) 
     1101                    # try to delete the plot 
     1102                    try: 
     1103                        plotter.close() 
     1104                        #self.parent.workspace().removeSubWindow(plotter) 
     1105                        self.plot_widgets[plot_name].close() 
     1106                        self.plot_widgets.pop(plot_name, None) 
     1107                    except AttributeError as ex: 
     1108                        logging.error("Closing of %s failed:\n %s" % (plot_name, str(ex))) 
     1109 
     1110        pass # debugger anchor 
    10621111 
    10631112    def onAnalysisUpdate(self, new_perspective=""): 
     
    11741223        self.theory_model.appendRow(model_item) 
    11751224 
    1176  
    1177 if __name__ == "__main__": 
    1178     app = QtWidgets.QApplication([]) 
    1179     dlg = DataExplorerWindow() 
    1180     dlg.show() 
    1181     sys.exit(app.exec_()) 
  • src/sas/qtgui/MainWindow/UnitTesting/DataExplorerTest.py

    r80468f6 rd9150d8  
    6767        # Buttons - data tab 
    6868        self.assertEqual(self.form.cmdLoad.text(), "Load data") 
    69         self.assertEqual(self.form.cmdDeleteData.text(), "Delete") 
     69        self.assertEqual(self.form.cmdDeleteData.text(), "Delete Data") 
    7070        self.assertEqual(self.form.cmdDeleteTheory.text(), "Delete") 
    7171        self.assertEqual(self.form.cmdFreeze.text(), "Freeze Theory") 
     
    819819        pass 
    820820 
    821     def notestDeleteItem(self): 
     821    def testDeleteItem(self): 
    822822        """ 
    823823        Delete selected item from data explorer 
     
    876876        self.assertEqual(self.form.model.rowCount(), 3) 
    877877 
     878    def testClosePlotsForItem(self): 
     879        """ 
     880        Delete selected item from data explorer should also delete corresponding plots 
     881        """ 
     882        # Mock the confirmation dialog with return=No 
     883        QMessageBox.question = MagicMock(return_value=QMessageBox.No) 
     884 
     885        loader = Loader() 
     886        manager = DataManager() 
     887        PlotHelper.clear() 
     888        self.form.enableGraphCombo(None) 
     889 
     890        # Make sure the controls are disabled 
     891        self.assertFalse(self.form.cbgraph.isEnabled()) 
     892        self.assertFalse(self.form.cmdAppend.isEnabled()) 
     893 
     894        # Populate the model 
     895        filename = ["cyl_400_20.txt"] 
     896        self.form.readData(filename) 
     897 
     898        # Mask plotting 
     899        self.form.parent.workspace = MagicMock() 
     900 
     901        # Call the plotting method 
     902        self.form.newPlot() 
     903 
     904        time.sleep(1) 
     905        QApplication.processEvents() 
     906 
     907        # The plot was registered 
     908        self.assertEqual(len(PlotHelper.currentPlots()), 1) 
     909        self.assertEqual(len(self.form.plot_widgets), 1) 
     910        self.assertEqual(list(self.form.plot_widgets.keys()), ['Graph3']) 
     911 
     912        # data index 
     913        model_item = self.form.model.item(0,0) 
     914 
     915        # Call the method 
     916        self.form.closePlotsForItem(model_item) 
     917 
     918        # See that no plot remained 
     919        self.assertEqual(len(PlotHelper.currentPlots()), 0) 
     920        self.assertEqual(len(self.form.plot_widgets), 0) 
     921 
    878922 
    879923if __name__ == "__main__": 
Note: See TracChangeset for help on using the changeset viewer.