KD Chart 2  [rev.2.7]
KDChartAbstractCartesianDiagram.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 
24 #include "KDChartAbstractCartesianDiagram_p.h"
25 
26 #include <KDABLibFakes>
27 
28 
29 using namespace KDChart;
30 
31 AbstractCartesianDiagram::Private::Private()
32  : referenceDiagram( 0 )
33 {
34 }
35 
36 AbstractCartesianDiagram::Private::~Private()
37 {
38 }
39 
41 {
42  if ( other == this ) return true;
43  if ( ! other ) {
44  return false;
45  }
46  return // compare the base class
47  ( static_cast<const AbstractDiagram*>(this)->compare( other ) ) &&
48  // compare own properties
49  (referenceDiagram() == other->referenceDiagram()) &&
50  ((! referenceDiagram()) || (referenceDiagramOffset() == other->referenceDiagramOffset()));
51 }
52 
53 
54 #define d d_func()
55 
57  : AbstractDiagram ( new Private(), parent, plane )
58 {
59  init();
60 }
61 
63 {
64  Q_FOREACH( CartesianAxis* axis, d->axesList ) {
65  axis->deleteObserver( this );
66  }
67  d->axesList.clear();
68 }
69 
70 void AbstractCartesianDiagram::init()
71 {
72  d->compressor.setModel( attributesModel() );
73  connect( this, SIGNAL( layoutChanged( AbstractDiagram* ) ),
74  &d->compressor, SLOT( slotDiagramLayoutChanged( AbstractDiagram* ) ) );
75  connect( this, SIGNAL( attributesModelAboutToChange( AttributesModel*, AttributesModel* ) ),
76  this, SLOT( connectAttributesModel( AttributesModel* ) ) );
77 
78  if ( d->plane ) {
79  connect( d->plane, SIGNAL( viewportCoordinateSystemChanged() ),
80  this, SIGNAL( viewportCoordinateSystemChanged() ) );
81  }
82 }
83 
85 {
86  if ( !d->axesList.contains( axis ) ) {
87  d->axesList.append( axis );
88  axis->createObserver( this );
89  layoutPlanes();
90  }
91 }
92 
94 {
95  const int idx = d->axesList.indexOf( axis );
96  if ( idx != -1 )
97  d->axesList.takeAt( idx );
98  axis->deleteObserver( this );
99  axis->setParentWidget( 0 );
100  layoutPlanes();
101 }
102 
104 {
105  return d->axesList;
106 }
107 
109 {
111  if ( plane ) {
112  plane->layoutPlanes();
113  }
114 }
115 
117 {
118  if ( coordinatePlane() ) {
119  disconnect( attributesModel(), SIGNAL( rowsRemoved( const QModelIndex&, int, int ) ),
120  coordinatePlane(), SLOT( relayout() ) );
121  disconnect( attributesModel(), SIGNAL( rowsInserted( const QModelIndex&, int, int ) ),
122  coordinatePlane(), SLOT( relayout() ) );
123  disconnect( attributesModel(), SIGNAL( columnsRemoved( const QModelIndex&, int, int ) ),
124  coordinatePlane(), SLOT( relayout() ) );
125  disconnect( attributesModel(), SIGNAL( columnsInserted( const QModelIndex&, int, int ) ),
126  coordinatePlane(), SLOT( relayout() ) );
127  disconnect( coordinatePlane() );
128  }
129 
131  if ( plane ) {
132  // Readjust the layout when the dataset count changes
133  connect( attributesModel(), SIGNAL( rowsRemoved( const QModelIndex&, int, int ) ),
134  plane, SLOT( relayout() ) );
135  connect( attributesModel(), SIGNAL( rowsInserted( const QModelIndex&, int, int ) ),
136  plane, SLOT( relayout() ) );
137  connect( attributesModel(), SIGNAL( columnsRemoved( const QModelIndex&, int, int ) ),
138  plane, SLOT( relayout() ) );
139  connect( attributesModel(), SIGNAL( columnsInserted( const QModelIndex&, int, int ) ),
140  plane, SLOT( relayout() ) );
141  connect( plane, SIGNAL( viewportCoordinateSystemChanged() ),
142  this, SIGNAL( viewportCoordinateSystemChanged() ) );
143  connect( plane, SIGNAL( viewportCoordinateSystemChanged() ), this, SLOT( update() ) );
144  }
145 }
146 
148 {
149  d->referenceDiagram = diagram;
150  d->referenceDiagramOffset = offset;
151 }
152 
154 {
155  return d->referenceDiagram;
156 }
157 
159 {
160  return d->referenceDiagramOffset;
161 }
162 
163 void AbstractCartesianDiagram::setRootIndex( const QModelIndex& index )
164 {
165  d->compressor.setRootIndex( attributesModel()->mapFromSource( index ) );
167 }
168 
169 void AbstractCartesianDiagram::setModel( QAbstractItemModel* m )
170 {
171  if ( m == model() ) {
172  return;
173  }
175 }
176 
178 {
179  if ( model == attributesModel() ) {
180  return;
181  }
183 }
184 
186 {
187  // The compressor must receive model signals before the diagram because the diagram will ask the
188  // compressor for data upon receiving dataChanged() et al. from the model, at which point the
189  // compressor must be up to date already.
190  // Starting from Qt 4.6, the invocation order of slots is guaranteed to be equal to connection
191  // order (and probably was before).
192  // This is our opportunity to connect to the AttributesModel before the AbstractDiagram does.
193 
194  // ### A better design would be to properly recognize that the compressor is the real data interface
195  // for Cartesian diagrams and make diagrams listen to updates from the *compressor*, not the model.
196  // However, this would change the outside interface of AbstractCartesianDiagram which would be bad.
197  // So we're stuck with the complication of this slot and the corresponding signal.
198  d->compressor.setModel( newModel );
199 }
Base class common for all coordinate planes, CartesianCoordinatePlane, PolarCoordinatePlane, TernaryCoordinatePlane.
virtual void setReferenceDiagram(AbstractCartesianDiagram *diagram, const QPointF &offset=QPointF())
Makes this diagram use another diagram diagram as reference diagram with relative offset offset...
virtual void addAxis(CartesianAxis *axis)
Add the axis to the diagram.
void deleteObserver(AbstractDiagram *diagram)
void setAttributesModel(AttributesModel *model) override
Associate an AttributesModel with this diagram.
void layoutPlanes()
Calling layoutPlanes() on the plane triggers the global KDChart::Chart::slotLayoutPlanes() ...
virtual AttributesModel * attributesModel() const
Returns the AttributesModel, that is used by this diagram.
AbstractCoordinatePlane * coordinatePlane() const
The coordinate plane associated with the diagram.
virtual void setParentWidget(QWidget *widget)
Inform the item about its widget: This enables the item, to trigger that widget&#39;s update...
virtual void layoutPlanes()
Triggers layouting of all coordinate planes on the current chart.
void createObserver(AbstractDiagram *diagram)
void setRootIndex(const QModelIndex &idx) override
Set the root index in the model, where the diagram starts referencing data for display.
AbstractDiagram defines the interface for diagram classes.
void setCoordinatePlane(AbstractCoordinatePlane *plane) override
[reimplemented]
The class for cartesian axes.
void setRootIndex(const QModelIndex &index) override
A proxy model used for decorating data with attributes.
virtual AbstractCartesianDiagram * referenceDiagram() const
virtual KDChart::CartesianAxisList axes() const
virtual void setAttributesModel(AttributesModel *model)
Associate an AttributesModel with this diagram.
virtual void setCoordinatePlane(AbstractCoordinatePlane *plane)
Set the coordinate plane associated with the diagram.
bool compare(const AbstractAxis *other) const
Returns true if both axes have the same settings.
void setModel(QAbstractItemModel *model) override
void setModel(QAbstractItemModel *model) override
Associate a model with the diagram.
Base class for diagrams based on a cartesian coordianate system.
AbstractCartesianDiagram(QWidget *parent=0, CartesianCoordinatePlane *plane=0)
void viewportCoordinateSystemChanged()
Emitted upon change of the view coordinate system.
void layoutChanged(AbstractDiagram *)
Diagrams are supposed to emit this signal, when the layout of one of their element changes...
Class only listed here to document inheritance of some KDChart classes.
void attributesModelAboutToChange(AttributesModel *newModel, AttributesModel *oldModel)
This signal is emitted just before the new attributes model is connected internally.
virtual void takeAxis(CartesianAxis *axis)
Removes the axis from the diagram, without deleting it.
bool compare(const AbstractCartesianDiagram *other) const
Returns true if both diagrams have the same settings.

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/