Warning, file /office/calligra/libs/pageapp/KoPAUtil.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KoPAUtil.h"
0021 
0022 #include <QRect>
0023 #include <QSize>
0024 
0025 #include <KoPageLayout.h>
0026 #include <KoZoomHandler.h>
0027 
0028 void KoPAUtil::setZoom( const KoPageLayout & pageLayout, const QSize & size, KoZoomHandler & zoomHandler )
0029 {
0030     qreal zoom = size.width() / ( zoomHandler.resolutionX() * pageLayout.width );
0031     zoom = qMin( zoom, size.height() / ( zoomHandler.resolutionY() * pageLayout.height ) );
0032     zoomHandler.setZoom( zoom );
0033 }
0034 
0035 void KoPAUtil::setSizeAndZoom(const KoPageLayout &pageLayout, QSize &thumbnailSize, KoZoomHandler &zoomHandler)
0036 {
0037     const qreal realWidth = zoomHandler.resolutionX() * pageLayout.width;
0038     const qreal realHeight = zoomHandler.resolutionY() * pageLayout.height;
0039 
0040     const qreal widthScale = thumbnailSize.width() / realWidth;
0041     const qreal heightScale = thumbnailSize.height() / realHeight;
0042 
0043     // adapt thumbnailSize to match the rendered page
0044     if (widthScale > heightScale) {
0045         const int thumbnailWidth = qMin(thumbnailSize.width(), qRound(realWidth*heightScale));
0046         thumbnailSize.setWidth(thumbnailWidth);
0047     } else {
0048         const int thumbnailHeight = qMin(thumbnailSize.height(), qRound(realHeight*widthScale));
0049         thumbnailSize.setHeight(thumbnailHeight);
0050     }
0051 
0052     // set zoom
0053     const qreal zoom = (widthScale > heightScale) ? heightScale : widthScale;
0054     zoomHandler.setZoom(zoom);
0055 }
0056 
0057 QRect KoPAUtil::pageRect( const KoPageLayout & pageLayout, const QSize & size, const KoZoomHandler & zoomHandler )
0058 {
0059     int width = int( 0.5 + zoomHandler.documentToViewX( pageLayout.width ) );
0060     int height = int( 0.5 + zoomHandler.documentToViewY( pageLayout.height ) );
0061     int x = int( ( size.width() - width ) / 2.0 );
0062     int y = int( ( size.height() - height ) / 2.0 );
0063     return QRect( x, y, width, height );
0064 }