KDChartDataIntern.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*-
00002    KDChart - a multi-platform charting engine
00003 */
00004 
00005 /****************************************************************************
00006  ** Copyright (C) 2001-2003 Klar�vdalens Datakonsult AB.  All rights reserved.
00007  **
00008  ** This file is part of the KDChart library.
00009  **
00010  ** This file may be distributed and/or modified under the terms of the
00011  ** GNU General Public License version 2 as published by the Free Software
00012  ** Foundation and appearing in the file LICENSE.GPL included in the
00013  ** packaging of this file.
00014  **
00015  ** Licensees holding valid commercial KDChart licenses may use this file in
00016  ** accordance with the KDChart Commercial License Agreement provided with
00017  ** the Software.
00018  **
00019  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021  **
00022  ** See http://www.klaralvdalens-datakonsult.se/?page=products for
00023  **   information about KDChart Commercial License Agreements.
00024  **
00025  ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
00026  ** licensing are not clear to you.
00027  **
00028  **********************************************************************/
00029 #ifndef __KDCHARTDATAINTERN_H__
00030 #define __KDCHARTDATAINTERN_H__
00031 
00032 #include <qvariant.h>
00033 #include <qstring.h>
00034 #include <qdatetime.h>
00035 #include "KDChartGlobal.h"
00036 
00045 // Please leave all methods in this class inline!
00046 // It's necessary since it's part of the interface provided by KDChart
00047 class KDCHART_EXPORT KDChartData
00048 {
00049 public:
00050     // 0. default c'tor: initializing all values as undefined
00051 
00052     KDChartData() :
00053         _valueType(  QVariant::Invalid ),
00054         _valueType2( QVariant::Invalid ),
00055         _propSetID(  0 )
00056     {}
00057 
00058     // 1. simple c'tors: used for 1-coordinate data
00059 
00060     KDChartData( double value ) :
00061         _valueType(  QVariant::Double ), dValue( value ),
00062         _valueType2( QVariant::Invalid ),
00063         _propSetID(  0 )
00064     {}
00065     /* string values are only supported for legend texts or axis labels */
00066     KDChartData( const QString& value ) :
00067         _valueType(  QVariant::String ), sValue( value ),
00068         _valueType2( QVariant::Invalid ),
00069         _propSetID(  0 )
00070     {}
00071     /* date/time values for /ordinate/ axes are not implemented yet
00072        KDChartData( QDateTime value ) :
00073        _valueType(  QVariant::DateTime ), dtValue( value ),
00074        _valueType2( QVariant::Invalid ),
00075        _propSetID(  0 )
00076        {}*/
00077 
00078     // 2. complex c'tors: used for 2-coordinate data
00079 
00080     // 2.a) with additional Date/Time: normally used when Date on x-axis
00081     //      e.g. for time related index numbers like water level measurements
00082     KDChartData( double yValue, QDateTime xValue ) :
00083         _valueType(  QVariant::Double   ), dValue(   yValue  ),
00084         _valueType2( QVariant::DateTime ), dtValue2( xValue ),
00085         _propSetID(  0 )
00086     {}
00087 
00088 
00089     KDChartData( const QVariant& _value1,
00090                  const QVariant& _value2 )
00091     {
00092         switch( _value1.type() ){
00093             case QVariant::Int:
00094             case QVariant::UInt:
00095             case QVariant::Double:
00096                 _valueType = QVariant::Double;
00097                 dValue     = _value1.toDouble();
00098                 break;
00099             case QVariant::DateTime:
00100                 _valueType = QVariant::DateTime;
00101                 dtValue    = _value1.toDateTime();
00102                 break;
00103             case QVariant::String:
00104                 _valueType = QVariant::String;
00105                 sValue     = _value1.toString();
00106                 break;
00107             default:
00108                 _valueType = QVariant::Invalid;
00109         }
00110         switch( _value2.type() ){
00111             case QVariant::Int:
00112             case QVariant::UInt:
00113             case QVariant::Double:
00114                 _valueType2 = QVariant::Double;
00115                 dValue2     = _value2.toDouble();
00116                 break;
00117             case QVariant::DateTime:
00118                 _valueType2 = QVariant::DateTime;
00119                 dtValue2    = _value2.toDateTime();
00120                 break;
00121             default:
00122                 _valueType2 = QVariant::Invalid;
00123         }
00124         _propSetID = 0;
00125     }
00126 
00127     /* date/time values for /ordinate/ axes are not implemented yet
00128        KDChartData( QDateTime yValue, QDateTime xValue ) :
00129        _valueType(  DateTime ), dtValue(  yValue  ),
00130        _valueType2( DateTime ), dtValue2( xValue ),
00131        _propSetID(  0 )
00132        {}*/
00133     // 2.b) with additional Double: may be used for mathematical data...
00134     KDChartData( double yValue, double xValue ) :
00135         _valueType(  QVariant::Double ), dValue(  yValue  ),
00136         _valueType2( QVariant::Double ), dValue2( xValue ),
00137         _propSetID(  0 )
00138     {}
00139     /* date/time values for /ordinate/ axes are not implemented yet
00140        KDChartData( QDateTime yValue, double xValue ) :
00141        _valueType(  DateTime ), dtValue( yValue  ),
00142        _valueType2( Double   ), dValue2( xValue ),
00143        _propSetID(  0 )
00144        {}*/
00145 
00146 /*
00147     /-**
00148        Copy constructor.
00149 
00150        \sa setData
00151     * /
00152     KDChartData( const KDChartData& other ) :
00153     {
00154         setData( other );
00155     }
00156 
00157     /-**
00158        Assignment operator.
00159 
00160        \sa setData
00161     * /
00162     KDChartData& operator=( const KDChartData& R )
00163     {
00164         setData( R );
00165         return *this;
00166     }
00167 */
00173     bool operator==( const KDChartData& it ) const
00174     {
00175         return isEqual(*this, it);
00176     }
00177 
00183     bool operator!=( const KDChartData& it ) const
00184     {
00185         return !isEqual(*this, it);
00186     }
00187 
00195     bool isEqual( const KDChartData& a, const KDChartData& b ) const
00196     {
00197         bool bRet = (a.hasValue( 1 ) == b.hasValue( 1 )) &&
00198                     (a.hasValue( 2 ) == b.hasValue( 2 ));
00199         if ( bRet && a.hasValue( 1 ) ) {
00200             bRet = a.valueType( 1 ) == b.valueType( 1 );
00201             if ( bRet ) {
00202                 switch ( a.valueType( 1 ) ) {
00203                 case QVariant::String:
00204                     bRet = a.stringValue( 1 ) == b.stringValue( 1 );
00205                     break;
00206                 case QVariant::Double:
00207                     bRet = a.doubleValue( 1 ) == b.doubleValue( 1 );
00208                     break;
00209                 case QVariant::DateTime:
00210                     bRet = a.dateTimeValue( 1 ) == b.dateTimeValue( 1 );
00211                     break;
00212                 default:
00213                     bRet = false;
00214                 }
00215             }
00216             if ( bRet && a.hasValue( 2 ) ) {
00217                 bRet = a.valueType( 2 ) == b.valueType( 2 );
00218                 if ( bRet ) {
00219                     switch ( a.valueType( 2 ) ) {
00220                         // note: the 2nd value can not be a string
00221                         //       - must be a date or a number!
00222                     case QVariant::Double:
00223                         bRet = a.doubleValue( 2 ) == b.doubleValue( 2 );
00224                         break;
00225                     case QVariant::DateTime:
00226                         bRet = a.dateTimeValue( 2 ) == b.dateTimeValue( 2 );
00227                         break;
00228                     default:
00229                         bRet = false;
00230                     }
00231                 }
00232             }
00233             // Note: We do *not* compare the _propSetID here since it contains
00234             //       no values but is used to handle some layout information...
00235         }
00236         return bRet;
00237     }
00238 
00246     void setAll( const KDChartData& R )
00247     {
00248         setData( R );
00249         setPropertySet( R.propertySet() );
00250     }
00251 
00252 
00261     void setData( const KDChartData& R )
00262     {
00263         if( &R != this ){
00264             _valueType  = R._valueType;
00265             _valueType2 = R._valueType2;
00266             switch ( valueType( 1 ) ) {
00267             case QVariant::String:
00268                 sValue  = R.sValue;
00269                 break;
00270             case QVariant::Double:
00271                 dValue  = R.dValue;
00272                 break;
00273             case QVariant::DateTime:
00274                 dtValue = R.dtValue;
00275                 break;
00276             default:
00277                 /* NOOP */;
00278             }
00279             switch ( valueType( 2 ) ) {
00280                 // note: the 2nd value can not be a string
00281                 //       - must be a date or a number!
00282             case QVariant::Double:
00283                 dValue2  = R.dValue2;
00284                 break;
00285             case QVariant::DateTime:
00286                 dtValue2 = R.dtValue2;
00287                 break;
00288             default:
00289                 /* NOOP */;
00290             }
00291             // Note: We do *not* copy the _propSetID here since it contains
00292             //       no values but is used to handle some layout information...
00293         }
00294     }
00295 
00296     QVariant::Type valueType( int valNo=1 ) const
00297     {
00298         return (1 == valNo)
00299                     ? _valueType
00300                     : _valueType2;
00301     }
00302     bool hasValue( int valNo=1 ) const
00303     {
00304         return (1 == valNo)
00305             ?  (_valueType != QVariant::Invalid)
00306             : ((_valueType2 == QVariant::Double) || (_valueType2 == QVariant::DateTime));
00307     }
00308     /* string values are only supported for legend texts or axis labels */
00309     bool isString( int valNo=1 ) const
00310     {
00311         return (1 == valNo)
00312                     ? (_valueType == QVariant::String)
00313                     : false;
00314     }
00315     bool isDouble( int valNo=1 ) const
00316     {
00317         return (1 == valNo)
00318                     ? (_valueType == QVariant::Double)
00319                     : (_valueType2 == QVariant::Double);
00320     }
00321     bool isDateTime( int valNo=1 ) const
00322     {
00323         return (1 == valNo)
00324                     ? (_valueType == QVariant::DateTime)
00325                     : (_valueType2 == QVariant::DateTime);
00326     }
00327 
00328 
00329     void clearValue()
00330     {
00331         _valueType  = QVariant::Invalid;
00332         _valueType2 = QVariant::Invalid;
00333         _propSetID  = 0;
00334     }
00335 
00336     QVariant value( int valNo=1 ) const
00337     {
00338         if( 1 == valNo )
00339             switch ( valueType( 1 ) ) {
00340             case QVariant::String:
00341                 return sValue;
00342             case QVariant::Double:
00343                 return dValue;
00344             case QVariant::DateTime:
00345                 return dtValue;
00346             default:
00347                 return QVariant();
00348             }
00349         else if( 2 == valNo )
00350             switch ( valueType( 2 ) ) {
00351             case QVariant::Double:
00352                 return dValue2;
00353             case QVariant::DateTime:
00354                 return dtValue2;
00355             default:
00356                 return QVariant();
00357             }
00358         else
00359             return QVariant();
00360     }
00361 
00362     /* string values are only supported for legend texts or axis labels */
00363     QString stringValue( int valNo=1 ) const
00364     {
00365         // note: the 2nd value can not be a string
00366         //       - must be a date or a number!
00367         if ((1 == valNo) && isString( valNo ))
00368             return sValue;
00369         else
00370             return QString::null;
00371     }
00372     double doubleValue( int valNo=1 ) const
00373     {
00374         return isDouble( valNo )
00375             ? ((1 == valNo) ? dValue : dValue2)
00376             : DBL_MIN;
00377     }
00378     QDateTime dateTimeValue( int valNo=1 ) const
00379     {
00380         return isDateTime( valNo )
00381             ? ((1 == valNo) ? dtValue : dtValue2)
00382             : QDateTime();
00383     }
00384 
00397     void setPropertySet( int propSetID = 0 )
00398     {
00399         _propSetID = propSetID;
00400     }
00409     int propertySet() const
00410     {
00411         return _propSetID;
00412     }
00413 
00414 
00415 private:
00416     // OBLIGATORY 1st value: usually used for ordinate axis
00417     QVariant::Type _valueType;
00418     QDateTime dtValue;
00419     double dValue;  // Sorry, d(t)Value and sValue cannot be a union,
00420     QString sValue; // since QString has a non-default constructor.
00421 
00422     // OPTIONAL 2nd value: if valid, normally used for abscissa axis
00423     // note: this 2nd value can not be a string - must be a date or a number!
00424     QVariant::Type _valueType2;
00425     QDateTime dtValue2;
00426     double dValue2;
00427 
00428     // ID number of the property set assigned to this cell
00429     int _propSetID;
00430 };
00431 
00432 #endif

Generated on Wed Jan 26 13:03:16 2011 for KMyMoney by  doxygen 1.5.6