jfjoch_viewer: image is auto scaled at the beginning
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <QScrollBar>
|
||||
#include <QWheelEvent>
|
||||
#include <QMouseEvent>
|
||||
#include <QTimer>
|
||||
|
||||
JFJochImage::JFJochImage(QWidget *parent) : QGraphicsView(parent) {
|
||||
setDragMode(QGraphicsView::NoDrag); // Disable default drag mode
|
||||
@@ -14,6 +15,8 @@ JFJochImage::JFJochImage(QWidget *parent) : QGraphicsView(parent) {
|
||||
setRenderHint(QPainter::Antialiasing); // Enable smooth rendering
|
||||
setRenderHint(QPainter::SmoothPixmapTransform);
|
||||
|
||||
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
|
||||
setFocusPolicy(Qt::ClickFocus);
|
||||
// Connect the horizontal scrollbar's valueChanged signal
|
||||
connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &JFJochImage::onScroll);
|
||||
@@ -25,6 +28,16 @@ JFJochImage::JFJochImage(QWidget *parent) : QGraphicsView(parent) {
|
||||
color_scale.Select(ColorScaleEnum::Indigo);
|
||||
}
|
||||
|
||||
void JFJochImage::showEvent(QShowEvent* event) {
|
||||
QGraphicsView::showEvent(event);
|
||||
// Defer fit until after this widget is laid out and has a real viewport size
|
||||
QTimer::singleShot(0, this, [this](){
|
||||
if (!initial_fit_done_) {
|
||||
fitToViewShorterSideOnce();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void JFJochImage::onScroll(int value) {
|
||||
updateOverlay();
|
||||
}
|
||||
@@ -100,6 +113,9 @@ void JFJochImage::wheelEvent(QWheelEvent *event) {
|
||||
|
||||
void JFJochImage::resizeEvent(QResizeEvent *event) {
|
||||
QGraphicsView::resizeEvent(event);
|
||||
if (!initial_fit_done_)
|
||||
fitToViewShorterSideOnce();
|
||||
|
||||
updateOverlay();
|
||||
}
|
||||
|
||||
@@ -695,36 +711,36 @@ void JFJochImage::CalcROI() {
|
||||
}
|
||||
emit roiCalculated(msg);
|
||||
}
|
||||
|
||||
void JFJochImage::fitToViewShorterSideOnce() {
|
||||
if (initial_fit_done_)
|
||||
return;
|
||||
if (W == 0 || H == 0 || !viewport())
|
||||
return;
|
||||
// Ensure scene rect matches the image bounds for correct fitting
|
||||
|
||||
// Guard against tiny or zero viewport (happens before layout settles)
|
||||
const QSize vp = viewport()->size();
|
||||
if (vp.width() < 8 || vp.height() < 8) {
|
||||
// remember last tried size; resizeEvent/showEvent will retry
|
||||
last_fit_viewport_ = vp;
|
||||
return;
|
||||
}
|
||||
|
||||
if (scene())
|
||||
scene()->setSceneRect(QRectF(0, 0, static_cast<qreal>(W), static_cast<qreal>(H)));
|
||||
|
||||
// Temporarily anchor to the view center to avoid drift during fit
|
||||
const auto oldAnchor = transformationAnchor();
|
||||
setTransformationAnchor(QGraphicsView::AnchorViewCenter);
|
||||
|
||||
// Clear any prior transform before fitting
|
||||
setTransform(QTransform());
|
||||
|
||||
// Use Qt's fitter to maximize the image in the viewport with aspect preserved.
|
||||
// This accounts for frame, scrollbars, DPR, and internal rounding.
|
||||
fitInView(QRectF(0, 0, static_cast<qreal>(W), static_cast<qreal>(H)), Qt::KeepAspectRatio);
|
||||
|
||||
// Cache the resulting uniform scale
|
||||
scale_factor = transform().m11(); // m11 == m22 for uniform fit
|
||||
|
||||
// Center explicitly (fitInView already centers, but keep it explicit)
|
||||
scale_factor = transform().m11();
|
||||
centerOn(QPointF(static_cast<qreal>(W) * 0.5, static_cast<qreal>(H) * 0.5));
|
||||
|
||||
// Restore anchor preference
|
||||
setTransformationAnchor(oldAnchor);
|
||||
|
||||
initial_fit_done_ = true;
|
||||
last_fit_W_ = W;
|
||||
last_fit_H_ = H;
|
||||
last_fit_viewport_ = vp;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ class JFJochImage : public QGraphicsView {
|
||||
void writePixelLabels();
|
||||
void wheelEvent(QWheelEvent* event) override;
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
void showEvent(QShowEvent* event) override;
|
||||
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
void mouseMoveEvent(QMouseEvent *event) override;
|
||||
@@ -75,6 +76,7 @@ protected:
|
||||
|
||||
// Perform initial fit-to-view (shorter direction), once per image size
|
||||
void fitToViewShorterSideOnce();
|
||||
QSize last_fit_viewport_ = {};
|
||||
signals:
|
||||
void foregroundChanged(float v);
|
||||
void backgroundChanged(float v);
|
||||
|
||||
Reference in New Issue
Block a user