00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <QPainter>
00027 #include <QDebug>
00028
00029 #include "KDChartBarDiagram.h"
00030 #include "KDChartBarDiagram_p.h"
00031 #include "KDChartThreeDBarAttributes.h"
00032 #include "KDChartPosition.h"
00033 #include "KDChartAttributesModel.h"
00034 #include "KDChartAbstractGrid.h"
00035
00036 #include <KDABLibFakes>
00037
00038 #include "KDChartBarDiagram_p.h"
00039 #include "KDChartNormalBarDiagram_p.h"
00040 #include "KDChartStackedBarDiagram_p.h"
00041 #include "KDChartPercentBarDiagram_p.h"
00042
00043
00044 using namespace KDChart;
00045
00046 BarDiagram::Private::Private()
00047 {
00048 }
00049
00050 BarDiagram::Private::~Private() {}
00051
00052 #define d d_func()
00053
00054
00055 BarDiagram::BarDiagram( QWidget* parent, CartesianCoordinatePlane* plane ) :
00056 AbstractCartesianDiagram( new Private(), parent, plane )
00057 {
00058 init();
00059 }
00060
00061 void BarDiagram::init()
00062 {
00063 d->diagram = this;
00064 d->normalDiagram = new NormalBarDiagram( this );
00065 d->stackedDiagram = new StackedBarDiagram( this );
00066 d->percentDiagram = new PercentBarDiagram( this );
00067 d->implementor = d->normalDiagram;
00068 d->compressor.setModel( attributesModel() );
00069 }
00070
00071 BarDiagram::~BarDiagram()
00072 {
00073 }
00074
00078 BarDiagram * BarDiagram::clone() const
00079 {
00080
00081 BarDiagram* newDiagram = new BarDiagram( new Private( *d ) );
00082 newDiagram->setType( type() );
00083 return newDiagram;
00084 }
00085
00086 bool BarDiagram::compare( const BarDiagram* other )const
00087 {
00088 if( other == this ) return true;
00089 if( ! other ){
00090 return false;
00091 }
00092
00093 return
00094 ( static_cast<const AbstractCartesianDiagram*>(this)->compare( other ) ) &&
00095
00096 (type() == other->type());
00097 }
00098
00103 void BarDiagram::setType( const BarType type )
00104 {
00105
00106 if ( d->implementor->type() == type ) return;
00107
00108 switch( type ) {
00109 case Normal:
00110 d->implementor = d->normalDiagram;
00111 break;
00112 case Stacked:
00113 d->implementor = d->stackedDiagram;
00114 break;
00115 case Percent:
00116 d->implementor = d->percentDiagram;
00117 break;
00118 default:
00119 Q_ASSERT_X( false, "BarDiagram::setType", "unknown diagram subtype" );
00120 };
00121
00122 Q_ASSERT( d->implementor->type() == type );
00123
00124
00125
00126 setPercentMode( type == BarDiagram::Percent );
00127 setDataBoundariesDirty();
00128 emit layoutChanged( this );
00129 emit propertiesChanged();
00130 }
00131
00135 BarDiagram::BarType BarDiagram::type() const
00136 {
00137 return d->implementor->type();
00138 }
00139
00143 void BarDiagram::setBarAttributes( const BarAttributes& ba )
00144 {
00145 d->attributesModel->setModelData( qVariantFromValue( ba ), BarAttributesRole );
00146 emit propertiesChanged();
00147 }
00148
00152 void BarDiagram::setBarAttributes( int column, const BarAttributes& ba )
00153 {
00154 d->attributesModel->setHeaderData(
00155 column, Qt::Vertical,
00156 qVariantFromValue( ba ),
00157 BarAttributesRole );
00158 emit propertiesChanged();
00159 }
00160
00164 void BarDiagram::setBarAttributes( const QModelIndex& index, const BarAttributes& ba )
00165 {
00166 attributesModel()->setData(
00167 d->attributesModel->mapFromSource( index ),
00168 qVariantFromValue( ba ),
00169 BarAttributesRole );
00170 emit propertiesChanged();
00171 }
00172
00176 BarAttributes BarDiagram::barAttributes() const
00177 {
00178 return qVariantValue<BarAttributes>(
00179 d->attributesModel->data( KDChart::BarAttributesRole ) );
00180 }
00181
00185 BarAttributes BarDiagram::barAttributes( int column ) const
00186 {
00187 return qVariantValue<BarAttributes>(
00188 d->attributesModel->data(
00189 d->attributesModel->mapFromSource( columnToIndex( column ) ),
00190 KDChart::BarAttributesRole ) );
00191 }
00192
00196 BarAttributes BarDiagram::barAttributes( const QModelIndex& index ) const
00197 {
00198 return qVariantValue<BarAttributes>(
00199 d->attributesModel->data(
00200 d->attributesModel->mapFromSource( index ),
00201 KDChart::BarAttributesRole ) );
00202 }
00203
00207 void BarDiagram::setThreeDBarAttributes( const ThreeDBarAttributes& threeDAttrs )
00208 {
00209 setDataBoundariesDirty();
00210 d->attributesModel->setModelData( qVariantFromValue( threeDAttrs ), ThreeDBarAttributesRole );
00211
00212 emit propertiesChanged();
00213 }
00214
00218 void BarDiagram::setThreeDBarAttributes( int column, const ThreeDBarAttributes& threeDAttrs )
00219 {
00220 setDataBoundariesDirty();
00221 d->attributesModel->setHeaderData(
00222 column, Qt::Vertical,
00223 qVariantFromValue( threeDAttrs ),
00224 ThreeDBarAttributesRole );
00225
00226 emit propertiesChanged();
00227
00228 }
00229
00233 void BarDiagram::setThreeDBarAttributes( const QModelIndex& index, const ThreeDBarAttributes& threeDAttrs )
00234 {
00235 setDataBoundariesDirty();
00236 d->attributesModel->setData(
00237 d->attributesModel->mapFromSource(index),
00238 qVariantFromValue( threeDAttrs ),
00239 ThreeDBarAttributesRole );
00240
00241 emit propertiesChanged();
00242 }
00243
00247 ThreeDBarAttributes BarDiagram::threeDBarAttributes() const
00248 {
00249 return qVariantValue<ThreeDBarAttributes>(
00250 d->attributesModel->data( KDChart::ThreeDBarAttributesRole ) );
00251 }
00252
00256 ThreeDBarAttributes BarDiagram::threeDBarAttributes( int column ) const
00257 {
00258 return qVariantValue<ThreeDBarAttributes>(
00259 d->attributesModel->data(
00260 d->attributesModel->mapFromSource( columnToIndex( column ) ),
00261 KDChart::ThreeDBarAttributesRole ) );
00262 }
00263
00267 ThreeDBarAttributes BarDiagram::threeDBarAttributes( const QModelIndex& index ) const
00268 {
00269 return qVariantValue<ThreeDBarAttributes>(
00270 d->attributesModel->data(
00271 d->attributesModel->mapFromSource(index),
00272 KDChart::ThreeDBarAttributesRole ) );
00273 }
00274
00275 double BarDiagram::threeDItemDepth( const QModelIndex& index ) const
00276 {
00277 return threeDBarAttributes( index ).validDepth();
00278 }
00279
00280 double BarDiagram::threeDItemDepth( int column ) const
00281 {
00282 return qVariantValue<ThreeDBarAttributes>(
00283 d->attributesModel->headerData (
00284 column,
00285 Qt::Vertical,
00286 KDChart::ThreeDBarAttributesRole ) ).validDepth();
00287 }
00288
00289 void BarDiagram::resizeEvent ( QResizeEvent*)
00290 {
00291
00292 }
00293
00294 const QPair<QPointF, QPointF> BarDiagram::calculateDataBoundaries() const
00295 {
00296 if ( !checkInvariants(true) ) return QPair<QPointF, QPointF>( QPointF( 0, 0 ), QPointF( 0, 0 ) );
00297
00298
00299
00300
00301
00302 return d->implementor->calculateDataBoundaries();
00303 }
00304
00305 void BarDiagram::paintEvent ( QPaintEvent*)
00306 {
00307 QPainter painter ( viewport() );
00308 PaintContext ctx;
00309 ctx.setPainter ( &painter );
00310 ctx.setRectangle( QRectF ( 0, 0, width(), height() ) );
00311 paint ( &ctx );
00312 }
00313
00314 void BarDiagram::paint( PaintContext* ctx )
00315 {
00316 if ( !checkInvariants( true ) ) return;
00317 if ( !AbstractGrid::isBoundariesValid(dataBoundaries()) ) return;
00318 const PainterSaver p( ctx->painter() );
00319 if( model()->rowCount() == 0 || model()->columnCount() == 0 )
00320 return;
00321
00322 AbstractCoordinatePlane* const plane = ctx->coordinatePlane();
00323 ctx->setCoordinatePlane( plane->sharedAxisMasterPlane( ctx->painter() ) );
00324
00325
00326 d->implementor->paint( ctx );
00327
00328 ctx->setCoordinatePlane( plane );
00329 }
00330
00331 void BarDiagram::resize( const QSizeF& size )
00332 {
00333 d->compressor.setResolution( static_cast< int >( size.width() ),
00334 static_cast< int >( size.height() ) );
00335 setDataBoundariesDirty();
00336 }
00337
00338 const int BarDiagram::numberOfAbscissaSegments () const
00339 {
00340 return d->attributesModel->rowCount(attributesModelRootIndex());
00341 }
00342
00343 const int BarDiagram::numberOfOrdinateSegments () const
00344 {
00345 return d->attributesModel->columnCount(attributesModelRootIndex());
00346 }
00347
00348