Changeset 8cb6cd6 in sasview


Ignore:
Timestamp:
Jul 12, 2016 6:44:53 AM (8 years ago)
Author:
Piotr Rozyczko <piotr.rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
e3ae090
Parents:
a95260d
Message:

Plot handler prototype + append plot functionality

Location:
src/sas/qtgui
Files:
2 added
9 edited

Legend:

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

    r0cd8612 r8cb6cd6  
    1313 
    1414# SAS 
    15 import GuiUtils 
    16 from Plotter import Plotter 
    1715from sas.sascalc.dataloader.loader import Loader 
    1816from sas.sasgui.guiframe.data_manager import DataManager 
     
    2018from sas.sasgui.guiframe.dataFitting import Data2D 
    2119 
     20import GuiUtils 
     21import PlotHelper 
     22from Plotter import Plotter 
    2223from DroppableDataLoadWidget import DroppableDataLoadWidget 
    2324 
     
    4647        # Be careful with twisted threads. 
    4748        self.mutex = QMutex() 
     49 
     50        # Active plots 
     51        self.active_plots = [] 
    4852 
    4953        # Connect the buttons 
     
    5458        self.cmdSendTo.clicked.connect(self.sendData) 
    5559        self.cmdNew.clicked.connect(self.newPlot) 
     60        self.cmdAppend.clicked.connect(self.appendPlot) 
    5661        self.cmdHelp.clicked.connect(self.displayHelp) 
    5762        self.cmdHelp_2.clicked.connect(self.displayHelp) 
     
    7681        self.communicator = self.parent.communicator() 
    7782        self.communicator.fileReadSignal.connect(self.loadFromURL) 
     83        self.communicator.activeGraphsSignal.connect(self.updateGraphCombo) 
     84        self.cbgraph.editTextChanged.connect(self.enableGraphCombo) 
     85        self.cbgraph.currentIndexChanged.connect(self.enableGraphCombo) 
    7886 
    7987        # Proxy model for showing a subset of Data1D/Data2D content 
     
    97105        self.freezeView.setModel(self.theory_proxy) 
    98106 
     107        self.enableGraphCombo(None) 
     108 
    99109    def closeEvent(self, event): 
    100110        """ 
     
    110120        self._helpView.load(QtCore.QUrl(_TreeLocation)) 
    111121        self._helpView.show() 
     122 
     123    def enableGraphCombo(self, combo_text): 
     124        """ 
     125        Enables/disables "Assign Plot" elements 
     126        """ 
     127        self.cbgraph.setEnabled(len(PlotHelper.currentPlots()) > 0) 
     128        self.cmdAppend.setEnabled(len(PlotHelper.currentPlots()) > 0) 
    112129 
    113130    def loadFromURL(self, url): 
     
    187204        delete_msg = "This operation will delete the checked data sets and all the dependents." +\ 
    188205                     "\nDo you want to continue?" 
    189         reply = QtGui.QMessageBox.question(self, 'Warning', delete_msg, 
    190                 QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) 
     206        reply = QtGui.QMessageBox.question(self, 
     207                                           'Warning', 
     208                                           delete_msg, 
     209                                           QtGui.QMessageBox.Yes, 
     210                                           QtGui.QMessageBox.No) 
    191211 
    192212        if reply == QtGui.QMessageBox.No: 
     
    263283            if not outer_item: 
    264284                continue 
    265             for inner_index in xrange(outer_item.rowCount()): # Should be just two rows: data and Info 
     285            # Should be just two rows: data and Info 
     286            for inner_index in xrange(outer_item.rowCount()): 
    266287                subitem = outer_item.child(inner_index) 
    267                 if subitem and subitem.isCheckable() and subitem.checkState() == QtCore.Qt.Checked: 
     288                if subitem and \ 
     289                   subitem.isCheckable() and \ 
     290                   subitem.checkState() == QtCore.Qt.Checked: 
    268291                    theories_copied += 1 
    269292                    new_item = self.recursivelyCloneItem(subitem) 
    270293                    # Append a "unique" descriptor to the name 
    271                     time_bit = str(time.time())[7:-1].replace('.','') 
     294                    time_bit = str(time.time())[7:-1].replace('.', '') 
    272295                    new_name = new_item.text() + '_@' + time_bit 
    273296                    new_item.setText(new_name) 
     
    300323        return new_item 
    301324 
     325    def updateGraphCombo(self, graph_list): 
     326        """ 
     327        Modify Graph combo box on graph add/delete 
     328        """ 
     329        orig_text = self.cbgraph.currentText() 
     330        self.cbgraph.clear() 
     331        graph_titles = [] 
     332        for graph in graph_list: 
     333            graph_titles.append("Graph"+str(graph)) 
     334        self.cbgraph.insertItems(0, graph_titles) 
     335        ind = self.cbgraph.findText(orig_text) 
     336        if ind > 0: 
     337            self.cbgraph.setCurrentIndex(ind) 
     338        pass 
     339 
    302340    def newPlot(self): 
    303341        """ 
     
    306344        TODO: Add 2D-functionality 
    307345        """ 
    308  
    309346        plots = GuiUtils.plotsFromCheckedItems(self.model) 
    310347 
    311348        # Call show on requested plots 
    312         new_plot = Plotter() 
     349        new_plot = Plotter(self) 
    313350        for plot_set in plots: 
    314351            new_plot.data(plot_set) 
    315352            new_plot.plot() 
    316353 
     354        # Update the global plot counter 
     355        title = "Graph"+str(PlotHelper.idOfPlot(new_plot)) 
     356        new_plot.setWindowTitle(title) 
     357 
     358        # Add the plot to the workspace 
     359        self.parent.workspace().addWindow(new_plot) 
     360 
     361        # Show the plot 
    317362        new_plot.show() 
     363 
     364        # Update the active chart list 
     365        self.active_plots.append(title) 
     366 
     367    def appendPlot(self): 
     368        """ 
     369        Add data set(s) to the existing matplotlib chart 
     370 
     371        TODO: Add 2D-functionality 
     372        """ 
     373        # new plot data 
     374        new_plots = GuiUtils.plotsFromCheckedItems(self.model) 
     375 
     376        # old plot data 
     377        plot_id = self.cbgraph.currentText() 
     378        plot_id = int(plot_id[5:]) 
     379 
     380        assert plot_id in PlotHelper.currentPlots(), "No such plot: Graph%s"%str(plot_id) 
     381 
     382        old_plot = PlotHelper.plotById(plot_id) 
     383 
     384        # Add new data to the old plot 
     385        for plot_set in new_plots: 
     386            old_plot.data(plot_set) 
     387            old_plot.plot() 
    318388 
    319389    def chooseFiles(self): 
     
    424494                else: 
    425495                    error_message += "%s\n" % str(p_file) 
    426                 info = "error" 
    427496 
    428497            current_percentage = int(100.0* index/number_of_files) 
     
    563632        Post message to status bar and update the data manager 
    564633        """ 
    565         assert type(output) == tuple 
     634        assert isinstance(output, tuple) 
    566635 
    567636        # Reset the model so the view gets updated. 
  • src/sas/qtgui/GuiManager.py

    r0cd8612 r8cb6cd6  
    6363 
    6464        # Fork off logging messages to the Log Window 
    65         XStream.stdout().messageWritten.connect( self.listWidget.insertPlainText  ) 
    66         XStream.stderr().messageWritten.connect( self.listWidget.insertPlainText  ) 
     65        XStream.stdout().messageWritten.connect(self.listWidget.insertPlainText) 
     66        XStream.stderr().messageWritten.connect(self.listWidget.insertPlainText) 
    6767 
    6868        # Log the start of the session 
     
    138138        self.statusLabel = QtGui.QLabel() 
    139139        self.statusLabel.setText("Welcome to SasView") 
    140         self._workspace.statusbar.addPermanentWidget(self.statusLabel,1) 
     140        self._workspace.statusbar.addPermanentWidget(self.statusLabel, 1) 
    141141        self._workspace.statusbar.addPermanentWidget(self.progress, stretch=0) 
    142         self.progress.setRange(0,100) 
     142        self.progress.setRange(0, 100) 
    143143        self.progress.setValue(0) 
    144144        self.progress.setTextVisible(True) 
     
    150150        """ 
    151151        pass 
     152 
     153    def workspace(self): 
     154        """ 
     155        Accessor for the main window workspace 
     156        """ 
     157        return self._workspace.workspace 
    152158 
    153159    def updatePerspective(self, data): 
     
    186192        Update progress bar with the required value (0-100) 
    187193        """ 
    188         assert(-1 <= value <= 100) 
     194        assert -1 <= value <= 100 
    189195        if value == -1: 
    190196            self.progress.setVisible(False) 
     
    253259                             % (content)) 
    254260                version_info = json.loads(content) 
    255             except: 
    256                 logging.info("Failed to connect to www.sasview.org") 
     261            except ValueError, ex: 
     262                logging.info("Failed to connect to www.sasview.org:", ex) 
    257263        self.processVersion(version_info) 
    258264 
  • src/sas/qtgui/GuiUtils.py

    re540cd2 r8cb6cd6  
    217217    progressBarUpdateSignal = QtCore.pyqtSignal(int) 
    218218 
     219    # Workspace charts added/removed 
     220    activeGraphsSignal = QtCore.pyqtSignal(list) 
     221 
    219222 
    220223def updateModelItem(item, update_data, name=""): 
  • src/sas/qtgui/Perspectives/Invariant/InvariantPerspective.py

    ra95260d r8cb6cd6  
    5858        self._helpView = QtWebKit.QWebView() 
    5959        self.detailsDialog = DetailsDialog(self) 
    60         self._plotter = Plotter(self) 
     60        # self._plotter = Plotter(self) 
    6161 
    6262        self._low_extrapolate = False 
     
    159159        """ 
    160160        """ 
     161        self._plotter = Plotter(self) 
    161162        if self._low_extrapolate or self._high_extrapolate: 
    162163            self._plotter.show() 
  • src/sas/qtgui/Plotter.py

    ra95260d r8cb6cd6  
     1import logging 
     2 
    13from PyQt4 import QtGui 
    24 
     
    911import matplotlib.pyplot as plt 
    1012 
     13import PlotHelper 
     14 
    1115class Plotter(QtGui.QDialog): 
    1216    def __init__(self, parent=None): 
    1317        super(Plotter, self).__init__(parent) 
     18 
     19        # Required for the communicator 
     20        self.parent = parent 
    1421 
    1522        # a figure instance to plot on 
     
    3946        self._ax = self.figure.add_subplot(self._current_plot) 
    4047 
     48        # Notify the helper 
     49        PlotHelper.addPlot(self) 
     50        # Notify the listeners 
     51        self.parent.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots()) 
    4152 
    4253    def data(self, data=None): 
    43         """ 
    44         """ 
     54        """ data setter """ 
    4555        self._data = data 
    4656 
    4757    def title(self, title=""): 
    48         """ 
    49         """ 
     58        """ title setter """ 
    5059        self._title = title 
    5160 
    5261    def id(self, id=""): 
    53         """ 
    54         """ 
     62        """ id setter """ 
    5563        self._id = id 
    5664 
    5765    def x_label(self, xlabel=""): 
    58         """ 
    59         """ 
     66        """ x-label setter """ 
    6067        self._xlabel = xlabel 
    6168 
    6269    def y_label(self, ylabel=""): 
    63         """ 
    64         """ 
     70        """ y-label setter """ 
    6571        self._ylabel = ylabel 
    6672 
    6773    def clean(self): 
    6874        """ 
     75        Redraw the graph 
    6976        """ 
    7077        self.figure.delaxes(self._ax) 
     
    7380    def plot(self): 
    7481        """ 
    75         plot self._data 
     82        Plot self._data 
    7683        """ 
    7784        # create an axis 
     
    9299        # refresh canvas 
    93100        self.canvas.draw() 
     101 
     102    def closeEvent(self, event): 
     103        """ 
     104        Overwrite the close event adding helper notification 
     105        """ 
     106        # Please remove me from your database. 
     107        PlotHelper.deletePlot(PlotHelper.idOfPlot(self)) 
     108        # Notify the listeners 
     109        self.parent.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots()) 
     110        event.accept() 
     111 
  • src/sas/qtgui/UI/DataExplorerUI.py

    re540cd2 r8cb6cd6  
    131131        self.cbgraph.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) 
    132132        self.cbgraph.setObjectName(_fromUtf8("cbgraph")) 
    133         self.cbgraph.addItem(_fromUtf8("")) 
    134133        self.gridLayout_3.addWidget(self.cbgraph, 1, 1, 1, 1) 
    135134        self.gridLayout_2.addWidget(self.groupBox_3, 0, 0, 1, 1) 
     
    227226        self.cmdNew.setText(_translate("DataLoadWidget", "Create New", None)) 
    228227        self.cmdAppend.setText(_translate("DataLoadWidget", "Append to", None)) 
    229         self.cbgraph.setItemText(0, _translate("DataLoadWidget", "Graph1", None)) 
    230228        self.cmdHelp.setText(_translate("DataLoadWidget", "Help", None)) 
    231229        DataLoadWidget.setTabText(DataLoadWidget.indexOf(self.dataTab), _translate("DataLoadWidget", "Data", None)) 
  • src/sas/qtgui/UI/DataExplorerUI.ui

    re540cd2 r8cb6cd6  
    252252            <enum>QComboBox::AdjustToContents</enum> 
    253253           </property> 
    254            <item> 
    255             <property name="text"> 
    256              <string>Graph1</string> 
    257             </property> 
    258            </item> 
    259254          </widget> 
    260255         </item> 
  • src/sas/qtgui/UnitTesting/DataExplorerTest.py

    r0cd8612 r8cb6cd6  
    1717from UnitTesting.TestUtils import QtSignalSpy 
    1818from Plotter import Plotter 
     19import PlotHelper 
    1920 
    2021app = QApplication(sys.argv) 
     
    3940            def perspective(self): 
    4041                return MyPerspective() 
     42            def workspace(self): 
     43                return None 
    4144 
    4245        self.form = DataExplorerWindow(None, dummy_manager()) 
     
    505508        loader = Loader() 
    506509        manager = DataManager() 
     510        PlotHelper.clear() 
     511        self.form.enableGraphCombo(None) 
     512 
     513        # Make sure the controls are disabled 
     514        self.assertFalse(self.form.cbgraph.isEnabled()) 
     515        self.assertFalse(self.form.cmdAppend.isEnabled()) 
    507516 
    508517        # get Data1D 
     
    517526        self.form.plotsFromCheckedItems = MagicMock(return_value=new_data) 
    518527 
     528        # Mask plotting 
     529        self.form.parent.workspace = MagicMock() 
     530 
    519531        # Call the plotting method 
    520532        self.form.newPlot() 
     
    522534        # The plot was displayed 
    523535        self.assertTrue(Plotter.show.called) 
     536 
     537        # The plot was registered 
     538        self.assertEqual(len(PlotHelper.currentPlots()), 1) 
     539 
     540        self.assertTrue(self.form.cbgraph.isEnabled()) 
     541        self.assertTrue(self.form.cmdAppend.isEnabled()) 
     542 
     543    def testAppendPlot(self): 
     544        """ 
     545        Creating new plots from Data1D/2D 
     546        """ 
     547        loader = Loader() 
     548        manager = DataManager() 
     549 
     550        PlotHelper.clear() 
     551        self.form.enableGraphCombo(None) 
     552 
     553        # Make sure the controls are disabled 
     554        self.assertFalse(self.form.cbgraph.isEnabled()) 
     555        self.assertFalse(self.form.cmdAppend.isEnabled()) 
     556 
     557        # get Data1D 
     558        p_file="cyl_400_20.txt" 
     559        output_object = loader.load(p_file) 
     560        new_data = [manager.create_gui_data(output_object, p_file)] 
     561 
     562        # Mask plotting 
     563        self.form.parent.workspace = MagicMock() 
     564 
     565        # Mask the plot show call 
     566        Plotter.show = MagicMock() 
     567 
     568        # Mask retrieval of the data 
     569        self.form.plotsFromCheckedItems = MagicMock(return_value=new_data) 
     570 
     571        # Call the plotting method 
     572        self.form.newPlot() 
     573 
     574        # Call the plotting method again, so we have 2 graphs 
     575        self.form.newPlot() 
     576 
     577        # See that we have two plots 
     578        self.assertEqual(len(PlotHelper.currentPlots()), 2) 
     579 
     580        # Add data to plot #1 
     581        self.form.cbgraph.setCurrentIndex(1) 
     582        self.form.appendPlot() 
     583 
     584        # See that we still have two plots 
     585        self.assertEqual(len(PlotHelper.currentPlots()), 2) 
     586 
     587    def testUpdateGraphCombo(self): 
     588        """ 
     589        Test the combo box update 
     590        """ 
     591        PlotHelper.clear() 
     592 
     593        graph_list=[1,2,3] 
     594        self.form.updateGraphCombo(graph_list) 
     595 
     596        self.assertEqual(self.form.cbgraph.count(), 3) 
     597        self.assertEqual(self.form.cbgraph.currentText(), 'Graph1') 
     598 
     599        graph_list=[] 
     600        self.form.updateGraphCombo(graph_list) 
     601        self.assertEqual(self.form.cbgraph.count(), 0) 
    524602 
    525603    def testUpdateModelFromPerspective(self): 
  • src/sas/qtgui/run_tests.sh

    r0cd8612 r8cb6cd6  
    88python -m UnitTesting.DroppableDataLoadWidgetTest 
    99python -m UnitTesting.SasviewLoggerTest 
     10python -m UnitTesting.PlotHelperTest 
Note: See TracChangeset for help on using the changeset viewer.