File: graphicsview.cpp

package info (click to toggle)
pineapple-pictures 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,424 kB
  • sloc: cpp: 3,794; xml: 339; sh: 8; makefile: 2
file content (509 lines) | stat: -rw-r--r-- 16,690 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// SPDX-FileCopyrightText: 2025 Gary Wang <git@blumia.net>
//
// SPDX-License-Identifier: MIT

#include "graphicsview.h"

#include "graphicsscene.h"
#include "settings.h"

#include <QDebug>
#include <QColorSpace>
#include <QMouseEvent>
#include <QScrollBar>
#include <QMimeData>
#include <QImageReader>
#include <QStyleOptionGraphicsItem>

GraphicsView::GraphicsView(QWidget *parent)
    : QGraphicsView (parent)
{
    setDragMode(QGraphicsView::ScrollHandDrag);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setResizeAnchor(QGraphicsView::AnchorUnderMouse);
    setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    setStyleSheet("background-color: rgba(0, 0, 0, 220);"
                  "border-radius: 3px;");
    setAcceptDrops(false);
    setCheckerboardEnabled(false);

    connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &GraphicsView::viewportRectChanged);
    connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &GraphicsView::viewportRectChanged);
}

void GraphicsView::showFileFromPath(const QString &filePath)
{
    emit navigatorViewRequired(false, transform());

    if (filePath.endsWith(".svg")) {
        showSvg(filePath);
    } else {
        QImageReader imageReader(filePath);
        imageReader.setAutoTransform(true);
        imageReader.setDecideFormatFromContent(true);
        imageReader.setAllocationLimit(0);

        // Since if the image format / plugin does not support this feature, imageFormat() will returns an invalid format.
        // So we cannot use imageFormat() and check if it returns QImage::Format_Invalid to detect if we support the file.
        // QImage::Format imageFormat = imageReader.imageFormat();
        if (imageReader.format().isEmpty()) {
            showText(tr("File is not a valid image"));
        } else if (imageReader.supportsAnimation() && imageReader.imageCount() > 1) {
            showAnimated(filePath);
        } else if (!imageReader.canRead()) {
            showText(tr("Image data is invalid or currently unsupported"));
        } else {
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
            QImage && img = imageReader.read();
            if (img.isNull()) {
                showText(tr("Image data is invalid or currently unsupported"));
            } else {
                if (img.format() == QImage::Format_CMYK8888) {
                    img.convertToColorSpace(QColorSpace::SRgb);
                }
                img.setDevicePixelRatio(devicePixelRatioF());
                showImage(img);
            }
#else
            QPixmap && pixmap = QPixmap::fromImageReader(&imageReader);
            if (pixmap.isNull()) {
                showText(tr("Image data is invalid or currently unsupported"));
            } else {
                pixmap.setDevicePixelRatio(devicePixelRatioF());
                showImage(pixmap);
            }
#endif // QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
        }
    }
}

void GraphicsView::showImage(const QPixmap &pixmap)
{
    resetTransform();
    scene()->showImage(pixmap);
    displayScene();
}

void GraphicsView::showImage(const QImage &image)
{
    resetTransform();
    scene()->showImage(QPixmap::fromImage(image));
    displayScene();
}

void GraphicsView::showText(const QString &text)
{
    resetTransform();
    scene()->showText(text);
    displayScene();
}

void GraphicsView::showSvg(const QString &filepath)
{
    resetTransform();
    scene()->showSvg(filepath);
    displayScene();
}

void GraphicsView::showAnimated(const QString &filepath)
{
    resetTransform();
    scene()->showAnimated(filepath);
    displayScene();
}

GraphicsScene *GraphicsView::scene() const
{
    return qobject_cast<GraphicsScene*>(QGraphicsView::scene());
}

void GraphicsView::setScene(GraphicsScene *scene)
{
    return QGraphicsView::setScene(scene);
}

qreal GraphicsView::scaleFactor() const
{
    return QStyleOptionGraphicsItem::levelOfDetailFromTransform(transform());
}

