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

« back to all changes in this revision

Viewing changes to kchart/kdchart/src/Ternary/KDChartTernaryAxis.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 "KDChartTernaryAxis.h"
29
 
 
30
 
#include <QPainter>
31
 
 
32
 
#include <KDChartChart>
33
 
#include <KDChartPaintContext>
34
 
 
35
 
#include "TernaryConstants.h"
36
 
#include "KDChartTernaryCoordinatePlane.h"
37
 
#include "KDChartAbstractTernaryDiagram.h"
38
 
 
39
 
 
40
 
#include "../src/KDChartLayoutItems.h"
41
 
#include "PrerenderedElements/KDChartTextLabelCache.h"
42
 
 
43
 
using namespace KDChart;
44
 
 
45
 
// m_label and m_fifty do not have to be pointers, once the class is
46
 
// pimpled (PrerenderedLabel is not published API)
47
 
 
48
 
TernaryAxis::TernaryAxis ( AbstractTernaryDiagram* diagram)
49
 
    : AbstractAxis( diagram )
50
 
    , m_position( KDChartEnums::PositionUnknown )
51
 
    , m_label( new PrerenderedLabel )
52
 
    , m_fifty( new PrerenderedLabel )
53
 
{
54
 
    resetTitleTextAttributes();
55
 
    setPosition( KDChartEnums::PositionSouth ); // arbitrary
56
 
    m_fifty->setText( QObject::tr( "50%" ) ); // const
57
 
    // FIXME is this consistent with other diagram/axis/plane implementations?
58
 
    diagram->addAxis( this );
59
 
}
60
 
 
61
 
TernaryAxis::~TernaryAxis()
62
 
{
63
 
    delete m_label; m_label = 0;
64
 
    delete m_label; m_fifty = 0;
65
 
}
66
 
 
67
 
void  TernaryAxis::paintAll (QPainter &)
68
 
{
69
 
    // not used
70
 
}
71
 
 
72
 
void  TernaryAxis::paint (QPainter *)
73
 
{
74
 
    // not used
75
 
}
76
 
 
77
 
void  TernaryAxis::paintCtx (PaintContext * paintContext)
78
 
{
79
 
    QPainter* p = paintContext->painter();
80
 
    TernaryCoordinatePlane* plane =
81
 
        (TernaryCoordinatePlane*) paintContext->coordinatePlane();
82
 
    // QObject* refArea = plane->parent();
83
 
    QRectF drawArea = paintContext->rectangle();
84
 
    QRectF titleArea;
85
 
 
86
 
    // paint the axis label (across the triangle, that one):
87
 
    QList<PrerenderedLabel*> labels;
88
 
    labels << m_label << m_fifty;
89
 
    Q_FOREACH( PrerenderedLabel* label, labels ) {
90
 
        const QPixmap& pixmap = label->pixmap();
91
 
        QPointF point = plane->translate( label->position() )
92
 
                        - label->referencePointLocation();
93
 
        p->drawPixmap( point, pixmap );
94
 
    }
95
 
}
96
 
 
97
 
bool TernaryAxis::isEmpty() const
98
 
{
99
 
    // todo: what's this method for?
100
 
    return false;
101
 
}
102
 
 
103
 
QRect TernaryAxis::geometry () const
104
 
{
105
 
    return m_geometry;
106
 
}
107
 
 
108
 
void TernaryAxis::setGeometry (const QRect &rect)
109
 
{
110
 
    m_geometry = rect;
111
 
}
112
 
 
113
 
QSize  TernaryAxis::minimumSize () const
114
 
{
115
 
    // todo: return realistic sizes
116
 
    return QSize( 100, 100 );
117
 
}
118
 
 
119
 
QSize  TernaryAxis::maximumSize () const
120
 
{
121
 
    return QSize( 300, 200 );
122
 
}
123
 
 
124
 
QSize  TernaryAxis::sizeHint () const
125
 
{
126
 
    return QSize( 150, 100 );
127
 
}
128
 
 
129
 
Qt::Orientations TernaryAxis::expandingDirections () const
130
 
{
131
 
    return Qt::Vertical | Qt::Horizontal;
132
 
}
133
 
 
134
 
const Position TernaryAxis::position () const
135
 
{
136
 
    return m_position;
137
 
}
138
 
 
139
 
void  TernaryAxis::setPosition (Position p)
140
 
{
141
 
    if ( p == position() ) return;
142
 
 
143
 
    if ( p != KDChartEnums::PositionWest
144
 
         && p != KDChartEnums::PositionEast
145
 
         && p != KDChartEnums::PositionSouth )
146
 
    {
147
 
        qDebug() << "TernaryAxis::setPosition: only south, east and west are supported "
148
 
            "positions for ternary axes.";
149
 
        return;
150
 
    }
151
 
 
152
 
    if ( m_title.isEmpty() )
153
 
        switch( p.value() ) {
154
 
        case KDChartEnums::PositionSouth:
155
 
            m_label->setText( tr( "A" ) );
156
 
            break;
157
 
        case KDChartEnums::PositionWest:
158
 
            m_label->setText( tr( "C" ) );
159
 
            break;
160
 
        case KDChartEnums::PositionEast:
161
 
            m_label->setText( tr( "B" ) );
162
 
            break;
163
 
        default:
164
 
            break;
165
 
        }
166
 
 
167
 
    m_position = p;
168
 
    updatePrerenderedLabels(); // position has changed
169
 
}
170
 
 
171
 
void TernaryAxis::setTitleText( const QString& text )
172
 
{
173
 
    m_title = text; // do not remove
174
 
    m_label->setText( text );
175
 
}
176
 
 
177
 
