KChart

KChartAbstractAxis.cpp
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KD Chart library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "KChartAbstractAxis.h"
10#include "KChartAbstractAxis_p.h"
11#include "KChartAbstractDiagram.h"
12#include "KChartAbstractCartesianDiagram.h"
13#include "KChartEnums.h"
14#include "KChartMeasure.h"
15#include "KChartMath_p.h"
16
17using namespace KChart;
18
19#define d d_func()
20
21AbstractAxis::Private::Private( AbstractDiagram* diagram, AbstractAxis* axis )
22 : observer( nullptr )
23 , mDiagram( diagram )
24 , mAxis( axis )
25{
26 // Note: We do NOT call setDiagram( diagram, axis );
27 // but it is called in AbstractAxis::delayedInit() instead!
28}
29
30AbstractAxis::Private::~Private()
31{
32 delete observer;
33 observer = nullptr;
34}
35
36bool AbstractAxis::Private::setDiagram( AbstractDiagram* diagram_, bool delayedInit )
37{
38 AbstractDiagram* diagram = delayedInit ? mDiagram : diagram_;
39 if ( delayedInit ) {
40 mDiagram = nullptr;
41 }
42
43 // do not set a diagram again that was already set
44 if ( diagram &&
45 ((diagram == mDiagram) || secondaryDiagrams.contains( diagram )) )
46 return false;
47
48 bool bNewDiagramStored = false;
49 if ( ! mDiagram ) {
50 mDiagram = diagram;
51 delete observer;
52 if ( mDiagram ) {
53 observer = new DiagramObserver( mDiagram, mAxis );
54 const bool con = connect( observer, &DiagramObserver::diagramDataChanged,
55 mAxis, &AbstractAxis::coordinateSystemChanged );
56 Q_UNUSED( con )
57 Q_ASSERT( con );
58 bNewDiagramStored = true;
59 } else {
60 observer = nullptr;
61 }
62 } else {
63 if ( diagram )
64 secondaryDiagrams.enqueue( diagram );
65 }
66 return bNewDiagramStored;
67}
68
69void AbstractAxis::Private::unsetDiagram( AbstractDiagram* diagram )
70{
71 if ( diagram == mDiagram ) {
72 mDiagram = nullptr;
73 delete observer;
74 observer = nullptr;
75 } else {
76 secondaryDiagrams.removeAll( diagram );
77 }
78 if ( !secondaryDiagrams.isEmpty() ) {
79 AbstractDiagram *nextDiagram = secondaryDiagrams.dequeue();
80 setDiagram( nextDiagram );
81 }
82}
83
84bool AbstractAxis::Private::hasDiagram( AbstractDiagram* diagram ) const
85{
86 return diagram == mDiagram || secondaryDiagrams.contains( diagram );
87}
88
89void AbstractAxis::Private::updateLayouts()
90{
91 if ( CartesianAxis* cartesianAxis = qobject_cast< CartesianAxis* >( mAxis ) ) {
92 cartesianAxis->layoutPlanes();
93 } else {
94 mAxis->update();
95 }
96}
97
98AbstractAxis::AbstractAxis ( AbstractDiagram* diagram )
99 : AbstractArea( new Private( diagram, this ) )
100{
101 init();
102 QTimer::singleShot(0, this, SLOT(delayedInit()));
103}
104
105AbstractAxis::~AbstractAxis()
106{
107 d->mDiagram = nullptr;
108 d->secondaryDiagrams.clear();
109}
110
111
112void AbstractAxis::init()
113{
114 Measure m( 14, KChartEnums::MeasureCalculationModeAuto, KChartEnums::MeasureOrientationAuto );
115 d->textAttributes.setFontSize( m );
116 m.setValue( 6 );
117 m.setCalculationMode( KChartEnums::MeasureCalculationModeAbsolute );
118 d->textAttributes.setMinimalFontSize( m );
119 if ( d->diagram() )
120 createObserver( d->diagram() );
121}
122
124{
125 // We call setDiagram() here, because the c'tor of Private
126 // only has stored the pointers, but it did not call setDiagram().
127 if ( d )
128 d->setDiagram( nullptr, true /* delayedInit */ );
129}
130
131bool AbstractAxis::compare( const AbstractAxis* other ) const
132{
133 if ( other == this ) {
134 return true;
135 }
136 if ( !other ) {
137 return false;
138 }
139
140 return ( static_cast<const AbstractAreaBase*>(this)->compare( other ) ) &&
141 (textAttributes() == other->textAttributes()) &&
142 (labels() == other->labels()) &&
143 (shortLabels() == other->shortLabels());
144}
145
146
148{
149 return label;
150}
151
152
154{
155 d->setDiagram( diagram );
156}
157
159{
160 d->unsetDiagram( diagram );
161}
162
164{
165 if ( d->observer ) {
166 const bool con = connect( d->observer, &DiagramObserver::diagramDataChanged,
167 this, &AbstractAxis::coordinateSystemChanged );
168 Q_UNUSED( con );
169 Q_ASSERT( con );
170 }
171}
172
174{
175 if ( d->textAttributes == a )
176 return;
177
178 d->textAttributes = a;
179 d->updateLayouts();
180}
181
183{
184 return d->textAttributes;
185}
186
187
189{
190 d->rulerAttributes = a;
191 d->updateLayouts();
192}
193
195{
196 return d->rulerAttributes;
197}
198
200{
201 if ( d->hardLabels == list )
202 return;
203
204 d->hardLabels = list;
205 d->updateLayouts();
206}
207
209{
210 return d->hardLabels;
211}
212
214{
215 if ( d->hardShortLabels == list )
216 return;
217
218 d->hardShortLabels = list;
219 d->updateLayouts();
220}
221
223{
224 return d->hardShortLabels;
225}
226
228{
229 if ( d->diagram() )
230 return d->diagram()->coordinatePlane();
231 return nullptr;
232}
233
234const AbstractDiagram * KChart::AbstractAxis::diagram() const
235{
236 return d->diagram();
237}
238
239bool KChart::AbstractAxis::observedBy( AbstractDiagram * diagram ) const
240{
241 return d->hasDiagram( diagram );
242}
243
244void KChart::AbstractAxis::update()
245{
246 if ( d->diagram() )
247 d->diagram()->update();
248}
Definition of global enums.
Declaring the class KChart::Measure.
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.
The base class for axes.
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.
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
Returns a list of strings, that are used as axis labels, as set via setLabels.
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
Returns true if both axes have the same settings.
TextAttributes textAttributes() const
Returns the text attributes to be used for axis labels.
QStringList shortLabels() const
Returns a list of strings, that are used as axis labels, as set via setShortLabels.
const AbstractCoordinatePlane * coordinatePlane() const
Convenience function, returns the coordinate plane, in which this axis is used.
virtual void delayedInit()
called for initializing after the c'tor has completed
virtual void connectSignals()
Wireing the signal/slot connections.
Base class common for all coordinate planes, CartesianCoordinatePlane, PolarCoordinatePlane,...
AbstractDiagram defines the interface for diagram classes.
The class for cartesian axes.
A DiagramObserver watches the associated diagram for changes and deletion and emits corresponding sig...
void diagramDataChanged(KChart::AbstractDiagram *diagram)
This signal is emitted whenever the data of the diagram changes.
Measure is used to specify relative and absolute sizes in KChart, e.g.
A set of attributes controlling the appearance of axis rulers.
A set of text attributes.
QCA_EXPORT void init()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.