home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / KoZoomHandler.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-30  |  5.7 KB  |  164 lines

  1. /* This file is part of the KDE project
  2.    Copyright (C) 2001-2005 David Faure <faure@kde.org>
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2 of the License, or (at your option) any later version.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public License
  15.    along with this library; see the file COPYING.LIB.  If not, write to
  16.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.    Boston, MA 02110-1301, USA.
  18. */
  19.  
  20. #ifndef kozoomhandler_h
  21. #define kozoomhandler_h
  22.  
  23. #include <KoRect.h>
  24. #include <koffice_export.h>
  25. #include <KoZoomMode.h>
  26.  
  27. /**
  28.  * This class handles the zooming and DPI stuff (conversions between pt values and pixels).
  29.  * An instance of KoZoomHandler operates at a given zoom (see setZoomAndResolution() and setZoom())
  30.  * so there is usually one instance of KoZoomHandler per view.
  31.  */
  32. class KOFFICEUI_EXPORT KoZoomHandler
  33. {
  34. public:
  35.     KoZoomHandler();
  36.     virtual ~KoZoomHandler() {}
  37.  
  38.     /**
  39.      * Change the zoom factor to @p z (e.g. 150 for 150%)
  40.      * and/or change the resolution, given in DPI.
  41.      * This is done on startup, when zooming, and when printing.
  42.      * The same call combines both so that all the updating done behind
  43.      * the scenes is done only once, even if both zoom and DPI must be changed.
  44.      */
  45.     virtual void setZoomAndResolution( int zoom, int dpiX, int dpiY );
  46.  
  47.     /**
  48.      * @return the conversion factor between pt and pixel, that
  49.      * takes care of the zoom and the DPI setting.
  50.      * Use zoomIt(pt) instead, though.
  51.      */
  52.     double zoomedResolutionX() const { return m_zoomedResolutionX; }
  53.     double zoomedResolutionY() const { return m_zoomedResolutionY; }
  54.  
  55.     double resolutionX() const { return m_resolutionX; }
  56.     double resolutionY() const { return m_resolutionY; }
  57.  
  58.     /**
  59.      * Zoom factor for X. Equivalent to zoomedResolutionX()/resolutionX()
  60.      */
  61.     double zoomFactorX() const { return m_zoomedResolutionX / m_resolutionX; }
  62.     /**
  63.      * Zoom factor for Y. Equivalent to zoomedResolutionY()/resolutionY()
  64.      */
  65.     double zoomFactorY() const { return m_zoomedResolutionY / m_resolutionY; }
  66.  
  67.  
  68.     /**
  69.      * Set a resolution for X and Y, when no zoom applies (e.g. when painting an
  70.      * embedded document. This will set the zoom to 100, and it will set
  71.      * zoomedResolution[XY] to the resolution[XY] parameters
  72.      * Helper method, equivalent to setZoomAndResolution(100,...).
  73.      */
  74.     void setResolution( double resolutionX, double resolutionY );
  75.  
  76.     /**
  77.      * Set the zoomed resolution for X and Y.
  78.      * Compared to the setZoom... methods, this allows to set a different
  79.      * zoom factor for X and for Y.
  80.      */
  81.     virtual void setZoomedResolution( double zoomedResolutionX, double zoomedResolutionY );
  82.  
  83.     /**
  84.      * Change the zoom level, keeping the resolution unchanged.
  85.      * @param zoom the zoom factor (e.g. 100 for 100%)
  86.      */
  87.     void setZoom( int zoom );
  88.     /**
  89.      * Change the zoom mode
  90.      * @param zoomMode the zoom mode.
  91.      */
  92.     void setZoomMode( KoZoomMode::Mode zoomMode ) { m_zoomMode = zoomMode; }
  93.     /**
  94.      * @return the global zoom factor (e.g. 100 for 100%).
  95.      * Only use this to display to the user, don't use in calculations
  96.      */
  97.     int zoom() const { return m_zoom; }
  98.     /**
  99.      * @return the global zoom mode (e.g. KoZoomMode::ZOOM_WIDTH).
  100.      * use this to determine how to zoom
  101.      */
  102.     KoZoomMode::Mode zoomMode() const { return m_zoomMode; }
  103.     
  104.     // Input: pt. Output: pixels. Resolution and zoom are applied.
  105.     int zoomItX( double z ) const {
  106.         return qRound( m_zoomedResolutionX * z );
  107.     }
  108.     int zoomItY( double z ) const {
  109.         return qRound( m_zoomedResolutionY * z );
  110.     }
  111.  
  112.     QPoint zoomPoint( const KoPoint & p ) const {
  113.         return QPoint( zoomItX( p.x() ), zoomItY( p.y() ) );
  114.     }
  115.     QRect zoomRect( const KoRect & r ) const {
  116.         QRect _r;
  117.         _r.setCoords( zoomItX( r.left() ),  zoomItY( r.top() ),
  118.                       zoomItX( r.right() ), zoomItY( r.bottom() ) );
  119.         return _r;
  120.     }
  121.     /**
  122.      * Returns the size in pixels for a input size in points.
  123.      *
  124.      * This function can return a size with 1 pixel to less, depending
  125.      * on the reference point and the width and/or the zoom level.
  126.      * It's save to use if the starting point is (0/0).
  127.      * You can use it if you don't know the starting point yet
  128.      * (like when inserting a picture), but then please take
  129.      * care of it afterwards, when you know the reference point.
  130.      */
  131.     QSize zoomSize( const KoSize & s ) const {
  132.         return QSize( zoomItX( s.width() ), zoomItY( s.height() ) );
  133.     }
  134.  
  135.     // Input: pixels. Output: pt.
  136.     double unzoomItX( int x ) const {
  137.         return static_cast<double>( x ) / m_zoomedResolutionX;
  138.     }
  139.     double unzoomItY( int y ) const {
  140.         return static_cast<double>( y ) / m_zoomedResolutionY;
  141.     }
  142.     KoPoint unzoomPoint( const QPoint & p ) const {
  143.         return KoPoint( unzoomItX( p.x() ), unzoomItY( p.y() ) );
  144.     }
  145.     KoRect unzoomRect( const QRect & r ) const {
  146.         KoRect _r;
  147.         _r.setCoords( unzoomItX( r.left() ),  unzoomItY( r.top() ),
  148.                       unzoomItX( r.right() ), unzoomItY( r.bottom() ) );
  149.         return _r;
  150.     }
  151.  
  152.  
  153. protected:
  154.     int m_zoom;
  155.     KoZoomMode::Mode m_zoomMode;
  156.     
  157.     double m_resolutionX;
  158.     double m_resolutionY;
  159.     double m_zoomedResolutionX;
  160.     double m_zoomedResolutionY;
  161. };
  162.  
  163. #endif
  164.