00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "KDChartLineDiagram.h"
00024 #include "KDChartLineDiagram_p.h"
00025
00026 #include "KDChartBarDiagram.h"
00027 #include "KDChartPalette.h"
00028 #include "KDChartPosition.h"
00029 #include "KDChartTextAttributes.h"
00030 #include "KDChartAttributesModel.h"
00031 #include "KDChartAbstractGrid.h"
00032 #include "KDChartDataValueAttributes.h"
00033
00034 #include <KDABLibFakes>
00035
00036 #include "KDChartNormalLineDiagram_p.h"
00037 #include "KDChartStackedLineDiagram_p.h"
00038 #include "KDChartPercentLineDiagram_p.h"
00039
00040 #include <QDebug>
00041 #include <QPainter>
00042 #include <QString>
00043 #include <QPainterPath>
00044 #include <QPen>
00045 #include <QVector>
00046
00047 using namespace KDChart;
00048
00049 LineDiagram::Private::Private()
00050 {
00051 }
00052
00053 LineDiagram::Private::~Private() {}
00054
00055
00056 #define d d_func()
00057
00058
00059 LineDiagram::LineDiagram( QWidget* parent, CartesianCoordinatePlane* plane ) :
00060 AbstractCartesianDiagram( new Private(), parent, plane )
00061 {
00062 init();
00063 }
00064
00065 void LineDiagram::init()
00066 {
00067 d->diagram = this;
00068 d->normalDiagram = new NormalLineDiagram( this );
00069 d->stackedDiagram = new StackedLineDiagram( this );
00070 d->percentDiagram = new PercentLineDiagram( this );
00071 d->implementor = d->normalDiagram;
00072 d->centerDataPoints = false;
00073 d->reverseDatasetOrder = false;
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
00099
00100
00101
00102 return
00103 ( static_cast<const AbstractCartesianDiagram*>(this)->compare( other ) ) &&
00104
00105 (type() == other->type()) &&
00106 (centerDataPoints() == other->centerDataPoints()) &&
00107 (reverseDatasetOrder() == other->reverseDatasetOrder());
00108 }
00109
00114 void LineDiagram::setType( const LineType type )
00115 {
00116 if ( d->implementor->type() == type ) return;
00117 if ( type != LineDiagram::Normal && datasetDimension() > 1 ) {
00118 Q_ASSERT_X ( false, "setType()",
00119 "This line chart type can't be used with multi-dimensional data." );
00120 return;
00121 }
00122 switch( type ) {
00123 case Normal:
00124 d->implementor = d->normalDiagram;
00125 break;
00126 case Stacked:
00127 d->implementor = d->stackedDiagram;
00128 break;
00129 case Percent:
00130 d->implementor = d->percentDiagram;
00131 break;
00132 default:
00133 Q_ASSERT_X( false, "LineDiagram::setType", "unknown diagram subtype" );
00134 };
00135
00136
00137 Q_ASSERT( d->implementor->type() == type );
00138
00139
00140 setPercentMode( type == LineDiagram::Percent );
00141 setDataBoundariesDirty();
00142 emit layoutChanged( this );
00143 emit propertiesChanged();
00144 }
00145
00149 LineDiagram::LineType LineDiagram::type() const
00150 {
00151 return d->implementor->type();
00152 }
00153
00154 void LineDiagram::setCenterDataPoints( bool center )
00155 {
00156 d->centerDataPoints = center;
00157 emit propertiesChanged();
00158 }
00159
00160 bool LineDiagram::centerDataPoints() const
00161 {
00162 return d->centerDataPoints;
00163 }
00164
00165 void LineDiagram::setReverseDatasetOrder( bool reverse )
00166 {
00167 d->reverseDatasetOrder = reverse;
00168 }
00169
00170 bool LineDiagram::reverseDatasetOrder() const
00171 {
00172 return d->reverseDatasetOrder;
00173 }
00174
00178 void LineDiagram::setLineAttributes( const LineAttributes& la )
00179 {
00180 d->attributesModel->setModelData(
00181 qVariantFromValue( la ),
00182 LineAttributesRole );
00183 emit propertiesChanged();
00184 }
00185
00189 void LineDiagram::setLineAttributes(
00190 int column,
00191 const LineAttributes& la )
00192 {
00193 d->setDatasetAttrs( column, qVariantFromValue( la ), LineAttributesRole );
00194 emit propertiesChanged();
00195 }
00196
00200 void LineDiagram::resetLineAttributes( int column )
00201 {
00202 d->resetDatasetAttrs( column, LineAttributesRole );
00203 emit propertiesChanged();
00204 }
00205
00209 void LineDiagram::setLineAttributes(
00210 const QModelIndex& index,
00211 const LineAttributes& la )
00212 {
00213 d->attributesModel->setData(
00214 d->attributesModel->mapFromSource(index),
00215 qVariantFromValue( la ),
00216 LineAttributesRole );
00217 emit propertiesChanged();
00218 }
00219
00223 void LineDiagram::resetLineAttributes( const QModelIndex & index )
00224 {
00225 d->attributesModel->resetData(
00226 d->attributesModel->mapFromSource(index), LineAttributesRole );
00227 emit propertiesChanged();
00228 }
00229
00233 LineAttributes LineDiagram::lineAttributes() const
00234 {
00235 return qVariantValue<LineAttributes>(
00236 d->attributesModel->data( KDChart::LineAttributesRole ) );
00237 }
00238
00242 LineAttributes LineDiagram::lineAttributes( int column ) const
00243 {
00244 const QVariant attrs( d->datasetAttrs( column, LineAttributesRole ) );
00245 if( attrs.isValid() )
00246 return qVariantValue< LineAttributes >( attrs );
00247 return lineAttributes();
00248 }
00249
00253 LineAttributes LineDiagram::lineAttributes(
00254 const QModelIndex& index ) const
00255 {
00256 return qVariantValue<LineAttributes>(
00257 d->attributesModel->data(
00258 d->attributesModel->mapFromSource(index),
00259 KDChart::LineAttributesRole ) );
00260 }
00261
00265 void LineDiagram::setThreeDLineAttributes(
00266 const ThreeDLineAttributes& la )
00267 {
00268 setDataBoundariesDirty();
00269 d->attributesModel->setModelData(
00270 qVariantFromValue( la ),
00271 ThreeDLineAttributesRole );
00272 emit propertiesChanged();
00273 }
00274
00278 void LineDiagram::setThreeDLineAttributes(
00279 int column,
00280 const ThreeDLineAttributes& la )
00281 {
00282 setDataBoundariesDirty();
00283 d->setDatasetAttrs( column, qVariantFromValue( la ), ThreeDLineAttributesRole );
00284 emit propertiesChanged();
00285 }
00286
00290 void LineDiagram::setThreeDLineAttributes(
00291 const QModelIndex & index,
00292 const ThreeDLineAttributes& la )
00293 {
00294 setDataBoundariesDirty();
00295 d->attributesModel->setData(
00296 d->attributesModel->mapFromSource(index),
00297 qVariantFromValue( la ),
00298 ThreeDLineAttributesRole );
00299 emit propertiesChanged();
00300 }
00301
00305 ThreeDLineAttributes LineDiagram::threeDLineAttributes() const
00306 {
00307 return qVariantValue<ThreeDLineAttributes>(
00308 d->attributesModel->data( KDChart::ThreeDLineAttributesRole ) );
00309 }
00310
00314 ThreeDLineAttributes LineDiagram::threeDLineAttributes( int column ) const
00315 {
00316 const QVariant attrs( d->datasetAttrs( column, ThreeDLineAttributesRole ) );
00317 if( attrs.isValid() )
00318 return qVariantValue< ThreeDLineAttributes >( attrs );
00319 return threeDLineAttributes();
00320 }
00321
00325 ThreeDLineAttributes LineDiagram::threeDLineAttributes( const QModelIndex& index ) const
00326 {
00327 return qVariantValue<ThreeDLineAttributes>(
00328 d->attributesModel->data(
00329 d->attributesModel->mapFromSource( index ),
00330 KDChart::ThreeDLineAttributesRole ) );
00331 }
00332
00333 double LineDiagram::threeDItemDepth( const QModelIndex& index ) const
00334 {
00335 return threeDLineAttributes( index ).validDepth();
00336 }
00337
00338 double LineDiagram::threeDItemDepth( int column ) const
00339 {
00340 return qVariantValue<ThreeDLineAttributes>(
00341 d->datasetAttrs( column, KDChart::ThreeDLineAttributesRole ) ).validDepth();
00342 }
00343
00347 void LineDiagram::setValueTrackerAttributes( const QModelIndex & index,
00348 const ValueTrackerAttributes & va )
00349 {
00350 d->attributesModel->setData( d->attributesModel->mapFromSource(index),
00351 qVariantFromValue( va ),
00352 KDChart::ValueTrackerAttributesRole );
00353 emit propertiesChanged();
00354 }
00355
00359 ValueTrackerAttributes LineDiagram::valueTrackerAttributes(
00360 const QModelIndex & index ) const
00361 {
00362 return qVariantValue<ValueTrackerAttributes>( d->attributesModel->data(
00363 d->attributesModel->mapFromSource( index ),
00364 KDChart::ValueTrackerAttributesRole ) );
00365 }
00366
00367 void LineDiagram::resizeEvent ( QResizeEvent* )
00368 {
00369 }
00370
00371 const QPair<QPointF, QPointF> LineDiagram::calculateDataBoundaries() const
00372 {
00373 if ( !checkInvariants( true ) ) return QPair<QPointF, QPointF>( QPointF( 0, 0 ), QPointF( 0, 0 ) );
00374
00375
00376
00377
00378
00379
00380 return d->implementor->calculateDataBoundaries();
00381 }
00382
00383
00384 void LineDiagram::paintEvent ( QPaintEvent*)
00385 {
00386
00387 QPainter painter ( viewport() );
00388 PaintContext ctx;
00389 ctx.setPainter ( &painter );
00390 ctx.setRectangle ( QRectF ( 0, 0, width(), height() ) );
00391 paint ( &ctx );
00392
00393 }
00394
00395
00396 double LineDiagram::valueForCellTesting( int row, int column,
00397 bool& bOK,
00398 bool showHiddenCellsAsInvalid ) const
00399 {
00400 double value;
00401 if( showHiddenCellsAsInvalid && isHidden( model()->index( row, column, rootIndex() ) ) )
00402 bOK = false;
00403 else
00404 value = d->attributesModel->data(
00405 d->attributesModel->index( row, column, attributesModelRootIndex() )
00406 ).toDouble( &bOK );
00407 return bOK ? value : 0.0;
00408 }
00409
00410 LineAttributes::MissingValuesPolicy LineDiagram::getCellValues(
00411 int row, int column,
00412 bool shiftCountedXValuesByHalfSection,
00413 double& valueX, double& valueY ) const
00414 {
00415 LineAttributes::MissingValuesPolicy policy;
00416
00417 bool bOK = true;
00418 valueX = ( datasetDimension() > 1 && column > 0 )
00419 ? valueForCellTesting( row, column-1, bOK, true )
00420 : ((shiftCountedXValuesByHalfSection ? 0.5 : 0.0) + row);
00421 if( bOK )
00422 valueY = valueForCellTesting( row, column, bOK, true );
00423 if( bOK ){
00424 policy = LineAttributes::MissingValuesPolicyIgnored;
00425 }else{
00426
00427 QModelIndex index = model()->index( row, column, rootIndex() );
00428 LineAttributes la = lineAttributes( index );
00429 policy = la.missingValuesPolicy();
00430 }
00431 return policy;
00432 }
00433
00434 void LineDiagram::paint( PaintContext* ctx )
00435 {
00436
00437
00438 if ( !checkInvariants( true ) ) return;
00439 if ( !AbstractGrid::isBoundariesValid(dataBoundaries()) ) return;
00440 const PainterSaver p( ctx->painter() );
00441 if( model()->rowCount( rootIndex() ) == 0 || model()->columnCount( rootIndex() ) == 0 )
00442 return;
00443
00444 AbstractCoordinatePlane* const plane = ctx->coordinatePlane();
00445 ctx->setCoordinatePlane( plane->sharedAxisMasterPlane( ctx->painter() ) );
00446
00447
00448
00449 d->implementor->paint( ctx );
00450
00451 ctx->setCoordinatePlane( plane );
00452 }
00453
00454 void LineDiagram::resize ( const QSizeF& size )
00455 {
00456 d->compressor.setResolution( static_cast<int>( size.width() * coordinatePlane()->zoomFactorX() ),
00457 static_cast<int>( size.height() * coordinatePlane()->zoomFactorY() ) );
00458 setDataBoundariesDirty();
00459 }
00460
00461 #if QT_VERSION < 0x040400 || defined(Q_COMPILER_MANGLES_RETURN_TYPE)
00462 const
00463 #endif
00464 int LineDiagram::numberOfAbscissaSegments () const
00465 {
00466 return d->attributesModel->rowCount(attributesModelRootIndex());
00467 }
00468
00469 #if QT_VERSION < 0x040400 || defined(Q_COMPILER_MANGLES_RETURN_TYPE)
00470 const
00471 #endif
00472 int LineDiagram::numberOfOrdinateSegments () const
00473 {
00474 return d->attributesModel->columnCount(attributesModelRootIndex());
00475 }