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 Thomas Zander <zander@kde.org>
0004    Copyright (C) 2010 KO GmbH <boud@kogmbh.com>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019    Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include "KReportZoomHandler_p.h"
0023 #include "KReportUnit.h" // for POINT_TO_INCH
0024 #include "KReportUtils_p.h"
0025 
0026 #include <QPointF>
0027 #include <QRectF>
0028 
0029 KReportZoomHandler::KReportZoomHandler()
0030     : m_zoomType(KReportZoomMode::Type::Constant)
0031     , m_resolutionX(0)
0032     , m_resolutionY(0)
0033     , m_zoomedResolutionX(0)
0034     , m_zoomedResolutionY(0)
0035     , m_zoomLevel(1.0)
0036 {
0037     setZoom(1.0);
0038     setZoomMode(KReportZoomMode::Type::Constant);
0039     setDpi(KReportPrivate::dpiX(), KReportPrivate::dpiY());
0040 }
0041 
0042 KReportZoomHandler::~KReportZoomHandler()
0043 {
0044 }
0045 
0046 void KReportZoomHandler::setResolutionToStandard()
0047 {
0048     setDpi(KReportPrivate::dpiX(), KReportPrivate::dpiY());
0049 }
0050 
0051 void KReportZoomHandler::setDpi(int dpiX, int dpiY)
0052 {
0053     setResolution(POINT_TO_INCH(static_cast<qreal>(dpiX)),
0054                   POINT_TO_INCH(static_cast<qreal>(dpiY)));
0055 }
0056 
0057 void KReportZoomHandler::setResolution( qreal resolutionX, qreal resolutionY )
0058 {
0059 
0060     m_resolutionX = resolutionX;
0061     m_resolutionY = resolutionY;
0062 
0063     if (qFuzzyCompare(m_resolutionX, 1))
0064         m_resolutionX = 1;
0065     if (qFuzzyCompare(m_resolutionY, 1))
0066         m_resolutionY = 1;
0067 
0068     m_zoomedResolutionX = zoom() * resolutionX;
0069     m_zoomedResolutionY = zoom() * resolutionY;
0070 }
0071 
0072 void KReportZoomHandler::setZoomedResolution( qreal zoomedResolutionX, qreal zoomedResolutionY )
0073 {
0074     // zoom() doesn't matter, it's only used in setZoom() to calculated the zoomed resolutions
0075     // Here we know them. The whole point of this method is to allow a different zoom factor
0076     // for X and for Y, as can be useful for e.g. fullscreen kpresenter presentations.
0077     m_zoomedResolutionX = zoomedResolutionX;
0078     m_zoomedResolutionY = zoomedResolutionY;
0079 }
0080 
0081 void KReportZoomHandler::setZoom( qreal zoom )
0082 {
0083     if (qFuzzyCompare(zoom, qreal(0.0)) || qFuzzyCompare(zoom, qreal(1.0))) {
0084         zoom = 1.0;
0085     }
0086 
0087     m_zoomLevel = zoom;
0088 
0089     if( zoom == 1.0 ) {
0090         m_zoomedResolutionX = m_resolutionX;
0091         m_zoomedResolutionY = m_resolutionY;
0092     } else {
0093         m_zoomedResolutionX = zoom * m_resolutionX;
0094         m_zoomedResolutionY = zoom * m_resolutionY;
0095     }
0096 }
0097 
0098 QPointF KReportZoomHandler::documentToView( const QPointF &documentPoint )  const
0099 {
0100     return QPointF( zoomItX( documentPoint.x() ),
0101                     zoomItY( documentPoint.y() ));
0102 }
0103 
0104 QPointF KReportZoomHandler::viewToDocument( const QPointF &viewPoint )  const
0105 {
0106     return QPointF( unzoomItX( viewPoint.x() ),
0107                     unzoomItY( viewPoint.y() ) );
0108 }
0109 
0110 QRectF KReportZoomHandler::documentToView( const QRectF &documentRect )  const
0111 {
0112     QRectF r (zoomItX( documentRect.x() ),
0113               zoomItY( documentRect.y() ),
0114               zoomItX( documentRect.width() ),
0115               zoomItY( documentRect.height() ) );
0116     return r;
0117 }
0118 
0119 QRectF KReportZoomHandler::viewToDocument( const QRectF &viewRect )  const
0120 {
0121     QRectF r (  unzoomItX( viewRect.x() ),
0122                 unzoomItY( viewRect.y()),
0123                 unzoomItX( viewRect.width() ),
0124                 unzoomItY( viewRect.height() ) );
0125     return r;
0126 }
0127 
0128 QSizeF KReportZoomHandler::documentToView( const QSizeF &documentSize ) const
0129 {
0130     return QSizeF( zoomItX( documentSize.width() ),
0131                    zoomItY( documentSize.height() ) );
0132 }
0133 
0134 QSizeF KReportZoomHandler::viewToDocument( const QSizeF &viewSize ) const
0135 {
0136     return QSizeF( unzoomItX( viewSize.width() ),
0137                    unzoomItY( viewSize.height() ) );
0138 }
0139 
0140 qreal KReportZoomHandler::documentToViewX( qreal documentX ) const
0141 {
0142     return zoomItX( documentX );
0143 }
0144 
0145 qreal KReportZoomHandler::documentToViewY( qreal documentY ) const
0146 {
0147     return zoomItY( documentY );
0148 }
0149 
0150 qreal KReportZoomHandler::viewToDocumentX( qreal viewX ) const
0151 {
0152     return unzoomItX( viewX );
0153 }
0154 
0155 qreal KReportZoomHandler::viewToDocumentY( qreal viewY ) const
0156 {
0157     return unzoomItY( viewY );
0158 }
0159 
0160 void KReportZoomHandler::zoom(qreal *zoomX, qreal *zoomY) const
0161 {
0162     *zoomX = zoomItX(100.0) / 100.0;
0163     *zoomY = zoomItY(100.0) / 100.0;
0164 }
0165 
0166 qreal KReportZoomHandler::zoom() const
0167 {
0168     return m_zoomLevel;
0169 }