File indexing completed on 2024-05-12 04:19:38

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2011 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 #ifndef ABSTRACTIMAGEVIEW_H
0022 #define ABSTRACTIMAGEVIEW_H
0023 
0024 // Local
0025 #include <lib/document/document.h>
0026 
0027 // KF
0028 
0029 // Qt
0030 #include <QGraphicsWidget>
0031 
0032 namespace Gwenview
0033 {
0034 class AlphaBackgroundItem;
0035 
0036 struct AbstractImageViewPrivate;
0037 /**
0038  * The abstract base class used to implement common functionality of raster and vector image views
0039  * like for example zooming, computing the area where the image will be displayed in the view and
0040  * dealing with a background for transparent areas.
0041  */
0042 class AbstractImageView : public QGraphicsWidget
0043 {
0044     Q_OBJECT
0045 public:
0046     enum UpdateType {
0047         UpdateIfNecessary,
0048         ForceUpdate,
0049     };
0050     enum AlphaBackgroundMode {
0051         AlphaBackgroundNone,
0052         AlphaBackgroundCheckBoard,
0053         AlphaBackgroundSolid,
0054     };
0055 
0056     AbstractImageView(QGraphicsItem *parent);
0057     ~AbstractImageView() override;
0058 
0059     qreal zoom() const;
0060 
0061     virtual void setZoom(qreal zoom, const QPointF &center = QPointF(-1, -1), UpdateType updateType = UpdateIfNecessary);
0062 
0063     bool zoomToFit() const;
0064 
0065     bool zoomToFill() const;
0066 
0067     virtual void setZoomToFit(bool value);
0068 
0069     virtual void setZoomToFill(bool value, const QPointF &center = QPointF(-1, -1));
0070 
0071     virtual void setDocument(const Document::Ptr &doc);
0072 
0073     Document::Ptr document() const;
0074 
0075     qreal computeZoomToFit() const;
0076 
0077     qreal computeZoomToFill() const;
0078 
0079     QSizeF documentSize() const;
0080 
0081     /**
0082      * Returns the size of the loaded document in device independent pixels.
0083      */
0084     QSizeF dipDocumentSize() const;
0085 
0086     /*
0087      * The size of the image that is currently visible,
0088      * in device independent pixels.
0089      */
0090     QSizeF visibleImageSize() const;
0091 
0092     /**
0093      * If the image is smaller than the view, imageOffset is the distance from
0094      * the topleft corner of the view to the topleft corner of the image.
0095      * Neither x nor y can be negative.
0096      * This is in device independent pixels.
0097      */
0098     QPointF imageOffset() const;
0099 
0100     /**
0101      * The scroll position, in zoomed image coordinates.
0102      * x and y are always between 0 and (docsize * zoom - viewsize)
0103      */
0104     QPointF scrollPos() const;
0105     void setScrollPos(const QPointF &pos);
0106 
0107     qreal devicePixelRatio() const;
0108 
0109     QPointF mapToView(const QPointF &imagePos) const;
0110     QPoint mapToView(const QPoint &imagePos) const;
0111     QRectF mapToView(const QRectF &imageRect) const;
0112     QRect mapToView(const QRect &imageRect) const;
0113 
0114     QPointF mapToImage(const QPointF &viewPos) const;
0115     QPoint mapToImage(const QPoint &viewPos) const;
0116     QRectF mapToImage(const QRectF &viewRect) const;
0117     QRect mapToImage(const QRect &viewRect) const;
0118 
0119     void setEnlargeSmallerImages(bool value);
0120 
0121     void applyPendingScrollPos();
0122 
0123     void resetDragCursor();
0124 
0125     AlphaBackgroundItem *backgroundItem() const;
0126 
0127 public Q_SLOTS:
0128     void updateCursor();
0129 
0130 Q_SIGNALS:
0131     /** Emitted when the zoom mode changes to or from "Fit". */
0132     void zoomToFitChanged(bool);
0133     /** Emitted when the zoom mode changes to or from "Fill". */
0134     void zoomToFillChanged(bool);
0135     /** Emitted when the zoom value changes in any way. */
0136     void zoomChanged(qreal);
0137     void zoomInRequested(const QPointF &);
0138     void zoomOutRequested(const QPointF &);
0139     void scrollPosChanged();
0140     void completed();
0141     void previousImageRequested();
0142     void nextImageRequested();
0143     void toggleFullScreenRequested();
0144 
0145 protected:
0146     virtual void loadFromDocument() = 0;
0147     virtual void onZoomChanged() = 0;
0148     /**
0149      * Called when the offset changes.
0150      * Note: to avoid multiple adjustments, this is not called if zoom changes!
0151      */
0152     virtual void onImageOffsetChanged() = 0;
0153     /**
0154      * Called when the scrollPos changes.
0155      * Note: to avoid multiple adjustments, this is not called if zoom changes!
0156      */
0157     virtual void onScrollPosChanged(const QPointF &oldPos) = 0;
0158 
0159     void onImageRectUpdated();
0160 
0161     void resizeEvent(QGraphicsSceneResizeEvent *event) override;
0162     void focusInEvent(QFocusEvent *event) override;
0163 
0164     void keyPressEvent(QKeyEvent *event) override;
0165     void keyReleaseEvent(QKeyEvent *event) override;
0166     void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
0167     void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
0168     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
0169     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
0170 
0171 private:
0172     friend struct AbstractImageViewPrivate;
0173     AbstractImageViewPrivate *const d;
0174 };
0175 
0176 } // namespace
0177 
0178 #endif /* ABSTRACTIMAGEVIEW_H */