2
KDChart - a multi-platform charting engine
5
/****************************************************************************
6
** Copyright (C) 2005-2007 Klarälvdalens Datakonsult AB. All rights reserved.
8
** This file is part of the KD Chart library.
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).
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.
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.
26
**********************************************************************/
28
#include "KDChartTernaryAxis.h"
32
#include <KDChartChart>
33
#include <KDChartPaintContext>
35
#include "TernaryConstants.h"
36
#include "KDChartTernaryCoordinatePlane.h"
37
#include "KDChartAbstractTernaryDiagram.h"
40
#include "../src/KDChartLayoutItems.h"
41
#include "PrerenderedElements/KDChartTextLabelCache.h"
43
using namespace KDChart;
45
// m_label and m_fifty do not have to be pointers, once the class is
46
// pimpled (PrerenderedLabel is not published API)
48
TernaryAxis::TernaryAxis ( AbstractTernaryDiagram* diagram)
49
: AbstractAxis( diagram )
50
, m_position( KDChartEnums::PositionUnknown )
51
, m_label( new PrerenderedLabel )
52
, m_fifty( new PrerenderedLabel )
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 );
61
TernaryAxis::~TernaryAxis()
63
delete m_label; m_label = 0;
64
delete m_label; m_fifty = 0;
67
void TernaryAxis::paintAll (QPainter &)
72
void TernaryAxis::paint (QPainter *)
77
void TernaryAxis::paintCtx (PaintContext * paintContext)
79
QPainter* p = paintContext->painter();
80
TernaryCoordinatePlane* plane =
81
(TernaryCoordinatePlane*) paintContext->coordinatePlane();
82
// QObject* refArea = plane->parent();
83
QRectF drawArea = paintContext->rectangle();
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 );
97
bool TernaryAxis::isEmpty() const
99
// todo: what's this method for?
103
QRect TernaryAxis::geometry () const
108
void TernaryAxis::setGeometry (const QRect &rect)
113
QSize TernaryAxis::minimumSize () const
115
// todo: return realistic sizes
116
return QSize( 100, 100 );
119
QSize TernaryAxis::maximumSize () const
121
return QSize( 300, 200 );
124
QSize TernaryAxis::sizeHint () const
126
return QSize( 150, 100 );
129
Qt::Orientations TernaryAxis::expandingDirections () const
131
return Qt::Vertical | Qt::Horizontal;
134
const Position TernaryAxis::position () const
139
void TernaryAxis::setPosition (Position p)
141
if ( p == position() ) return;
143
if ( p != KDChartEnums::PositionWest
144
&& p != KDChartEnums::PositionEast
145
&& p != KDChartEnums::PositionSouth )
147
qDebug() << "TernaryAxis::setPosition: only south, east and west are supported "
148
"positions for ternary axes.";
152
if ( m_title.isEmpty() )
153
switch( p.value() ) {
154
case KDChartEnums::PositionSouth:
155
m_label->setText( tr( "A" ) );
157
case KDChartEnums::PositionWest:
158
m_label->setText( tr( "C" ) );
160
case KDChartEnums::PositionEast:
161
m_label->setText( tr( "B" ) );
168
updatePrerenderedLabels(); // position has changed
171
void TernaryAxis::setTitleText( const QString& text )
173
m_title = text; // do not remove
174
m_label->setText( text );
177
QString TernaryAxis::titleText() const
179
return m_label->text();
182
void TernaryAxis::setTitleTextAttributes( const TextAttributes &a )
184
m_titleAttributes = a;
185
updatePrerenderedLabels();
188
TextAttributes TernaryAxis::titleTextAttributes() const
190
return m_titleAttributes;
193
void TernaryAxis::resetTitleTextAttributes()
196
m_titleAttributes = a;
197
updatePrerenderedLabels();
200
bool TernaryAxis::hasDefaultTitleTextAttributes() const
203
return m_titleAttributes == a;
206
void TernaryAxis::updatePrerenderedLabels()
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;
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;
224
case KDChartEnums::PositionEast:
225
// this is the axis on the other side of B
226
axisLabelAngle = 240.0;
228
axisLabelPosition = TriangleBottomLeft;
229
fiftyMarkPosition = AxisVector_B_C + 0.5 * AxisVector_C_A - RelMarkerLength * Norm_C_A;
230
fiftyMarkReferencePoint = KDChartEnums::PositionSouth;
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;
240
case KDChartEnums::PositionUnknown:
241
break; // initial value
243
qDebug() << "TernaryAxis::updatePrerenderedLabel: unknown location";
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 );
259
QPair<QSizeF, QSizeF> TernaryAxis::requiredMargins() const
261
QSizeF topleft( 0.0, 0.0 );
262
QSizeF bottomRight( 0.0, 0.0 );
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() );
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() );
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() ) );
285
qDebug() << "TernaryAxis::requiredMargins: unknown location";
287
// qDebug() << "TernaryAxis::requiredMargins:" << topleft << bottomRight;
288
return QPair<QSizeF, QSizeF>( topleft, bottomRight );