void GraphicsView::resetTransform()
{
    if (!shouldAvoidTransform()) {
        QGraphicsView::resetTransform();
    }
}

void GraphicsView::zoomView(qreal scaleFactor)
{
    m_enableFitInView = false;
    m_longImageMode = false;
    scale(scaleFactor, scaleFactor);
    applyTransformationModeByScaleFactor();
    emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), transform());
}

// This is always according to user's view.
// the direction of the rotation will NOT be clockwise because the y-axis points downwards.
void GraphicsView::rotateView(bool clockwise)
{
    resetScale();

    QTransform tf(0,                  clockwise ? 1 : -1, 0,
                  clockwise ? -1 : 1, 0,                  0,
                  0,                  0,                  1);
    tf = transform() * tf;
    setTransform(tf);
    
    // Apply transformation mode but don't emit navigator signal here
    // Let displayScene() handle the navigator visibility correctly
    applyTransformationModeByScaleFactor();
}

void GraphicsView::flipView(bool horizontal)
{
    QTransform tf(horizontal ? -1 : 1, 0,                   0,
                  0,                   horizontal ? 1 : -1, 0,
                  0,                   0,                   1);
    tf = transform() * tf;
    setTransform(tf);

    // Ensure the navigation view is also flipped.
    emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), transform());
}

void GraphicsView::resetScale()
{
    setTransform(resetScale(transform()));
    applyTransformationModeByScaleFactor();
    emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), transform());
}

void GraphicsView::fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode)
{
    QGraphicsView::fitInView(rect, aspectRadioMode);
    applyTransformationModeByScaleFactor();
}

void GraphicsView::fitByOrientation(Qt::Orientation ori, bool scaleDownOnly)
{
    resetScale();

    QRectF viewRect = this->viewport()->rect();
    QRectF imageRect = transform().mapRect(sceneRect());
    QSize viewSize = viewRect.size().toSize();

    qreal ratio;

    if (ori == Qt::Horizontal) {
        // Horizontal fit means fit by width
        if (scaleDownOnly && imageRect.width() <= viewSize.width()) {
            // Image width already fits, no scaling needed
            ratio = 1;
        } else {
            ratio = viewRect.width() / imageRect.width();
        }
    } else {
        // Vertical fit means fit by height
        if (scaleDownOnly && imageRect.height() <= viewSize.height()) {
            // Image height already fits, no scaling needed
            ratio = 1;
        } else {
            ratio = viewRect.height() / imageRect.height();
        }
    }

    if (ratio != 1) {
        scale(ratio, ratio);
    }

    // Position the image correctly based on orientation with rotation consideration
    QRectF originalScene = sceneRect();
    QTransform currentTransform = transform();

    if (ori == Qt::Horizontal) {
        // For horizontal fit (fit by width), position at top (for tall images)
        // Find the scene point that corresponds to the top-center of the transformed image
        QPointF sceneTopCenter;

        if (qFuzzyIsNull(currentTransform.m12()) && qFuzzyIsNull(currentTransform.m21())) {
            // 0° or 180° rotation
            if (currentTransform.m11() > 0 && currentTransform.m22() > 0) {
                // 0° rotation: use original top-center
                sceneTopCenter = QPointF(originalScene.center().x(), originalScene.top());
            } else {
                // 180° rotation: the visual "top" is now at the scene bottom
                sceneTopCenter = QPointF(originalScene.center().x(), originalScene.bottom());
            }
        } else {
            // 90/270 degree rotation: the "top" in view corresponds to left/right in scene
            if (currentTransform.m12() > 0) {
                // 90 degree: top in view = left in scene
                sceneTopCenter = QPointF(originalScene.left(), originalScene.center().y());
            } else {
                // 270 degree: top in view = right in scene
                sceneTopCenter = QPointF(originalScene.right(), originalScene.center().y());
            }
        }
        centerOn(sceneTopCenter);
    } else {
        // For vertical fit (fit by height), position at left (for wide images)
        // Find the scene point that corresponds to the left-center of the transformed image
        QPointF sceneLeftCenter;

        if (qFuzzyIsNull(currentTransform.m12()) && qFuzzyIsNull(currentTransform.m21())) {
            // 0° or 180° rotation
            if (currentTransform.m11() > 0 && currentTransform.m22() > 0) {
                // 0° rotation: use original left-center
                sceneLeftCenter = QPointF(originalScene.left(), originalScene.center().y());
            } else {
                // 180° rotation: the visual "left" is now at the scene right
                sceneLeftCenter = QPointF(originalScene.right(), originalScene.center().y());
            }
        } else {
            // 90/270 degree rotation: the "left" in view corresponds to top/bottom in scene
            if (currentTransform.m21() > 0) {
                // 90 degree: left in view = top in scene
                sceneLeftCenter = QPointF(originalScene.center().x(), originalScene.top());
            } else {
                // 270 degree: left in view = bottom in scene
                sceneLeftCenter = QPointF(originalScene.center().x(), originalScene.bottom());
            }
        }
        centerOn(sceneLeftCenter);
    }

    m_enableFitInView = false;

    applyTransformationModeByScaleFactor();
    emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), transform());
}

