KD Chart 2  [rev.2.8]
KDChartTernaryAxis.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2021 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
PositionValue
Numerical values of the static KDChart::Position instances, for using a Position::value() with a swit...
Definition: KDChartEnums.h:192
KDChart::AbstractAxis
The base class for axes.
Definition: KDChartAbstractAxis.h:58
KDChartAbstractTernaryDiagram.h
KDChart::TernaryAxis::resetTitleTextAttributes
void resetTitleTextAttributes()
Definition: KDChartTernaryAxis.cpp:187
KDChart::Position
Defines a position, using compass terminology.
Definition: KDChartPosition.h:76
TriangleBottomLeft
const QPointF TriangleBottomLeft
Norm_B_C
const QPointF Norm_B_C
KDChart::TernaryAxis::paintCtx
void paintCtx(PaintContext *) override
Default impl: Paint the complete item using its layouted position and size.
Definition: KDChartTernaryAxis.cpp:72
QList
Definition: KDChartPosition.h:36
KDChart::TernaryAxis::TernaryAxis
TernaryAxis(AbstractTernaryDiagram *diagram=0)
Definition: KDChartTernaryAxis.cpp:43
KDChartEnums
Project global class providing some enums needed both by KDChartParams and by KDChartCustomBox.
Definition: KDChartEnums.h:41
TriangleTop
const QPointF TriangleTop
KDChartTernaryCoordinatePlane.h
PrerenderedLabel::text
const QString & text() const
Definition: KDChartTextLabelCache.cpp:129
KDChart::TernaryAxis::sizeHint
QSize sizeHint() const override
Definition: KDChartTernaryAxis.cpp:118
KDChart::TernaryAxis::setTitleText
void setTitleText(const QString &text)
Definition: KDChartTernaryAxis.cpp:165
KDChartTernaryAxis.h
KDChartTextLabelCache.h
PrerenderedLabel::setAngle
void setAngle(qreal angle)
Sets the angle of the label to angle degrees.
Definition: KDChartTextLabelCache.cpp:154
KDChart
Definition: KDChartAbstractCartesianDiagram.h:30
KDChart::PaintContext::coordinatePlane
AbstractCoordinatePlane * coordinatePlane() const
Definition: KDChartPaintContext.cpp:78
PrerenderedLabel::referencePointLocation
QPointF referencePointLocation(KDChartEnums::PositionValue position) const override
Return the location of the reference point relatively to the pixmap's origin.
Definition: KDChartTextLabelCache.cpp:286
KDChart::TernaryAxis::setGeometry
void setGeometry(const QRect &rect) override
Definition: KDChartTernaryAxis.cpp:102
TriangleBottomRight
const QPointF TriangleBottomRight
KDChartEnums::PositionEast
@ PositionEast
Definition: KDChartEnums.h:198
KDChartPaintContext.h
KDChart::Position::value
KDChartEnums::PositionValue value() const
Returns an integer value corresponding to this Position.
Definition: KDChartPosition.cpp:126
KDChart::TernaryAxis::~TernaryAxis
~TernaryAxis() override
Definition: KDChartTernaryAxis.cpp:56
KDChart::PaintContext
Stores information about painting diagrams.
Definition: KDChartPaintContext.h:42
KDChart::TernaryAxis::position
virtual const Position position() const
Definition: KDChartTernaryAxis.cpp:128
KDChart::TextAttributes
A set of text attributes.
Definition: KDChartTextAttributes.h:47
KDChartEnums::PositionSouth
@ PositionSouth
Definition: KDChartEnums.h:200
KDChart::TernaryAxis::requiredMargins
QPair< QSizeF, QSizeF > requiredMargins() const
Definition: KDChartTernaryAxis.cpp:253
PrerenderedLabel
PrerenderedLabel is an internal KDChart class that simplifies creation and caching of cached text lab...
Definition: KDChartTextLabelCache.h:98
KDChart::TernaryAxis::hasDefaultTitleTextAttributes
bool hasDefaultTitleTextAttributes() const
Definition: KDChartTernaryAxis.cpp:194
RelMarkerLength
const qreal RelMarkerLength
KDChart::PaintContext::painter
QPainter * painter() const
Definition: KDChartPaintContext.cpp:68
KDChart::TernaryAxis::maximumSize
QSize maximumSize() const override
Definition: KDChartTernaryAxis.cpp:113
KDChart::AbstractAxis::labels
QStringList labels() const
Returns a list of strings, that are used as axis labels, as set via setLabels.
Definition: KDChartAbstractAxis.cpp:223
PrerenderedElement::setReferencePoint
void setReferencePoint(KDChartEnums::PositionValue)
Set the reference point of the element.
Definition: KDChartTextLabelCache.cpp:67
KDChart::TernaryAxis::geometry
QRect geometry() const override
Definition: KDChartTernaryAxis.cpp:97
KDChart::AbstractTernaryDiagram
Base class for diagrams based on a ternary coordinate plane.
Definition: KDChartAbstractTernaryDiagram.h:38
KDChart::TernaryAxis::expandingDirections
Qt::Orientations expandingDirections() const override
Definition: KDChartTernaryAxis.cpp:123
KDChart::TernaryAxis::paint
void paint(QPainter *) override
Definition: KDChartTernaryAxis.cpp:67
PrerenderedElement::position
const QPointF & position() const
Get the position of the element.
Definition: KDChartTextLabelCache.cpp:62
KDChart::TernaryAxis::isEmpty
bool isEmpty() const override
Definition: KDChartTernaryAxis.cpp:91
Norm_B_A
const QPointF Norm_B_A
KDChartLayoutItems.h
TernaryConstants.h
KDChartEnums::PositionUnknown
@ PositionUnknown
Definition: KDChartEnums.h:193
Norm_C_A
const QPointF Norm_C_A
KDChart::TernaryAxis::setPosition
virtual void setPosition(Position p)
Definition: KDChartTernaryAxis.cpp:133
KDChart::TernaryAxis::setTitleTextAttributes
void setTitleTextAttributes(const TextAttributes &a)
Definition: KDChartTernaryAxis.cpp:176
AxisVector_B_A
const QPointF AxisVector_B_A
KDChartChart.h
QPair
Definition: KDChartWidget.h:35
KDChart::TernaryAxis::minimumSize
QSize minimumSize() const override
Definition: KDChartTernaryAxis.cpp:107
PrerenderedLabel::setText
void setText(const QString &text)
Sets the label's text to text.
Definition: KDChartTextLabelCache.cpp:120
KDChartEnums::PositionNorth
@ PositionNorth
Definition: KDChartEnums.h:196
KDChart::AbstractAxis::diagram
const AbstractDiagram * diagram() const
Definition: KDChartAbstractAxis.cpp:249
KDChart::TernaryCoordinatePlane::translate
const QPointF translate(const QPointF &diagramPoint) const override
Translate the given point in value space coordinates to a position in pixel space.
Definition: KDChartTernaryCoordinatePlane.cpp:135
AxisVector_C_A
const QPointF AxisVector_C_A
KDChart::TernaryAxis::titleText
QString titleText() const
Definition: KDChartTernaryAxis.cpp:171
PrerenderedElement::setPosition
void setPosition(const QPointF &position)
Set the position of the element.
Definition: KDChartTextLabelCache.cpp:57
KDChart::TernaryAxis::paintAll
void paintAll(QPainter &) override
Call paintAll, if you want the background and the frame to be drawn before the normal paint() is invo...
Definition: KDChartTernaryAxis.cpp:62
PrerenderedLabel::setFont
void setFont(const QFont &font)
Sets the label's font to font.
Definition: KDChartTextLabelCache.cpp:103
AxisVector_B_C
const QPointF AxisVector_B_C
KDChart::TernaryCoordinatePlane
Ternary coordinate plane.
Definition: KDChartTernaryCoordinatePlane.h:37
KDChart::TernaryAxis::titleTextAttributes
TextAttributes titleTextAttributes() const
Definition: KDChartTernaryAxis.cpp:182
KDChart::TextAttributes::font
QFont font() const
Definition: KDChartTextAttributes.cpp:135
PrerenderedLabel::pixmap
const QPixmap & pixmap() const override
Returns the rendered element.
Definition: KDChartTextLabelCache.cpp:168
KDChartEnums::PositionWest
@ PositionWest
Definition: KDChartEnums.h:202

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/