Changeset 5d89f43 in sasview for src


Ignore:
Timestamp:
Jan 25, 2017 8:51:26 AM (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:
3bdbfcc
Parents:
03c372d
Message:

Code review for ColorMap?

Location:
src/sas/qtgui
Files:
1 added
4 edited

Legend:

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

    • Property mode changed from 100755 to 100644
    r03c372d r5d89f43  
    1313from sas.sasgui.guiframe.dataFitting import Data2D 
    1414from sas.qtgui.GuiUtils import formatNumber 
     15from rangeSlider import RangeSlider 
    1516 
    1617DEFAULT_MAP = 'jet' 
     
    2021 
    2122class ColorMap(QtGui.QDialog, Ui_ColorMapUI): 
     23    apply_signal = QtCore.pyqtSignal(tuple, str) 
    2224    def __init__(self, parent=None, cmap=None, vmin=0.0, vmax=100.0, data=None): 
    2325        super(ColorMap, self).__init__() 
     
    3234        self.rmaps = sorted(set(self.all_maps) - set(self.maps)) 
    3335 
    34         self.vmin = vmin 
    35         self.vmax = vmax 
     36        self.vmin = self.vmin_orig = vmin 
     37        self.vmax = self.vmax_orig = vmax 
    3638 
    3739        # Initialize detector labels 
     
    4042        # Initialize the combo box 
    4143        self.initMapCombobox() 
     44 
     45        self.initRangeSlider() 
    4246 
    4347        # Add the color map component 
     
    5256        self.txtMaxAmplitude.setValidator(validator_max) 
    5357 
     58        # Set the initial amplitudes 
     59        self.txtMinAmplitude.setText(formatNumber(self.vmin)) 
     60        self.txtMaxAmplitude.setText(formatNumber(self.vmax)) 
     61 
    5462        # Enforce constant size on the widget 
    5563        self.setFixedSize(self.minimumSizeHint()) 
     
    6169        self.chkReverse.stateChanged.connect(self.onColorMapReversed) 
    6270 
    63         # Handle the reset button click 
     71        # Handle the Reset button click 
    6472        self.buttonBox.button(QtGui.QDialogButtonBox.Reset).clicked.connect(self.onReset) 
     73 
     74        # Handle the Apply button click 
     75        self.buttonBox.button(QtGui.QDialogButtonBox.Apply).clicked.connect(self.onApply) 
    6576 
    6677        # Handle the amplitude setup 
     
    8697        # Go back to original settings 
    8798        self._cmap = self._cmap_orig 
     99        self.vmin = self.vmin_orig 
     100        self.vmax = self.vmax_orig 
    88101        self._norm = mpl.colors.Normalize(vmin=self.vmin, vmax=self.vmax) 
    89         self.txtMaxAmplitude.clear() 
    90         self.txtMinAmplitude.clear() 
     102        self.txtMinAmplitude.setText(formatNumber(self.vmin)) 
     103        self.txtMaxAmplitude.setText(formatNumber(self.vmax)) 
    91104        self.initMapCombobox() 
     105        self.slider.setMinimum(self.vmin) 
     106        self.slider.setMaximum(self.vmax) 
     107        self.slider.setLowValue(self.vmin) 
     108        self.slider.setHighValue(self.vmax) 
    92109        # Redraw the widget 
    93110        self.redrawColorBar() 
    94111        self.canvas.draw() 
     112 
     113    def onApply(self): 
     114        """ 
     115        Respond to the Apply button click. 
     116        Send a signal to the plotter with vmin/vmax/cmap for chart update 
     117        """ 
     118        self.apply_signal.emit(self.norm(), self.cmap()) 
    95119 
    96120    def initDetectorData(self): 
     
    125149        self.cbColorMap.setCurrentIndex(self.cbColorMap.findText(self._cmap)) 
    126150 
     151    def initRangeSlider(self): 
     152        """ 
     153        Create and display the double slider for data range mapping. 
     154        """ 
     155        self.slider = RangeSlider() 
     156        self.slider.setMinimum(self.vmin) 
     157        self.slider.setMaximum(self.vmax) 
     158        self.slider.setLowValue(self.vmin) 
     159        self.slider.setHighValue(self.vmax) 
     160        self.slider.setOrientation(QtCore.Qt.Horizontal) 
     161 
     162        self.slider_label = QtGui.QLabel() 
     163        self.slider_label.setText("Drag the sliders to adjust color range.") 
     164 
     165        def set_vmin(value): 
     166            self.vmin = value 
     167            self.txtMinAmplitude.setText(str(value)) 
     168            self.updateMap() 
     169        def set_vmax(value): 
     170            self.vmax = value 
     171            self.txtMaxAmplitude.setText(str(value)) 
     172            self.updateMap() 
     173 
     174        self.slider.lowValueChanged.connect(set_vmin) 
     175        self.slider.highValueChanged.connect(set_vmin) 
     176 
     177    def updateMap(self): 
     178        self._norm = mpl.colors.Normalize(vmin=self.vmin, vmax=self.vmax) 
     179        self.redrawColorBar() 
     180        self.canvas.draw() 
     181 
    127182    def initColorMap(self): 
    128183        """ 
     
    131186        self.fig = mpl.figure.Figure(figsize=(4, 1)) 
    132187        self.ax1 = self.fig.add_axes([0.05, 0.65, 0.9, 0.15]) 
     188 
    133189        self._norm = mpl.colors.Normalize(vmin=self.vmin, vmax=self.vmax) 
    134190        self.redrawColorBar() 
    135191        self.canvas = FigureCanvas(self.fig) 
     192 
    136193        layout = QtGui.QVBoxLayout() 
     194        layout.addWidget(self.slider_label) 
     195        layout.addWidget(self.slider) 
    137196        layout.addWidget(self.canvas) 
     197 
    138198        self.widget.setLayout(layout) 
    139199 
     
    154214                                            norm=self._norm, 
    155215                                            orientation='horizontal') 
    156         self.cb.set_label('Detector Colors') 
     216        self.cb.set_label('Color map range') 
    157217 
    158218    def onColorMapReversed(self, isChecked): 
     
    177237 
    178238        self._cmap = new_map 
    179         # Clearning the content of the combobox. 
     239        # Clear the content of the combobox. 
    180240        # Needs signal blocking, or else onMapIndexChange() spoils it all 
    181241        self.cbColorMap.blockSignals(True) 
  • src/sas/qtgui/Plotter2D.py

    r03c372d r5d89f43  
    2929        # Default scale 
    3030        self.scale = 'log_{10}' 
     31        self.vmin = None 
     32        self.vmax = None 
    3133 
    3234    @property 
     
    208210                                    data=self.data) 
    209211 
     212        color_map_dialog.apply_signal.connect(self.onApplyMap) 
     213 
    210214        if color_map_dialog.exec_() == QtGui.QDialog.Accepted: 
    211             self.cmap = color_map_dialog.cmap() 
    212             self.vmin, self.vmax = color_map_dialog.norm() 
    213             # Redraw the chart with new cmap 
    214             self.plot() 
    215         pass 
     215            self.onApplyMap(color_map_dialog.norm(), color_map_dialog.cmap()) 
     216 
     217    def onApplyMap(self, v_values, cmap): 
     218        """ 
     219        Update the chart color map based on data passed from the widget 
     220        """ 
     221        self.cmap = str(cmap) 
     222        self.vmin, self.vmax = v_values 
     223        # Redraw the chart with new cmap 
     224        self.plot() 
    216225 
    217226    def showPlot(self, data, qx_data, qy_data, xmin, xmax, ymin, ymax, 
     
    261270            self.figure.subplots_adjust(left=0.2, right=.8, bottom=.2) 
    262271 
     272            zmax_temp = self.zmax 
     273            if self.vmin is not None: 
     274                zmin_temp = self.vmin 
     275                zmax_temp = self.vmax 
     276 
    263277            im = self.ax.imshow(output, interpolation='nearest', 
    264278                                origin='lower', 
    265                                 vmin=zmin_temp, vmax=self.zmax, 
     279                                vmin=zmin_temp, vmax=zmax_temp, 
    266280                                cmap=self.cmap, 
    267281                                extent=(self.xmin, self.xmax, 
     
    301315 
    302316            ax = Axes3D(self.figure) 
    303             #cbax = self.figure.add_axes([0.84, 0.1, 0.02, 0.8]) 
    304317 
    305318            # Disable rotation for large sets. 
  • src/sas/qtgui/UI/ColorMapUI.ui

    r092a3d9 r5d89f43  
    5656       </property> 
    5757       <property name="standardButtons"> 
    58         <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set> 
     58        <set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set> 
    5959       </property> 
    6060      </widget> 
     
    147147        <item row="0" column="0"> 
    148148         <widget class="QLabel" name="label_4"> 
     149          <property name="toolTip"> 
     150           <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Detector width.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> 
     151          </property> 
    149152          <property name="text"> 
    150153           <string>Width in pixels</string> 
     
    161164        <item row="1" column="0"> 
    162165         <widget class="QLabel" name="label_5"> 
     166          <property name="toolTip"> 
     167           <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Detector height.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> 
     168          </property> 
    163169          <property name="text"> 
    164170           <string>Height in pixels</string> 
     
    175181        <item row="2" column="0"> 
    176182         <widget class="QLabel" name="label_6"> 
    177           <property name="text"> 
    178            <string>Q max</string> 
     183          <property name="toolTip"> 
     184           <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Maximum absolute Q&lt;span style=&quot; vertical-align:sub;&quot;&gt;x,y&lt;/span&gt; value.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> 
     185          </property> 
     186          <property name="text"> 
     187           <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Q&lt;span style=&quot; vertical-align:sub;&quot;&gt;max&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> 
    179188          </property> 
    180189         </widget> 
     
    189198        <item row="3" column="0"> 
    190199         <widget class="QLabel" name="label_7"> 
     200          <property name="toolTip"> 
     201           <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Minimum value of Q&lt;span style=&quot; vertical-align:sub;&quot;&gt;x&lt;/span&gt; in units of Q.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> 
     202          </property> 
    191203          <property name="text"> 
    192204           <string>Beam stop radius</string> 
  • src/sas/qtgui/UnitTesting/ColorMapTest.py

    r03c372d r5d89f43  
    1313import sas.qtgui.Plotter2D as Plotter2D 
    1414from UnitTesting.TestUtils import WarningTestNotImplemented 
     15from UnitTesting.TestUtils import QtSignalSpy 
    1516 
    1617# Local 
     
    6566        self.assertIsInstance(self.widget.txtMaxAmplitude.validator(), QtGui.QDoubleValidator) 
    6667 
     68        # Ranges 
     69        self.assertEqual(self.widget.txtMinAmplitude.text(), "0") 
     70        self.assertEqual(self.widget.txtMaxAmplitude.text(), "100") 
     71        self.assertIsInstance(self.widget.slider, QtGui.QSlider) 
     72 
    6773    def testOnReset(self): 
    6874        '''Check the dialog reset function''' 
     
    7884        self.assertEqual(self.widget.cbColorMap.currentIndex(), 20) 
    7985        self.assertFalse(self.widget.chkReverse.isChecked()) 
    80         self.assertEqual(self.widget.txtMinAmplitude.text(), "") 
     86        self.assertEqual(self.widget.txtMinAmplitude.text(), "0") 
     87 
     88    def testOnApply(self): 
     89        '''Check the dialog apply function''' 
     90        # Set some controls to non-default state 
     91        self.widget.show() 
     92        self.widget.cbColorMap.setCurrentIndex(20) # PuRd_r 
     93        self.widget.chkReverse.setChecked(True) 
     94        self.widget.txtMinAmplitude.setText("20.0") 
     95 
     96        spy_apply = QtSignalSpy(self.widget, self.widget.apply_signal) 
     97        # Reset the widget state 
     98        self.widget.onApply() 
     99 
     100        # Assure the widget is still up and the signal was sent. 
     101        self.assertTrue(self.widget.isVisible()) 
     102        self.assertEqual(spy_apply.count(), 1) 
     103        self.assertIn('PuRd_r', spy_apply.called()[0]['args'][1]) 
    81104 
    82105    def testInitMapCombobox(self): 
     
    97120        self.assertTrue(self.widget.chkReverse.isChecked()) 
    98121 
     122    def testInitRangeSlider(self): 
     123        '''Test the range slider initializer''' 
     124        # Set a color map from the direct list 
     125        self.widget._cmap = "gnuplot" 
     126        self.widget.initRangeSlider() 
     127 
     128        # Check the values 
     129        self.assertEqual(self.widget.slider.minimum(), 0) 
     130        self.assertEqual(self.widget.slider.maximum(), 100) 
     131        self.assertEqual(self.widget.slider.orientation(), 1) 
     132 
     133        # Emit new low value 
     134        self.widget.slider.lowValueChanged.emit(5) 
     135        # Assure the widget received changes 
     136        self.assertEqual(self.widget.txtMinAmplitude.text(), "5") 
     137 
     138        # Emit new high value 
     139        self.widget.slider.highValueChanged.emit(45) 
     140        # Assure the widget received changes 
     141        self.assertEqual(self.widget.txtMinAmplitude.text(), "45") 
    99142 
    100143    def testOnMapIndexChange(self): 
Note: See TracChangeset for help on using the changeset viewer.