File indexing completed on 2024-04-28 04:42:12

0001 /* This file is part of the KDE project
0002    Copyright (C) 2001-2005 David Faure <faure@kde.org>
0003    Copyright (C) 2006, 2009 Thomas Zander <zander@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #ifndef KREPORTZOOMHANDLER_H
0022 #define KREPORTZOOMHANDLER_H
0023 
0024 #include "KReportZoomMode_p.h"
0025 
0026 class QPointF;
0027 class QRectF;
0028 class QSizeF;
0029 
0030 /**
0031  * This class handles the zooming and DPI stuff (conversions between
0032  * postscript pt values and pixels). If the internal data of your
0033  * document does not work with postscript points (for instance raster
0034  * image pixels), you need to some additional converting yourself.
0035  *
0036  * An instance of KReportZoomHandler operates at a given zoom  and resolution
0037  * so there is usually one instance of KReportZoomHandler per view.
0038  */
0039 class KReportZoomHandler
0040 {
0041 public:
0042 
0043     KReportZoomHandler();
0044     ~KReportZoomHandler();
0045 
0046     /**
0047      * @return the conversion factor between document and view, that
0048      * includes the zoom and also the DPI setting.
0049      */
0050     inline qreal zoomedResolutionX() const { return m_zoomedResolutionX; }
0051 
0052     /**
0053      * @return the conversion factor between document and view, that
0054      * includes the zoom and also the DPI setting.
0055      */
0056     inline qreal zoomedResolutionY() const { return m_zoomedResolutionY; }
0057 
0058     inline qreal resolutionX() const { return m_resolutionX; }
0059     inline qreal resolutionY() const { return m_resolutionY; }
0060 
0061     /**
0062      * Zoom factor for X. Equivalent to zoomedResolutionX()/resolutionX()
0063      */
0064     inline qreal zoomFactorX() const { return m_zoomedResolutionX / m_resolutionX; }
0065 
0066     /**
0067      * Zoom factor for Y. Equivalent to zoomedResolutionY()/resolutionY()
0068      */
0069     inline qreal zoomFactorY() const { return m_zoomedResolutionY / m_resolutionY; }
0070 
0071 
0072     /**
0073      * Set resolution expressed in dots-per-inch
0074      */
0075     void setDpi(int dpiX, int dpiY);
0076 
0077     /**
0078      * Set a resolution for X and Y of the output device.
0079      * The zoom factor is not changed.
0080      *
0081      * This number should be the result of:
0082      * POINT_TO_INCH(static_cast<qreal>(DOTS_PER_INCH))
0083      */
0084     void setResolution(qreal resolutionX, qreal resolutionY);
0085 
0086     /**
0087      * Set the resolution for X and Y to the display values reported by KGlobal.
0088      * The zoom factor is not changed.
0089      */
0090     void setResolutionToStandard( );
0091 
0092     /**
0093      * Set the zoomed resolution for X and Y.
0094      * Compared to the setZoom... methods, this allows to set a different
0095      * zoom factor for X and for Y.
0096      */
0097     void setZoomedResolution(qreal zoomedResolutionX, qreal zoomedResolutionY);
0098 
0099     /**
0100      * Change the zoom level, keeping the resolution unchanged.
0101      * @param zoom the zoom factor (e.g. 1.0 for 100%)
0102      */
0103     void setZoom(qreal zoom);
0104 
0105     /**
0106      * Change the zoom type
0107      * @param zoomType the zoom type.
0108      */
0109     inline void setZoomMode(KReportZoomMode::Type zoomType) { m_zoomType = zoomType; }
0110     /**
0111      * @return the global zoom factor (e.g. 100 for 100%).
0112      * Only use this to display to the user, don't use in calculations
0113      */
0114     inline int zoomInPercent() const { return qRound(zoom() * 100); }
0115 
0116     /**
0117      * @return the global zoom type (e.g. KReportZoomMode::Type::Width).
0118      * use this to determine how to zoom
0119      */
0120     KReportZoomMode::Type zoomType() const { return m_zoomType; }
0121 
0122     // Input: pt. Output: pixels. Resolution and zoom are applied.
0123 
0124     inline qreal zoomItX(qreal z) const
0125         {
0126             return m_zoomedResolutionX * z;
0127         }
0128 
0129     inline qreal zoomItY(qreal z) const
0130         {
0131             return m_zoomedResolutionY * z ;
0132         }
0133 
0134     // Input: pixels. Output: pt.
0135     inline qreal unzoomItX(qreal x) const
0136         {
0137             return  x / m_zoomedResolutionX;
0138         }
0139 
0140     inline qreal unzoomItY(qreal y) const
0141         {
0142             return  y / m_zoomedResolutionY;
0143         }
0144 
0145     /**
0146      * Convert a coordinate in pt to pixels.
0147      * @param documentPoint the point in the document coordinate system of a KoShape.
0148      */
0149     QPointF documentToView(const QPointF &documentPoint) const;
0150 
0151     /**
0152      * Convert a coordinate in pixels to pt.
0153      * @param viewPoint the point in the coordinate system of the widget, or window.
0154      */
0155     QPointF viewToDocument(const QPointF &viewPoint) const;
0156 
0157     /**
0158      * Convert a rectangle in pt to pixels.
0159      * @param documentRect the rect in the document coordinate system of a KoShape.
0160      */
0161     QRectF documentToView(const QRectF &documentRect) const;
0162 
0163     /**
0164      * Convert a rectangle in pixels to pt.
0165      * @param viewRect the rect in the coordinate system of the widget, or window.
0166      */
0167     QRectF viewToDocument(const QRectF &viewRect) const;
0168 
0169     /**
0170      * Convert a size in pt to pixels.
0171      * @param documentSize the size in pt.
0172      * @return the size in pixels.
0173      */
0174     QSizeF documentToView(const QSizeF &documentSize) const;
0175 
0176     /**
0177      * Convert a size in pixels to pt.
0178      * @param viewSize the size in pixels.
0179      * @return the size in pt.
0180      */
0181     QSizeF viewToDocument(const QSizeF &viewSize) const;
0182 
0183     /**
0184      * Convert a single x coordinate in pt to pixels.
0185      * @param documentX the x coordinate in pt.
0186      * @return the x coordinate in pixels.
0187      */
0188     qreal documentToViewX(qreal documentX) const;
0189 
0190     /**
0191      * Convert a single y coordinate in pt to pixels.
0192      * @param documentY the y coordinate in pt.
0193      * @return the y coordinate in pixels.
0194      */
0195     qreal documentToViewY(qreal documentY) const;
0196 
0197     /**
0198      * Convert a single x coordinate in pixels to pt.
0199      * @param viewX the x coordinate in pixels.
0200      * @return the x coordinate in pt.
0201      */
0202     qreal viewToDocumentX(qreal viewX) const;
0203 
0204     /**
0205      * Convert a single y coordinate in pixels to pt.
0206      * @param viewY the y coordinate in pixels.
0207      * @return the y coordinate in pt.
0208      */
0209     qreal viewToDocumentY(qreal viewY) const;
0210 
0211     /**
0212      * Get the zoom levels of the individual x and y axis. Copy them to the pointer parameters.
0213      * @param zoomX a pointer to a qreal which will be modified to set the horizontal zoom.
0214      * @param zoomY a pointer to a qreal which will be modified to set the vertical zoom.
0215      */
0216     void zoom(qreal *zoomX, qreal *zoomY) const;
0217 
0218     /**
0219      * Return the current zoom level. 1.0 is 100%.
0220      */
0221     qreal zoom() const;
0222 
0223 private:
0224     KReportZoomMode::Type m_zoomType;
0225 
0226     qreal m_resolutionX;
0227     qreal m_resolutionY;
0228     qreal m_zoomedResolutionX;
0229     qreal m_zoomedResolutionY;
0230 
0231     qreal m_zoomLevel; // 1.0 is 100%
0232 };
0233 
0234 #endif