# KDChartPlotter.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 "KDChartPlotter.h"
    00027
    00028 #include "KDChartAbstractGrid.h"
    00029
    00030 #include <KDABLibFakes>
    00031
    00032 #include "KDChartPlotter_p.h"
    00033 #include "KDChartNormalPlotter_p.h"
    00034
    00035 using namespace KDChart;
    00036
    00037 Plotter::Private::Private()
    00038 {
    00039 }
    00040
    00041 Plotter::Private::~Private() {}
    00042
    00043
    00044 #define d d_func()
    00045
    00046
    00047 Plotter::Plotter( QWidget* parent, CartesianCoordinatePlane* plane ) :
    00048     AbstractCartesianDiagram( new Private(), parent, plane )
    00049 {
    00050     init();
    00051 }
    00052
    00053 void Plotter::init()
    00054 {
    00055     d->diagram = this;
    00056     d->normalPlotter = new NormalPlotter( this );
    00057     d->implementor = d->normalPlotter;
    00058
    00059     setDatasetDimension( 2 );
    00060 }
    00061
    00062 Plotter::~Plotter()
    00063 {
    00064 }
    00065
    00069 Plotter* Plotter::clone() const
    00070 {
    00071     Plotter* newDiagram = new Plotter( new Private( *d ) );
    00072     newDiagram->setType( type() );
    00073     return newDiagram;
    00074 }
    00075
    00076 bool Plotter::compare( const Plotter* other )const
    00077 {
    00078     if( other == this )
    00079         return true;
    00080     if( other == 0 )
    00081         return false;
    00082     return  // compare the base class
    00083             ( static_cast< const AbstractCartesianDiagram* >( this )->compare( other ) ) &&
    00084             // compare own properties
    00085             ( type() == other->type() );
    00086 }
    00087
    00092 void Plotter::setType( const PlotType type )
    00093 {
    00094     if( d->implementor->type() == type )
    00095         return;
    00096     if( type != Plotter::Normal && datasetDimension() != 2 )
    00097     {
    00098        Q_ASSERT_X ( false, "setType()",
    00099                     "This line chart type can only be used with two-dimensional data." );
    00100        return;
    00101    }
    00102    switch( type ) {
    00103    case Normal:
    00104        d->implementor = d->normalPlotter;
    00105        break;
    00106    default:
    00107        Q_ASSERT_X( false, "Plotter::setType", "unknown plotter subtype" );
    00108    };
    00109
    00110    // d->lineType = type;
    00111    Q_ASSERT( d->implementor->type() == type );
    00112
    00113    setDataBoundariesDirty();
    00114    emit layoutChanged( this );
    00115    emit propertiesChanged();
    00116 }
    00117
    00121 Plotter::PlotType Plotter::type() const
    00122 {
    00123     return d->implementor->type();
    00124 }
    00125
    00129 void Plotter::setLineAttributes( const LineAttributes& la )
    00130 {
    00131     d->attributesModel->setModelData(
    00132         qVariantFromValue( la ),
    00133         LineAttributesRole );
    00134     emit propertiesChanged();
    00135 }
    00136
    00140 void Plotter::setLineAttributes(
    00141         int column,
    00142     const LineAttributes& la )
    00143 {
    00144     d->attributesModel->setHeaderData(
    00145             column,
    00146             Qt::Vertical,
    00147             qVariantFromValue( la ),
    00148             LineAttributesRole );
    00149     emit propertiesChanged();
    00150 }
    00151
    00155 void Plotter::resetLineAttributes( int column )
    00156 {
    00157     d->attributesModel->resetHeaderData(
    00158             column, Qt::Vertical, LineAttributesRole );
    00159     emit propertiesChanged();
    00160 }
    00161
    00165 void Plotter::setLineAttributes(
    00166         const QModelIndex & index,
    00167     const LineAttributes& la )
    00168 {
    00169     d->attributesModel->setData(
    00170             d->attributesModel->mapFromSource(index),
    00171     qVariantFromValue( la ),
    00172     LineAttributesRole );
    00173     emit propertiesChanged();
    00174 }
    00175
    00179 void Plotter::resetLineAttributes( const QModelIndex & index )
    00180 {
    00181     d->attributesModel->resetData(
    00182             d->attributesModel->mapFromSource(index), LineAttributesRole );
    00183     emit propertiesChanged();
    00184 }
    00185
    00189 LineAttributes Plotter::lineAttributes() const
    00190 {
    00191     return qVariantValue<LineAttributes>(
    00192         d->attributesModel->data( KDChart::LineAttributesRole ) );
    00193 }
    00194
    00198 LineAttributes Plotter::lineAttributes( int column ) const
    00199 {
    00200     return qVariantValue<LineAttributes>(
    00201         d->attributesModel->data(
    00202             d->attributesModel->mapFromSource( columnToIndex( column ) ),
    00203             KDChart::LineAttributesRole ) );
    00204 }
    00205
    00209 LineAttributes Plotter::lineAttributes(
    00210     const QModelIndex& index ) const
    00211 {
    00212     return qVariantValue<LineAttributes>(
    00213         d->attributesModel->data(
    00214             d->attributesModel->mapFromSource(index),
    00215             KDChart::LineAttributesRole ) );
    00216 }
    00217
    00221 void Plotter::setThreeDLineAttributes(
    00222     const ThreeDLineAttributes& la )
    00223 {
    00224     setDataBoundariesDirty();
    00225     d->attributesModel->setModelData(
    00226         qVariantFromValue( la ),
    00227         ThreeDLineAttributesRole );
    00228    emit propertiesChanged();
    00229 }
    00230
    00234 void Plotter::setThreeDLineAttributes(
    00235     int column,
    00236     const ThreeDLineAttributes& la )
    00237 {
    00238     setDataBoundariesDirty();
    00239     d->attributesModel->setHeaderData(
    00240         column,
    00241         Qt::Vertical,
    00242         qVariantFromValue( la ),
    00243         ThreeDLineAttributesRole );
    00244    emit propertiesChanged();
    00245 }
    00246
    00250 void Plotter::setThreeDLineAttributes(
    00251     const QModelIndex& index,
    00252     const ThreeDLineAttributes& la )
    00253 {
    00254     setDataBoundariesDirty();
    00255     d->attributesModel->setData(
    00256         d->attributesModel->mapFromSource(index),
    00257         qVariantFromValue( la ),
    00258         ThreeDLineAttributesRole );
    00259    emit propertiesChanged();
    00260 }
    00261
    00265 ThreeDLineAttributes Plotter::threeDLineAttributes() const
    00266 {
    00267     return qVariantValue<ThreeDLineAttributes>(
    00268         d->attributesModel->data( KDChart::ThreeDLineAttributesRole ) );
    00269 }
    00270
    00274 ThreeDLineAttributes Plotter::threeDLineAttributes( int column ) const
    00275 {
    00276     return qVariantValue<ThreeDLineAttributes>(
    00277         d->attributesModel->data(
    00278             d->attributesModel->mapFromSource( columnToIndex( column ) ),
    00279             KDChart::ThreeDLineAttributesRole ) );
    00280 }
    00281
    00285 ThreeDLineAttributes Plotter::threeDLineAttributes(
    00286     const QModelIndex& index ) const
    00287 {
    00288     return qVariantValue<ThreeDLineAttributes>(
    00289         d->attributesModel->data(
    00290             d->attributesModel->mapFromSource( index ),
    00291             KDChart::ThreeDLineAttributesRole ) );
    00292 }
    00293
    00294 double Plotter::threeDItemDepth( const QModelIndex & index ) const
    00295 {
    00296     return threeDLineAttributes( index ).validDepth();
    00297 }
    00298
    00299 double Plotter::threeDItemDepth( int column ) const
    00300 {
    00301     return qVariantValue<ThreeDLineAttributes>(
    00302         d->attributesModel->headerData (
    00303             column,
    00304             Qt::Vertical,
    00305             KDChart::ThreeDLineAttributesRole ) ).validDepth();
    00306 }
    00307
    00311 void Plotter::setValueTrackerAttributes( const QModelIndex & index,
    00312                                              const ValueTrackerAttributes & va )
    00313 {
    00314     d->attributesModel->setData( d->attributesModel->mapFromSource(index),
    00315                                  qVariantFromValue( va ),
    00316                                  KDChart::ValueTrackerAttributesRole );
    00317     emit propertiesChanged();
    00318 }
    00319
    00323 ValueTrackerAttributes Plotter::valueTrackerAttributes(
    00324         const QModelIndex & index ) const
    00325 {
    00326     return qVariantValue<ValueTrackerAttributes>( d->attributesModel->data(
    00327             d->attributesModel->mapFromSource( index ),
    00328             KDChart::ValueTrackerAttributesRole ) );
    00329 }
    00330
    00331 void Plotter::resizeEvent ( QResizeEvent* )
    00332 {
    00333 }
    00334
    00335 const QPair< QPointF, QPointF > Plotter::calculateDataBoundaries() const
    00336 {
    00337     if ( !checkInvariants( true ) )
    00338         return QPair< QPointF, QPointF >( QPointF( 0, 0 ), QPointF( 0, 0 ) );
    00339
    00340     // note: calculateDataBoundaries() is ignoring the hidden flags.
    00341     //       That's not a bug but a feature: Hiding data does not mean removing them.
    00342     // For totally removing data from KD Chart's view people can use e.g. a proxy model ...
    00343
    00344     // calculate boundaries for different line types Normal - Stacked - Percent - Default Normal
    00345     return d->implementor->calculateDataBoundaries();
    00346 }
    00347
    00348
    00349 void Plotter::paintEvent ( QPaintEvent*)
    00350 {
    00351     QPainter painter ( viewport() );
    00352     PaintContext ctx;
    00353     ctx.setPainter ( &painter );
    00354     ctx.setRectangle ( QRectF ( 0, 0, width(), height() ) );
    00355     paint ( &ctx );
    00356 }
    00357
    00358 void Plotter::paint( PaintContext* ctx )
    00359 {
    00360     // note: Not having any data model assigned is no bug
    00361     //       but we can not draw a diagram then either.
    00362     if ( !checkInvariants( true ) ) return;
    00363     if ( !AbstractGrid::isBoundariesValid(dataBoundaries()) ) return;
    00364     const PainterSaver p( ctx->painter() );
    00365     if( model()->rowCount() == 0 || model()->columnCount() == 0 )
    00366         return; // nothing to paint for us
    00367
    00368     AbstractCoordinatePlane* const plane = ctx->coordinatePlane();
    00369     ctx->setCoordinatePlane( plane->sharedAxisMasterPlane( ctx->painter() ) );
    00370
    00371
    00372     // paint different line types Normal - Stacked - Percent - Default Normal
    00373     d->implementor->paint( ctx );
    00374
    00375     ctx->setCoordinatePlane( plane );
    00376 }
    00377
    00378 void Plotter::resize ( const QSizeF& size )
    00379 {
    00380     d->compressor.setResolution( static_cast<int>( size.width() ),
    00381                                  static_cast<int>( size.height() ) );
    00382     setDataBoundariesDirty();
    00383 }
    00384
    00385 const int Plotter::numberOfAbscissaSegments () const
    00386 {
    00387     return d->attributesModel->rowCount( attributesModelRootIndex() );
    00388 }
    00389
    00390 const int Plotter::numberOfOrdinateSegments () const
    00391 {
    00392     return d->attributesModel->columnCount( attributesModelRootIndex() );
    00393 }