lib Library API Documentation

kozoomhandler.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "kozoomhandler.h"
00021 #include <kdebug.h>
00022 #include <qpaintdevice.h>
00023 #include <koUnit.h>
00024 #include <koGlobal.h>
00025 
00026 // Layout text at 1440 DPI
00027 // Well, not really always 1440 DPI, but always 20 times the point size
00028 // This is constant, no need to litterally apply 1440 DPI at all resolutions.
00029 int KoTextZoomHandler::m_layoutUnitFactor = 20;
00030 
00031 KoZoomHandler::KoZoomHandler()
00032 {
00033     // Note that this calls the method below, not the derived one
00034     setZoomAndResolution( 100, KoGlobal::dpiX(), KoGlobal::dpiY() );
00035 }
00036 
00037 void KoZoomHandler::setZoomAndResolution( int zoom, int dpiX, int dpiY )
00038 {
00039     // m_resolution[XY] is in pixel per pt
00040     m_resolutionX = POINT_TO_INCH( static_cast<double>(dpiX) );
00041     m_resolutionY = POINT_TO_INCH( static_cast<double>(dpiY) );
00042     setZoom( zoom );
00043     /*kdDebug(32500) << "KoZoomHandler::setZoomAndResolution " << zoom << " " << dpiX << "," << dpiY
00044               << " m_resolutionX=" << m_resolutionX
00045               << " m_zoomedResolutionX=" << m_zoomedResolutionX
00046               << " m_resolutionY=" << m_resolutionY
00047               << " m_zoomedResolutionY=" << m_zoomedResolutionY << endl;*/
00048 }
00049 
00050 void KoZoomHandler::setResolution( double resolutionX, double resolutionY )
00051 {
00052     m_zoom = 100;
00053     m_resolutionX = resolutionX;
00054     m_resolutionY = resolutionY;
00055     m_zoomedResolutionX = resolutionX;
00056     m_zoomedResolutionY = resolutionY;
00057 }
00058 
00059 void KoZoomHandler::setZoomedResolution( double zoomedResolutionX, double zoomedResolutionY )
00060 {
00061     // m_zoom doesn't matter, it's only used in setZoom() to calculated the zoomed resolutions
00062     // Here we know them. The whole point of this method is to allow a different zoom factor
00063     // for X and for Y, as can be useful for e.g. fullscreen kpresenter presentations.
00064     m_zoomedResolutionX = zoomedResolutionX;
00065     m_zoomedResolutionY = zoomedResolutionY;
00066 }
00067 
00068 void KoZoomHandler::setZoom( int zoom )
00069 {
00070     m_zoom = zoom;
00071     if( m_zoom == 100 ) {
00072         m_zoomedResolutionX = m_resolutionX;
00073         m_zoomedResolutionY = m_resolutionY;
00074     } else {
00075         m_zoomedResolutionX = static_cast<double>(m_zoom) * m_resolutionX / 100.0;
00076         m_zoomedResolutionY = static_cast<double>(m_zoom) * m_resolutionY / 100.0;
00077     }
00078 }
00079 
00080 #if 0
00081 int KoZoomHandler::fontSizeToLayoutUnit( double ptSizeFloat, bool forPrint ) const
00082 {
00083     return ptToLayoutUnit( ptSizeFloat / ( m_zoomedResolutionY *
00084         ( forPrint ? 1.0 : (72.0 / KoGlobal::dpiY()) ) ) );
00085 }
00086 #endif
00087 
00088 double KoZoomHandler::layoutUnitToFontSize( int luSize, bool /*forPrint*/ ) const
00089 {
00090     // Qt will use QPaintDevice::x11AppDpiY() to go from pt to pixel for fonts
00091     return layoutUnitPtToPt( luSize ) * m_zoomedResolutionY
00092 #ifdef Q_WS_X11
00093         / POINT_TO_INCH(QPaintDevice::x11AppDpiY())
00094 #endif
00095         ;
00096 }
00097 
00098 int KoZoomHandler::layoutUnitToPixelX( int x, int w ) const
00099 {
00100     // We call layoutUnitToPixelX on the right value, i.e. x+w-1,
00101     // and then determine the height from the result (i.e. right-left+1).
00102     // Calling layoutUnitToPixelX(w) leads to rounding problems.
00103     return layoutUnitToPixelY( x + w - 1 ) - layoutUnitToPixelY( x ) + 1;
00104 }
00105 
00106 int KoZoomHandler::layoutUnitToPixelY( int y, int h ) const
00107 {
00108     // We call layoutUnitToPixelY on the bottom value, i.e. y+h-1,
00109     // and then determine the height from the result (i.e. bottom-top+1).
00110     // Calling layoutUnitToPixelY(h) leads to rounding problems.
00111     return layoutUnitToPixelY( y + h - 1 ) - layoutUnitToPixelY( y ) + 1;
00112 }
00113 
00114 int KoZoomHandler::layoutUnitToPixelX( int lupix ) const
00115 {
00116     return int( static_cast<double>( lupix * m_zoomedResolutionX )
00117                 / ( static_cast<double>( m_layoutUnitFactor ) * m_resolutionX ) );
00118 }
00119 
00120 int KoZoomHandler::layoutUnitToPixelY( int lupix ) const
00121 {
00122     // qRound replaced with a truncation, too many problems (e.g. bottom of parags)
00123     return int( static_cast<double>( lupix * m_zoomedResolutionY )
00124                 / ( static_cast<double>( m_layoutUnitFactor ) * m_resolutionY ) );
00125 }
00126 
00127 int KoZoomHandler::pixelToLayoutUnitX( int x ) const
00128 {
00129     return qRound( static_cast<double>( x * m_layoutUnitFactor * m_resolutionX )
00130                    / m_zoomedResolutionX );
00131 }
00132 
00133 int KoZoomHandler::pixelToLayoutUnitY( int y ) const
00134 {
00135     return qRound( static_cast<double>( y * m_layoutUnitFactor * m_resolutionY )
00136                    / m_zoomedResolutionY );
00137 }
00138 
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:14 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003