Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

qwt_layout_metrics.cpp

00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include <qapplication.h>
00011 #include <qpainter.h>
00012 #include <qpaintdevicemetrics.h> 
00013 #include <qsimplerichtext.h> 
00014 #if QT_VERSION >= 300
00015 #include <qdesktopwidget.h> 
00016 #endif
00017 #include "qwt_layout_metrics.h"
00018 
00019 QwtMetricsMap::QwtMetricsMap()
00020 {
00021     d_screenToLayoutX = d_screenToLayoutY = 
00022         d_deviceToLayoutX = d_deviceToLayoutY = 1.0;
00023 }
00024 
00025 void QwtMetricsMap::setMetrics(const QPaintDeviceMetrics &layoutMetrics, 
00026     const QPaintDeviceMetrics &deviceMetrics)
00027 {
00028     const QPaintDeviceMetrics screenMetrics(QApplication::desktop());
00029 
00030     d_screenToLayoutX = double(layoutMetrics.logicalDpiX()) / 
00031         double(screenMetrics.logicalDpiX());
00032     d_screenToLayoutY = double(layoutMetrics.logicalDpiY()) / 
00033         double(screenMetrics.logicalDpiY());
00034 
00035     d_deviceToLayoutX = double(layoutMetrics.logicalDpiX()) / 
00036         double(deviceMetrics.logicalDpiX());
00037     d_deviceToLayoutY = double(layoutMetrics.logicalDpiY()) / 
00038         double(deviceMetrics.logicalDpiY());
00039 }
00040 
00041 #ifndef QT_NO_TRANSFORMATIONS
00042 QPoint QwtMetricsMap::layoutToDevice(const QPoint &point, 
00043     const QPainter *painter) const
00044 #else
00045 QPoint QwtMetricsMap::layoutToDevice(const QPoint &point, 
00046     const QPainter *) const
00047 #endif
00048 {
00049     if ( isIdentity() )
00050         return point;
00051 
00052     QPoint mappedPoint(point);
00053 
00054 #ifndef QT_NO_TRANSFORMATIONS
00055     if ( painter )
00056         mappedPoint = painter->worldMatrix().map(mappedPoint);
00057 #endif
00058 
00059     mappedPoint.setX(layoutToDeviceX(mappedPoint.x()));
00060     mappedPoint.setY(layoutToDeviceY(mappedPoint.y()));
00061 
00062 #ifndef QT_NO_TRANSFORMATIONS
00063     if ( painter )
00064         mappedPoint = painter->worldMatrix().invert().map(mappedPoint);
00065 #endif
00066 
00067     return mappedPoint;
00068 }
00069 
00070 #ifndef QT_NO_TRANSFORMATIONS
00071 QPoint QwtMetricsMap::deviceToLayout(const QPoint &point, 
00072     const QPainter *painter) const
00073 #else
00074 QPoint QwtMetricsMap::deviceToLayout(const QPoint &point, 
00075     const QPainter *) const
00076 #endif
00077 {
00078     if ( isIdentity() )
00079         return point;
00080 
00081     QPoint mappedPoint(point);
00082 
00083 #ifndef QT_NO_TRANSFORMATIONS
00084     if ( painter )
00085         mappedPoint = painter->worldMatrix().map(mappedPoint);
00086 #endif
00087 
00088     mappedPoint.setX(deviceToLayoutX(mappedPoint.x()));
00089     mappedPoint.setY(deviceToLayoutY(mappedPoint.y()));
00090 
00091 #ifndef QT_NO_TRANSFORMATIONS
00092     if ( painter )
00093         mappedPoint = painter->worldMatrix().invert().map(mappedPoint);
00094 #endif
00095 
00096     return mappedPoint;
00097 }
00098 
00099 QPoint QwtMetricsMap::screenToLayout(const QPoint &point) const
00100 {
00101     if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
00102         return point;
00103 
00104     return QPoint(screenToLayoutX(point.x()), screenToLayoutY(point.y()));
00105 }
00106 
00107 #ifndef QT_NO_TRANSFORMATIONS
00108 QRect QwtMetricsMap::layoutToDevice(const QRect &rect, 
00109     const QPainter *painter) const
00110 #else
00111 QRect QwtMetricsMap::layoutToDevice(const QRect &rect, 
00112     const QPainter *) const
00113 #endif
00114 {
00115     if ( isIdentity() )
00116         return rect;
00117 
00118     QRect mappedRect(rect);
00119 #ifndef QT_NO_TRANSFORMATIONS
00120     if ( painter )
00121         mappedRect = translate(painter->worldMatrix(), mappedRect);
00122 #endif
00123 
00124     mappedRect = QRect(
00125         layoutToDeviceX(mappedRect.x()), 
00126         layoutToDeviceY(mappedRect.y()),
00127         layoutToDeviceX(mappedRect.width()), 
00128         layoutToDeviceY(mappedRect.height())
00129     );
00130 
00131 #ifndef QT_NO_TRANSFORMATIONS
00132     if ( painter )
00133         mappedRect = translate(painter->worldMatrix().invert(), mappedRect);
00134 #endif
00135 
00136     return mappedRect;
00137 }
00138 
00139 #ifndef QT_NO_TRANSFORMATIONS
00140 QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
00141     const QPainter *painter) const
00142 #else
00143 QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
00144     const QPainter *) const
00145 #endif
00146 {
00147     if ( isIdentity() )
00148         return rect;
00149 
00150     QRect mappedRect(rect);
00151 #ifndef QT_NO_TRANSFORMATIONS
00152     if ( painter )
00153         mappedRect = translate(painter->worldMatrix(), mappedRect);
00154 #endif
00155 
00156     mappedRect = QRect(
00157         deviceToLayoutX(mappedRect.x()), 
00158         deviceToLayoutY(mappedRect.y()),
00159         deviceToLayoutX(mappedRect.width()), 
00160         deviceToLayoutY(mappedRect.height())
00161     );
00162 
00163 #ifndef QT_NO_TRANSFORMATIONS
00164     if ( painter )
00165         mappedRect = translate(painter->worldMatrix().invert(), mappedRect);
00166 #endif
00167 
00168     return mappedRect;
00169 }
00170 
00171 QRect QwtMetricsMap::screenToLayout(const QRect &rect) const
00172 {
00173     if ( d_deviceToLayoutX == 1.0 && d_deviceToLayoutY == 1.0 )
00174         return rect;
00175 
00176     return QRect(screenToLayoutX(rect.x()), screenToLayoutY(rect.y()),
00177         screenToLayoutX(rect.width()), screenToLayoutY(rect.height()));
00178 }
00179 
00180 #ifndef QT_NO_TRANSFORMATIONS
00181 QPointArray QwtMetricsMap::layoutToDevice(const QPointArray &pa, 
00182     const QPainter *painter) const
00183 #else
00184 QPointArray QwtMetricsMap::layoutToDevice(const QPointArray &pa, 
00185     const QPainter *) const
00186 #endif
00187 {
00188     if ( isIdentity() )
00189         return pa;
00190     
00191     QPointArray mappedPa(pa);
00192 
00193 #ifndef QT_NO_TRANSFORMATIONS
00194     if ( painter )
00195         mappedPa = translate(painter->worldMatrix(), mappedPa);
00196 #endif
00197 
00198     QWMatrix m;
00199     m.scale(1.0 / d_deviceToLayoutX, 1.0 / d_deviceToLayoutY);
00200     mappedPa = translate(m, mappedPa);
00201 
00202 #ifndef QT_NO_TRANSFORMATIONS
00203     if ( painter )
00204         mappedPa = translate(painter->worldMatrix().invert(), mappedPa);
00205 #endif
00206 
00207     return mappedPa;
00208 
00209 }
00210 
00211 #ifndef QT_NO_TRANSFORMATIONS
00212 QPointArray QwtMetricsMap::deviceToLayout(const QPointArray &pa, 
00213     const QPainter *painter) const
00214 #else
00215 QPointArray QwtMetricsMap::deviceToLayout(const QPointArray &pa, 
00216     const QPainter *) const
00217 #endif
00218 {
00219     if ( isIdentity() )
00220         return pa;
00221     
00222     QPointArray mappedPa(pa);
00223 
00224 #ifndef QT_NO_TRANSFORMATIONS
00225     if ( painter )
00226         mappedPa = translate(painter->worldMatrix(), mappedPa);
00227 #endif
00228 
00229     QWMatrix m;
00230     m.scale(d_deviceToLayoutX, d_deviceToLayoutY);
00231     mappedPa = translate(m, mappedPa);
00232 
00233 #ifndef QT_NO_TRANSFORMATIONS
00234     if ( painter )
00235         mappedPa = translate(painter->worldMatrix().invert(), mappedPa);
00236 #endif
00237 
00238     return mappedPa;
00239 }
00240 
00245 QRect QwtMetricsMap::translate(
00246     const QWMatrix &m, const QRect &rect) 
00247 {
00248 #if QT_VERSION < 300
00249     return m.map(rect.normalize());
00250 #else
00251     return m.mapRect(rect);
00252 #endif
00253 }
00254 
00260 QPointArray QwtMetricsMap::translate(
00261     const QWMatrix &m, const QPointArray &pa) 
00262 {
00263 #if QT_VERSION < 300
00264     return m.map(pa);
00265 #elif QT_VERSION < 400
00266     return m * pa;
00267 #else
00268     return m.map(pa);
00269 #endif
00270 }
00271 
00272 QwtLayoutMetrics::QwtLayoutMetrics()
00273 {
00274 }
00275 
00276 QwtLayoutMetrics::QwtLayoutMetrics(const QwtMetricsMap &map):
00277     d_map(map)
00278 {
00279 }
00280 
00281 void QwtLayoutMetrics::setMap(const QwtMetricsMap &map)
00282 {
00283     d_map = map;
00284 }
00285 
00286 int QwtLayoutMetrics::heightForWidth(const QString &text,
00287     int width, int flags, const QFontMetrics &fm) const
00288 {
00289     const QRect rect = fm.boundingRect(
00290         0, 0, d_map.layoutToScreenX(width), QCOORD_MAX, flags, text);
00291     return d_map.screenToLayoutY(rect.height());
00292 }
00293 
00294 int QwtLayoutMetrics::heightForWidth(const QString &text,
00295     int width, int flags, QPainter *painter) const
00296 {
00297     const QRect rect = painter->boundingRect(
00298         0, 0, d_map.layoutToDeviceX(width), QCOORD_MAX, flags, text);
00299 
00300     return d_map.deviceToLayoutY(rect.height());
00301 }
00302 
00303 QRect QwtLayoutMetrics::boundingRect(const QString &text, 
00304     int flags, QPainter *painter) const
00305 {
00306     const QRect rect = painter->boundingRect(
00307         0, 0, 0, 0, flags, text);
00308 
00309     return d_map.deviceToLayout(rect);
00310 }
00311 
00312 QRect QwtLayoutMetrics::boundingRect(const QString &text, 
00313     int flags, const QFontMetrics &fm) const
00314 {
00315     QRect rect = fm.boundingRect(
00316         0, 0, QCOORD_MAX, QCOORD_MAX, flags, text);
00317 
00318     return d_map.screenToLayout(rect);
00319 }
00320 
00321 #ifndef QT_NO_RICHTEXT
00322 
00323 int QwtLayoutMetrics::heightForWidth(QSimpleRichText &text, int width) const
00324 {
00325     text.setWidth(d_map.layoutToScreenX(width));
00326     return d_map.screenToLayoutY(text.height());
00327 }
00328 
00329 QRect QwtLayoutMetrics::boundingRect(
00330     const QSimpleRichText &text, int flags, QPainter *painter) const
00331 {
00332     const int tw = text.width();
00333 
00334     int w, h;
00335     if ( painter )
00336     {
00337         ((QSimpleRichText &)text).setWidth(painter, QCOORD_MAX);
00338         w = d_map.deviceToLayoutX(text.widthUsed());
00339         h = d_map.deviceToLayoutY(text.height());
00340     }
00341     else
00342     {
00343         ((QSimpleRichText &)text).setWidth(QCOORD_MAX);
00344         w = d_map.screenToLayoutX(text.widthUsed());
00345         h = d_map.screenToLayoutY(text.height());
00346     }
00347 
00348     ((QSimpleRichText &)text).setWidth(tw); // reset width
00349 
00350     int x = 0; 
00351     int y = 0;
00352     if (flags & Qt::AlignHCenter)
00353         x -= w/2;
00354     else if (flags & Qt::AlignRight)
00355         x -= w;
00356 
00357     if (flags & Qt::AlignVCenter)
00358         y -= h/2;
00359     else if (flags & Qt::AlignBottom)
00360         y -= h;
00361 
00362     return QRect(x, y, w, h);
00363 }
00364 
00365 #endif // !QT_NO_RICHTEXT

Generated on Sun Nov 21 11:12:43 2004 for Qwt User's Guide by doxygen 1.3.5