KD Chart 2: KDChartNormalLineDiagram_p.cpp Source File

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- File List
- File Members

# KDChartNormalLineDiagram_p.cpp

Go to the documentation of this file.

    00001 /* -*- Mode: C++ -*-
    00002    KDChart - a multi-platform charting engine
    00003    */
    00004
    00005 /****************************************************************************
    00006  ** Copyright (C) 2005-2007 Klarälvdalens Datakonsult AB.  All rights reserved.
    00007  **
    00008  ** This file is part of the KD Chart library.
    00009  **
    00010  ** This file may be distributed and/or modified under the terms of the
    00011  ** GNU General Public License version 2 as published by the Free Software
    00012  ** Foundation and appearing in the file LICENSE.GPL included in the
    00013  ** packaging of this file.
    00014  **
    00015  ** Licensees holding valid commercial KD Chart licenses may use this file in
    00016  ** accordance with the KD Chart Commercial License Agreement provided with
    00017  ** the Software.
    00018  **
    00019  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    00020  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    00021  **
    00022  ** See http://www.kdab.net/kdchart for
    00023  **   information about KD Chart Commercial License Agreements.
    00024  **
    00025  ** Contact info@kdab.net if any conditions of this
    00026  ** licensing are not clear to you.
    00027  **
    00028  **********************************************************************/
    00029
    00030 #include <QAbstractItemModel>
    00031
    00032 #include "KDChartBarDiagram.h"
    00033 #include "KDChartLineDiagram.h"
    00034 #include "KDChartTextAttributes.h"
    00035 #include "KDChartAttributesModel.h"
    00036 #include "KDChartAbstractCartesianDiagram.h"
    00037 #include "KDChartNormalLineDiagram_p.h"
    00038
    00039 using namespace KDChart;
    00040
    00041 NormalLineDiagram::NormalLineDiagram( LineDiagram* d )
    00042     : LineDiagramType( d )
    00043 {
    00044 }
    00045
    00046 LineDiagram::LineType NormalLineDiagram::type() const
    00047 {
    00048     return LineDiagram::Normal;
    00049 }
    00050
    00051 const QPair< QPointF, QPointF > NormalLineDiagram::calculateDataBoundaries() const
    00052 {
    00053     const int rowCount = compressor().modelDataRows();
    00054     const int colCount = compressor().modelDataColumns();
    00055     double xMin = 0;
    00056     double xMax = diagram()->model() ? diagram()->model()->rowCount( diagram()->rootIndex() ) - 1 : 0;
    00057     double yMin = 0;
    00058     double yMax = 0;
    00059
    00060     bool first = true;
    00061     for( int column = 0; column < colCount; ++column )
    00062     {
    00063         for ( int row = 0; row < rowCount; ++row )
    00064         {
    00065             const CartesianDiagramDataCompressor::CachePosition position( row, column );
    00066             const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
    00067
    00068             if ( first ) {
    00069                     yMin = point.value;
    00070                     yMax = point.value;
    00071             } else {
    00072                 yMin = qMin( yMin, point.value );
    00073                 yMax = qMax( yMax, point.value );
    00074             }
    00075
    00076             first = false;
    00077         }
    00078     }
    00079
    00080     const QPointF bottomLeft( QPointF( xMin, qMin( 0.0, yMin ) ) );
    00081     const QPointF topRight( QPointF( xMax, yMax ) );
    00082     return QPair< QPointF, QPointF >( bottomLeft, topRight );
    00083 }
    00084
    00085 void NormalLineDiagram::paint( PaintContext* ctx )
    00086 {
    00087     reverseMapper().clear();
    00088     Q_ASSERT( dynamic_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() ) );
    00089     CartesianCoordinatePlane* plane = static_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() );
    00090     const int columnCount = compressor().modelDataColumns();
    00091     const int rowCount = compressor().modelDataRows();
    00092     if ( columnCount == 0 || rowCount == 0 ) return; // maybe blank out the area?
    00093
    00094 // FIXME integrate column index retrieval to compressor:
    00095 // the compressor should only pass through visiblel columns
    00096     int maxFound = 0;
    00097 //     {   // find the last column number that is not hidden
    00098 //         for( int column =  datasetDimension() - 1;
    00099 //              column <  columnCount;
    00100 //              column += datasetDimension() )
    00101 //             if( ! diagram()->isHidden( column ) )
    00102 //                 maxFound = column;
    00103 //     }
    00104     maxFound = columnCount;
    00105     // ^^^ temp
    00106
    00107     DataValueTextInfoList textInfoList;
    00108     LineAttributesInfoList lineList;
    00109     LineAttributes::MissingValuesPolicy policy; // ???
    00110
    00111     for( int column  = 0; column < columnCount; ++column ) {
    00112         LineAttributes laPreviousCell;
    00113         CartesianDiagramDataCompressor::CachePosition previousCellPosition;
    00114
    00115         for ( int row = 0; row < rowCount; ++row ) {
    00116             const CartesianDiagramDataCompressor::CachePosition position( row, column );
    00117             // get where to draw the line from:
    00118             const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
    00119             LineAttributes laCell;
    00120             if ( row > 0 ) { // position 0 is not really painted, since it takes two points to make a line :-)
    00121                 const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
    00122                 const CartesianDiagramDataCompressor::DataPoint lastPoint = compressor().data( previousCellPosition );
    00123                 // area corners, a + b are the line ends:
    00124                 const QPointF a( plane->translate( QPointF( lastPoint.key, lastPoint.value ) ) );
    00125                 const QPointF b( plane->translate( QPointF( point.key, point.value ) ) );
    00126                 const QPointF c( plane->translate( QPointF( lastPoint.key, 0.0 ) ) );
    00127                 const QPointF d( plane->translate( QPointF( point.key, 0.0 ) ) );
    00128                 // add the line to the list:
    00129                 laCell = diagram()->lineAttributes( sourceIndex );
    00130                 // add data point labels:
    00131                 const PositionPoints pts = PositionPoints( b, a, d, c );
    00132                 // if necessary, add the area to the area list:
    00133                 QList<QPolygonF> areas;
    00134                 if ( laCell.displayArea() ) {
    00135                     QPolygonF polygon;
    00136                     polygon << a << b << d << c;
    00137                     areas << polygon;
    00138                 }
    00139                 // add the pieces to painting if this is not hidden:
    00140                 if ( ! point.hidden ) {
    00141                     appendDataValueTextInfoToList( diagram(), textInfoList, sourceIndex, pts,
    00142                                                    Position::NorthWest, Position::SouthWest,
    00143                                                    point.value );
    00144                     paintAreas( ctx, attributesModel()->mapToSource( lastPoint.index ), areas, laCell.transparency() );
    00145                     lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
    00146                 }
    00147             }
    00148             // wrap it up:
    00149             previousCellPosition = position;
    00150             laPreviousCell = laCell;
    00151         }
    00152     }
    00153
    00154     paintElements( ctx, textInfoList, lineList, policy );
    00155 }