KD Chart API Documentation  3.1
KDChartAbstractAxis.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 "KDChartAbstractAxis.h"
12 #include "KDChartAbstractAxis_p.h"
14 #include "KDChartAbstractDiagram.h"
15 #include "KDChartEnums.h"
16 #include "KDChartMeasure.h"
17 
18 #include <KDABLibFakes>
19 
20 using namespace KDChart;
21 
22 #define d d_func()
23 
24 AbstractAxis::Private::Private(AbstractDiagram *diagram, AbstractAxis *axis)
25  : observer(nullptr)
26  , mDiagram(diagram)
27  , mAxis(axis)
28 {
29  // Note: We do NOT call setDiagram( diagram, axis );
30  // but it is called in AbstractAxis::delayedInit() instead!
31 }
32 
33 AbstractAxis::Private::~Private()
34 {
35  delete observer;
36  observer = nullptr;
37 }
38 
39 bool AbstractAxis::Private::setDiagram(AbstractDiagram *diagram_, bool delayedInit)
40 {
41  AbstractDiagram *diagram = delayedInit ? mDiagram : diagram_;
42  if (delayedInit) {
43  mDiagram = nullptr;
44  }
45 
46  // do not set a diagram again that was already set
47  if (diagram && ((diagram == mDiagram) || secondaryDiagrams.contains(diagram)))
48  return false;
49 
50  bool bNewDiagramStored = false;
51  if (!mDiagram) {
52  mDiagram = diagram;
53  delete observer;
54  if (mDiagram) {
55  observer = new DiagramObserver(mDiagram, mAxis);
56  const bool con = connect(observer, &DiagramObserver::diagramDataChanged,
57  mAxis, &AbstractAxis::coordinateSystemChanged);
58  Q_UNUSED(con)
59  Q_ASSERT(con);
60  bNewDiagramStored = true;
61  } else {
62  observer = nullptr;
63  }
64  } else {
65  if (diagram)
66  secondaryDiagrams.enqueue(diagram);
67  }
68  return bNewDiagramStored;
69 }
70 
71 void AbstractAxis::Private::unsetDiagram(AbstractDiagram *diagram)
72 {
73  if (diagram == mDiagram) {
74  mDiagram = nullptr;
75  delete observer;
76  observer = nullptr;
77  } else {
78  secondaryDiagrams.removeAll(diagram);
79  }
80  if (!secondaryDiagrams.isEmpty()) {
81  AbstractDiagram *nextDiagram = secondaryDiagrams.dequeue();
82  setDiagram(nextDiagram);
83  }
84 }
85 
86 bool AbstractAxis::Private::hasDiagram(AbstractDiagram *diagram) const
87 {
88  return diagram == mDiagram || secondaryDiagrams.contains(diagram);
89 }
90 
91 void AbstractAxis::Private::updateLayouts()
92 {
93  if (auto *cartesianAxis = qobject_cast<CartesianAxis *>(mAxis)) {
94  cartesianAxis->layoutPlanes();
95  } else {
96  mAxis->update();
97  }
98 }
99 
100 AbstractAxis::AbstractAxis(AbstractDiagram *diagram)
101  : AbstractArea(new Private(diagram, this))
102 {
103  init();
104  QTimer::singleShot(0, this, &AbstractAxis::delayedInit);
105 }
106 
108 {
109  d->mDiagram = nullptr;
110  d->secondaryDiagrams.clear();
111 }
112 
113 void AbstractAxis::init()
114 {
116  d->textAttributes.setFontSize(m);
117  m.setValue(6);
118  m.setCalculationMode(KDChartEnums::MeasureCalculationModeAbsolute);
119  d->textAttributes.setMinimalFontSize(m);
120  if (d->diagram())
121  createObserver(d->diagram());
122 }
123 
125 {
126  // We call setDiagram() here, because the c'tor of Private
127  // only has stored the pointers, but it did not call setDiagram().
128  if (d)
129  d->setDiagram(nullptr, true /* delayedInit */);
130 }
131 
132 bool AbstractAxis::compare(const AbstractAxis *other) const
133 {
134  if (other == this) {
135  return true;
136  }
137  if (!other) {
138  return false;
139  }
140 
141  return (static_cast<const AbstractAreaBase *>(this)->compare(other)) && (textAttributes() == other->textAttributes()) && (labels() == other->labels()) && (shortLabels() == other->shortLabels());
142 }
143 
144 const QString AbstractAxis::customizedLabel(const QString &label) const
145 {
146  return label;
147 }
148 
150 {
151  d->setDiagram(diagram);
152 }
153 
155 {
156  d->unsetDiagram(diagram);
157 }
158 
160 {
161  if (d->observer) {
162  const bool con = connect(d->observer, &DiagramObserver::diagramDataChanged,
164  Q_UNUSED(con);
165  Q_ASSERT(con);
166  }
167 }
168 
170 {
171  if (d->textAttributes == a)
172  return;
173 
174  d->textAttributes = a;
175  d->updateLayouts();
176 }
177 
179 {
180  return d->textAttributes;
181 }
182 
184 {
185  d->rulerAttributes = a;
186  d->updateLayouts();
187 }
188 
190 {
191  return d->rulerAttributes;
192 }
193 
194 void AbstractAxis::setLabels(const QStringList &list)
195 {
196  if (d->hardLabels == list)
197  return;
198 
199  d->hardLabels = list;
200  d->updateLayouts();
201 }
202 
203 QStringList AbstractAxis::labels() const
204 {
205  return d->hardLabels;
206 }
207 
208 void AbstractAxis::setShortLabels(const QStringList &list)
209 {
210  if (d->hardShortLabels == list)
211  return;
212 
213  d->hardShortLabels = list;
214  d->updateLayouts();
215 }
216 
217 QStringList AbstractAxis::shortLabels() const
218 {
219  return d->hardShortLabels;
220 }
221 
223 {
224  if (d->diagram())
225  return d->diagram()->coordinatePlane();
226  return nullptr;
227 }
228 
230 {
231  return d->diagram();
232 }
233 
235 {
236  return d->hasDiagram(diagram);
237 }
238 
240 {
241  if (d->diagram())
242  d->diagram()->update();
243 }
#define d
Definition of global enums.
Declaring the class KDChart::Measure.
@ MeasureCalculationModeAbsolute
Definition: KDChartEnums.h:215
@ MeasureCalculationModeAuto
Definition: KDChartEnums.h:217
@ MeasureOrientationAuto
Definition: KDChartEnums.h:287
Base class for AbstractArea and AbstractAreaWidget: An area in the chart with a background,...
An area in the chart with a background, a frame, etc.
RulerAttributes rulerAttributes() const
Returns the attributes to be used for painting the rulers.
virtual const QString customizedLabel(const QString &label) const
Reimplement this method if you want to adjust axis labels before they are printed.
const AbstractDiagram * diagram() const
bool observedBy(AbstractDiagram *diagram) const
void setLabels(const QStringList &list)
Use this to specify your own set of strings, to be used as axis labels.
void deleteObserver(AbstractDiagram *diagram)
void createObserver(AbstractDiagram *diagram)
QStringList labels() const
void setShortLabels(const QStringList &list)
Use this to specify your own set of strings, to be used as axis labels, in case the normal labels are...
void setRulerAttributes(const RulerAttributes &a)
Use this to specify the attributes used to paint the axis ruler.
void setTextAttributes(const TextAttributes &a)
Use this to specify the text attributes to be used for axis labels.
bool compare(const AbstractAxis *other) const
TextAttributes textAttributes() const
Returns the text attributes to be used for axis labels.
QStringList shortLabels() const
const AbstractCoordinatePlane * coordinatePlane() const
Convenience function, returns the coordinate plane, in which this axis is used.
Base class common for all coordinate planes, CartesianCoordinatePlane, PolarCoordinatePlane,...
AbstractDiagram defines the interface for diagram classes.
A DiagramObserver watches the associated diagram for changes and deletion and emits corresponding sig...
void diagramDataChanged(AbstractDiagram *diagram)
Measure is used to specify relative and absolute sizes in KDChart, e.g. font sizes.
A set of attributes controlling the appearance of axis rulers.
A set of text attributes.

© 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