# KDChartRingDiagram.cpp

Go to the documentation of this file.

    00001 /****************************************************************************
    00002  ** Copyright (C) 2007 Klarälvdalens 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  ** Foundation 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 <QPainter>
    00027
    00028 #include "KDChartAttributesModel.h"
    00029 #include "KDChartPaintContext.h"
    00030 #include "KDChartRingDiagram.h"
    00031 #include "KDChartRingDiagram_p.h"
    00032 #include "KDChartPainterSaver_p.h"
    00033 #include "KDChartPieAttributes.h"
    00034 #include "KDChartThreeDPieAttributes.h"
    00035 #include "KDChartDataValueAttributes.h"
    00036
    00037 #include <KDABLibFakes>
    00038
    00039 using namespace KDChart;
    00040
    00041 RingDiagram::Private::Private() :
    00042     relativeThickness( false )
    00043 {
    00044 }
    00045
    00046 RingDiagram::Private::~Private() {}
    00047
    00048 #define d d_func()
    00049
    00050 RingDiagram::RingDiagram( QWidget* parent, PolarCoordinatePlane* plane ) :
    00051     AbstractPieDiagram( new Private(), parent, plane )
    00052 {
    00053     init();
    00054 }
    00055
    00056 RingDiagram::~RingDiagram()
    00057 {
    00058 }
    00059
    00060 void RingDiagram::init()
    00061 {
    00062 }
    00063
    00067 RingDiagram * RingDiagram::clone() const
    00068 {
    00069     return new RingDiagram( new Private( *d ) );
    00070 }
    00071
    00072 void RingDiagram::setRelativeThickness( bool relativeThickness )
    00073 {
    00074     d->relativeThickness = relativeThickness;
    00075 }
    00076
    00077 bool RingDiagram::relativeThickness() const
    00078 {
    00079     return d->relativeThickness;
    00080 }
    00081
    00082 const QPair<QPointF, QPointF> RingDiagram::calculateDataBoundaries () const
    00083 {
    00084     if ( !checkInvariants(true) ) return QPair<QPointF, QPointF>( QPointF( 0, 0 ), QPointF( 0, 0 ) );
    00085
    00086     QPointF bottomLeft ( QPointF( 0, 0 ) );
    00087     QPointF topRight ( QPointF( 1, 1 ) );
    00088     return QPair<QPointF, QPointF> ( bottomLeft,  topRight );
    00089 }
    00090
    00091 void RingDiagram::paintEvent( QPaintEvent* )
    00092 {
    00093     QPainter painter ( viewport() );
    00094     PaintContext ctx;
    00095     ctx.setPainter ( &painter );
    00096     ctx.setRectangle( QRectF ( 0, 0, width(), height() ) );
    00097     paint ( &ctx );
    00098 }
    00099
    00100 void RingDiagram::resizeEvent ( QResizeEvent*)
    00101 {
    00102 }
    00103
    00104 void RingDiagram::paint( PaintContext* ctx )
    00105 {
    00106     // note: Not having any data model assigned is no bug
    00107     //       but we can not draw a diagram then either.
    00108     if ( !checkInvariants(true) )
    00109         return;
    00110
    00111     const int colCount = model()->columnCount(rootIndex());
    00112     DataValueTextInfoList list;
    00113     for ( int j=0; j<colCount; ++j ) {
    00114         QBrush brush = qVariantValue<QBrush>( attributesModel()->headerData( j, Qt::Vertical, KDChart::DatasetBrushRole ) );
    00115         PainterSaver painterSaver( ctx->painter() );
    00116         ctx->painter()->setRenderHint ( QPainter::Antialiasing );
    00117         ctx->painter()->setBrush( brush );
    00118         QPen p( ctx->painter()->pen() );
    00119         p.setColor( brush.color() );
    00120         p.setWidth( 2 );// FIXME properties, use DatasetPenRole
    00121         ctx->painter()->setPen( p );
    00122         //ctx->painter()->drawPolyline( polygon );
    00123     }
    00124     DataValueTextInfoListIterator it( list );
    00125     while ( it.hasNext() ) {
    00126         const DataValueTextInfo& info = it.next();
    00127         paintDataValueText( ctx->painter(), info.index, info.pos, info.value );
    00128     }
    00129 }
    00130
    00131 void RingDiagram::resize ( const QSizeF& )
    00132 {
    00133 }
    00134
    00135 /*virtual*/
    00136 double RingDiagram::valueTotals () const
    00137 {
    00138     double total = 0;
    00139     const int colCount = model()->columnCount(rootIndex());
    00140     for ( int j=0; j<colCount; ++j ) {
    00141       total += model()->data( model()->index( 0, j, rootIndex() ) ).toDouble();
    00142     }
    00143     return total;
    00144 }
    00145
    00146 /*virtual*/
    00147 double RingDiagram::numberOfValuesPerDataset() const
    00148 {
    00149     return model() ? model()->columnCount(rootIndex()) : 0.0;
    00150 }
    00151
    00152 /*virtual*/
    00153 double RingDiagram::numberOfGridRings() const
    00154 {
    00155     return 1;
    00156 }
    00157