KD Chart 2  [rev.2.7]
KDChartTernaryAxis.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #include "KDChartTernaryAxis.h"
24 
25 #include <QPainter>
26 
27 #include <KDChartChart.h>
28 #include <KDChartPaintContext.h>
29 
30 #include "TernaryConstants.h"
33 
34 
35 #include "KDChartLayoutItems.h"
36 #include "KDChartTextLabelCache.h"
37 
38 using namespace KDChart;
39 
40 // m_label and m_fifty do not have to be pointers, once the class is
41 // pimpled (PrerenderedLabel is not published API)
42 
44  : AbstractAxis( diagram )
45  , m_position( KDChartEnums::PositionUnknown )
46  , m_label( new PrerenderedLabel )
47  , m_fifty( new PrerenderedLabel )
48 {
51  m_fifty->setText( QObject::tr( "50%" ) ); // const
52  // FIXME is this consistent with other diagram/axis/plane implementations?
53  diagram->addAxis( this );
54 }
55 
57 {
58  delete m_label; m_label = 0;
59  delete m_fifty; m_fifty = 0;
60 }
61 
62 void TernaryAxis::paintAll (QPainter &)
63 {
64  // not used
65 }
66 
67 void TernaryAxis::paint (QPainter *)
68 {
69  // not used
70 }
71 
72 void TernaryAxis::paintCtx (PaintContext * paintContext)
73 {
74  QPainter* p = paintContext->painter();
75  TernaryCoordinatePlane* plane =
76  (TernaryCoordinatePlane*) paintContext->coordinatePlane();
77  // QObject* refArea = plane->parent();
78  QRectF titleArea;
79 
80  // paint the axis label (across the triangle, that one):
82  labels << m_label << m_fifty;
83  Q_FOREACH( PrerenderedLabel* label, labels ) {
84  const QPixmap& pixmap = label->pixmap();
85  QPointF point = plane->translate( label->position() )
86  - label->referencePointLocation();
87  p->drawPixmap( point, pixmap );
88  }
89 }
90 
92 {
93  // todo: what's this method for?
94  return false;
95 }
96 
97 QRect TernaryAxis::geometry () const
98 {
99  return m_geometry;
100 }
101 
102 void TernaryAxis::setGeometry (const QRect &rect)
103 {
104  m_geometry = rect;
105 }
106 
108 {
109  // todo: return realistic sizes
110  return QSize( 100, 100 );
111 }
112 
114 {
115  return QSize( 300, 200 );
116 }
117 
118 QSize TernaryAxis::sizeHint () const
119 {
120  return QSize( 150, 100 );
121 }
122 
123 Qt::Orientations TernaryAxis::expandingDirections () const
124 {
125  return Qt::Vertical | Qt::Horizontal;
126 }
127 
129 {
130  return m_position;
131 }
132 
134 {
135  if ( p == position() ) return;
136 
140  {
141  qDebug() << "TernaryAxis::setPosition: only south, east and west are supported "
142  "positions for ternary axes.";
143  return;
144  }
145 
146  if ( m_title.isEmpty() )
147  switch ( p.value() ) {
149  m_label->setText( tr( "A" ) );
150  break;
152  m_label->setText( tr( "C" ) );
153  break;
155  m_label->setText( tr( "B" ) );
156  break;
157  default:
158  break;
159  }
160 
161  m_position = p;
162  updatePrerenderedLabels(); // position has changed
163 }
164 
165 void TernaryAxis::setTitleText( const QString& text )
166 {
167  m_title = text; // do not remove
168  m_label->setText( text );
169 }
170 
171 QString TernaryAxis::titleText() const
172 {
173  return m_label->text();
174 }
175 
177 {
178  m_titleAttributes = a;
179  updatePrerenderedLabels();
180 }
181 
183 {
184  return m_titleAttributes;
185 }
186 
188 {
189  TextAttributes a;
190  m_titleAttributes = a;
191  updatePrerenderedLabels();
192 }
193 
195 {
196  TextAttributes a;
197  return m_titleAttributes == a;
198 }
199 
200 void TernaryAxis::updatePrerenderedLabels()
201 {
202  TextAttributes attributes = titleTextAttributes();
203  qreal axisLabelAngle = 0.0;
204  qreal fiftyMarkAngle = 0.0;
205  QPointF axisLabelPosition;
206  QPointF fiftyMarkPosition;
208 
209  switch ( position().value() ) {
211  // this is the axis on the other side of A
212  axisLabelAngle = 0.0;
213  fiftyMarkAngle = 0.0;
214  axisLabelPosition = TriangleTop;
215  fiftyMarkPosition = 0.5 * AxisVector_B_C - RelMarkerLength * Norm_B_C;
216  fiftyMarkReferencePoint = KDChartEnums::PositionNorth;
217  break;
219  // this is the axis on the other side of B
220  axisLabelAngle = 240.0;
221  fiftyMarkAngle = 60;
222  axisLabelPosition = TriangleBottomLeft;
223  fiftyMarkPosition = AxisVector_B_C + 0.5 * AxisVector_C_A - RelMarkerLength * Norm_C_A;
224  fiftyMarkReferencePoint = KDChartEnums::PositionSouth;
225  break;
227  // this is the axis on the other side of C
228  axisLabelAngle = 120.0;
229  fiftyMarkAngle = 300.0;
230  axisLabelPosition = TriangleBottomRight;
231  fiftyMarkPosition = 0.5 * AxisVector_B_A + RelMarkerLength * Norm_B_A;
232  fiftyMarkReferencePoint = KDChartEnums::PositionSouth;
233  break;
235  break; // initial value
236  default:
237  qDebug() << "TernaryAxis::updatePrerenderedLabel: unknown location";
238  };
239 
240  m_label->setFont( attributes.font() );
241  // m_label->setText( titleText() ); // done by setTitleText()
242  m_label->setAngle( axisLabelAngle );
243  m_label->setPosition( axisLabelPosition );
245  QFont font = attributes.font();
246  font.setPointSizeF( 0.85 * font.pointSizeF() );
247  m_fifty->setFont( font );
248  m_fifty->setAngle( fiftyMarkAngle );
249  m_fifty->setPosition( fiftyMarkPosition );
250  m_fifty->setReferencePoint( fiftyMarkReferencePoint );
251 }
252 
254 {
255  QSizeF topleft( 0.0, 0.0 );
256  QSizeF bottomRight( 0.0, 0.0 );
257 
258  switch ( position().value() ) {
260  // the label of the south axis is, in fact, up north.
261  topleft.setHeight( m_label->pixmap().height() );
262  bottomRight.setHeight( m_fifty->pixmap().height() );
263  break;
265  bottomRight.setWidth( m_label->pixmap().width()
266  - m_label->referencePointLocation().x() );
267  bottomRight.setHeight( m_label->pixmap().height()
268  - m_label->referencePointLocation().y() );
269  break;
271  topleft.setWidth( m_label->pixmap().width()
272  - ( m_label->pixmap().width()
273  - m_label->referencePointLocation().x() ) );
274  bottomRight.setHeight( m_label->pixmap().height()
275  - ( m_label->pixmap().height()
276  - m_label->referencePointLocation().y() ) );
277  break;
278  default:
279  qDebug() << "TernaryAxis::requiredMargins: unknown location";
280  }
281 // qDebug() << "TernaryAxis::requiredMargins:" << topleft << bottomRight;
282  return QPair<QSizeF, QSizeF>( topleft, bottomRight );
283 }
KDChartEnums::PositionValue value() const
Returns an integer value corresponding to this Position.
const QString & text() const
Project global class providing some enums needed both by KDChartParams and by KDChartCustomBox.
Definition: KDChartEnums.h:40
void setTitleTextAttributes(const TextAttributes &a)
const QPointF AxisVector_B_A
const QPointF TriangleBottomRight
AbstractCoordinatePlane * coordinatePlane() const
const QPixmap & pixmap() const override
Returns the rendered element.
const QPointF AxisVector_C_A
QPainter * painter() const
const QPointF Norm_C_A
const QPointF & position() const
Get the position of the element.
const QPointF AxisVector_B_C
QSize minimumSize() const override
QSize maximumSize() const override
The base class for axes.
const QPointF Norm_B_C
QRect geometry() const override
QStringList labels() const
Returns a list of strings, that are used as axis labels, as set via setLabels.
void setFont(const QFont &font)
Sets the label&#39;s font to font.
const QPointF TriangleBottomLeft
QPointF referencePointLocation(KDChartEnums::PositionValue position) const override
Return the location of the reference point relatively to the pixmap&#39;s origin.
virtual void setPosition(Position p)
bool isEmpty() const override
void paintAll(QPainter &) override
Call paintAll, if you want the background and the frame to be drawn before the normal paint() is invo...
PrerenderedLabel is an internal KDChart class that simplifies creation and caching of cached text lab...
virtual const Position position() const
void setReferencePoint(KDChartEnums::PositionValue)
Set the reference point of the element.
void setText(const QString &text)
Sets the label&#39;s text to text.
Qt::Orientations expandingDirections() const override
PositionValue
Numerical values of the static KDChart::Position instances, for using a Position::value() with a swit...
Definition: KDChartEnums.h:192
Stores information about painting diagrams.
Base class for diagrams based on a ternary coordinate plane.
bool hasDefaultTitleTextAttributes() const
void setPosition(const QPointF &position)
Set the position of the element.
TextAttributes titleTextAttributes() const
void paint(QPainter *) override
void setAngle(qreal angle)
Sets the angle of the label to angle degrees.
void setGeometry(const QRect &rect) override
QSize sizeHint() const override
void setTitleText(const QString &text)
QPair< QSizeF, QSizeF > requiredMargins() const
Defines a position, using compass terminology.
const QPointF TriangleTop
QString titleText() const
const qreal RelMarkerLength
void paintCtx(PaintContext *) override
Default impl: Paint the complete item using its layouted position and size.
TernaryAxis(AbstractTernaryDiagram *diagram=0)
const QPointF Norm_B_A
A set of text attributes.
const QPointF translate(const QPointF &diagramPoint) const override
Translate the given point in value space coordinates to a position in pixel space.

Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/

https://www.kdab.com/development-resources/qt-tools/kd-chart/