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

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