diff options
Diffstat (limited to 'silx/gui/plot/PlotTools.py')
-rw-r--r-- | silx/gui/plot/PlotTools.py | 152 |
1 files changed, 75 insertions, 77 deletions
diff --git a/silx/gui/plot/PlotTools.py b/silx/gui/plot/PlotTools.py index 7158d0e..85dcc31 100644 --- a/silx/gui/plot/PlotTools.py +++ b/silx/gui/plot/PlotTools.py @@ -29,7 +29,7 @@ from __future__ import division __authors__ = ["V.A. Sole", "T. Vincent"] __license__ = "MIT" -__date__ = "03/03/2017" +__date__ = "02/10/2017" import logging @@ -40,9 +40,9 @@ import weakref import numpy from .. import qt +from silx.gui.widgets.FloatEdit import FloatEdit _logger = logging.getLogger(__name__) -_logger.setLevel(logging.DEBUG) # PositionInfo ################################################################ @@ -150,56 +150,70 @@ class PositionInfo(qt.QWidget): :param dict event: Plot event """ if event['event'] == 'mouseMoved': - x, y = event['x'], event['y'] # Position in data - styleSheet = "color: rgb(0, 0, 0);" # Default style - - if self.autoSnapToActiveCurve and self.plot.getGraphCursor(): - # Check if near active curve with symbols. - - styleSheet = "color: rgb(255, 0, 0);" # Style far from curve - - activeCurve = self.plot.getActiveCurve() - if activeCurve: - xData = activeCurve.getXData(copy=False) - yData = activeCurve.getYData(copy=False) - if activeCurve.getSymbol(): # Only handled if symbols on curve - closestIndex = numpy.argmin( - pow(xData - x, 2) + pow(yData - y, 2)) - - xClosest = xData[closestIndex] - yClosest = yData[closestIndex] - - closestInPixels = self.plot.dataToPixel( - xClosest, yClosest, axis=activeCurve.getYAxis()) - if closestInPixels is not None: - xPixel, yPixel = event['xpixel'], event['ypixel'] - - if (abs(closestInPixels[0] - xPixel) < 5 and - abs(closestInPixels[1] - yPixel) < 5): - # Update label style sheet - styleSheet = "color: rgb(0, 0, 0);" - - # if close enough, wrap to data point coords - x, y = xClosest, yClosest - - for label, name, func in self._fields: - label.setStyleSheet(styleSheet) - - try: - value = func(x, y) - except: - label.setText('Error') - _logger.error( - "Error while converting coordinates (%f, %f)" - "with converter '%s'" % (x, y, name)) - _logger.error(traceback.format_exc()) - else: - if isinstance(value, numbers.Real): - value = '%.7g' % value # Use this for floats and int - else: - value = str(value) # Fallback for other types - label.setText(value) + x, y = event['x'], event['y'] + self._updateStatusBar(x, y) + def _updateStatusBar(self, x, y): + """Update information from the status bar using the definitions. + + :param float x: Position-x in data + :param float y: Position-y in data + """ + styleSheet = "color: rgb(0, 0, 0);" # Default style + + if self.autoSnapToActiveCurve and self.plot.getGraphCursor(): + # Check if near active curve with symbols. + + styleSheet = "color: rgb(255, 0, 0);" # Style far from curve + + activeCurve = self.plot.getActiveCurve() + if activeCurve: + xData = activeCurve.getXData(copy=False) + yData = activeCurve.getYData(copy=False) + if activeCurve.getSymbol(): # Only handled if symbols on curve + closestIndex = numpy.argmin( + pow(xData - x, 2) + pow(yData - y, 2)) + + xClosest = xData[closestIndex] + yClosest = yData[closestIndex] + + closestInPixels = self.plot.dataToPixel( + xClosest, yClosest, axis=activeCurve.getYAxis()) + if closestInPixels is not None: + xPixel, yPixel = event['xpixel'], event['ypixel'] + + if (abs(closestInPixels[0] - xPixel) < 5 and + abs(closestInPixels[1] - yPixel) < 5): + # Update label style sheet + styleSheet = "color: rgb(0, 0, 0);" + + # if close enough, wrap to data point coords + x, y = xClosest, yClosest + + for label, name, func in self._fields: + label.setStyleSheet(styleSheet) + + try: + value = func(x, y) + text = self.valueToString(value) + label.setText(text) + except: + label.setText('Error') + _logger.error( + "Error while converting coordinates (%f, %f)" + "with converter '%s'" % (x, y, name)) + _logger.error(traceback.format_exc()) + + def valueToString(self, value): + if isinstance(value, (tuple, list)): + value = [self.valueToString(v) for v in value] + return ", ".join(value) + elif isinstance(value, numbers.Real): + # Use this for floats and int + return '%.7g' % value + else: + # Fallback for other types + return str(value) # LimitsToolBar ############################################################## @@ -226,22 +240,6 @@ class LimitsToolBar(qt.QToolBar): :param str title: See :class:`QToolBar`. """ - class _FloatEdit(qt.QLineEdit): - """Field to edit a float value.""" - def __init__(self, value=None, *args, **kwargs): - qt.QLineEdit.__init__(self, *args, **kwargs) - self.setValidator(qt.QDoubleValidator()) - self.setFixedWidth(100) - self.setAlignment(qt.Qt.AlignLeft) - if value is not None: - self.setValue(value) - - def value(self): - return float(self.text()) - - def setValue(self, value): - self.setText('%g' % value) - def __init__(self, parent=None, plot=None, title='Limits'): super(LimitsToolBar, self).__init__(title, parent) assert plot is not None @@ -257,28 +255,28 @@ class LimitsToolBar(qt.QToolBar): def _initWidgets(self): """Create and init Toolbar widgets.""" - xMin, xMax = self.plot.getGraphXLimits() - yMin, yMax = self.plot.getGraphYLimits() + xMin, xMax = self.plot.getXAxis().getLimits() + yMin, yMax = self.plot.getYAxis().getLimits() self.addWidget(qt.QLabel('Limits: ')) self.addWidget(qt.QLabel(' X: ')) - self._xMinFloatEdit = self._FloatEdit(xMin) + self._xMinFloatEdit = FloatEdit(self, xMin) self._xMinFloatEdit.editingFinished[()].connect( self._xFloatEditChanged) self.addWidget(self._xMinFloatEdit) - self._xMaxFloatEdit = self._FloatEdit(xMax) + self._xMaxFloatEdit = FloatEdit(self, xMax) self._xMaxFloatEdit.editingFinished[()].connect( self._xFloatEditChanged) self.addWidget(self._xMaxFloatEdit) self.addWidget(qt.QLabel(' Y: ')) - self._yMinFloatEdit = self._FloatEdit(yMin) + self._yMinFloatEdit = FloatEdit(self, yMin) self._yMinFloatEdit.editingFinished[()].connect( self._yFloatEditChanged) self.addWidget(self._yMinFloatEdit) - self._yMaxFloatEdit = self._FloatEdit(yMax) + self._yMaxFloatEdit = FloatEdit(self, yMax) self._yMaxFloatEdit.editingFinished[()].connect( self._yFloatEditChanged) self.addWidget(self._yMaxFloatEdit) @@ -288,8 +286,8 @@ class LimitsToolBar(qt.QToolBar): if event['event'] not in ('limitsChanged',): return - xMin, xMax = self.plot.getGraphXLimits() - yMin, yMax = self.plot.getGraphYLimits() + xMin, xMax = self.plot.getXAxis().getLimits() + yMin, yMax = self.plot.getYAxis().getLimits() self._xMinFloatEdit.setValue(xMin) self._xMaxFloatEdit.setValue(xMax) @@ -302,7 +300,7 @@ class LimitsToolBar(qt.QToolBar): if xMax < xMin: xMin, xMax = xMax, xMin - self.plot.setGraphXLimits(xMin, xMax) + self.plot.getXAxis().setLimits(xMin, xMax) def _yFloatEditChanged(self): """Handle Y limits changed from the GUI.""" @@ -310,4 +308,4 @@ class LimitsToolBar(qt.QToolBar): if yMax < yMin: yMin, yMax = yMax, yMin - self.plot.setGraphYLimits(yMin, yMax) + self.plot.getYAxis().setLimits(yMin, yMax) |