~ubuntu-branches/ubuntu/oneiric/koffice/oneiric-updates

« back to all changes in this revision

Viewing changes to kchart/kdchart/src/Ternary/KDChartTernaryLineDiagram.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C++ -*-
2
 
   KDChart - a multi-platform charting engine
3
 
   */
4
 
 
5
 
/****************************************************************************
6
 
 ** Copyright (C) 2005-2007 Klarälvdalens Datakonsult AB.  All rights reserved.
7
 
 **
8
 
 ** This file is part of the KD Chart library.
9
 
 **
10
 
 ** This file may be used under the terms of the GNU General Public
11
 
 ** License versions 2.0 or 3.0 as published by the Free Software
12
 
 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
13
 
 ** included in the packaging of this file.  Alternatively you may (at
14
 
 ** your option) use any later version of the GNU General Public
15
 
 ** License if such license has been publicly approved by
16
 
 ** Klarälvdalens Datakonsult AB (or its successors, if any).
17
 
 ** 
18
 
 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
19
 
 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
20
 
 ** A PARTICULAR PURPOSE. Klarälvdalens Datakonsult AB reserves all rights
21
 
 ** not expressly granted herein.
22
 
 ** 
23
 
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24
 
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25
 
 **
26
 
 **********************************************************************/
27
 
 
28
 
#include "KDChartTernaryLineDiagram.h"
29
 
#include "KDChartTernaryLineDiagram_p.h"
30
 
 
31
 
#include <limits>
32
 
 
33
 
#include <QPainter>
34
 
 
35
 
#include <KDChartPaintContext>
36
 
 
37
 
#include "KDChartLineAttributes.h"
38
 
#include "KDChartDataValueAttributes.h"
39
 
#include "KDChartMarkerAttributes.h"
40
 
#include "TernaryPoint.h"
41
 
#include "TernaryConstants.h"
42
 
#include "KDChartPainterSaver_p.h"
43
 
 
44
 
using namespace KDChart;
45
 
 
46
 
#define d d_func()
47
 
 
48
 
TernaryLineDiagram::Private::Private()
49
 
    : AbstractTernaryDiagram::Private()
50
 
{
51
 
}
52
 
 
53
 
TernaryLineDiagram::TernaryLineDiagram ( QWidget* parent,
54
 
                                         TernaryCoordinatePlane* plane )
55
 
    : AbstractTernaryDiagram( new Private(), parent, plane )
56
 
{
57
 
    init();
58
 
    setDatasetDimensionInternal( 3 ); // the third column is implicit
59
 
 
60
 
    DataValueAttributes dataValueAttributes;
61
 
    dataValueAttributes.setVisible( true );
62
 
    MarkerAttributes markerAttributes;
63
 
    markerAttributes.setMarkerStyle( MarkerAttributes::MarkerCircle );
64
 
    markerAttributes.setVisible( true );
65
 
    dataValueAttributes.setMarkerAttributes( markerAttributes );
66
 
    attributesModel()->setDefaultForRole(
67
 
        KDChart::DataValueLabelAttributesRole,
68
 
        qVariantFromValue( dataValueAttributes ) );
69
 
}
70
 
 
71
 
TernaryLineDiagram::~TernaryLineDiagram()
72
 
{
73
 
}
74
 
 
75
 
void TernaryLineDiagram::init()
76
 
{
77
 
}
78
 
 
79
 
void  TernaryLineDiagram::resize (const QSizeF& area)
80
 
{
81
 
    Q_UNUSED( area );
82
 
}
83
 
 
84
 
void  TernaryLineDiagram::paint (PaintContext *paintContext)
85
 
{
86
 
    d->reverseMapper.clear();
87
 
 
88
 
    d->paint( paintContext );
89
 
    // sanity checks:
90
 
    if ( model() == 0 ) return;
91
 
 
92
 
    QPainter* p = paintContext->painter();
93
 
    PainterSaver s( p );
94
 
 
95
 
    TernaryCoordinatePlane* plane =
96
 
        (TernaryCoordinatePlane*) paintContext->coordinatePlane();
97
 
    Q_ASSERT( plane );
98
 
 
99
 
    double x, y, z;
100
 
 
101
 
 
102
 
    // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
103
 
    const DataValueAttributes attrs( dataValueAttributes() );
104
 
 
105
 
 
106
 
    d->clearListOfAlreadyDrawnDataValueTexts();
107
 
 
108
 
    int columnCount = model()->columnCount( rootIndex() );
109
 
    QPointF start;
110
 
    for(int column=0; column<columnCount; column+=datasetDimension() )
111
 
    {
112
 
        int numrows = model()->rowCount( rootIndex() );
113
 
        for( int row = 0; row < numrows; row++ )
114
 
        {
115
 
            // see if there is data otherwise skip
116
 
            QModelIndex base = model()->index( row, column );
117
 
            if( ! model()->data( base ).isNull() )
118
 
            {
119
 
                p->setPen( PrintingParameters::scalePen( pen( base ) ) );
120
 
                p->setBrush( brush( base ) );
121
 
 
122
 
                // retrieve data
123
 
                x = qMax( model()->data( model()->index( row, column, rootIndex() ) ).toDouble(),
124
 
                          0.0 );
125
 
                y = qMax( model()->data( model()->index( row, column+1, rootIndex() ) ).toDouble(),
126
 
                          0.0 );
127
 
                z = qMax( model()->data( model()->index( row, column+2, rootIndex() ) ).toDouble(),
128
 
                          0.0 );
129
 
 
130
 
                double total = x + y + z;
131
 
                if ( fabs( total ) > 3 * std::numeric_limits<double>::epsilon() ) {
132
 
                    TernaryPoint tPunkt( x / total, y / total );
133
 
                    QPointF diagramLocation = translate( tPunkt );
134
 
                    QPointF widgetLocation = plane->translate( diagramLocation );
135
 
 
136
 
                    if ( row > 0 ) {
137
 
                        p->drawLine( start, widgetLocation );
138
 
                    }
139
 
                    paintMarker( p, model()->index( row, column, rootIndex() ), widgetLocation );
140
 
                    start = widgetLocation;
141
 
                    // retrieve text and data value attributes
142
 
                    // FIXME use data model DisplayRole text
143
 
                    QString text = tr( "(%1, %2, %3)" )
144
 
                                   .arg( x * 100, 0, 'f', 0 )
145
 
                                   .arg( y * 100, 0, 'f', 0 )
146
 
                                   .arg( z * 100, 0, 'f', 0 );
147
 
                    d->paintDataValueText( this, p, attrs, widgetLocation, text, true );
148
 
                } else {
149
 
                    // ignore and do not paint this point, garbage data
150
 
                    qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
151
 
                             << x << "/" << y << "/" << z << "ignored, unusable.";
152
 
                }
153
 
            }
154
 
        }
155
 
    }
156
 
}
157
 
 
158
 
const QPair< QPointF, QPointF >  TernaryLineDiagram::calculateDataBoundaries () const
159
 
{
160
 
    // this is a constant, because we defined it to be one:
161
 
    static QPair<QPointF, QPointF> Boundaries(
162
 
        TriangleBottomLeft,
163
 
        QPointF( TriangleBottomRight.x(), TriangleHeight ) );
164
 
    return Boundaries;
165
 
}