QString TernaryAxis::titleText() const
178
 
{
179
 
    return m_label->text();
180
 
}
181
 
 
182
 
void TernaryAxis::setTitleTextAttributes( const TextAttributes &a )
183
 
{
184
 
    m_titleAttributes = a;
185
 
    updatePrerenderedLabels();
186
 
}
187
 
 
188
 
TextAttributes TernaryAxis::titleTextAttributes() const
189
 
{
190
 
    return m_titleAttributes;
191
 
}
192
 
 
193
 
void TernaryAxis::resetTitleTextAttributes()
194
 
{
195
 
    TextAttributes a;
196
 
    m_titleAttributes = a;
197
 
    updatePrerenderedLabels();
198
 
}
199
 
 
200
 
bool TernaryAxis::hasDefaultTitleTextAttributes() const
201
 
{
202
 
    TextAttributes a;
203
 
    return m_titleAttributes == a;
204
 
}
205
 
 
206
 
void TernaryAxis::updatePrerenderedLabels()
207
 
{
208
 
    TextAttributes attributes = titleTextAttributes();
209
 
    double axisLabelAngle = 0.0;
210
 
    double fiftyMarkAngle = 0.0;
211
 
    QPointF axisLabelPosition;
212
 
    QPointF fiftyMarkPosition(0.0, 0.0);
213
 
    KDChartEnums::PositionValue fiftyMarkReferencePoint = KDChartEnums::PositionUnknown;
214
 
 
215
 
    switch( position().value() ) {
216
 
    case KDChartEnums::PositionSouth:
217
 
        // this is the axis on the other side of A
218
 
        axisLabelAngle = 0.0;
219
 
        fiftyMarkAngle = 0.0;
220
 
        axisLabelPosition = TriangleTop;
221
 
        fiftyMarkPosition = 0.5 * AxisVector_B_C - RelMarkerLength * Norm_B_C;
222
 
        fiftyMarkReferencePoint = KDChartEnums::PositionNorth;
223
 
        break;
224
 
    case KDChartEnums::PositionEast:
225
 
        // this is the axis on the other side of B
226
 
        axisLabelAngle = 240.0;
227
 
        fiftyMarkAngle = 60;
228
 
        axisLabelPosition = TriangleBottomLeft;
229
 
        fiftyMarkPosition = AxisVector_B_C + 0.5 * AxisVector_C_A - RelMarkerLength * Norm_C_A;
230
 
        fiftyMarkReferencePoint = KDChartEnums::PositionSouth;
231
 
        break;
232
 
    case KDChartEnums::PositionWest:
233
 
        // this is the axis on the other side of C
234
 
        axisLabelAngle = 120.0;
235
 
        fiftyMarkAngle = 300.0;
236
 
        axisLabelPosition = TriangleBottomRight;
237
 
        fiftyMarkPosition = 0.5 * AxisVector_B_A + RelMarkerLength * Norm_B_A;
238
 
        fiftyMarkReferencePoint = KDChartEnums::PositionSouth;
239
 
        break;
240
 
    case KDChartEnums::PositionUnknown:
241
 
        break; // initial value
242
 
    default:
243
 
        qDebug() << "TernaryAxis::updatePrerenderedLabel: unknown location";
244
 
    };
245
 
 
246
 
    m_label->setFont( attributes.font() );
247
 
    // m_label->setText( titleText() ); // done by setTitleText()
248
 
    m_label->setAngle( axisLabelAngle );
249
 
    m_label->setPosition( axisLabelPosition );
250
 
    m_label->setReferencePoint( KDChartEnums::PositionSouth );
251
 
    QFont font = attributes.font();
252
 
    font.setPointSizeF( 0.85 * font.pointSizeF() );
253
 
    m_fifty->setFont( font );
254
 
    m_fifty->setAngle( fiftyMarkAngle );
255
 
    m_fifty->setPosition( fiftyMarkPosition );
256
 
    m_fifty->setReferencePoint( fiftyMarkReferencePoint );
257
 
}
258
 
 
259
 
QPair<QSizeF, QSizeF> TernaryAxis::requiredMargins() const
260
 
{
261
 
    QSizeF topleft( 0.0, 0.0 );
262
 
    QSizeF bottomRight( 0.0, 0.0 );
263
 
 
264
 
    switch( position().value() ) {
265
 
    case KDChartEnums::PositionSouth:
266
 
        // the label of the south axis is, in fact, up north.
267
 
        topleft.setHeight( m_label->pixmap().height() );
268
 
        bottomRight.setHeight( m_fifty->pixmap().height() );
269
 
        break;
270
 
    case KDChartEnums::PositionWest:
271
 
        bottomRight.setWidth( m_label->pixmap().width()
272
 
                              - m_label->referencePointLocation().x() );
273
 
        bottomRight.setHeight( m_label->pixmap().height()
274
 
                               - m_label->referencePointLocation().y() );
275
 
        break;
276
 
    case KDChartEnums::PositionEast:
277
 
        topleft.setWidth( m_label->pixmap().width()
278
 
                          - ( m_label->pixmap().width()
279
 
                              - m_label->referencePointLocation().x() ) );
280
 
        bottomRight.setHeight( m_label->pixmap().height()
281
 
                               - ( m_label->pixmap().height()
282
 
                                   - m_label->referencePointLocation().y() ) );
283
 
        break;
284
 
    default:
285
 
        qDebug() << "TernaryAxis::requiredMargins: unknown location";
286
 
    }
287
 
//     qDebug() << "TernaryAxis::requiredMargins:" << topleft << bottomRight;
288
 
    return QPair<QSizeF, QSizeF>( topleft, bottomRight );
289
 
}