Changeset 9290b1a in sasview for src/sas/qtgui/Plotter.py


Ignore:
Timestamp:
Dec 16, 2016 12:43:18 PM (7 years ago)
Author:
Piotr Rozyczko <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:
d3ca363
Parents:
0ba0774
Message:

Added AddText? to plot, enabled legend drag - SASVIEW-378

File:
1 edited

Legend:

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

    r27313b7 r9290b1a  
    22 
    33import matplotlib.pyplot as plt 
    4  
     4from matplotlib.font_manager import FontProperties 
     5 
     6from sas.sasgui.guiframe.dataFitting import Data1D 
    57from sas.sasgui.plottools import transform 
    68from sas.sasgui.plottools.convert_units import convert_unit 
    79from sas.qtgui.PlotterBase import PlotterBase 
     10from sas.qtgui.AddText import AddText 
    811 
    912class PlotterWidget(PlotterBase): 
     
    2730        self.title(title=value.title) 
    2831 
    29     def plot(self, marker=None, linestyle=None, hide_error=False): 
     32    def plot(self, data=None, marker=None, linestyle=None, hide_error=False): 
    3033        """ 
    3134        Plot self._data 
    3235        """ 
     36        # Data1D 
     37        if isinstance(data, Data1D): 
     38            self.data = data 
     39        assert(self._data) 
     40 
    3341        # Shortcut for an axis 
    3442        ax = self.ax 
     
    4553                    marker=marker, 
    4654                    linestyle=linestyle, 
    47                     label=self._title) 
     55                    label=self._title, 
     56                    picker=True) 
    4857        else: 
    4958            ax.errorbar(self._data.view.x, self._data.view.y, 
     
    5463                        lolims=False, uplims=False, 
    5564                        xlolims=False, xuplims=False, 
    56                         label=self._title) 
     65                        label=self._title, 
     66                        picker=True) 
    5767 
    5868        # Now add the legend with some customizations. 
    59         legend = ax.legend(loc='upper right', shadow=True) 
     69        self.legend = ax.legend(loc='upper right', shadow=True) 
     70        #self.legend.get_frame().set_alpha(0.4) 
     71        self.legend.set_picker(True) 
    6072 
    6173        # Current labels for axes 
     
    142154        Show a dialog allowing adding custom text to the chart 
    143155        """ 
    144         print("onAddText") 
    145         pass 
     156        self.addText = AddText(self) 
     157        if self.addText.exec_() == QtGui.QDialog.Accepted: 
     158            # Retrieve the new text, its font and color 
     159            extra_text = self.addText.text() 
     160            extra_font = self.addText.font() 
     161            extra_color = self.addText.color() 
     162 
     163            # Place the text on the screen at (0,0) 
     164            pos_x = self.x_click 
     165            pos_y = self.y_click 
     166 
     167            # Map QFont onto MPL font 
     168            mpl_font = FontProperties() 
     169            mpl_font.set_size(int(extra_font.pointSize())) 
     170            mpl_font.set_family(str(extra_font.family())) 
     171            mpl_font.set_weight(int(extra_font.weight())) 
     172            # MPL style names 
     173            styles = ['normal', 'italic', 'oblique'] 
     174            # QFont::Style maps directly onto the above 
     175            try: 
     176                mpl_font.set_style(styles[extra_font.style()]) 
     177            except: 
     178                pass 
     179 
     180            if len(extra_text) > 0: 
     181                new_text = self.ax.text(str(pos_x), 
     182                                        str(pos_y), 
     183                                        extra_text, 
     184                                        color=extra_color, 
     185                                        fontproperties=mpl_font) 
     186                # Update the list of annotations 
     187                self.textList.append(new_text) 
     188                self.canvas.draw_idle() 
    146189 
    147190    def onRemoveText(self): 
    148191        """ 
    149         Remove the most recent added text 
    150         """ 
    151         print("onRemoveText") 
    152         pass 
     192        Remove the most recently added text 
     193        """ 
     194        num_text = len(self.textList) 
     195        if num_text < 1: 
     196            return 
     197        txt = self.textList[num_text - 1] 
     198        text_remove = txt.get_text() 
     199        txt.remove() 
     200        self.textList.remove(txt) 
     201 
     202        self.canvas.draw_idle() 
    153203 
    154204    def onSetGraphRange(self): 
     
    172222        # Clear the plot first 
    173223        plt.cla() 
     224        self.ax.cla() 
    174225 
    175226        # Changing the scale might be incompatible with 
Note: See TracChangeset for help on using the changeset viewer.