bool GraphicsView::isLongImage() const
{
    // Get the transformed image size (considering rotation and other transforms)
    QRectF transformedRect = transform().mapRect(sceneRect());
    QSizeF imageSize = transformedRect.size();
    
    if (imageSize.isEmpty()) return false;
    
    qreal aspectRatio = imageSize.width() / imageSize.height();
    
    // Check if aspect ratio exceeds 5:2 (wide) or 2:5 (tall)
    return aspectRatio > 2.5 || aspectRatio < 0.4;
}

void GraphicsView::fitLongImage()
{
    QRectF transformedRect = transform().mapRect(sceneRect());

    if (transformedRect.width() < transformedRect.height()) {
        fitByOrientation(Qt::Horizontal, true);
    } else {
        fitByOrientation(Qt::Vertical, true);
    }
}

void GraphicsView::displayScene()
{
    if (shouldAvoidTransform()) {
        emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), transform());
        return;
    }

    // Check if should apply long image mode
    if (Settings::instance()->autoLongImageMode() && isLongImage()) {
        m_longImageMode = true;
        m_firstUserMediaLoaded = true;
        if (isSceneBiggerThanView()) fitLongImage();
        return;
    }

    if (isSceneBiggerThanView()) {
        // Do fit-in-view
        fitInView(sceneRect(), Qt::KeepAspectRatio);
        // After fitInView, the image should fit the window, so hide navigator
        emit navigatorViewRequired(false, transform());
    } else {
        // Image is already smaller than window, no navigator needed
        emit navigatorViewRequired(false, transform());
    }

    m_longImageMode = false;
    m_enableFitInView = true;
    m_firstUserMediaLoaded = true;
}

bool GraphicsView::isSceneBiggerThanView() const
{
    if (!isThingSmallerThanWindowWith(transform())) {
        return true;
    } else {
        return false;
    }
}

// Automately do fit in view when viewport(window) smaller than image original size.
void GraphicsView::setEnableAutoFitInView(bool enable)
{
    m_enableFitInView = enable;
}

void GraphicsView::setLongImageMode(bool enable)
{
    m_longImageMode = enable;
}

bool GraphicsView::avoidResetTransform() const
{
    return m_avoidResetTransform;
}

void GraphicsView::setAvoidResetTransform(bool avoidReset)
{
    m_avoidResetTransform = avoidReset;
}

inline double zeroOrOne(double number)
{
    return qFuzzyIsNull(number) ? 0 : (number > 0 ? 1 : -1);
}

// Note: this only works if we only have 90 degree based rotation
//       and no shear/translate.
QTransform GraphicsView::resetScale(const QTransform & orig)
{
    return QTransform(zeroOrOne(orig.m11()), zeroOrOne(orig.m12()),
                      zeroOrOne(orig.m21()), zeroOrOne(orig.m22()),
                      orig.dx(),             orig.dy());
}

