File indexing completed on 2024-05-12 08:34:05

0001 /* ============================================================
0002 * Date        : 2008-08-26
0003 * Description : Preview image viewer.
0004 *
0005 * SPDX-FileCopyrightText: 2008-2012 Kåre Särs <kare.sars@iki .fi>
0006 *
0007 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0008 *
0009 * ============================================================ */
0010 
0011 #include "ImageViewer.h"
0012 
0013 #include <QGraphicsPixmapItem>
0014 #include <QGraphicsScene>
0015 #include <QScrollBar>
0016 #include <QAction>
0017 #include <QIcon>
0018 #include <QWheelEvent>
0019 
0020 #include <KLocalizedString>
0021 
0022 struct ImageViewer::Private {
0023     QGraphicsScene      *scene;
0024     QImage              *img;
0025 
0026     QAction *zoomInAction;
0027     QAction *zoomOutAction;
0028     QAction *zoom100Action;
0029     QAction *zoom2FitAction;
0030 };
0031 
0032 ImageViewer::ImageViewer(QWidget *parent) : QGraphicsView(parent), d(new Private)
0033 {
0034     //setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
0035     //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
0036     setMouseTracking(true);
0037 
0038     // Init the scene
0039     d->scene = new QGraphicsScene;
0040     setScene(d->scene);
0041 
0042     // create context menu
0043     d->zoomInAction = new QAction(QIcon::fromTheme(QStringLiteral("zoom-in")), i18n("Zoom In"), this);
0044     connect(d->zoomInAction, &QAction::triggered, this, &ImageViewer::zoomIn);
0045 
0046     d->zoomOutAction = new QAction(QIcon::fromTheme(QStringLiteral("zoom-out")), i18n("Zoom Out"), this);
0047     connect(d->zoomOutAction, &QAction::triggered, this, &ImageViewer::zoomOut);
0048 
0049     d->zoom100Action = new QAction(QIcon::fromTheme(QStringLiteral("zoom-fit-best")), i18n("Zoom to Actual size"), this);
0050     connect(d->zoom100Action, &QAction::triggered, this, &ImageViewer::zoomActualSize);
0051 
0052     d->zoom2FitAction = new QAction(QIcon::fromTheme(QStringLiteral("document-preview")), i18n("Zoom to Fit"), this);
0053     connect(d->zoom2FitAction, &QAction::triggered, this, &ImageViewer::zoom2Fit);
0054 
0055     addAction(d->zoomInAction);
0056     addAction(d->zoomOutAction);
0057     addAction(d->zoom100Action);
0058     addAction(d->zoom2FitAction);
0059     setContextMenuPolicy(Qt::ActionsContextMenu);
0060 }
0061 
0062 // ------------------------------------------------------------------------
0063 ImageViewer::~ImageViewer()
0064 {
0065     delete d;
0066 }
0067 
0068 // ------------------------------------------------------------------------
0069 void ImageViewer::setQImage(QImage *img)
0070 {
0071     if (img == nullptr) {
0072         return;
0073     }
0074 
0075     d->img = img;
0076     const auto dpr = devicePixelRatioF();
0077     d->img->setDevicePixelRatio(dpr);
0078     d->scene->setSceneRect(0, 0, img->width() / dpr, img->height() / dpr);
0079 }
0080 
0081 // ------------------------------------------------------------------------
0082 void ImageViewer::drawBackground(QPainter *painter, const QRectF &rect)
0083 {
0084     painter->fillRect(rect, QColor(0x70, 0x70, 0x70));
0085     QRectF r = rect & sceneRect();
0086     const auto dpr = d->img->devicePixelRatio();
0087     QRectF srcRect = QRectF(r.topLeft() * dpr, r.size() * dpr);
0088     painter->drawImage(r, *d->img, srcRect);
0089 }
0090 
0091 // ------------------------------------------------------------------------
0092 void ImageViewer::zoomIn()
0093 {
0094     scale(1.5, 1.5);
0095 }
0096 
0097 // ------------------------------------------------------------------------
0098 void ImageViewer::zoomOut()
0099 {
0100     scale(1.0 / 1.5, 1.0 / 1.5);
0101 }
0102 
0103 // ------------------------------------------------------------------------
0104 void ImageViewer::zoomActualSize()
0105 {
0106     resetTransform();
0107 }
0108 
0109 // ------------------------------------------------------------------------
0110 void ImageViewer::zoom2Fit()
0111 {
0112     QRectF r = d->img->rect();
0113     const auto dpr = d->img->devicePixelRatio();
0114     r = QRectF(r.topLeft() / dpr, r.size() / dpr);
0115     fitInView(r, Qt::KeepAspectRatio);
0116 }
0117 
0118 // ------------------------------------------------------------------------
0119 void ImageViewer::wheelEvent(QWheelEvent *e)
0120 {
0121     if (e->modifiers() == Qt::ControlModifier) {
0122         if (e->angleDelta().y() > 0) {
0123             zoomIn();
0124         }
0125         else {
0126             zoomOut();
0127         }
0128     }
0129     else {
0130         QGraphicsView::wheelEvent(e);
0131     }
0132 }
0133 
0134 
0135 #include "moc_ImageViewer.cpp"