File indexing completed on 2024-05-12 16:02:13

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2001-2005 David Faure <faure@kde.org>
0003    SPDX-FileCopyrightText: 2006, 2009 Thomas Zander <zander@kde.org>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KOZOOMHANDLER_H
0009 #define KOZOOMHANDLER_H
0010 
0011 #include "kritawidgets_export.h"
0012 #include <KoZoomMode.h>
0013 #include <KoViewConverter.h>
0014 
0015 /**
0016  * This class handles the zooming and DPI stuff (conversions between
0017  * postscript pt values and pixels). If the internal data of your
0018  * document does not work with postscript points (for instance raster
0019  * image pixels), you need to some additional converting yourself.
0020  *
0021  * An instance of KoZoomHandler operates at a given zoom  and resolution
0022  * so there is usually one instance of KoZoomHandler per view.
0023  */
0024 class KRITAWIDGETS_EXPORT KoZoomHandler : public KoViewConverter
0025 {
0026 public:
0027 
0028     KoZoomHandler();
0029     ~KoZoomHandler() override;
0030     
0031     /**
0032      * @return the conversion factor between document and view, that
0033      * includes the zoom and also the DPI setting.
0034      */
0035     inline qreal zoomedResolutionX() const { return m_zoomedResolutionX; }
0036 
0037     /**
0038      * @return the conversion factor between document and view, that
0039      * includes the zoom and also the DPI setting.
0040      */
0041     inline qreal zoomedResolutionY() const { return m_zoomedResolutionY; }
0042 
0043     inline qreal resolutionX() const { return m_resolutionX; }
0044     inline qreal resolutionY() const { return m_resolutionY; }
0045 
0046     /**
0047      * Zoom factor for X. Equivalent to zoomedResolutionX()/resolutionX()
0048      */
0049     inline qreal zoomFactorX() const { return m_zoomedResolutionX / m_resolutionX; }
0050 
0051     /**
0052      * Zoom factor for Y. Equivalent to zoomedResolutionY()/resolutionY()
0053      */
0054     inline qreal zoomFactorY() const { return m_zoomedResolutionY / m_resolutionY; }
0055 
0056 
0057     /**
0058      * Set resolution expressed in dots-per-inch
0059      */
0060     void setDpi(int dpiX, int dpiY);
0061 
0062     /**
0063      * Set a resolution for X and Y of the output device.
0064      * The zoom factor is not changed.
0065      *
0066      * This number should be the result of:
0067      * POINT_TO_INCH(static_cast<qreal>(DOTS_PER_INCH))
0068      */
0069     void setResolution(qreal resolutionX, qreal resolutionY);
0070 
0071     /**
0072      * Set the zoomed resolution for X and Y.
0073      * Compared to the setZoom... methods, this allows to set a different
0074      * zoom factor for X and for Y.
0075      */
0076     virtual void setZoomedResolution(qreal zoomedResolutionX, qreal zoomedResolutionY);
0077 
0078     /**
0079      * Change the zoom level, keeping the resolution unchanged.
0080      * @param zoom the zoom factor (e.g. 1.0 for 100%)
0081      */
0082     void setZoom(qreal zoom) override;
0083 
0084     /**
0085      * Change the zoom mode
0086      * @param zoomMode the zoom mode.
0087      */
0088     inline void setZoomMode(KoZoomMode::Mode zoomMode) { m_zoomMode = zoomMode; }
0089     /**
0090      * @return the global zoom factor (e.g. 100 for 100%).
0091      * Only use this to display to the user, don't use in calculations
0092      */
0093     inline int zoomInPercent() const { return qRound(KoViewConverter::zoom() * 100); }
0094     /**
0095      * @return the global zoom mode (e.g. KoZoomMode::ZOOM_WIDTH).
0096      * use this to determine how to zoom
0097      */
0098     KoZoomMode::Mode zoomMode() const { return m_zoomMode; }
0099 
0100     // Input: pt. Output: pixels. Resolution and zoom are applied.
0101 
0102     inline qreal zoomItX(qreal z) const
0103         {
0104             return m_zoomedResolutionX * z;
0105         }
0106 
0107     inline qreal zoomItY(qreal z) const
0108         {
0109             return m_zoomedResolutionY * z ;
0110         }
0111 
0112     // Input: pixels. Output: pt.
0113     inline qreal unzoomItX(qreal x) const
0114         {
0115             return  x / m_zoomedResolutionX;
0116         }
0117 
0118     inline qreal unzoomItY(qreal y) const
0119         {
0120             return  y / m_zoomedResolutionY;
0121         }
0122 
0123     void setZoomMarginSize( int size );
0124 
0125     int zoomMarginSize() const;
0126 
0127     // KoViewConverter-interface methods
0128 
0129     /**
0130      * Convert a coordinate in pt to pixels.
0131      * @param documentPoint the point in the document coordinate system of a KoShape.
0132      */
0133     QPointF documentToView(const QPointF &documentPoint) const override;
0134 
0135     /**
0136      * Convert a coordinate in pixels to pt.
0137      * @param viewPoint the point in the coordinate system of the widget, or window.
0138      */
0139     QPointF viewToDocument(const QPointF &viewPoint) const override;
0140 
0141     /**
0142      * Convert a rectangle in pt to pixels.
0143      * @param documentRect the rect in the document coordinate system of a KoShape.
0144      */
0145     QRectF documentToView(const QRectF &documentRect) const override;
0146 
0147     /**
0148      * Convert a rectangle in pixels to pt.
0149      * @param viewRect the rect in the coordinate system of the widget, or window.
0150      */
0151     QRectF viewToDocument(const QRectF &viewRect) const override;
0152 
0153     /**
0154      * Convert a size in pt to pixels.
0155      * @param documentSize the size in pt.
0156      * @return the size in pixels.
0157      */
0158     QSizeF documentToView(const QSizeF &documentSize) const override;
0159 
0160     /**
0161      * Convert a size in pixels to pt.
0162      * @param viewSize the size in pixels.
0163      * @return the size in pt.
0164      */
0165     QSizeF viewToDocument(const QSizeF &viewSize) const override;
0166 
0167     /**
0168      * Convert a single x coordinate in pt to pixels.
0169      * @param documentX the x coordinate in pt.
0170      * @return the x coordinate in pixels.
0171      */
0172     qreal documentToViewX(qreal documentX) const override;
0173 
0174     /**
0175      * Convert a single y coordinate in pt to pixels.
0176      * @param documentY the y coordinate in pt.
0177      * @return the y coordinate in pixels.
0178      */
0179     qreal documentToViewY(qreal documentY) const override;
0180 
0181     /**
0182      * Convert a single x coordinate in pixels to pt.
0183      * @param viewX the x coordinate in pixels.
0184      * @return the x coordinate in pt.
0185      */
0186     qreal viewToDocumentX(qreal viewX) const override;
0187 
0188     /**
0189      * Convert a single y coordinate in pixels to pt.
0190      * @param viewY the y coordinate in pixels.
0191      * @return the y coordinate in pt.
0192      */
0193     qreal viewToDocumentY(qreal viewY) const override;
0194 
0195     /**
0196      * Get the zoom levels of the individual x and y axis. Copy them to the pointer parameters.
0197      * @param zoomX a pointer to a qreal which will be modified to set the horizontal zoom.
0198      * @param zoomY a pointer to a qreal which will be modified to set the vertical zoom.
0199      */
0200     void zoom(qreal *zoomX, qreal *zoomY) const override;
0201 
0202     using KoViewConverter::zoom;
0203 
0204 protected:
0205 
0206     KoZoomMode::Mode m_zoomMode;
0207 
0208     qreal m_resolutionX;
0209     qreal m_resolutionY;
0210     qreal m_zoomedResolutionX;
0211     qreal m_zoomedResolutionY;
0212 
0213     int m_zoomMarginSize;
0214 };
0215 
0216 #endif