Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions GNodeGUI/src/graph_viewer.cpp
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>

Expand Down Expand Up @@ -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());
Expand Down