KDChartLineDiagram.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002  ** Copyright (C) 2007 Klaralvdalens 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  ** Foundtion 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 <QDebug>
00027 #include <QPainter>
00028 #include <QString>
00029 #include <QPainterPath>
00030 #include <QPen>
00031 #include <QVector>
00032 
00033 #include "KDChartLineDiagram.h"
00034 #include "KDChartBarDiagram.h"
00035 #include "KDChartPalette.h"
00036 #include "KDChartPosition.h"
00037 #include "KDChartTextAttributes.h"
00038 #include "KDChartAttributesModel.h"
00039 #include "KDChartAbstractGrid.h"
00040 #include "KDChartDataValueAttributes.h"
00041 
00042 #include <KDABLibFakes>
00043 
00044 #include "KDChartLineDiagram_p.h"
00045 #include "KDChartNormalLineDiagram_p.h"
00046 #include "KDChartStackedLineDiagram_p.h"
00047 #include "KDChartPercentLineDiagram_p.h"
00048 
00049 using namespace KDChart;
00050 
00051 LineDiagram::Private::Private()
00052 {
00053 }
00054 
00055 LineDiagram::Private::~Private() {}
00056 
00057 
00058 #define d d_func()
00059 
00060 
00061 LineDiagram::LineDiagram( QWidget* parent, CartesianCoordinatePlane* plane ) :
00062     AbstractCartesianDiagram( new Private(), parent, plane )
00063 {
00064     init();
00065 }
00066 
00067 void LineDiagram::init()
00068 {
00069     d->diagram = this;
00070     d->normalDiagram = new NormalLineDiagram( this );
00071     d->stackedDiagram = new StackedLineDiagram( this );
00072     d->percentDiagram = new PercentLineDiagram( this );
00073     d->implementor = d->normalDiagram;
00074 }
00075 
00076 LineDiagram::~LineDiagram()
00077 {
00078 }
00079 
00083 LineDiagram * LineDiagram::clone() const
00084 {
00085     LineDiagram* newDiagram = new LineDiagram( new Private( *d ) );
00086     newDiagram->setType( type() );
00087     return newDiagram;
00088 }
00089 
00090 
00091 bool LineDiagram::compare( const LineDiagram* other )const
00092 {
00093     if( other == this ) return true;
00094     if( ! other ){
00095         return false;
00096     }
00097     /*
00098     qDebug() <<"\n             LineDiagram::compare():";
00099             // compare own properties
00100     qDebug() << (type() == other->type());
00101     */
00102     return  // compare the base class
00103             ( static_cast<const AbstractCartesianDiagram*>(this)->compare( other ) ) &&
00104             // compare own properties
00105             (type() == other->type());
00106 }
00107 
00112 void LineDiagram::setType( const LineType type )
00113 {
00114     if ( d->implementor->type() == type ) return;
00115    if ( type != LineDiagram::Normal && datasetDimension() > 1 ) {
00116        Q_ASSERT_X ( false, "setType()",
00117                     "This line chart type can't be used with multi-dimensional data." );
00118        return;
00119    }
00120    switch( type ) {
00121    case Normal:
00122        d->implementor = d->normalDiagram;
00123        break;
00124    case Stacked:
00125        d->implementor = d->stackedDiagram;
00126        break;
00127    case Percent:
00128        d->implementor = d->percentDiagram;
00129        break;
00130    default:
00131        Q_ASSERT_X( false, "LineDiagram::setType", "unknown diagram subtype" );
00132    };
00133 
00134    // d->lineType = type;
00135    Q_ASSERT( d->implementor->type() == type );
00136 
00137    // AbstractAxis settings - see AbstractDiagram and CartesianAxis
00138    setPercentMode( type == LineDiagram::Percent );
00139    setDataBoundariesDirty();
00140    emit layoutChanged( this );
00141    emit propertiesChanged();
00142 }
00143 
00147 LineDiagram::LineType LineDiagram::type() const
00148 {
00149     return d->implementor->type();
00150 }
00151 
00155 void LineDiagram::setLineAttributes( const LineAttributes& la )
00156 {
00157     d->attributesModel->setModelData(
00158         qVariantFromValue( la ),
00159         LineAttributesRole );
00160     emit propertiesChanged();
00161 }
00162 
00166 void LineDiagram::setLineAttributes(
00167         int column,
00168     const LineAttributes& la )
00169 {
00170     d->attributesModel->setHeaderData(
00171             column,
00172             Qt::Vertical,
00173             qVariantFromValue( la ),
00174             LineAttributesRole );
00175     emit propertiesChanged();
00176 }
00177 
00181 void LineDiagram::resetLineAttributes( int column )
00182 {
00183     d->attributesModel->resetHeaderData(
00184             column, Qt::Vertical, LineAttributesRole );
00185     emit propertiesChanged();
00186 }
00187 
00191 void LineDiagram::setLineAttributes(
00192         const QModelIndex& index,
00193     const LineAttributes& la )
00194 {
00195     d->attributesModel->setData(
00196             d->attributesModel->mapFromSource(index),
00197     qVariantFromValue( la ),
00198     LineAttributesRole );
00199     emit propertiesChanged();
00200 }
00201 
00205 void LineDiagram::resetLineAttributes( const QModelIndex & index )
00206 {
00207     d->attributesModel->resetData(
00208             d->attributesModel->mapFromSource(index), LineAttributesRole );
00209     emit propertiesChanged();
00210 }
00211 
00215 LineAttributes LineDiagram::lineAttributes() const
00216 {
00217     return qVariantValue<LineAttributes>(
00218         d->attributesModel->data( KDChart::LineAttributesRole ) );
00219 }
00220 
00224 LineAttributes LineDiagram::lineAttributes( int column ) const
00225 {
00226     return qVariantValue<LineAttributes>(
00227         d->attributesModel->data(
00228             d->attributesModel->mapFromSource( columnToIndex( column ) ),
00229             KDChart::LineAttributesRole ) );
00230 }
00231 
00235 LineAttributes LineDiagram::lineAttributes(
00236     const QModelIndex& index ) const
00237 {
00238     return qVariantValue<LineAttributes>(
00239         d->attributesModel->data(
00240             d->attributesModel->mapFromSource(index),
00241             KDChart::LineAttributesRole ) );
00242 }
00243 
00247 void LineDiagram::setThreeDLineAttributes(
00248     const ThreeDLineAttributes& la )
00249 {
00250     setDataBoundariesDirty();
00251     d->attributesModel->setModelData(
00252         qVariantFromValue( la ),
00253         ThreeDLineAttributesRole );
00254    emit propertiesChanged();
00255 }
00256 
00260 void LineDiagram::setThreeDLineAttributes(
00261     int column,
00262     const ThreeDLineAttributes& la )
00263 {
00264     setDataBoundariesDirty();
00265     d->attributesModel->setHeaderData(
00266         column,
00267         Qt::Vertical,
00268         qVariantFromValue( la ),
00269         ThreeDLineAttributesRole );
00270    emit propertiesChanged();
00271 }
00272 
00276 void LineDiagram::setThreeDLineAttributes(
00277     const QModelIndex & index,
00278     const ThreeDLineAttributes& la )
00279 {
00280     setDataBoundariesDirty();
00281     d->attributesModel->setData(
00282         d->attributesModel->mapFromSource(index),
00283         qVariantFromValue( la ),
00284         ThreeDLineAttributesRole );
00285    emit propertiesChanged();
00286 }
00287 
00291 ThreeDLineAttributes LineDiagram::threeDLineAttributes() const
00292 {
00293     return qVariantValue<ThreeDLineAttributes>(
00294         d->attributesModel->data( KDChart::ThreeDLineAttributesRole ) );
00295 }
00296 
00300 ThreeDLineAttributes LineDiagram::threeDLineAttributes( int column ) const
00301 {
00302     return qVariantValue<ThreeDLineAttributes>(
00303         d->attributesModel->data(
00304             d->attributesModel->mapFromSource( columnToIndex( column ) ),
00305             KDChart::ThreeDLineAttributesRole ) );
00306 }
00307 
00311 ThreeDLineAttributes LineDiagram::threeDLineAttributes( const QModelIndex& index ) const
00312 {
00313     return qVariantValue<ThreeDLineAttributes>(
00314         d->attributesModel->data(
00315             d->attributesModel->mapFromSource( index ),
00316             KDChart::ThreeDLineAttributesRole ) );
00317 }
00318 
00319 double LineDiagram::threeDItemDepth( const QModelIndex& index ) const
00320 {
00321     return threeDLineAttributes( index ).validDepth();
00322 }
00323 
00324 double LineDiagram::threeDItemDepth( int column ) const
00325 {
00326     return qVariantValue<ThreeDLineAttributes>(
00327         d->attributesModel->headerData (
00328             column,
00329             Qt::Vertical,
00330             KDChart::ThreeDLineAttributesRole ) ).validDepth();
00331 }
00332 
00336 void LineDiagram::setValueTrackerAttributes( const QModelIndex & index,
00337                                              const ValueTrackerAttributes & va )
00338 {
00339     d->attributesModel->setData( d->attributesModel->mapFromSource(index),
00340                                  qVariantFromValue( va ),
00341                                  KDChart::ValueTrackerAttributesRole );
00342     emit propertiesChanged();
00343 }
00344 
00348 ValueTrackerAttributes LineDiagram::valueTrackerAttributes(
00349         const QModelIndex & index ) const
00350 {
00351     return qVariantValue<ValueTrackerAttributes>( d->attributesModel->data(
00352             d->attributesModel->mapFromSource( index ),
00353             KDChart::ValueTrackerAttributesRole ) );
00354 }
00355 
00356 void LineDiagram::resizeEvent ( QResizeEvent* )
00357 {
00358 }
00359 
00360 const QPair<QPointF, QPointF> LineDiagram::calculateDataBoundaries() const
00361 {
00362     if ( !checkInvariants( true ) ) return QPair<QPointF, QPointF>( QPointF( 0, 0 ), QPointF( 0, 0 ) );
00363 
00364     // note: calculateDataBoundaries() is ignoring the hidden flags.
00365     //       That's not a bug but a feature: Hiding data does not mean removing them.
00366     // For totally removing data from KD Chart's view people can use e.g. a proxy model ...
00367 
00368     // calculate boundaries for different line types Normal - Stacked - Percent - Default Normal
00369     return d->implementor->calculateDataBoundaries();
00370 }
00371 
00372 
00373 void LineDiagram::paintEvent ( QPaintEvent*)
00374 {
00375 //qDebug() << "starting LineDiagram::paintEvent ( QPaintEvent*)";
00376     QPainter painter ( viewport() );
00377     PaintContext ctx;
00378     ctx.setPainter ( &painter );
00379     ctx.setRectangle ( QRectF ( 0, 0, width(), height() ) );
00380     paint ( &ctx );
00381 //qDebug() << "         LineDiagram::paintEvent ( QPaintEvent*) ended.";
00382 }
00383 
00384 
00385 double LineDiagram::valueForCellTesting( int row, int column,
00386                                          bool& bOK,
00387                                          bool showHiddenCellsAsInvalid ) const
00388 {
00389     double value;
00390     if( showHiddenCellsAsInvalid && isHidden( model()->index( row, column, rootIndex() ) ) )
00391         bOK = false;
00392     else
00393         value = d->attributesModel->data(
00394                     d->attributesModel->index( row, column, attributesModelRootIndex() )
00395                 ).toDouble( &bOK );
00396     return bOK ? value : 0.0;
00397 }
00398 
00399 LineAttributes::MissingValuesPolicy LineDiagram::getCellValues(
00400       int row, int column,
00401       bool shiftCountedXValuesByHalfSection,
00402       double& valueX, double& valueY ) const
00403 {
00404     LineAttributes::MissingValuesPolicy policy;
00405 
00406     bool bOK = true;
00407     valueX = ( datasetDimension() > 1 && column > 0 )
00408              ? valueForCellTesting( row, column-1, bOK, true )
00409              : ((shiftCountedXValuesByHalfSection ? 0.5 : 0.0) + row);
00410     if( bOK )
00411         valueY = valueForCellTesting( row, column, bOK, true );
00412     if( bOK ){
00413         policy = LineAttributes::MissingValuesPolicyIgnored;
00414     }else{
00415         // missing value: find out the policy
00416         QModelIndex index = model()->index( row, column, rootIndex() );
00417         LineAttributes la = lineAttributes( index );
00418         policy = la.missingValuesPolicy();
00419     }
00420     return policy;
00421 }
00422 
00423 void LineDiagram::paint( PaintContext* ctx )
00424 {
00425     // note: Not having any data model assigned is no bug
00426     //       but we can not draw a diagram then either.
00427     if ( !checkInvariants( true ) ) return;
00428     if ( !AbstractGrid::isBoundariesValid(dataBoundaries()) ) return;
00429     const PainterSaver p( ctx->painter() );
00430     if( model()->rowCount() == 0 || model()->columnCount() == 0 )
00431         return; // nothing to paint for us
00432 
00433     AbstractCoordinatePlane* const plane = ctx->coordinatePlane();
00434     ctx->setCoordinatePlane( plane->sharedAxisMasterPlane( ctx->painter() ) );
00435 
00436 
00437     // paint different line types Normal - Stacked - Percent - Default Normal
00438     d->implementor->paint( ctx );
00439 
00440     ctx->setCoordinatePlane( plane );
00441 }
00442 
00443 void LineDiagram::resize ( const QSizeF& size )
00444 {
00445     d->compressor.setResolution( static_cast<int>( size.width() ),
00446                                  static_cast<int>( size.height() ) );
00447     setDataBoundariesDirty();
00448 }
00449 
00450 const int LineDiagram::numberOfAbscissaSegments () const
00451 {
00452     return d->attributesModel->rowCount(attributesModelRootIndex());
00453 }
00454 
00455 const int LineDiagram::numberOfOrdinateSegments () const
00456 {
00457     return d->attributesModel->columnCount(attributesModelRootIndex());
00458 }

Generated on Mon Sep 17 16:16:50 2007 for KD Chart 2 by  doxygen 1.5.1