git repos / SagoImageBrowser

blame: imageviewwidget.cpp

normal view · raw

61136b77 Poul Sander 2026-02-20 22:45 1
#include "imageviewwidget.h"
61136b77 Poul Sander 2026-02-20 22:45 2
61136b77 Poul Sander 2026-02-20 22:45 3
#include <QPainter>
61136b77 Poul Sander 2026-02-20 22:45 4
#include <QKeyEvent>
61136b77 Poul Sander 2026-02-20 22:45 5
#include <QImageReader>
c58dbdb7 Poul Sander 2026-02-22 17:49 6
#include <algorithm>
61136b77 Poul Sander 2026-02-20 22:45 7
61136b77 Poul Sander 2026-02-20 22:45 8
ImageViewWidget::ImageViewWidget(QWidget *parent)
61136b77 Poul Sander 2026-02-20 22:45 9
    : QWidget(parent)
61136b77 Poul Sander 2026-02-20 22:45 10
{
61136b77 Poul Sander 2026-02-20 22:45 11
    setFocusPolicy(Qt::StrongFocus);
61136b77 Poul Sander 2026-02-20 22:45 12
    setStyleSheet("background-color: black;");
61136b77 Poul Sander 2026-02-20 22:45 13
}
61136b77 Poul Sander 2026-02-20 22:45 14
61136b77 Poul Sander 2026-02-20 22:45 15
void ImageViewWidget::setImage(const QString &path)
61136b77 Poul Sander 2026-02-20 22:45 16
{
61136b77 Poul Sander 2026-02-20 22:45 17
    QImageReader reader(path);
61136b77 Poul Sander 2026-02-20 22:45 18
    reader.setAutoTransform(true);
61136b77 Poul Sander 2026-02-20 22:45 19
    QImage image = reader.read();
61136b77 Poul Sander 2026-02-20 22:45 20
61136b77 Poul Sander 2026-02-20 22:45 21
    if (!image.isNull())
61136b77 Poul Sander 2026-02-20 22:45 22
        m_pixmap = QPixmap::fromImage(image);
61136b77 Poul Sander 2026-02-20 22:45 23
    else
61136b77 Poul Sander 2026-02-20 22:45 24
        m_pixmap = QPixmap();
61136b77 Poul Sander 2026-02-20 22:45 25
c58dbdb7 Poul Sander 2026-02-22 17:49 26
    m_zoomMode = FitToScreen;
c58dbdb7 Poul Sander 2026-02-22 17:49 27
    m_zoomFactor = 1.0;
61136b77 Poul Sander 2026-02-20 22:45 28
    update();
61136b77 Poul Sander 2026-02-20 22:45 29
}
61136b77 Poul Sander 2026-02-20 22:45 30
61136b77 Poul Sander 2026-02-20 22:45 31
void ImageViewWidget::paintEvent(QPaintEvent *)
61136b77 Poul Sander 2026-02-20 22:45 32
{
61136b77 Poul Sander 2026-02-20 22:45 33
    if (m_pixmap.isNull())
61136b77 Poul Sander 2026-02-20 22:45 34
        return;
61136b77 Poul Sander 2026-02-20 22:45 35
61136b77 Poul Sander 2026-02-20 22:45 36
    QPainter painter(this);
c58dbdb7 Poul Sander 2026-02-22 17:49 37
    QPixmap drawn;
c58dbdb7 Poul Sander 2026-02-22 17:49 38
c58dbdb7 Poul Sander 2026-02-22 17:49 39
    switch (m_zoomMode) {
c58dbdb7 Poul Sander 2026-02-22 17:49 40
    case FitToScreen:
c58dbdb7 Poul Sander 2026-02-22 17:49 41
        drawn = m_pixmap.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
c58dbdb7 Poul Sander 2026-02-22 17:49 42
        break;
c58dbdb7 Poul Sander 2026-02-22 17:49 43
    case OriginalSize:
c58dbdb7 Poul Sander 2026-02-22 17:49 44
        drawn = m_pixmap;
c58dbdb7 Poul Sander 2026-02-22 17:49 45
        break;
c58dbdb7 Poul Sander 2026-02-22 17:49 46
    case CustomZoom: {
c58dbdb7 Poul Sander 2026-02-22 17:49 47
        QSize target(static_cast<int>(m_pixmap.width() * m_zoomFactor),
c58dbdb7 Poul Sander 2026-02-22 17:49 48
                     static_cast<int>(m_pixmap.height() * m_zoomFactor));
c58dbdb7 Poul Sander 2026-02-22 17:49 49
        drawn = m_pixmap.scaled(target, Qt::KeepAspectRatio, Qt::SmoothTransformation);
c58dbdb7 Poul Sander 2026-02-22 17:49 50
        break;
c58dbdb7 Poul Sander 2026-02-22 17:49 51
    }
c58dbdb7 Poul Sander 2026-02-22 17:49 52
    }
c58dbdb7 Poul Sander 2026-02-22 17:49 53
c58dbdb7 Poul Sander 2026-02-22 17:49 54
    int x = (width()  - drawn.width())  / 2;
c58dbdb7 Poul Sander 2026-02-22 17:49 55
    int y = (height() - drawn.height()) / 2;
c58dbdb7 Poul Sander 2026-02-22 17:49 56
    painter.drawPixmap(x, y, drawn);
61136b77 Poul Sander 2026-02-20 22:45 57
}
61136b77 Poul Sander 2026-02-20 22:45 58
61136b77 Poul Sander 2026-02-20 22:45 59
void ImageViewWidget::keyPressEvent(QKeyEvent *event)
61136b77 Poul Sander 2026-02-20 22:45 60
{
61136b77 Poul Sander 2026-02-20 22:45 61
    if (event->key() == Qt::Key_Escape)
61136b77 Poul Sander 2026-02-20 22:45 62
    {
61136b77 Poul Sander 2026-02-20 22:45 63
        emit closeRequested();
61136b77 Poul Sander 2026-02-20 22:45 64
        event->accept();
61136b77 Poul Sander 2026-02-20 22:45 65
        return;
61136b77 Poul Sander 2026-02-20 22:45 66
    }
61136b77 Poul Sander 2026-02-20 22:45 67
1ffc1962 Poul Sander 2026-02-20 23:41 68
    if (event->key() == Qt::Key_PageDown)
1ffc1962 Poul Sander 2026-02-20 23:41 69
    {
1ffc1962 Poul Sander 2026-02-20 23:41 70
        emit nextRequested();
1ffc1962 Poul Sander 2026-02-20 23:41 71
        event->accept();
1ffc1962 Poul Sander 2026-02-20 23:41 72
        return;
1ffc1962 Poul Sander 2026-02-20 23:41 73
    }
1ffc1962 Poul Sander 2026-02-20 23:41 74
1ffc1962 Poul Sander 2026-02-20 23:41 75
    if (event->key() == Qt::Key_PageUp)
1ffc1962 Poul Sander 2026-02-20 23:41 76
    {
1ffc1962 Poul Sander 2026-02-20 23:41 77
        emit previousRequested();
1ffc1962 Poul Sander 2026-02-20 23:41 78
        event->accept();
1ffc1962 Poul Sander 2026-02-20 23:41 79
        return;
1ffc1962 Poul Sander 2026-02-20 23:41 80
    }
1ffc1962 Poul Sander 2026-02-20 23:41 81
c58dbdb7 Poul Sander 2026-02-22 17:49 82
    if (event->key() == Qt::Key_Slash)
c58dbdb7 Poul Sander 2026-02-22 17:49 83
    {
c58dbdb7 Poul Sander 2026-02-22 17:49 84
        zoomOriginal();
c58dbdb7 Poul Sander 2026-02-22 17:49 85
        event->accept();
c58dbdb7 Poul Sander 2026-02-22 17:49 86
        return;
c58dbdb7 Poul Sander 2026-02-22 17:49 87
    }
c58dbdb7 Poul Sander 2026-02-22 17:49 88
c58dbdb7 Poul Sander 2026-02-22 17:49 89
    if (event->key() == Qt::Key_Asterisk)
c58dbdb7 Poul Sander 2026-02-22 17:49 90
    {
c58dbdb7 Poul Sander 2026-02-22 17:49 91
        zoomFitToScreen();
c58dbdb7 Poul Sander 2026-02-22 17:49 92
        event->accept();
c58dbdb7 Poul Sander 2026-02-22 17:49 93
        return;
c58dbdb7 Poul Sander 2026-02-22 17:49 94
    }
c58dbdb7 Poul Sander 2026-02-22 17:49 95
c58dbdb7 Poul Sander 2026-02-22 17:49 96
    if (event->key() == Qt::Key_Plus)
c58dbdb7 Poul Sander 2026-02-22 17:49 97
    {
c58dbdb7 Poul Sander 2026-02-22 17:49 98
        zoomIn();
c58dbdb7 Poul Sander 2026-02-22 17:49 99
        event->accept();
c58dbdb7 Poul Sander 2026-02-22 17:49 100
        return;
c58dbdb7 Poul Sander 2026-02-22 17:49 101
    }
c58dbdb7 Poul Sander 2026-02-22 17:49 102
c58dbdb7 Poul Sander 2026-02-22 17:49 103
    if (event->key() == Qt::Key_Minus)
c58dbdb7 Poul Sander 2026-02-22 17:49 104
    {
c58dbdb7 Poul Sander 2026-02-22 17:49 105
        zoomOut();
c58dbdb7 Poul Sander 2026-02-22 17:49 106
        event->accept();
c58dbdb7 Poul Sander 2026-02-22 17:49 107
        return;
c58dbdb7 Poul Sander 2026-02-22 17:49 108
    }
c58dbdb7 Poul Sander 2026-02-22 17:49 109
61136b77 Poul Sander 2026-02-20 22:45 110
    QWidget::keyPressEvent(event);
61136b77 Poul Sander 2026-02-20 22:45 111
}
c58dbdb7 Poul Sander 2026-02-22 17:49 112
c58dbdb7 Poul Sander 2026-02-22 17:49 113
void ImageViewWidget::zoomIn()
c58dbdb7 Poul Sander 2026-02-22 17:49 114
{
c58dbdb7 Poul Sander 2026-02-22 17:49 115
    if (m_zoomMode == FitToScreen) {
c58dbdb7 Poul Sander 2026-02-22 17:49 116
        // Calculate the current effective scale so zooming feels continuous
c58dbdb7 Poul Sander 2026-02-22 17:49 117
        double sx = static_cast<double>(width()) / m_pixmap.width();
c58dbdb7 Poul Sander 2026-02-22 17:49 118
        double sy = static_cast<double>(height()) / m_pixmap.height();
c58dbdb7 Poul Sander 2026-02-22 17:49 119
        m_zoomFactor = std::min(sx, sy);
c58dbdb7 Poul Sander 2026-02-22 17:49 120
    }
c58dbdb7 Poul Sander 2026-02-22 17:49 121
    m_zoomFactor *= 1.25;
c58dbdb7 Poul Sander 2026-02-22 17:49 122
    m_zoomMode = CustomZoom;
c58dbdb7 Poul Sander 2026-02-22 17:49 123
    update();
c58dbdb7 Poul Sander 2026-02-22 17:49 124
}
c58dbdb7 Poul Sander 2026-02-22 17:49 125
c58dbdb7 Poul Sander 2026-02-22 17:49 126
void ImageViewWidget::zoomOut()
c58dbdb7 Poul Sander 2026-02-22 17:49 127
{
c58dbdb7 Poul Sander 2026-02-22 17:49 128
    if (m_zoomMode == FitToScreen) {
c58dbdb7 Poul Sander 2026-02-22 17:49 129
        double sx = static_cast<double>(width()) / m_pixmap.width();
c58dbdb7 Poul Sander 2026-02-22 17:49 130
        double sy = static_cast<double>(height()) / m_pixmap.height();
c58dbdb7 Poul Sander 2026-02-22 17:49 131
        m_zoomFactor = std::min(sx, sy);
c58dbdb7 Poul Sander 2026-02-22 17:49 132
    }
c58dbdb7 Poul Sander 2026-02-22 17:49 133
    m_zoomFactor /= 1.25;
c58dbdb7 Poul Sander 2026-02-22 17:49 134
    if (m_zoomFactor < 0.01)
c58dbdb7 Poul Sander 2026-02-22 17:49 135
        m_zoomFactor = 0.01;
c58dbdb7 Poul Sander 2026-02-22 17:49 136
    m_zoomMode = CustomZoom;
c58dbdb7 Poul Sander 2026-02-22 17:49 137
    update();
c58dbdb7 Poul Sander 2026-02-22 17:49 138
}
c58dbdb7 Poul Sander 2026-02-22 17:49 139
c58dbdb7 Poul Sander 2026-02-22 17:49 140
void ImageViewWidget::zoomOriginal()
c58dbdb7 Poul Sander 2026-02-22 17:49 141
{
c58dbdb7 Poul Sander 2026-02-22 17:49 142
    m_zoomMode = OriginalSize;
c58dbdb7 Poul Sander 2026-02-22 17:49 143
    m_zoomFactor = 1.0;
c58dbdb7 Poul Sander 2026-02-22 17:49 144
    update();
c58dbdb7 Poul Sander 2026-02-22 17:49 145
}
c58dbdb7 Poul Sander 2026-02-22 17:49 146
c58dbdb7 Poul Sander 2026-02-22 17:49 147
void ImageViewWidget::zoomFitToScreen()
c58dbdb7 Poul Sander 2026-02-22 17:49 148
{
c58dbdb7 Poul Sander 2026-02-22 17:49 149
    m_zoomMode = FitToScreen;
c58dbdb7 Poul Sander 2026-02-22 17:49 150
    update();
c58dbdb7 Poul Sander 2026-02-22 17:49 151
}
1970-01-01 00:00 152