KD Chart 2: KDChartMeasure.cpp Source File

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

# KDChartMeasure.cpp

Go to the documentation of this file.

    00001 /****************************************************************************
    00002  ** Copyright (C) 2006 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 <QWidget>
    00027
    00028 #include "KDChartMeasure.h"
    00029 #include <QtXml/QDomDocumentFragment>
    00030 #include <KDChartAbstractArea.h>
    00031 #include <KDChartTextAttributes.h>
    00032 #include <KDChartFrameAttributes.h>
    00033 #include <KDChartBackgroundAttributes.h>
    00034
    00035 #include <KDABLibFakes>
    00036
    00037
    00038 namespace KDChart {
    00039
    00040
    00041 Measure::Measure()
    00042   : mValue( 0.0 ),
    00043     mMode(  KDChartEnums::MeasureCalculationModeAuto ),
    00044     mArea(  0 ),
    00045     mOrientation( KDChartEnums::MeasureOrientationAuto )
    00046 {
    00047     // this bloc left empty intentionally
    00048 }
    00049
    00050 Measure::Measure( qreal value,
    00051     KDChartEnums::MeasureCalculationMode mode,
    00052     KDChartEnums::MeasureOrientation orientation )
    00053   : mValue( value ),
    00054     mMode(  mode ),
    00055     mArea(  0 ),
    00056     mOrientation( orientation )
    00057 {
    00058     // this bloc left empty intentionally
    00059 }
    00060
    00061 Measure::Measure( const Measure& r )
    00062   : mValue( r.value() ),
    00063     mMode(  r.calculationMode() ),
    00064     mArea(  r.referenceArea() ),
    00065     mOrientation( r.referenceOrientation() )
    00066 {
    00067     // this bloc left empty intentionally
    00068 }
    00069
    00070 Measure & Measure::operator=( const Measure& r )
    00071 {
    00072     if( this != &r ){
    00073         mValue = r.value();
    00074         mMode  = r.calculationMode();
    00075         mArea  = r.referenceArea();
    00076         mOrientation = r.referenceOrientation();
    00077     }
    00078
    00079     return *this;
    00080 }
    00081
    00082
    00083 qreal Measure::calculatedValue( const QSizeF& autoSize,
    00084                                 KDChartEnums::MeasureOrientation autoOrientation) const
    00085 {
    00086     if( mMode == KDChartEnums::MeasureCalculationModeAbsolute ){
    00087         return mValue;
    00088     }else{
    00089         qreal value = 0.0;
    00090         const QObject theAutoArea;
    00091         const QObject* autoArea = &theAutoArea;
    00092         const QObject* area = mArea ? mArea : autoArea;
    00093         KDChartEnums::MeasureOrientation orientation = mOrientation;
    00094         switch( mMode ){
    00095             case KDChartEnums::MeasureCalculationModeAuto:
    00096                 area = autoArea;
    00097                 orientation = autoOrientation;
    00098                 break;
    00099             case KDChartEnums::MeasureCalculationModeAutoArea:
    00100                 area = autoArea;
    00101                 break;
    00102             case KDChartEnums::MeasureCalculationModeAutoOrientation:
    00103                 orientation = autoOrientation;
    00104                 break;
    00105             case KDChartEnums::MeasureCalculationModeAbsolute: // fall through intended
    00106             case KDChartEnums::MeasureCalculationModeRelative:
    00107                 break;
    00108         }
    00109         if( area ){
    00110             QSizeF size;
    00111             if( area == autoArea )
    00112                 size = autoSize;
    00113             else
    00114                 size = sizeOfArea( area );
    00115             //qDebug() << "size" << size;
    00116             qreal referenceValue;
    00117             switch( orientation ){
    00118                 case KDChartEnums::MeasureOrientationAuto: // fall through intended
    00119                 case KDChartEnums::MeasureOrientationMinimum:
    00120                     referenceValue = qMin( size.width(), size.height() );
    00121                     break;
    00122                 case KDChartEnums::MeasureOrientationMaximum:
    00123                     referenceValue = qMax( size.width(), size.height() );
    00124                     break;
    00125                 case KDChartEnums::MeasureOrientationHorizontal:
    00126                     referenceValue = size.width();
    00127                     break;
    00128                 case KDChartEnums::MeasureOrientationVertical:
    00129                     referenceValue = size.height();
    00130                     break;
    00131             }
    00132             value = mValue / 1000.0 * referenceValue;
    00133         }
    00134         return value;
    00135     }
    00136 }
    00137
    00138
    00139 qreal Measure::calculatedValue( const QObject* autoArea,
    00140                                 KDChartEnums::MeasureOrientation autoOrientation) const
    00141 {
    00142     return calculatedValue( sizeOfArea( autoArea ), autoOrientation);
    00143 }
    00144
    00145
    00146 const QSizeF Measure::sizeOfArea( const QObject* area ) const
    00147 {
    00148     QSizeF size;
    00149     const AbstractArea* kdcArea = dynamic_cast<const AbstractArea*>(area);
    00150     if( kdcArea ){
    00151         size = kdcArea->geometry().size();
    00152         //qDebug() << "Measure::sizeOfArea() found kdcArea with size" << size;
    00153     }else{
    00154         const QWidget* widget = dynamic_cast<const QWidget*>(area);
    00155         if( widget ){
    00156             /* ATTENTION: Using the layout does not work: The Legend will never get the right size then!
    00157             const QLayout * layout = widget->layout();
    00158             if( layout ){
    00159                 size = layout->geometry().size();
    00160                 //qDebug() << "Measure::sizeOfArea() found widget with layout size" << size;
    00161             }else*/
    00162             {
    00163                 size = widget->geometry().size();
    00164                 //qDebug() << "Measure::sizeOfArea() found widget with size" << size;
    00165             }
    00166         }else if( mMode != KDChartEnums::MeasureCalculationModeAbsolute ){
    00167             size = QSizeF(1.0, 1.0);
    00168             //qDebug("Measure::sizeOfArea() got no valid area.");
    00169         }
    00170     }
    00171     const QPair< qreal, qreal > factors
    00172             = GlobalMeasureScaling::instance()->currentFactors();
    00173     return QSizeF(size.width() * factors.first, size.height() * factors.second);
    00174 }
    00175
    00176
    00177 bool Measure::operator==( const Measure& r ) const
    00178 {
    00179     return( mValue == r.value() &&
    00180             mMode  == r.calculationMode() &&
    00181             mArea  == r.referenceArea() &&
    00182             mOrientation == r.referenceOrientation() );
    00183 }
    00184
    00185
    00186
    00187 GlobalMeasureScaling::GlobalMeasureScaling()
    00188 {
    00189     mFactors.push( qMakePair(1.0, 1.0) );
    00190 }
    00191
    00192 GlobalMeasureScaling::~GlobalMeasureScaling()
    00193 {
    00194     // this space left empty intentionally
    00195 }
    00196
    00197 GlobalMeasureScaling* GlobalMeasureScaling::instance()
    00198 {
    00199     static GlobalMeasureScaling instance;
    00200     return &instance;
    00201 }
    00202
    00203 void GlobalMeasureScaling::setFactors(qreal factorX, qreal factorY)
    00204 {
    00205     instance()->mFactors.push( qMakePair(factorX, factorY) );
    00206 }
    00207
    00208 void GlobalMeasureScaling::resetFactors()
    00209 {
    00210     // never remove the initial (1.0. 1.0) setting
    00211     if( instance()->mFactors.count() > 1 )
    00212         instance()->mFactors.pop();
    00213 }
    00214
    00215 const QPair< qreal, qreal > GlobalMeasureScaling::currentFactors()
    00216 {
    00217     return instance()->mFactors.top();
    00218 }
    00219
    00220
    00221
    00222
    00223 }
    00224
    00225 #if !defined(QT_NO_DEBUG_STREAM)
    00226 QDebug operator<<(QDebug dbg, const KDChart::Measure& m)
    00227 {
    00228     dbg << "KDChart::Measure("
    00229         << "value="<<m.value()
    00230         << "calculationmode="<<m.calculationMode()
    00231         << "referencearea="<<m.referenceArea()
    00232         << "referenceorientation="<<m.referenceOrientation()
    00233         << ")";
    00234     return dbg;
    00235 }
    00236 #endif /* QT_NO_DEBUG_STREAM */