Files
Jungfraujoch/viewer/widgets/JFJochImage.cpp

87 lines
2.6 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "JFJochImage.h"
#include <QScrollBar>
#include <QWheelEvent>
JFJochImage::JFJochImage(QWidget *parent) : QGraphicsView(parent) {
setDragMode(QGraphicsView::NoDrag); // Disable default drag mode
setTransformationAnchor(QGraphicsView::AnchorUnderMouse); // Zoom anchors
setRenderHint(QPainter::Antialiasing); // Enable smooth rendering
setRenderHint(QPainter::SmoothPixmapTransform);
setFocusPolicy(Qt::ClickFocus);
// Connect the horizontal scrollbar's valueChanged signal
connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &JFJochImage::onScroll);
// Connect the vertical scrollbar's valueChanged signal
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &JFJochImage::onScroll);
}
void JFJochImage::onScroll(int value) {
updateOverlay();
}
void JFJochImage::changeBackground(float val) {
background = val;
Redraw();
}
void JFJochImage::changeForeground(float val) {
foreground = val;
Redraw();
}
void JFJochImage::setColorMap(int color_map) {
try {
color_scale.Select(static_cast<ColorScaleEnum>(color_map));
Redraw();
} catch (...) {
}
}
void JFJochImage::setFeatureColor(QColor input) {
feature_color = input;
Redraw();
}
void JFJochImage::wheelEvent(QWheelEvent *event) {
if (!scene()) return;
const double zoomFactor = 1.15; // Zoom factor
// Get the position of the mouse in scene coordinates
QPointF targetScenePos = mapToScene(event->position().toPoint());
if (event->modifiers() == Qt::ShiftModifier) {
float new_foreground = foreground + event->angleDelta().y() / 120.0f;
if (new_foreground < 1)
new_foreground = 1.0;
foreground = new_foreground;
emit foregroundChanged(foreground);
Redraw();
} else {
// Perform zooming
if (event->angleDelta().y() > 0) {
if (scale_factor * zoomFactor < 500.0) {
scale_factor *= zoomFactor;
scale(zoomFactor, zoomFactor);
}
} else {
if (scale_factor > 0.2) {
scale_factor *= 1.0 / zoomFactor;
scale(1.0 / zoomFactor, 1.0 / zoomFactor);
}
}
// Adjust the view's center to keep the zoom focused on the mouse position
QPointF updatedViewportCenter = mapToScene(viewport()->rect().center());
QPointF delta = targetScenePos - updatedViewportCenter;
translate(delta.x(), delta.y()); // Shift the view
updateOverlay();
}
}