Skip to content

Commit

Permalink
Merge pull request #3960 from silx-kit/vasole-patch
Browse files Browse the repository at this point in the history
[GUI] Histogram Action. QHoverEvent does not have attribute pos
  • Loading branch information
payno authored Nov 14, 2023
2 parents b3e2bfa + 51d11cb commit ea03f17
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
20 changes: 17 additions & 3 deletions src/silx/gui/plot/actions/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"""

__authors__ = ["V.A. Sole", "T. Vincent", "P. Knobel"]
__date__ = "01/12/2020"
__date__ = "07/11/2023"
__license__ = "MIT"

from typing import Optional, Tuple
Expand Down Expand Up @@ -275,6 +275,10 @@ def __init__(self, *args, **kwargs):
self.__updateHistogramFromControls)
self.__rangeSlider.sigValueChanged.connect(self.__rangeChanged)
controlsLayout.addWidget(self.__rangeSlider)
self.__weightCheckBox = qt.QCheckBox(self)
self.__weightCheckBox.setText("Use weights")
self.__weightCheckBox.clicked.connect(self.__weightChanged)
controlsLayout.addWidget(self.__weightCheckBox)
controlsLayout.addStretch(1)

# Stats display
Expand Down Expand Up @@ -347,6 +351,9 @@ def __rangeChanged(self, first, second):
self.__rangeSlider.setToolTip(tooltip)
self.__rangeLabel.setToolTip(tooltip)

def __weightChanged(self, value):
self._updateFromItem()

def _updateFromItem(self):
"""Update histogram and stats from the item"""
item = self.getItem()
Expand Down Expand Up @@ -394,17 +401,24 @@ def _updateFromItem(self):
self.__rangeSlider.setRange(*range_)
self.__rangeSlider.setPositions(*previousPositions)

data = array.ravel().astype(numpy.float32)
histogram = Histogramnd(
array.ravel().astype(numpy.float32),
data,
n_bins=max(2, self.__nbinsLineEdit.getValue()),
histo_range=self.__rangeSlider.getValues(),
weights=data,
)
if len(histogram.edges) != 1:
_logger.error("Error while computing the histogram")
self.reset()
return

self.setHistogram(histogram.histo, histogram.edges[0])
if self.__weightCheckBox.isChecked():
self.setHistogram(histogram.weighted_histo, histogram.edges[0])
self.__plot.getYAxis().setLabel("Count * Value")
else:
self.setHistogram(histogram.histo, histogram.edges[0])
self.__plot.getYAxis().setLabel("Count")
self.resetZoom()
self.setStatistics(
min_=xmin,
Expand Down
27 changes: 21 additions & 6 deletions src/silx/gui/widgets/RangeSlider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# /*##########################################################################
#
# Copyright (c) 2015-2021 European Synchrotron Radiation Facility
# Copyright (c) 2015-2023 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
Expand Down Expand Up @@ -29,7 +29,7 @@

__authors__ = ["D. Naudet", "T. Vincent"]
__license__ = "MIT"
__date__ = "26/11/2018"
__date__ = "03/11/2023"


import numpy as numpy
Expand Down Expand Up @@ -122,7 +122,12 @@ def __init__(self, parent=None):
def event(self, event):
t = event.type()
if t == qt.QEvent.HoverEnter or t == qt.QEvent.HoverLeave or t == qt.QEvent.HoverMove:
return self.__updateHoverControl(event.pos())
if qt.BINDING in ("PyQt5",):
# qt-5
return self.__updateHoverControl(event.pos())
else:
# qt-6
return self.__updateHoverControl(event.position().toPoint())
else:
return super(RangeSlider, self).event(event)

Expand Down Expand Up @@ -500,6 +505,14 @@ def setGroovePixmapFromProfile(self, profile, colormap=None):
self.setGroovePixmap(qpixmap)

# Handle interaction
def _mouseEventPosition(self, event):
if qt.BINDING in ("PyQt5",):
# qt-5 returns QPoint
position = event.pos()
else:
# qt-6 returns QPointF
position = event.position()
return position

def mousePressEvent(self, event):
super(RangeSlider, self).mousePressEvent(event)
Expand All @@ -508,7 +521,8 @@ def mousePressEvent(self, event):
picked = None
for name in ('first', 'second'):
area = self.__sliderRect(name)
if area.contains(event.pos()):
position = self._mouseEventPosition(event)
if area.contains(position):
picked = name
break

Expand All @@ -520,12 +534,13 @@ def mouseMoveEvent(self, event):
super(RangeSlider, self).mouseMoveEvent(event)

if self.__moving is not None:
event_pos = self._mouseEventPosition(event)
delta = self._SLIDER_WIDTH // 2
if self.__moving == 'first':
position = self.__xPixelToPosition(event.pos().x() + delta)
position = self.__xPixelToPosition(event_pos.x() + delta)
self.setFirstPosition(position)
else:
position = self.__xPixelToPosition(event.pos().x() - delta)
position = self.__xPixelToPosition(event_pos.x() - delta)
self.setSecondPosition(position)

def mouseReleaseEvent(self, event):
Expand Down

0 comments on commit ea03f17

Please sign in to comment.