Files
Jungfraujoch/viewer/widgets/JFJochImage.cpp
T
leonarski_f f61401b704
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Failing after 10m2s
Build Packages / Generate python client (push) Has been cancelled
Build Packages / Build documentation (push) Has been cancelled
Build Packages / Unit tests (push) Has been cancelled
Build Packages / Create release (push) Has been cancelled
Build Packages / build:rpm (rocky8_sls9) (push) Has started running
Build Packages / build:rpm (ubuntu2404) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2204) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Has been cancelled
Build Packages / build:rpm (rocky8_nocuda) (push) Has been cancelled
Build Packages / build:rpm (rocky8) (push) Has been cancelled
Build Packages / build:rpm (rocky9_nocuda) (push) Has been cancelled
Build Packages / build:rpm (rocky9) (push) Has been cancelled
jfjoch_viewer: Work in (slow) progress
2025-11-07 13:45:15 +01:00

116 lines
3.4 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>
#include <QMouseEvent>
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);
// Optional: a sensible default colormap
color_scale.Select(ColorScaleEnum::Indigo);
}
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();
}
}
void JFJochImage::resizeEvent(QResizeEvent *event) {
QGraphicsView::resizeEvent(event);
updateOverlay();
}
QPointF JFJochImage::RoundPoint(const QPointF &input) {
return QPointF(qRound(input.x()), qRound(input.y()));
}
void JFJochImage::SetROIBox(QRect box) {
roi_type = RoiType::RoiBox;
roiBox= box;
roiStartPos = roiBox.topLeft();
roiEndPos = roiBox.bottomRight();
Redraw();
}
void JFJochImage::SetROICircle(double x, double y, double radius) {
roi_type = RoiType::RoiCircle;
roiBox= QRectF(x - radius, y - radius, 2 * radius, 2 * radius).normalized();
roiStartPos = roiBox.topLeft();
roiEndPos = roiBox.bottomRight();
Redraw();
}