summaryrefslogtreecommitdiff
path: root/silx/gui/plot/tools/PositionInfo.py
diff options
context:
space:
mode:
Diffstat (limited to 'silx/gui/plot/tools/PositionInfo.py')
-rw-r--r--silx/gui/plot/tools/PositionInfo.py90
1 files changed, 59 insertions, 31 deletions
diff --git a/silx/gui/plot/tools/PositionInfo.py b/silx/gui/plot/tools/PositionInfo.py
index 4b63cdb..81d312a 100644
--- a/silx/gui/plot/tools/PositionInfo.py
+++ b/silx/gui/plot/tools/PositionInfo.py
@@ -1,7 +1,7 @@
# coding: utf-8
# /*##########################################################################
#
-# Copyright (c) 2016-2020 European Synchrotron Radiation Facility
+# Copyright (c) 2016-2021 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -44,11 +44,25 @@ import numpy
from ....utils.deprecation import deprecated
from ... import qt
from .. import items
+from ...widgets.ElidedLabel import ElidedLabel
_logger = logging.getLogger(__name__)
+class _PositionInfoLabel(ElidedLabel):
+ """QLabel with a default size larger than what is displayed."""
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.setTextInteractionFlags(qt.Qt.TextSelectableByMouse)
+
+ def sizeHint(self):
+ hint = super().sizeHint()
+ width = self.fontMetrics().boundingRect('##############').width()
+ return qt.QSize(max(hint.width(), width), hint.height())
+
+
# PositionInfo ################################################################
class PositionInfo(qt.QWidget):
@@ -117,11 +131,8 @@ class PositionInfo(qt.QWidget):
for name, func in converters:
layout.addWidget(qt.QLabel('<b>' + name + ':</b>'))
- contentWidget = qt.QLabel()
+ contentWidget = _PositionInfoLabel(self)
contentWidget.setText('------')
- contentWidget.setTextInteractionFlags(qt.Qt.TextSelectableByMouse)
- contentWidget.setFixedWidth(
- contentWidget.fontMetrics().boundingRect('##############').width())
layout.addWidget(contentWidget)
self._fields.append((contentWidget, name, func))
@@ -213,10 +224,11 @@ class PositionInfo(qt.QWidget):
kinds = []
if snappingMode & self.SNAPPING_CURVE:
kinds.append(items.Curve)
+ kinds.append(items.Histogram)
if snappingMode & self.SNAPPING_SCATTER:
kinds.append(items.Scatter)
selectedItems = [item for item in plot.getItems()
- if isinstance(item, kinds) and item.isVisible()]
+ if isinstance(item, tuple(kinds)) and item.isVisible()]
# Compute distance threshold
if qt.BINDING in ('PyQt5', 'PySide2'):
@@ -233,38 +245,54 @@ class PositionInfo(qt.QWidget):
distInPixels = (self.SNAP_THRESHOLD_DIST * ratio)**2
for item in selectedItems:
- if (snappingMode & self.SNAPPING_SYMBOLS_ONLY and
- not item.getSymbol()):
+ if (snappingMode & self.SNAPPING_SYMBOLS_ONLY and (
+ not isinstance(item, items.SymbolMixIn) or
+ not item.getSymbol())):
# Only handled if item symbols are visible
continue
- xArray = item.getXData(copy=False)
- yArray = item.getYData(copy=False)
- closestIndex = numpy.argmin(
- pow(xArray - x, 2) + pow(yArray - y, 2))
+ if isinstance(item, items.Histogram):
+ result = item.pick(xPixel, yPixel)
+ if result is not None: # Histogram picked
+ index = result.getIndices()[0]
+ edges = item.getBinEdgesData(copy=False)
- xClosest = xArray[closestIndex]
- yClosest = yArray[closestIndex]
+ # Snap to bin center and value
+ xData = 0.5 * (edges[index] + edges[index + 1])
+ yData = item.getValueData(copy=False)[index]
- if isinstance(item, items.YAxisMixIn):
- axis = item.getYAxis()
- else:
- axis = 'left'
-
- closestInPixels = plot.dataToPixel(
- xClosest, yClosest, axis=axis)
- if closestInPixels is not None:
- curveDistInPixels = (
- (closestInPixels[0] - xPixel)**2 +
- (closestInPixels[1] - yPixel)**2)
-
- if curveDistInPixels <= distInPixels:
# Update label style sheet
styleSheet = "color: rgb(0, 0, 0);"
-
- # if close enough, snap to data point coord
- xData, yData = xClosest, yClosest
- distInPixels = curveDistInPixels
+ break
+
+ else: # Curve, Scatter
+ xArray = item.getXData(copy=False)
+ yArray = item.getYData(copy=False)
+ closestIndex = numpy.argmin(
+ pow(xArray - x, 2) + pow(yArray - y, 2))
+
+ xClosest = xArray[closestIndex]
+ yClosest = yArray[closestIndex]
+
+ if isinstance(item, items.YAxisMixIn):
+ axis = item.getYAxis()
+ else:
+ axis = 'left'
+
+ closestInPixels = plot.dataToPixel(
+ xClosest, yClosest, axis=axis)
+ if closestInPixels is not None:
+ curveDistInPixels = (
+ (closestInPixels[0] - xPixel)**2 +
+ (closestInPixels[1] - yPixel)**2)
+
+ if curveDistInPixels <= distInPixels:
+ # Update label style sheet
+ styleSheet = "color: rgb(0, 0, 0);"
+
+ # if close enough, snap to data point coord
+ xData, yData = xClosest, yClosest
+ distInPixels = curveDistInPixels
for label, name, func in self._fields:
label.setStyleSheet(styleSheet)