File indexing completed on 2025-04-27 03:58:24

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-04-30
0007  * Description : Image zoom settings
0008  *
0009  * SPDX-FileCopyrightText: 2010-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #ifndef DIGIKAM_IMAGE_ZOOM_SETTINGS_H
0016 #define DIGIKAM_IMAGE_ZOOM_SETTINGS_H
0017 
0018 // Qt includes
0019 
0020 #include <QSizeF>
0021 #include <QRectF>
0022 #include <QPointF>
0023 #include <QWidget>
0024 
0025 // Local includes
0026 
0027 #include "digikam_export.h"
0028 
0029 namespace Digikam
0030 {
0031 
0032 class DIGIKAM_EXPORT ImageZoomSettings
0033 {
0034 
0035 public:
0036 
0037     enum FitToSizeMode
0038     {
0039         AlwaysFit,
0040         OnlyScaleDown
0041     };
0042 
0043 public:
0044 
0045     ImageZoomSettings();
0046     explicit ImageZoomSettings(const QSize& imageSize, const QSize& originalSize = QSize());
0047 
0048     /**
0049      * Sets the size of the (available) image data.
0050      * Optionally, you can specify an original size, if the available image
0051      * data is a scaled-down version. In this case, zoom factors will refer
0052      * to the original size.
0053      * The zoom factor is unchanged, you need to call fitToSize again.
0054      */
0055     void setImageSize(const QSize& size, const QSize& originalSize = QSize());
0056 
0057     /**
0058      * Set the graphics view widget to track the device pixel ratio.
0059      */
0060     void setDisplayWidget(QWidget* const widget);
0061 
0062     /**
0063      * Return the currently set zoom factor
0064      */
0065     double zoomFactor()                                                             const;
0066 
0067     /**
0068      * Return the real zoom factor dependent on device pixel ratio
0069      */
0070     double realZoomFactor()                                                         const;
0071 
0072     /**
0073      * Returns the (available) image size
0074      */
0075     QSizeF imageSize()                                                              const;
0076 
0077     /**
0078      * Return the original image size. Can be identical to size().
0079      */
0080     QSizeF originalImageSize()                                                      const;
0081 
0082     /**
0083      * Return the size of the image when the current zoom factor is applied.
0084      * This is the size the image should be displayed at.
0085      */
0086     QSizeF zoomedSize()                                                             const;
0087 
0088     bool isFitToSize(const QSizeF& frameSize)                                       const;
0089 
0090     /**
0091      * Sets the current zoom factor, relative to (original) size.
0092      */
0093     void setZoomFactor(double zoom);
0094 
0095     /**
0096      * Sets the current zoom factor to the factor needed to fit
0097      * the current (original) image size into the given view size.
0098      * Aspect ratio will be respected, that means the frameSize may not be
0099      * completely filled in one dimension, and zoomedSize()
0100      * can differ from frameSize in one dimension.
0101      */
0102     void fitToSize(const QSizeF& frameSize, FitToSizeMode = AlwaysFit);
0103 
0104     /**
0105      * For a given rectangle contained in ((0,0), zoomedSize())
0106      * returns the corresponding rectangle in (0,0),imageSize().
0107      */
0108     QRectF sourceRect(const QRectF& imageRect)                                      const;
0109 
0110     QRectF mapZoomToImage(const QRectF& imageRect)                                  const;
0111 
0112     /**
0113      * For a given rect contained in ((0,0), imageSize())
0114      * returns the corresponding rectangle in (0,0),zoomedSize().
0115      */
0116     QRectF mapImageToZoom(const QRectF& imagePoint)                                 const;
0117 
0118     /**
0119      * For a given point (in (0,0), zoomedSize())
0120      * returns the corresponding point in (0,0),imageSize().
0121      */
0122     QPointF mapZoomToImage(const QPointF& zoomedPoint)                              const;
0123 
0124     /**
0125      * For a given point (in (0,0), imageSize())
0126      * returns the corresponding point in (0,0),zoomedSize().
0127      */
0128     QPointF mapImageToZoom(const QPointF& imagePoint)                               const;
0129 
0130     /**
0131      * Returns the zoom factor that would be used by fitToSize()
0132      * called with the given frameSize.
0133      */
0134     double fitToSizeZoomFactor(const QSizeF& frameSize, FitToSizeMode = AlwaysFit)  const;
0135 
0136     /**
0137      * When changing the zoom from current zoom to given nextZoom,
0138      * sometimes a special value may be crossed, and this could then be used
0139      * instead of nextZoom. Returns this special zoom, or nextZoom if not applicable.
0140      */
0141     double snappedZoomStep(double nextZoom, const QSizeF& frameSize)                const;
0142 
0143     /**
0144      * When setting a new zoom factor (absolute value), the new value may be
0145      * very close to a special value. Returns this special value if this is the case,
0146      * returns newZoom if not applicable.
0147      */
0148     double snappedZoomFactor(double newZoom, const QSizeF& frameSize)               const;
0149 
0150 private:
0151 
0152     double displayRatio()                                                           const;
0153 
0154 protected:
0155 
0156     QSizeF   m_size;
0157     double   m_zoom;
0158     double   m_zoomConst;
0159 
0160     QWidget* m_displayWidget;
0161 };
0162 
0163 } // namespace Digikam
0164 
0165 #endif // DIGIKAM_IMAGE_ZOOM_SETTINGS_H