KD Chart API Documentation  3.1
KDChartTernaryAxis.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Chart library.
4 **
5 ** SPDX-FileCopyrightText: 2001 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
11 #include "KDChartTernaryAxis.h"
12 
13 #include <QPainter>
14 
15 #include <KDChartChart.h>
16 #include <KDChartPaintContext.h>
17 
20 #include "TernaryConstants.h"
21 
22 #include "KDChartLayoutItems.h"
23 #include "KDChartTextLabelCache.h"
24 
25 using namespace KDChart;
26 
27 // m_label and m_fifty do not have to be pointers, once the class is
28 // pimpled (PrerenderedLabel is not published API)
29 
31  : AbstractAxis(diagram)
32  , m_position(KDChartEnums::PositionUnknown)
33  , m_label(new PrerenderedLabel)
34  , m_fifty(new PrerenderedLabel)
35 {
38  m_fifty->setText(QObject::tr("50%")); // const
39  // FIXME is this consistent with other diagram/axis/plane implementations?
40  diagram->addAxis(this);
41 }
42 
44 {
45  delete m_label;
46  m_label = nullptr;
47  delete m_fifty;
48  m_fifty = nullptr;
49 }
50 
51 void TernaryAxis::paintAll(QPainter &)
52 {
53  // not used
54 }
55 
56 void TernaryAxis::paint(QPainter *)
57 {
58  // not used
59 }
60 
62 {
63  QPainter *p = paintContext->painter();
64  auto *plane =
65  ( TernaryCoordinatePlane * )paintContext->coordinatePlane();
66  // QObject* refArea = plane->parent();
67 
68  // paint the axis label (across the triangle, that one):
69  const QList<PrerenderedLabel *> labels = {
70  m_label,
71  m_fifty,
72  };
73  for (PrerenderedLabel *label : labels) {
74  const QPixmap &pixmap = label->pixmap();
75  QPointF point = plane->translate(label->position())
76  - label->referencePointLocation();
77  p->drawPixmap(point, pixmap);
78  }
79 }
80 
82 {
83  // todo: what's this method for?
84  return false;
85 }
86 
87 QRect TernaryAxis::geometry() const
88 {
89  return m_geometry;
90 }
91 
92 void TernaryAxis::setGeometry(const QRect &rect)
93 {
94  m_geometry = rect;
95 }
96 
98 {
99  // todo: return realistic sizes
100  return QSize(100, 100);
101 }
102 
104 {
105  return QSize(300, 200);
106 }
107 
109 {
110  return QSize(150, 100);
111 }
112 
113 Qt::Orientations TernaryAxis::expandingDirections() const
114 {
115  return Qt::Vertical | Qt::Horizontal;
116 }
117 
119 {
120  return m_position;
121 }
122 
124 {
125  if (p == position())
126  return;
127 
130  && p != KDChartEnums::PositionSouth) {
131  qDebug() << "TernaryAxis::setPosition: only south, east and west are supported "
132  "positions for ternary axes.";
133  return;
134  }
135 
136  if (m_title.isEmpty())
137  switch (p.value()) {
139  m_label->setText(tr("A"));
140  break;
142  m_label->setText(tr("C"));
143  break;
145  m_label->setText(tr("B"));
146  break;
147  default:
148  break;
149  }
150 
151  m_position = p;
152  updatePrerenderedLabels(); // position has changed
153 }
154 
155 void TernaryAxis::setTitleText(const QString &text)
156 {
157  m_title = text; // do not remove
158  m_label->setText(text);
159 }
160 
161 QString TernaryAxis::titleText() const
162 {
163  return m_label->text();
164 }
165 
167 {
168  m_titleAttributes = a;
169  updatePrerenderedLabels();
170 }
171 
173 {
174  return m_titleAttributes;
175 }
176 
178 {
179  TextAttributes a;
180  m_titleAttributes = a;
181  updatePrerenderedLabels();
182 }
183 
185 {
186  TextAttributes a;
187  return m_titleAttributes == a;
188 }
189 
190 void TernaryAxis::updatePrerenderedLabels()
191 {
192  TextAttributes attributes = titleTextAttributes();
193  qreal axisLabelAngle = 0.0;
194  qreal fiftyMarkAngle = 0.0;
195  QPointF axisLabelPosition;
196  QPointF fiftyMarkPosition;
198 
199  switch (position().value()) {
201  // this is the axis on the other side of A
202  axisLabelAngle = 0.0;
203  fiftyMarkAngle = 0.0;
204  axisLabelPosition = TriangleTop;
205  fiftyMarkPosition = 0.5 * AxisVector_B_C - RelMarkerLength * Norm_B_C;
206  fiftyMarkReferencePoint = KDChartEnums::PositionNorth;
207  break;
209  // this is the axis on the other side of B
210  axisLabelAngle = 240.0;
211  fiftyMarkAngle = 60;
212  axisLabelPosition = TriangleBottomLeft;
213  fiftyMarkPosition = AxisVector_B_C + 0.5 * AxisVector_C_A - RelMarkerLength * Norm_C_A;
214  fiftyMarkReferencePoint = KDChartEnums::PositionSouth;
215  break;
217  // this is the axis on the other side of C
218  axisLabelAngle = 120.0;
219  fiftyMarkAngle = 300.0;
220  axisLabelPosition = TriangleBottomRight;
221  fiftyMarkPosition = 0.5 * AxisVector_B_A + RelMarkerLength * Norm_B_A;
222  fiftyMarkReferencePoint = KDChartEnums::PositionSouth;
223  break;
225  break; // initial value
226  default:
227  qDebug() << "TernaryAxis::updatePrerenderedLabel: unknown location";
228  };
229 
230  m_label->setFont(attributes.font());
231  // m_label->setText( titleText() ); // done by setTitleText()
232  m_label->setAngle(axisLabelAngle);
233  m_label->setPosition(axisLabelPosition);
235  QFont font = attributes.font();
236  font.setPointSizeF(0.85 * font.pointSizeF());
237  m_fifty->setFont(font);
238  m_fifty->setAngle(fiftyMarkAngle);
239  m_fifty->setPosition(fiftyMarkPosition);
240  m_fifty->setReferencePoint(fiftyMarkReferencePoint);
241 }
242 
243 QPair<QSizeF, QSizeF> TernaryAxis::requiredMargins() const
244 {
245  QSizeF topleft(0.0, 0.0);
246  QSizeF bottomRight(0.0, 0.0);
247 
248  switch (position().value()) {
250  // the label of the south axis is, in fact, up north.
251  topleft.setHeight(m_label->pixmap().height());
252  bottomRight.setHeight(m_fifty->pixmap().height());
253  break;
255  bottomRight.setWidth(m_label->pixmap().width()
256  - m_label->referencePointLocation().x());
257  bottomRight.setHeight(m_label->pixmap().height()
258  - m_label->referencePointLocation().y());
259  break;
261  topleft.setWidth(m_label->pixmap().width()
262  - (m_label->pixmap().width()
263  - m_label->referencePointLocation().x()));
264  bottomRight.setHeight(m_label->pixmap().height()
265  - (m_label->pixmap().height()
266  - m_label->referencePointLocation().y()));
267  break;
268  default:
269  qDebug() << "TernaryAxis::requiredMargins: unknown location";
270  }
271  // qDebug() << "TernaryAxis::requiredMargins:" << topleft << bottomRight;
272  return QPair<QSizeF, QSizeF>(topleft, bottomRight);
273 }
const qreal RelMarkerLength
const QPointF Norm_B_C
const QPointF Norm_C_A
const QPointF Norm_B_A
const QPointF TriangleTop
const QPointF AxisVector_B_C
const QPointF TriangleBottomLeft
const QPointF AxisVector_B_A
const QPointF TriangleBottomRight
const QPointF AxisVector_C_A
const AbstractDiagram * diagram() const
QStringList labels() const
Base class for diagrams based on a ternary coordinate plane.
Stores information about painting diagrams.
AbstractCoordinatePlane * coordinatePlane() const
QPainter * painter() const
Defines a position, using compass terminology.
KDChartEnums::PositionValue value() const
bool isEmpty() const override
void paintAll(QPainter &) override
virtual const Position position() const
void paintCtx(PaintContext *) override
void paint(QPainter *) override
QPair< QSizeF, QSizeF > requiredMargins() const
QSize sizeHint() const override
bool hasDefaultTitleTextAttributes() const
virtual void setPosition(Position p)
Qt::Orientations expandingDirections() const override
void setTitleTextAttributes(const TextAttributes &a)
QRect geometry() const override
void setTitleText(const QString &text)
void setGeometry(const QRect &rect) override
TernaryAxis(AbstractTernaryDiagram *diagram=nullptr)
QSize minimumSize() const override
QSize maximumSize() const override
TextAttributes titleTextAttributes() const
A set of text attributes.
void setReferencePoint(KDChartEnums::PositionValue)
void setPosition(const QPointF &position)
PrerenderedLabel is an internal KDChart class that simplifies creation and caching of cached text lab...
void setAngle(qreal angle)
const QString & text() const
void setFont(const QFont &font)
QPointF referencePointLocation(KDChartEnums::PositionValue position) const override
const QPixmap & pixmap() const override
void setText(const QString &text)

© 2001 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/
Generated by doxygen 1.9.1