void GraphicsView::toggleCheckerboard(bool invertCheckerboardColor)
{
    setCheckerboardEnabled(!m_checkerboardEnabled, invertCheckerboardColor);
}

void GraphicsView::mousePressEvent(QMouseEvent *event)
{
    if (shouldIgnoreMousePressMoveEvent(event)) {
        event->ignore();
        // blumia: return here, or the QMouseEvent event transparency won't
        //         work if we set a QGraphicsView::ScrollHandDrag drag mode.
        return;
    }

    return QGraphicsView::mousePressEvent(event);
}

void GraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    if (shouldIgnoreMousePressMoveEvent(event)) {
        event->ignore();
    }

    return QGraphicsView::mouseMoveEvent(event);
}

void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::ForwardButton || event->button() == Qt::BackButton) {
        event->ignore();
    } else {
        QGraphicsItem *item = itemAt(event->pos());
        if (!item) {
            event->ignore();
        }
    }

    return QGraphicsView::mouseReleaseEvent(event);
}

void GraphicsView::wheelEvent(QWheelEvent *event)
{
    event->ignore();
    // blumia: no need for calling parent method.
}

void GraphicsView::resizeEvent(QResizeEvent *event)
{
    if (m_longImageMode) {
        // In long image mode, reapply long image logic on resize
        // We directly apply the long image mode logic without rechecking
        // if we should enter long image mode, as the mode is already active
        fitLongImage();
    } else if (m_enableFitInView) {
        bool originalSizeSmallerThanWindow = isThingSmallerThanWindowWith(resetScale(transform()));
        if (originalSizeSmallerThanWindow && scaleFactor() >= 1) {
            // no longer need to do fitInView()
            // but we leave the m_enableFitInView value unchanged in case
            // user resize down the window again.
        } else if (originalSizeSmallerThanWindow && scaleFactor() < 1) {
            resetScale();
        } else {
            fitInView(sceneRect(), Qt::KeepAspectRatio);
        }
    } else {
        emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), transform());
    }
    return QGraphicsView::resizeEvent(event);
}

bool GraphicsView::isThingSmallerThanWindowWith(const QTransform &transform) const
{
    return rect().size().expandedTo(transform.mapRect(sceneRect()).size().toSize())
            == rect().size();
}

bool GraphicsView::shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const
{
    if (event->buttons() == Qt::NoButton) {
        return true;
    }

    QGraphicsItem *item = itemAt(event->pos());
    if (!item) {
        return true;
    }

    if (isThingSmallerThanWindowWith(transform())) {
        return true;
    }

    return false;
}

void GraphicsView::setCheckerboardEnabled(bool enabled, bool invertColor)
{
    m_checkerboardEnabled = enabled;
    bool isLightCheckerboard = Settings::instance()->useLightCheckerboard() ^ invertColor;
    if (m_checkerboardEnabled) {
        // Prepare background check-board pattern
        QPixmap tilePixmap(0x20, 0x20);
        tilePixmap.fill(isLightCheckerboard ? QColor(220, 220, 220, 170) : QColor(35, 35, 35, 170));
        QPainter tilePainter(&tilePixmap);
        constexpr QColor color(45, 45, 45, 170);
        constexpr QColor invertedColor(210, 210, 210, 170);
        tilePainter.fillRect(0, 0, 0x10, 0x10, isLightCheckerboard ? invertedColor : color);
        tilePainter.fillRect(0x10, 0x10, 0x10, 0x10, isLightCheckerboard ? invertedColor : color);
        tilePainter.end();

        setBackgroundBrush(tilePixmap);
    } else {
        setBackgroundBrush(Qt::transparent);
    }
}

void GraphicsView::applyTransformationModeByScaleFactor()
{
    if (this->scaleFactor() < 1) {
        scene()->trySetTransformationMode(Qt::SmoothTransformation, this->scaleFactor());
    } else {
        scene()->trySetTransformationMode(Qt::FastTransformation, this->scaleFactor());
    }
}

bool GraphicsView::shouldAvoidTransform() const
{
    return m_firstUserMediaLoaded && m_avoidResetTransform;
}