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 #include "KDChartPlotter_p.h"
00028 
00029 #include "KDChartAbstractGrid.h"
00030 
00031 #include <KDABLibFakes>
00032 
00033 #include "KDChartNormalPlotter_p.h"
00034 #include "KDChartPercentPlotter_p.h"
00035 
00036 using namespace KDChart;
00037 
00038 Plotter::Private::Private()
00039 {
00040 }
00041 
00042 Plotter::Private::~Private() {}
00043 
00044 
00045 #define d d_func()
00046 
00047 
00048 Plotter::Plotter( QWidget* parent, CartesianCoordinatePlane* plane ) :
00049     AbstractCartesianDiagram( new Private(), parent, plane )
00050 {
00051     init();
00052 }
00053 
00054 void Plotter::init()
00055 {
00056     d->diagram = this;
00057     d->normalPlotter = new NormalPlotter( this );
00058     d->percentPlotter = new PercentPlotter( this );
00059     d->implementor = d->normalPlotter;
00060 
00061     setDatasetDimensionInternal( 2 );
00062 }
00063 
00064 Plotter::~Plotter()
00065 {
00066 }
00067 
00071 Plotter* Plotter::clone() const
00072 {
00073     Plotter* newDiagram = new Plotter( new Private( *d ) );
00074     newDiagram->setType( type() );
00075     return newDiagram;
00076 }
00077 
00078 bool Plotter::compare( const Plotter* other )const
00079 {
00080     if( other == this )
00081         return true;
00082     if( other == 0 )
00083         return false;
00084     return  // compare the base class
00085             ( static_cast< const AbstractCartesianDiagram* >( this )->compare( other ) ) &&
00086             // compare own properties
00087             ( type() == other->type() );
00088 }
00089 
00093 void Plotter::setType( const PlotType type )
00094 {
00095     if( d->implementor->type() == type )
00096         return;
00097     if( datasetDimension() != 2 )
00098     {
00099        Q_ASSERT_X ( false, "setType()",
00100                     "This line chart type can only be used with two-dimensional data." );
00101        return;
00102     }
00103     switch( type ) {
00104     case Normal:
00105         d->implementor = d->normalPlotter;
00106         break;
00107     case Percent:
00108         d->implementor = d->percentPlotter;
00109         break;
00110     default:
00111         Q_ASSERT_X( false, "Plotter::setType", "unknown plotter subtype" );
00112     };
00113 
00114     // d->lineType = type;
00115     Q_ASSERT( d->implementor->type() == type );
00116 
00117     setDataBoundariesDirty();
00118     emit layoutChanged( this );
00119     emit propertiesChanged();
00120 }
00121 
00125 Plotter::PlotType Plotter::type() const
00126 {
00127     return d->implementor->type();
00128 }
00129 
00133 void Plotter::setLineAttributes( const LineAttributes& la )
00134 {
00135     d->attributesModel->setModelData(
00136         qVariantFromValue( la ),
00137         LineAttributesRole );
00138     emit propertiesChanged();
00139 }
00140 
00144 void Plotter::setLineAttributes(
00145         int column,
00146     const LineAttributes& la )
00147 {
00148     d->attributesModel->setHeaderData(
00149             column,
00150             Qt::Vertical,
00151             qVariantFromValue( la ),
00152             LineAttributesRole );
00153     emit propertiesChanged();
00154 }
00155 
00159 void Plotter::resetLineAttributes( int column )
00160 {
00161     d->attributesModel->resetHeaderData(
00162             column, Qt::Vertical, LineAttributesRole );
00163     emit propertiesChanged();
00164 }
00165 
00169 void Plotter::setLineAttributes(
00170         const QModelIndex & index,
00171     const LineAttributes& la )
00172 {
00173     d->attributesModel->setData(
00174             d->attributesModel->mapFromSource(index),
00175     qVariantFromValue( la ),
00176     LineAttributesRole );
00177     emit propertiesChanged();
00178 }
00179 
00183 void Plotter::resetLineAttributes( const QModelIndex & index )
00184 {
00185     d->attributesModel->resetData(
00186             d->attributesModel->mapFromSource(index), LineAttributesRole );
00187     emit propertiesChanged();
00188 }
00189 
00193 LineAttributes Plotter::lineAttributes() const
00194 {
00195     return qVariantValue<LineAttributes>(
00196         d->attributesModel->data( KDChart::LineAttributesRole ) );
00197 }
00198 
00202 LineAttributes Plotter::lineAttributes( int column ) const
00203 {
00204     const QVariant attrs(
00205             d->attributesModel->headerData( column, Qt::Vertical,
00206                                             LineAttributesRole ) );
00207     if( attrs.isValid() )
00208         return qVariantValue< LineAttributes >( attrs );
00209     return lineAttributes();
00210 }
00211 
00215 LineAttributes Plotter::lineAttributes(
00216     const QModelIndex& index ) const
00217 {
00218     return qVariantValue<LineAttributes>(
00219         d->attributesModel->data(
00220             d->attributesModel->mapFromSource(index),
00221             KDChart::LineAttributesRole ) );
00222 }
00223 
00227 void Plotter::setThreeDLineAttributes(
00228     const ThreeDLineAttributes& la )
00229 {
00230     setDataBoundariesDirty();
00231     d->attributesModel->setModelData(
00232         qVariantFromValue( la ),
00233         ThreeDLineAttributesRole );
00234    emit propertiesChanged();
00235 }
00236 
00240 void Plotter::setThreeDLineAttributes(
00241     int column,
00242     const ThreeDLineAttributes& la )
00243 {
00244     setDataBoundariesDirty();
00245     d->attributesModel->setHeaderData(
00246         column,
00247         Qt::Vertical,
00248         qVariantFromValue( la ),
00249         ThreeDLineAttributesRole );
00250    emit propertiesChanged();
00251 }
00252 
00256 void Plotter::setThreeDLineAttributes(
00257     const QModelIndex& index,
00258     const ThreeDLineAttributes& la )
00259 {
00260     setDataBoundariesDirty();
00261     d->attributesModel->setData(
00262         d->attributesModel->mapFromSource(index),
00263         qVariantFromValue( la ),
00264         ThreeDLineAttributesRole );
00265    emit propertiesChanged();
00266 }
00267 
00271 ThreeDLineAttributes Plotter::threeDLineAttributes() const
00272 {
00273     return qVariantValue<ThreeDLineAttributes>(
00274         d->attributesModel->data( KDChart::ThreeDLineAttributesRole ) );
00275 }
00276 
00280 ThreeDLineAttributes Plotter::threeDLineAttributes( int column ) const
00281 {
00282     const QVariant attrs(
00283             d->attributesModel->headerData( column, Qt::Vertical,
00284                                             ThreeDLineAttributesRole ) );
00285     if( attrs.isValid() )
00286         return qVariantValue< ThreeDLineAttributes >( attrs );
00287     return threeDLineAttributes();
00288 }
00289 
00293 ThreeDLineAttributes Plotter::threeDLineAttributes(
00294     const QModelIndex& index ) const
00295 {
00296     return qVariantValue<ThreeDLineAttributes>(
00297         d->attributesModel->data(
00298             d->attributesModel->mapFromSource( index ),
00299             KDChart::ThreeDLineAttributesRole ) );
00300 }
00301 
00302 double Plotter::threeDItemDepth( const QModelIndex & index ) const
00303 {
00304     return threeDLineAttributes( index ).validDepth();
00305 }
00306 
00307 double Plotter::threeDItemDepth( int column ) const
00308 {
00309     return qVariantValue<ThreeDLineAttributes>(
00310         d->attributesModel->headerData (
00311             column,
00312             Qt::Vertical,
00313             KDChart::ThreeDLineAttributesRole ) ).validDepth();
00314 }
00315 
00319 void Plotter::setValueTrackerAttributes( const QModelIndex & index,
00320                                              const ValueTrackerAttributes & va )
00321 {
00322     d->attributesModel->setData( d->attributesModel->mapFromSource(index),
00323                                  qVariantFromValue( va ),
00324                                  KDChart::ValueTrackerAttributesRole );
00325     emit propertiesChanged();
00326 }
00327 
00331 ValueTrackerAttributes Plotter::valueTrackerAttributes(
00332         const QModelIndex & index ) const
00333 {
00334     return qVariantValue<ValueTrackerAttributes>( d->attributesModel->data(
00335             d->attributesModel->mapFromSource( index ),
00336             KDChart::ValueTrackerAttributesRole ) );
00337 }
00338 
00339 void Plotter::resizeEvent ( QResizeEvent* )
00340 {
00341 }
00342 
00343 const QPair< QPointF, QPointF > Plotter::calculateDataBoundaries() const
00344 {
00345     if ( !checkInvariants( true ) )
00346         return QPair< QPointF, QPointF >( QPointF( 0, 0 ), QPointF( 0, 0 ) );
00347 
00348     // note: calculateDataBoundaries() is ignoring the hidden flags.
00349     //       That's not a bug but a feature: Hiding data does not mean removing them.
00350     // For totally removing data from KD Chart's view people can use e.g. a proxy model ...
00351 
00352     // calculate boundaries for different line types Normal - Stacked - Percent - Default Normal
00353     return d->implementor->calculateDataBoundaries();
00354 }
00355 
00356 
00357 void Plotter::paintEvent ( QPaintEvent*)
00358 {
00359     QPainter painter ( viewport() );
00360     PaintContext ctx;
00361     ctx.setPainter ( &painter );
00362     ctx.setRectangle ( QRectF ( 0, 0, width(), height() ) );
00363     paint ( &ctx );
00364 }
00365 
00366 void Plotter::paint( PaintContext* ctx )
00367 {
00368     // note: Not having any data model assigned is no bug
00369     //       but we can not draw a diagram then either.
00370     if ( !checkInvariants( true ) ) return;
00371 
00372     AbstractCoordinatePlane* const plane = ctx->coordinatePlane();
00373     if( ! plane ) return;
00374     d->setCompressorResolution( size(), plane );
00375 
00376     if ( !AbstractGrid::isBoundariesValid(dataBoundaries()) ) return;
00377 
00378     const PainterSaver p( ctx->painter() );
00379     if( model()->rowCount( rootIndex() ) == 0 || model()->columnCount( rootIndex() ) == 0 )
00380         return; // nothing to paint for us
00381 
00382     ctx->setCoordinatePlane( plane->sharedAxisMasterPlane( ctx->painter() ) );
00383 
00384     // paint different line types Normal - Stacked - Percent - Default Normal
00385     d->implementor->paint( ctx );
00386 
00387     ctx->setCoordinatePlane( plane );
00388 }
00389 
00390 void Plotter::resize ( const QSizeF& size )
00391 {
00392     d->setCompressorResolution( size, coordinatePlane() );
00393     setDataBoundariesDirty();
00394 }
00395 
00396 #if QT_VERSION < 0x040400 || defined(Q_COMPILER_MANGLES_RETURN_TYPE)
00397 const
00398 #endif
00399 int Plotter::numberOfAbscissaSegments () const
00400 {
00401     return d->attributesModel->rowCount( attributesModelRootIndex() );
00402 }
00403 
00404 #if QT_VERSION < 0x040400 || defined(Q_COMPILER_MANGLES_RETURN_TYPE)
00405 const
00406 #endif
00407 int Plotter::numberOfOrdinateSegments () const
00408 {
00409     return d->attributesModel->columnCount( attributesModelRootIndex() );
00410 }

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