KD Chart 2  [rev.2.5]
KDChartTernaryLineDiagram.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2012 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
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 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #include "KDChartTernaryLineDiagram.h"
00024 #include "KDChartTernaryLineDiagram_p.h"
00025 
00026 #include <limits>
00027 
00028 #include <QPainter>
00029 
00030 #include <KDChartPaintContext.h>
00031 
00032 #include "KDChartLineAttributes.h"
00033 #include "KDChartDataValueAttributes.h"
00034 #include "KDChartMarkerAttributes.h"
00035 #include "TernaryPoint.h"
00036 #include "TernaryConstants.h"
00037 #include "KDChartPainterSaver_p.h"
00038 
00039 using namespace KDChart;
00040 
00041 #define d d_func()
00042 
00043 TernaryLineDiagram::Private::Private()
00044     : AbstractTernaryDiagram::Private()
00045 {
00046 }
00047 
00048 TernaryLineDiagram::TernaryLineDiagram ( QWidget* parent,
00049                                          TernaryCoordinatePlane* plane )
00050     : AbstractTernaryDiagram( new Private(), parent, plane )
00051 {
00052     init();
00053     setDatasetDimensionInternal( 3 ); // the third column is implicit
00054 
00055     DataValueAttributes dataValueAttributes;
00056     dataValueAttributes.setVisible( true );
00057     MarkerAttributes markerAttributes;
00058     markerAttributes.setMarkerStyle( MarkerAttributes::MarkerCircle );
00059     markerAttributes.setVisible( true );
00060     dataValueAttributes.setMarkerAttributes( markerAttributes );
00061     attributesModel()->setDefaultForRole(
00062         KDChart::DataValueLabelAttributesRole,
00063         qVariantFromValue( dataValueAttributes ) );
00064 }
00065 
00066 TernaryLineDiagram::~TernaryLineDiagram()
00067 {
00068 }
00069 
00070 void TernaryLineDiagram::init()
00071 {
00072 }
00073 
00074 void  TernaryLineDiagram::resize (const QSizeF& area)
00075 {
00076     Q_UNUSED( area );
00077 }
00078 
00079 void  TernaryLineDiagram::paint (PaintContext *paintContext)
00080 {
00081     d->reverseMapper.clear();
00082 
00083     d->paint( paintContext );
00084     // sanity checks:
00085     if ( model() == 0 ) return;
00086 
00087     QPainter* p = paintContext->painter();
00088     PainterSaver s( p );
00089 
00090     TernaryCoordinatePlane* plane =
00091         (TernaryCoordinatePlane*) paintContext->coordinatePlane();
00092     Q_ASSERT( plane );
00093 
00094     qreal x, y, z;
00095 
00096     // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
00097     const DataValueAttributes attrs( dataValueAttributes() );
00098 
00099     d->forgetAlreadyPaintedDataValues();
00100 
00101     int columnCount = model()->columnCount( rootIndex() );
00102     QPointF start;
00103     for(int column=0; column<columnCount; column+=datasetDimension() )
00104     {
00105         int numrows = model()->rowCount( rootIndex() );
00106         for( int row = 0; row < numrows; row++ )
00107         {
00108             // see if there is data otherwise skip
00109             QModelIndex base = model()->index( row, column ); // checked
00110             if( ! model()->data( base ).isNull() )
00111             {
00112                 p->setPen( PrintingParameters::scalePen( pen( base ) ) );
00113                 p->setBrush( brush( base ) );
00114 
00115                 // retrieve data
00116                 x = qMax( model()->data( model()->index( row, column, rootIndex() ) ).toReal(), // checked
00117                           0.0 );
00118                 y = qMax( model()->data( model()->index( row, column+1, rootIndex() ) ).toReal(), // checked
00119                           0.0 );
00120                 z = qMax( model()->data( model()->index( row, column+2, rootIndex() ) ).toReal(), // checked
00121                           0.0 );
00122 
00123                 qreal total = x + y + z;
00124                 if ( fabs( total ) > 3 * std::numeric_limits<qreal>::epsilon() ) {
00125                     TernaryPoint tPunkt( x / total, y / total );
00126                     QPointF diagramLocation = translate( tPunkt );
00127                     QPointF widgetLocation = plane->translate( diagramLocation );
00128 
00129                     if ( row > 0 ) {
00130                         p->drawLine( start, widgetLocation );
00131                     }
00132                     paintMarker( p, model()->index( row, column, rootIndex() ), widgetLocation ); // checked
00133                     start = widgetLocation;
00134                     // retrieve text and data value attributes
00135                     // FIXME use data model DisplayRole text
00136                     QString text = tr( "(%1, %2, %3)" )
00137                                    .arg( x * 100, 0, 'f', 0 )
00138                                    .arg( y * 100, 0, 'f', 0 )
00139                                    .arg( z * 100, 0, 'f', 0 );
00140                     d->paintDataValueText( p, attrs, widgetLocation, true, text, true );
00141                 } else {
00142                     // ignore and do not paint this point, garbage data
00143                     qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
00144                              << x << "/" << y << "/" << z << "ignored, unusable.";
00145                 }
00146             }
00147         }
00148     }
00149 }
00150 
00151 const QPair< QPointF, QPointF >  TernaryLineDiagram::calculateDataBoundaries () const
00152 {
00153     // this is a constant, because we defined it to be one:
00154     static QPair<QPointF, QPointF> Boundaries(
00155         TriangleBottomLeft,
00156         QPointF( TriangleBottomRight.x(), TriangleHeight ) );
00157     return Boundaries;
00158 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/