KDChartMeasure.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002  ** Copyright (C) 2007 Klarälvdalens Datakonsult AB.  All rights reserved.
00003  **
00004  ** This file is part of the KD Chart library.
00005  **
00006  ** This file may be distributed and/or modified under the terms of the
00007  ** GNU General Public License version 2 as published by the Free Software
00008  ** Foundation and appearing in the file LICENSE.GPL included in the
00009  ** packaging of this file.
00010  **
00011  ** Licensees holding valid commercial KD Chart licenses may use this file in
00012  ** accordance with the KD Chart Commercial License Agreement provided with
00013  ** the Software.
00014  **
00015  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017  **
00018  ** See http://www.kdab.net/kdchart for
00019  **   information about KDChart Commercial License Agreements.
00020  **
00021  ** Contact info@kdab.net if any conditions of this
00022  ** licensing are not clear to you.
00023  **
00024  **********************************************************************/
00025 
00026 #include "KDChartMeasure.h"
00027 
00028 #include <QWidget>
00029 
00030 #include <QtXml/QDomDocumentFragment>
00031 #include <KDChartAbstractArea.h>
00032 #include <KDChartCartesianCoordinatePlane.h>
00033 #include <KDChartTextAttributes.h>
00034 #include <KDChartFrameAttributes.h>
00035 #include <KDChartBackgroundAttributes.h>
00036 
00037 #include <KDABLibFakes>
00038 
00039 
00040 namespace KDChart {
00041 
00042 
00043 Measure::Measure()
00044   : mValue( 0.0 ),
00045     mMode(  KDChartEnums::MeasureCalculationModeAuto ),
00046     mArea(  0 ),
00047     mOrientation( KDChartEnums::MeasureOrientationAuto )
00048 {
00049     // this bloc left empty intentionally
00050 }
00051 
00052 Measure::Measure( qreal value,
00053     KDChartEnums::MeasureCalculationMode mode,
00054     KDChartEnums::MeasureOrientation orientation )
00055   : mValue( value ),
00056     mMode(  mode ),
00057     mArea(  0 ),
00058     mOrientation( orientation )
00059 {
00060     // this bloc left empty intentionally
00061 }
00062 
00063 Measure::Measure( const Measure& r )
00064   : mValue( r.value() ),
00065     mMode(  r.calculationMode() ),
00066     mArea(  r.referenceArea() ),
00067     mOrientation( r.referenceOrientation() )
00068 {
00069     // this bloc left empty intentionally
00070 }
00071 
00072 Measure & Measure::operator=( const Measure& r )
00073 {
00074     if( this != &r ){
00075         mValue = r.value();
00076         mMode  = r.calculationMode();
00077         mArea  = r.referenceArea();
00078         mOrientation = r.referenceOrientation();
00079     }
00080 
00081     return *this;
00082 }
00083 
00084 
00085 qreal Measure::calculatedValue( const QSizeF& autoSize,
00086                                 KDChartEnums::MeasureOrientation autoOrientation) const
00087 {
00088     if( mMode == KDChartEnums::MeasureCalculationModeAbsolute ){
00089         return mValue;
00090     }else{
00091         qreal value = 0.0;
00092         const QObject theAutoArea;
00093         const QObject* autoArea = &theAutoArea;
00094         const QObject* area = mArea ? mArea : autoArea;
00095         KDChartEnums::MeasureOrientation orientation = mOrientation;
00096         switch( mMode ){
00097             case KDChartEnums::MeasureCalculationModeAuto:
00098                 area = autoArea;
00099                 orientation = autoOrientation;
00100                 break;
00101             case KDChartEnums::MeasureCalculationModeAutoArea:
00102                 area = autoArea;
00103                 break;
00104             case KDChartEnums::MeasureCalculationModeAutoOrientation:
00105                 orientation = autoOrientation;
00106                 break;
00107             case KDChartEnums::MeasureCalculationModeAbsolute: // fall through intended
00108             case KDChartEnums::MeasureCalculationModeRelative:
00109                 break;
00110         }
00111         if( area ){
00112             QSizeF size;
00113             if( area == autoArea )
00114                 size = autoSize;
00115             else
00116                 size = sizeOfArea( area );
00117             //qDebug() << ( area == autoArea ) << "size" << size;
00118             qreal referenceValue;
00119             switch( orientation ){
00120                 case KDChartEnums::MeasureOrientationAuto: // fall through intended
00121                 case KDChartEnums::MeasureOrientationMinimum:
00122                     referenceValue = qMin( size.width(), size.height() );
00123                     break;
00124                 case KDChartEnums::MeasureOrientationMaximum:
00125                     referenceValue = qMax( size.width(), size.height() );
00126                     break;
00127                 case KDChartEnums::MeasureOrientationHorizontal:
00128                     referenceValue = size.width();
00129                     break;
00130                 case KDChartEnums::MeasureOrientationVertical:
00131                     referenceValue = size.height();
00132                     break;
00133             }
00134             value = mValue / 1000.0 * referenceValue;
00135         }
00136         return value;
00137     }
00138 }
00139 
00140 
00141 qreal Measure::calculatedValue( const QObject* autoArea,
00142                                 KDChartEnums::MeasureOrientation autoOrientation) const
00143 {
00144     return calculatedValue( sizeOfArea( autoArea ), autoOrientation);
00145 }
00146 
00147 
00148 const QSizeF Measure::sizeOfArea( const QObject* area ) const
00149 {
00150     QSizeF size;
00151     const CartesianCoordinatePlane* plane = dynamic_cast<const CartesianCoordinatePlane*>( area );
00152     if ( false ) {
00153         size = plane->visibleDiagramArea().size();
00154     } else {
00155         const AbstractArea* kdcArea = dynamic_cast<const AbstractArea*>(area);
00156         if( kdcArea ){
00157             size = kdcArea->geometry().size();
00158             //qDebug() << "Measure::sizeOfArea() found kdcArea with size" << size;
00159         }else{
00160             const QWidget* widget = dynamic_cast<const QWidget*>(area);
00161             if( widget ){
00162                 /* ATTENTION: Using the layout does not work: The Legend will never get the right size then!
00163                 const QLayout * layout = widget->layout();
00164                 if( layout ){
00165                     size = layout->geometry().size();
00166                     //qDebug() << "Measure::sizeOfArea() found widget with layout size" << size;
00167                 }else*/
00168                 {
00169                     size = widget->geometry().size();
00170                     //qDebug() << "Measure::sizeOfArea() found widget with size" << size;
00171                 }
00172             }else if( mMode != KDChartEnums::MeasureCalculationModeAbsolute ){
00173                 size = QSizeF(1.0, 1.0);
00174                 //qDebug("Measure::sizeOfArea() got no valid area.");
00175             }
00176         }
00177     }
00178     const QPair< qreal, qreal > factors
00179             = GlobalMeasureScaling::instance()->currentFactors();
00180     return QSizeF(size.width() * factors.first, size.height() * factors.second);
00181 }
00182 
00183 
00184 bool Measure::operator==( const Measure& r ) const
00185 {
00186     return( mValue == r.value() &&
00187             mMode  == r.calculationMode() &&
00188             mArea  == r.referenceArea() &&
00189             mOrientation == r.referenceOrientation() );
00190 }
00191 
00192 
00193 
00194 GlobalMeasureScaling::GlobalMeasureScaling()
00195 {
00196     mFactors.push( qMakePair(qreal(1.0), qreal(1.0)) );
00197 }
00198 
00199 GlobalMeasureScaling::~GlobalMeasureScaling()
00200 {
00201     // this space left empty intentionally
00202 }
00203 
00204 GlobalMeasureScaling* GlobalMeasureScaling::instance()
00205 {
00206     static GlobalMeasureScaling instance;
00207     return &instance;
00208 }
00209 
00210 void GlobalMeasureScaling::setFactors(qreal factorX, qreal factorY)
00211 {
00212     instance()->mFactors.push( qMakePair(factorX, factorY) );
00213 }
00214 
00215 void GlobalMeasureScaling::resetFactors()
00216 {
00217     // never remove the initial (1.0. 1.0) setting
00218     if( instance()->mFactors.count() > 1 )
00219         instance()->mFactors.pop();
00220 }
00221 
00222 const QPair< qreal, qreal > GlobalMeasureScaling::currentFactors()
00223 {
00224     return instance()->mFactors.top();
00225 }
00226 
00227 void GlobalMeasureScaling::setPaintDevice( QPaintDevice* paintDevice )
00228 {
00229     instance()->m_paintDevice = paintDevice;
00230 }
00231 
00232 QPaintDevice* GlobalMeasureScaling::paintDevice()
00233 {
00234     return instance()->m_paintDevice;
00235 }
00236 
00237 }
00238 
00239 #if !defined(QT_NO_DEBUG_STREAM)
00240 QDebug operator<<(QDebug dbg, const KDChart::Measure& m)
00241 {
00242     dbg << "KDChart::Measure("
00243         << "value="<<m.value()
00244         << "calculationmode="<<m.calculationMode()
00245         << "referencearea="<<m.referenceArea()
00246         << "referenceorientation="<<m.referenceOrientation()
00247         << ")";
00248     return dbg;
00249 }
00250 #endif /* QT_NO_DEBUG_STREAM */

Generated on Thu Mar 4 23:19:12 2010 for KD Chart 2 by  doxygen 1.5.4