[PATCH v2] kernelshark: fix bug in the behavior of ksglwidget rubber band

Mircea Cirjaliu <[email protected]> Tue, 28 Oct 2025 21:45:27 +0100
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
Accidentally pressing right mouse button while dragging
the range will cause the rubber band to behave abnormally.
Improved the logic behind range dragging to account for this case.
The state of the rubber band will be reset on right click.

Signed-off-by: Mircea Cirjaliu <[email protected]>
---
 src/KsGLWidget.cpp | 23 +++++++++++++++++++----
 src/KsGLWidget.hpp |  2 ++
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/KsGLWidget.cpp b/src/KsGLWidget.cpp
index 7f2001c..ea8cb93 100644
--- a/src/KsGLWidget.cpp
+++ b/src/KsGLWidget.cpp
@@ -187,9 +187,15 @@ void KsGLWidget::reset()
 /** Reimplemented event handler used to receive mouse press events. */
 void KsGLWidget::mousePressEvent(QMouseEvent *event)
 {
+	if (isEmpty())
+		return;
+
 	if (event->button() == Qt::LeftButton) {
 		_posMousePress = _posInRange(event->pos().x());
 		_rangeBoundInit(_posMousePress);
+	} else if (event->button() == Qt::RightButton) {
+		if (_rubberBand.isVisible())
+			_rangeBoundCancel();
 	}
 }
 
@@ -262,8 +268,10 @@ void KsGLWidget::mouseMoveEvent(QMouseEvent *event)
 	if (isEmpty())
 		return;
 
-	if (_rubberBand.isVisible())
-		_rangeBoundStretched(_posInRange(event->pos().x()));
+	if (_rubberBand.isVisible()) {
+		size_t posMouseRel = _posInRange(event->pos().x());
+		_rangeBoundStretched(posMouseRel);
+	}
 
 	bin = event->pos().x() - _bin0Offset();
 	getPlotInfo(event->pos(), &sd, &cpu, &pid);
@@ -287,11 +295,12 @@ void KsGLWidget::mouseMoveEvent(QMouseEvent *event)
 /** Reimplemented event handler used to receive mouse release events. */
 void KsGLWidget::mouseReleaseEvent(QMouseEvent *event)
 {
-	if (isEmpty())
+	if (isEmpty() || !_rubberBand.isVisible())
 		return;
 
 	if (event->button() == Qt::LeftButton) {
 		size_t posMouseRel = _posInRange(event->pos().x());
+
 		int min, max;
 		if (_posMousePress < posMouseRel) {
 			min = _posMousePress - _bin0Offset();
@@ -1132,7 +1141,7 @@ void KsGLWidget::_rangeChanged(int binMin, int binMax)
 	/* The rubber band is no longer needed. Make it invisible. */
 	_rubberBand.hide();
 
-	if ( (binMax - binMin) < 4) {
+	if ((binMax - binMin) < 4) {
 		/* Most likely this is an accidental click. Do nothing. */
 		return;
 	}
@@ -1182,6 +1191,12 @@ void KsGLWidget::_rangeChanged(int binMin, int binMax)
 	}
 }
 
+void KsGLWidget::_rangeBoundCancel()
+{
+	/* The rubber band is no longer needed. Make it invisible. */
+	_rubberBand.hide();
+}
+
 int KsGLWidget::_posInRange(int x)
 {
 	int posX;
diff --git a/src/KsGLWidget.hpp b/src/KsGLWidget.hpp
index cafc70b..8fcac55 100644
--- a/src/KsGLWidget.hpp
+++ b/src/KsGLWidget.hpp
@@ -315,6 +315,8 @@ private:
 
 	void _rangeChanged(int binMin, int binMax);
 
+	void _rangeBoundCancel();
+
 	bool _findAndSelect(QMouseEvent *event);
 
 	bool _find(int bin, int sd, int cpu, int pid,
-- 
2.43.0