From 76fa974a9f2169b8a5adb2639088760cc520066b Mon Sep 17 00:00:00 2001 From: "V. Armando Sole" Date: Fri, 3 Nov 2023 15:39:06 +0100 Subject: [PATCH 1/7] QHoverEvent does not have attribute pos --- src/silx/gui/widgets/RangeSlider.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/silx/gui/widgets/RangeSlider.py b/src/silx/gui/widgets/RangeSlider.py index 4db0470d6f..6de9dd36e3 100644 --- a/src/silx/gui/widgets/RangeSlider.py +++ b/src/silx/gui/widgets/RangeSlider.py @@ -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 @@ -29,7 +29,7 @@ __authors__ = ["D. Naudet", "T. Vincent"] __license__ = "MIT" -__date__ = "26/11/2018" +__date__ = "03/11/2023" import numpy as numpy @@ -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 hasattr(event, "position"): + # qt-6 + return self.__updateHoverControl(event.position().toPoint()) + else: + # qt-5 + return self.__updateHoverControl(event.pos()) else: return super(RangeSlider, self).event(event) @@ -500,6 +505,14 @@ def setGroovePixmapFromProfile(self, profile, colormap=None): self.setGroovePixmap(qpixmap) # Handle interaction + def _mouseEventPosition(self, event): + if hasattr(event, "pos"): + # 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) @@ -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 @@ -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): From 50285a66f89ec1c21c6dff73f7cf1353afed5e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=2E=20Armando=20Sol=C3=A9?= Date: Tue, 7 Nov 2023 17:38:20 +0100 Subject: [PATCH 2/7] Offer the possibility to use a weighted histogram --- src/silx/gui/plot/actions/histogram.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/silx/gui/plot/actions/histogram.py b/src/silx/gui/plot/actions/histogram.py index 5ec391898c..89005380c1 100644 --- a/src/silx/gui/plot/actions/histogram.py +++ b/src/silx/gui/plot/actions/histogram.py @@ -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 @@ -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.stateChanged[int].connect(self.__weightChanged) + controlsLayout.addWidget(self.__weightCheckBox) controlsLayout.addStretch(1) # Stats display @@ -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() @@ -398,13 +405,19 @@ def _updateFromItem(self): array.ravel().astype(numpy.float32), n_bins=max(2, self.__nbinsLineEdit.getValue()), histo_range=self.__rangeSlider.getValues(), + weights=array.ravel().astype(numpy.float32), ) 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, From 170809e2fe83358617cd0a88db151f074e833a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=2E=20Armando=20Sol=C3=A9?= Date: Wed, 8 Nov 2023 10:25:13 +0100 Subject: [PATCH 3/7] Use a temporary array --- src/silx/gui/plot/actions/histogram.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/silx/gui/plot/actions/histogram.py b/src/silx/gui/plot/actions/histogram.py index 89005380c1..95b689463d 100644 --- a/src/silx/gui/plot/actions/histogram.py +++ b/src/silx/gui/plot/actions/histogram.py @@ -401,11 +401,12 @@ 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=array.ravel().astype(numpy.float32), + weights=data, ) if len(histogram.edges) != 1: _logger.error("Error while computing the histogram") From 8ecd6218efd7590c45e44a1587a6a78cbe783a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=2E=20Armando=20Sol=C3=A9?= Date: Wed, 8 Nov 2023 10:25:43 +0100 Subject: [PATCH 4/7] Use position also for PySide6 --- src/silx/gui/widgets/RangeSlider.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/silx/gui/widgets/RangeSlider.py b/src/silx/gui/widgets/RangeSlider.py index 6de9dd36e3..c3e0ec14ce 100644 --- a/src/silx/gui/widgets/RangeSlider.py +++ b/src/silx/gui/widgets/RangeSlider.py @@ -506,12 +506,12 @@ def setGroovePixmapFromProfile(self, profile, colormap=None): # Handle interaction def _mouseEventPosition(self, event): - if hasattr(event, "pos"): - # qt-5 returns QPoint - position = event.pos() - else: + if hasattr(event, "position"): # qt-6 returns QPointF position = event.position() + else: + # qt-5 returns QPoint + position = event.pos() return position def mousePressEvent(self, event): From a39eaabb9acc39744e7180f449d323843d265e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=2E=20Armando=20Sol=C3=A9?= Date: Tue, 14 Nov 2023 13:45:48 +0100 Subject: [PATCH 5/7] Use BINDING --- src/silx/gui/widgets/RangeSlider.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/silx/gui/widgets/RangeSlider.py b/src/silx/gui/widgets/RangeSlider.py index c3e0ec14ce..36077defb0 100644 --- a/src/silx/gui/widgets/RangeSlider.py +++ b/src/silx/gui/widgets/RangeSlider.py @@ -506,12 +506,12 @@ def setGroovePixmapFromProfile(self, profile, colormap=None): # Handle interaction def _mouseEventPosition(self, event): - if hasattr(event, "position"): - # qt-6 returns QPointF - position = event.position() - else: + 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): From eb67ba258622b5d1db08f35016b053ff38d063b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=2E=20Armando=20Sol=C3=A9?= Date: Tue, 14 Nov 2023 13:49:26 +0100 Subject: [PATCH 6/7] Use clicked (without bool signature as in other parts)!!!!) --- src/silx/gui/plot/actions/histogram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/silx/gui/plot/actions/histogram.py b/src/silx/gui/plot/actions/histogram.py index 95b689463d..2e9f1f05fb 100644 --- a/src/silx/gui/plot/actions/histogram.py +++ b/src/silx/gui/plot/actions/histogram.py @@ -277,7 +277,7 @@ def __init__(self, *args, **kwargs): controlsLayout.addWidget(self.__rangeSlider) self.__weightCheckBox = qt.QCheckBox(self) self.__weightCheckBox.setText("Use weights") - self.__weightCheckBox.stateChanged[int].connect(self.__weightChanged) + self.__weightCheckBox.clicked.connect(self.__weightChanged) controlsLayout.addWidget(self.__weightCheckBox) controlsLayout.addStretch(1) From 51d11cb34c435a5da809a7b62fa5417f24aabcb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=2E=20Armando=20Sol=C3=A9?= Date: Tue, 14 Nov 2023 15:36:41 +0100 Subject: [PATCH 7/7] Update src/silx/gui/widgets/RangeSlider.py Co-authored-by: payno --- src/silx/gui/widgets/RangeSlider.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/silx/gui/widgets/RangeSlider.py b/src/silx/gui/widgets/RangeSlider.py index 36077defb0..8ae3defae2 100644 --- a/src/silx/gui/widgets/RangeSlider.py +++ b/src/silx/gui/widgets/RangeSlider.py @@ -122,12 +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: - if hasattr(event, "position"): - # qt-6 - return self.__updateHoverControl(event.position().toPoint()) - else: + 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)