1 /* This file is part of the KDE project 2 Copyright (C) 2001-2005 David Faure <faure@kde.org> 3 Copyright (C) 2006, 2009 Thomas Zander <zander@kde.org> 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Library General Public 7 License as published by the Free Software Foundation; either 8 version 2 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Library General Public License for more details. 14 15 You should have received a copy of the GNU Library General Public License 16 along with this library; see the file COPYING.LIB. If not, write to 17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 */ 20 21 #ifndef KOZOOMHANDLER_H 22 #define KOZOOMHANDLER_H 23 24 #include "kowidgets_export.h" 25 #include <KoZoomMode.h> 26 #include <KoViewConverter.h> 27 28 /** 29 * This class handles the zooming and DPI stuff (conversions between 30 * postscript pt values and pixels). If the internal data of your 31 * document does not work with postscript points (for instance raster 32 * image pixels), you need to some additional converting yourself. 33 * 34 * An instance of KoZoomHandler operates at a given zoom and resolution 35 * so there is usually one instance of KoZoomHandler per view. 36 */ 37 class KOWIDGETS_EXPORT KoZoomHandler : public KoViewConverter 38 { 39 public: 40 41 KoZoomHandler(); 42 ~KoZoomHandler() override = default; 43 44 /** 45 * @return the conversion factor between document and view, that 46 * includes the zoom and also the DPI setting. 47 */ zoomedResolutionX()48 inline qreal zoomedResolutionX() const { return m_zoomedResolutionX; } 49 50 /** 51 * @return the conversion factor between document and view, that 52 * includes the zoom and also the DPI setting. 53 */ zoomedResolutionY()54 inline qreal zoomedResolutionY() const { return m_zoomedResolutionY; } 55 resolutionX()56 inline qreal resolutionX() const { return m_resolutionX; } resolutionY()57 inline qreal resolutionY() const { return m_resolutionY; } 58 59 /** 60 * Zoom factor for X. Equivalent to zoomedResolutionX()/resolutionX() 61 */ zoomFactorX()62 inline qreal zoomFactorX() const { return m_zoomedResolutionX / m_resolutionX; } 63 64 /** 65 * Zoom factor for Y. Equivalent to zoomedResolutionY()/resolutionY() 66 */ zoomFactorY()67 inline qreal zoomFactorY() const { return m_zoomedResolutionY / m_resolutionY; } 68 69 70 /** 71 * Set resolution expressed in dots-per-inch 72 */ 73 void setDpi(int dpiX, int dpiY); 74 75 /** 76 * Set a resolution for X and Y of the output device. 77 * The zoom factor is not changed. 78 * 79 * This number should be the result of: 80 * POINT_TO_INCH(static_cast<qreal>(DOTS_PER_INCH)) 81 */ 82 void setResolution(qreal resolutionX, qreal resolutionY); 83 84 /** 85 * Set the resolution for X and Y to the display values reported by KGlobal. 86 * The zoom factor is not changed. 87 */ 88 void setResolutionToStandard( ); 89 90 /** 91 * Set the zoomed resolution for X and Y. 92 * Compared to the setZoom... methods, this allows to set a different 93 * zoom factor for X and for Y. 94 */ 95 virtual void setZoomedResolution(qreal zoomedResolutionX, qreal zoomedResolutionY); 96 97 /** 98 * Change the zoom level, keeping the resolution unchanged. 99 * @param zoom the zoom factor (e.g. 1.0 for 100%) 100 */ 101 void setZoom(qreal zoom) override; 102 103 /** 104 * Change the zoom mode 105 * @param zoomMode the zoom mode. 106 */ setZoomMode(KoZoomMode::Mode zoomMode)107 inline void setZoomMode(KoZoomMode::Mode zoomMode) { m_zoomMode = zoomMode; } 108 /** 109 * @return the global zoom factor (e.g. 100 for 100%). 110 * Only use this to display to the user, don't use in calculations 111 */ zoomInPercent()112 inline int zoomInPercent() const { return qRound(KoViewConverter::zoom() * 100); } 113 /** 114 * @return the global zoom mode (e.g. KoZoomMode::ZOOM_WIDTH). 115 * use this to determine how to zoom 116 */ zoomMode()117 KoZoomMode::Mode zoomMode() const { return m_zoomMode; } 118 119 // Input: pt. Output: pixels. Resolution and zoom are applied. 120 zoomItX(qreal z)121 inline qreal zoomItX(qreal z) const 122 { 123 return m_zoomedResolutionX * z; 124 } 125 zoomItY(qreal z)126 inline qreal zoomItY(qreal z) const 127 { 128 return m_zoomedResolutionY * z ; 129 } 130 131 // Input: pixels. Output: pt. unzoomItX(qreal x)132 inline qreal unzoomItX(qreal x) const 133 { 134 return x / m_zoomedResolutionX; 135 } 136 unzoomItY(qreal y)137 inline qreal unzoomItY(qreal y) const 138 { 139 return y / m_zoomedResolutionY; 140 } 141 142 // KoViewConverter-interface methods 143 144 /** 145 * Convert a coordinate in pt to pixels. 146 * @param documentPoint the point in the document coordinate system of a KoShape. 147 */ 148 QPointF documentToView(const QPointF &documentPoint) const override; 149 150 /** 151 * Convert a coordinate in pixels to pt. 152 * @param viewPoint the point in the coordinate system of the widget, or window. 153 */ 154 QPointF viewToDocument(const QPointF &viewPoint) const override; 155 156 /** 157 * Convert a rectangle in pt to pixels. 158 * @param documentRect the rect in the document coordinate system of a KoShape. 159 */ 160 QRectF documentToView(const QRectF &documentRect) const override; 161 162 /** 163 * Convert a rectangle in pixels to pt. 164 * @param viewRect the rect in the coordinate system of the widget, or window. 165 */ 166 QRectF viewToDocument(const QRectF &viewRect) const override; 167 168 /** 169 * Convert a size in pt to pixels. 170 * @param documentSize the size in pt. 171 * @return the size in pixels. 172 */ 173 QSizeF documentToView(const QSizeF &documentSize) const override; 174 175 /** 176 * Convert a size in pixels to pt. 177 * @param viewSize the size in pixels. 178 * @return the size in pt. 179 */ 180 QSizeF viewToDocument(const QSizeF &viewSize) const override; 181 182 /** 183 * Convert a single x coordinate in pt to pixels. 184 * @param documentX the x coordinate in pt. 185 * @return the x coordinate in pixels. 186 */ 187 qreal documentToViewX(qreal documentX) const override; 188 189 /** 190 * Convert a single y coordinate in pt to pixels. 191 * @param documentY the y coordinate in pt. 192 * @return the y coordinate in pixels. 193 */ 194 qreal documentToViewY(qreal documentY) const override; 195 196 /** 197 * Convert a single x coordinate in pixels to pt. 198 * @param viewX the x coordinate in pixels. 199 * @return the x coordinate in pt. 200 */ 201 qreal viewToDocumentX(qreal viewX) const override; 202 203 /** 204 * Convert a single y coordinate in pixels to pt. 205 * @param viewY the y coordinate in pixels. 206 * @return the y coordinate in pt. 207 */ 208 qreal viewToDocumentY(qreal viewY) const override; 209 210 /** 211 * Get the zoom levels of the individual x and y axis. Copy them to the pointer parameters. 212 * @param zoomX a pointer to a qreal which will be modified to set the horizontal zoom. 213 * @param zoomY a pointer to a qreal which will be modified to set the vertical zoom. 214 */ 215 void zoom(qreal *zoomX, qreal *zoomY) const override; 216 217 using KoViewConverter::zoom; 218 219 protected: 220 221 KoZoomMode::Mode m_zoomMode; 222 223 qreal m_resolutionX; 224 qreal m_resolutionY; 225 qreal m_zoomedResolutionX; 226 qreal m_zoomedResolutionY; 227 }; 228 229 #endif 230