From e648678c286a3dbc2c7d9a47fda88e8ba4529029 Mon Sep 17 00:00:00 2001 From: Leonhardmaster2 Date: Sun, 26 Jul 2026 14:52:44 +0200 Subject: [PATCH] smooth graph viewer zoom --- GNodeGUI/src/graph_viewer.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/GNodeGUI/src/graph_viewer.cpp b/GNodeGUI/src/graph_viewer.cpp index 7f65af9..00538ac 100644 --- a/GNodeGUI/src/graph_viewer.cpp +++ b/GNodeGUI/src/graph_viewer.cpp @@ -1,6 +1,8 @@ /* Copyright (c) 2024 Otto Link. Distributed under the terms of the GNU General * Public License. The full license is in the file LICENSE, distributed with * this software. */ +#include +#include #include #include @@ -1371,13 +1373,25 @@ void GraphViewer::unpin_nodes() void GraphViewer::wheelEvent(QWheelEvent *event) { - const float factor = 1.2f; - QPointF mouse_scene_pos = this->mapToScene(event->position().toPoint()); + constexpr qreal min_zoom = 0.05; + constexpr qreal max_zoom = 8.0; + constexpr qreal zoom_base = 1.0015; + + const qreal current_zoom = this->transform().m11(); + const qreal requested_factor = std::pow(zoom_base, event->angleDelta().y()); + const qreal target_zoom = std::clamp(current_zoom * requested_factor, + min_zoom, + max_zoom); + const qreal factor = target_zoom / current_zoom; + + if (qFuzzyCompare(factor, 1.0)) + { + event->accept(); + return; + } - if (event->angleDelta().y() > 0) - this->scale(factor, factor); - else - this->scale(1.f / factor, 1.f / factor); + QPointF mouse_scene_pos = this->mapToScene(event->position().toPoint()); + this->scale(factor, factor); // adjust the view to maintain the zoom centered on the mouse position QPointF new_mouse_scene_pos = this->mapToScene(event->position().toPoint());