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

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
https://www.kdab.com/
https://www.kdab.com/products/kd-chart/