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 Thomas Zander <zander@kde.org>
0004    SPDX-FileCopyrightText: 2010 KO GmbH <boud@valdyas.org>
0005 
0006    SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "KoZoomHandler.h"
0010 #include <WidgetsDebug.h>
0011 #include <KoUnit.h> // for POINT_TO_INCH
0012 #include <QPointF>
0013 #include <QRectF>
0014 
0015 KoZoomHandler::KoZoomHandler()
0016     : KoViewConverter()
0017     , m_zoomMode(KoZoomMode::ZOOM_CONSTANT)
0018     , m_resolutionX(0)
0019     , m_resolutionY(0)
0020     , m_zoomedResolutionX(0)
0021     , m_zoomedResolutionY(0)
0022     , m_zoomMarginSize(0)
0023 {
0024     setZoom(1.0);
0025     setZoomMode( KoZoomMode::ZOOM_CONSTANT );
0026     // Use 72 dpi as a placeholder. KisView will immediately update the
0027     // screen resolution correctly after this, using the values initialized
0028     // by KisZoomManager::updateScreenResolution().
0029     setDpi(72, 72);
0030 }
0031 
0032 KoZoomHandler::~KoZoomHandler()
0033 {
0034 }
0035 
0036 void KoZoomHandler::setDpi(int dpiX, int dpiY)
0037 {
0038     setResolution(POINT_TO_INCH(static_cast<qreal>(dpiX)),
0039                   POINT_TO_INCH(static_cast<qreal>(dpiY)));
0040 }
0041 
0042 void KoZoomHandler::setResolution( qreal resolutionX, qreal resolutionY )
0043 {
0044 
0045     m_resolutionX = resolutionX;
0046     m_resolutionY = resolutionY;
0047 
0048     if (qFuzzyCompare(m_resolutionX, 1))
0049         m_resolutionX = 1;
0050     if (qFuzzyCompare(m_resolutionY, 1))
0051         m_resolutionY = 1;
0052 
0053     m_zoomedResolutionX = zoom() * resolutionX;
0054     m_zoomedResolutionY = zoom() * resolutionY;
0055 }
0056 
0057 void KoZoomHandler::setZoomedResolution( qreal zoomedResolutionX, qreal zoomedResolutionY )
0058 {
0059     // zoom() doesn't matter, it's only used in setZoom() to calculated the zoomed resolutions
0060     // Here we know them. The whole point of this method is to allow a different zoom factor
0061     // for X and for Y, as can be useful for e.g. fullscreen kpresenter presentations.
0062     m_zoomedResolutionX = zoomedResolutionX;
0063     m_zoomedResolutionY = zoomedResolutionY;
0064 }
0065 
0066 void KoZoomHandler::setZoom( qreal zoom )
0067 {
0068     if (qFuzzyCompare(zoom, qreal(1.0))) {
0069         zoom = 1.0;
0070     }
0071 
0072     KoViewConverter::setZoom(zoom);
0073     if( zoom == 1.0 ) {
0074         m_zoomedResolutionX = m_resolutionX;
0075         m_zoomedResolutionY = m_resolutionY;
0076     } else {
0077         m_zoomedResolutionX = zoom * m_resolutionX;
0078         m_zoomedResolutionY = zoom * m_resolutionY;
0079     }
0080 }
0081 
0082 void KoZoomHandler::setZoomMarginSize( int size )
0083 {
0084     m_zoomMarginSize = size;
0085 }
0086 
0087 int KoZoomHandler::zoomMarginSize() const
0088 {
0089     return m_zoomMarginSize;
0090 }
0091 
0092 QPointF KoZoomHandler::documentToView( const QPointF &documentPoint )  const
0093 {
0094     return QPointF( zoomItX( documentPoint.x() ),
0095                     zoomItY( documentPoint.y() ));
0096 }
0097 
0098 QPointF KoZoomHandler::viewToDocument( const QPointF &viewPoint )  const
0099 {
0100     return QPointF( unzoomItX( viewPoint.x() ),
0101                     unzoomItY( viewPoint.y() ) );
0102 }
0103 
0104 QRectF KoZoomHandler::documentToView( const QRectF &documentRect )  const
0105 {
0106     QRectF r (zoomItX( documentRect.x() ),
0107               zoomItY( documentRect.y() ),
0108               zoomItX( documentRect.width() ),
0109               zoomItY( documentRect.height() ) );
0110     return r;
0111 }
0112 
0113 QRectF KoZoomHandler::viewToDocument( const QRectF &viewRect )  const
0114 {
0115     QRectF r (  unzoomItX( viewRect.x() ),
0116                 unzoomItY( viewRect.y()),
0117                 unzoomItX( viewRect.width() ),
0118                 unzoomItY( viewRect.height() ) );
0119     return r;
0120 }
0121 
0122 QSizeF KoZoomHandler::documentToView( const QSizeF &documentSize ) const
0123 {
0124     return QSizeF( zoomItX( documentSize.width() ),
0125                    zoomItY( documentSize.height() ) );
0126 }
0127 
0128 QSizeF KoZoomHandler::viewToDocument( const QSizeF &viewSize ) const
0129 {
0130     return QSizeF( unzoomItX( viewSize.width() ),
0131                    unzoomItY( viewSize.height() ) );
0132 }
0133 
0134 qreal KoZoomHandler::documentToViewX( qreal documentX ) const
0135 {
0136     return zoomItX( documentX );
0137 }
0138 
0139 qreal KoZoomHandler::documentToViewY( qreal documentY ) const
0140 {
0141     return zoomItY( documentY );
0142 }
0143 
0144 qreal KoZoomHandler::viewToDocumentX( qreal viewX ) const
0145 {
0146     return unzoomItX( viewX );
0147 }
0148 
0149 qreal KoZoomHandler::viewToDocumentY( qreal viewY ) const
0150 {
0151     return unzoomItY( viewY );
0152 }
0153 
0154 void KoZoomHandler::zoom(qreal *zoomX, qreal *zoomY) const
0155 {
0156     *zoomX = zoomItX(100.0) / 100.0;
0157     *zoomY = zoomItY(100.0) / 100.0;
0158 }