KD Chart 2  [rev.2.8]
KDChartAbstractCartesianDiagram.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2021 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()) &&
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 {
110  AbstractCoordinatePlane* plane = coordinatePlane();
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 }
QWidget
Class only listed here to document inheritance of some KDChart classes.
KDChart::AbstractCartesianDiagram::~AbstractCartesianDiagram
~AbstractCartesianDiagram() override
Definition: KDChartAbstractCartesianDiagram.cpp:62
QList
Definition: KDChartPosition.h:36
KDChart::AbstractDiagram::setRootIndex
void setRootIndex(const QModelIndex &idx) override
Set the root index in the model, where the diagram starts referencing data for display.
Definition: KDChartAbstractDiagram.cpp:208
KDChart::AbstractCartesianDiagram::addAxis
virtual void addAxis(CartesianAxis *axis)
Add the axis to the diagram.
Definition: KDChartAbstractCartesianDiagram.cpp:84
KDChart::AbstractCartesianDiagram::connectAttributesModel
void connectAttributesModel(AttributesModel *)
Definition: KDChartAbstractCartesianDiagram.cpp:185
KDChart::AbstractCoordinatePlane::layoutPlanes
void layoutPlanes()
Calling layoutPlanes() on the plane triggers the global KDChart::Chart::slotLayoutPlanes()
Definition: KDChartAbstractCoordinatePlane.cpp:265
KDChart::AbstractCartesianDiagram::setReferenceDiagram
virtual void setReferenceDiagram(AbstractCartesianDiagram *diagram, const QPointF &offset=QPointF())
Makes this diagram use another diagram diagram as reference diagram with relative offset offset.
Definition: KDChartAbstractCartesianDiagram.cpp:147
KDChart::CartesianAxis
The class for cartesian axes.
Definition: KDChartCartesianAxis.h:43
KDChart::AbstractDiagram::layoutChanged
void layoutChanged(AbstractDiagram *)
Diagrams are supposed to emit this signal, when the layout of one of their element changes.
KDChart
Definition: KDChartAbstractCartesianDiagram.h:30
KDChart::AbstractDiagram::viewportCoordinateSystemChanged
void viewportCoordinateSystemChanged()
Emitted upon change of the view coordinate system.
KDChart::AbstractAxis::deleteObserver
void deleteObserver(AbstractDiagram *diagram)
Definition: KDChartAbstractAxis.cpp:173
KDChart::AbstractCartesianDiagram::referenceDiagram
virtual AbstractCartesianDiagram * referenceDiagram() const
Definition: KDChartAbstractCartesianDiagram.cpp:153
KDChart::AbstractLayoutItem::setParentWidget
virtual void setParentWidget(QWidget *widget)
Inform the item about its widget: This enables the item, to trigger that widget's update,...
Definition: KDChartLayoutItems.cpp:59
KDChart::AbstractCartesianDiagram::layoutPlanes
virtual void layoutPlanes()
Triggers layouting of all coordinate planes on the current chart.
Definition: KDChartAbstractCartesianDiagram.cpp:108
KDChart::AbstractCartesianDiagram::setCoordinatePlane
void setCoordinatePlane(AbstractCoordinatePlane *plane) override
[reimplemented]
Definition: KDChartAbstractCartesianDiagram.cpp:116
KDChart::AbstractCartesianDiagram
Base class for diagrams based on a cartesian coordianate system.
Definition: KDChartAbstractCartesianDiagram.h:42
KDChartAbstractCartesianDiagram.h
KDChart::AbstractCartesianDiagram::setModel
void setModel(QAbstractItemModel *model) override
Definition: KDChartAbstractCartesianDiagram.cpp:169
KDChart::AttributesModel
A proxy model used for decorating data with attributes.
Definition: KDChartAttributesModel.h:47
KDChart::AbstractDiagram::attributesModel
virtual AttributesModel * attributesModel() const
Returns the AttributesModel, that is used by this diagram.
Definition: KDChartAbstractDiagram.cpp:196
KDChart::AbstractDiagram::setModel
void setModel(QAbstractItemModel *model) override
Associate a model with the diagram.
Definition: KDChartAbstractDiagram.cpp:131
KDChart::AbstractDiagram::attributesModelAboutToChange
void attributesModelAboutToChange(AttributesModel *newModel, AttributesModel *oldModel)
This signal is emitted just before the new attributes model is connected internally.
KDChart::AbstractCartesianDiagram::compare
bool compare(const AbstractCartesianDiagram *other) const
Returns true if both diagrams have the same settings.
Definition: KDChartAbstractCartesianDiagram.cpp:40
KDChart::AbstractDiagram
AbstractDiagram defines the interface for diagram classes.
Definition: KDChartAbstractDiagram.h:51
KDChart::AbstractCartesianDiagram::setRootIndex
void setRootIndex(const QModelIndex &index) override
Definition: KDChartAbstractCartesianDiagram.cpp:163
KDChart::AbstractAxis::createObserver
void createObserver(AbstractDiagram *diagram)
Definition: KDChartAbstractAxis.cpp:168
KDChart::AbstractDiagram::setAttributesModel
virtual void setAttributesModel(AttributesModel *model)
Associate an AttributesModel with this diagram.
Definition: KDChartAbstractDiagram.cpp:171
KDChart::CartesianCoordinatePlane
Cartesian coordinate plane.
Definition: KDChartCartesianCoordinatePlane.h:40
KDChart::AbstractCartesianDiagram::AbstractCartesianDiagram
AbstractCartesianDiagram(QWidget *parent=0, CartesianCoordinatePlane *plane=0)
Definition: KDChartAbstractCartesianDiagram.cpp:56
KDChart::AbstractCartesianDiagram::axes
virtual KDChart::CartesianAxisList axes() const
Definition: KDChartAbstractCartesianDiagram.cpp:103
KDChart::AbstractDiagram::setCoordinatePlane
virtual void setCoordinatePlane(AbstractCoordinatePlane *plane)
Set the coordinate plane associated with the diagram.
Definition: KDChartAbstractDiagram.cpp:231
KDChart::AbstractCoordinatePlane
Base class common for all coordinate planes, CartesianCoordinatePlane, PolarCoordinatePlane,...
Definition: KDChartAbstractCoordinatePlane.h:45
KDChart::AbstractCartesianDiagram::takeAxis
virtual void takeAxis(CartesianAxis *axis)
Removes the axis from the diagram, without deleting it.
Definition: KDChartAbstractCartesianDiagram.cpp:93
KDChart::AbstractCartesianDiagram::setAttributesModel
void setAttributesModel(AttributesModel *model) override
Associate an AttributesModel with this diagram.
Definition: KDChartAbstractCartesianDiagram.cpp:177
d
#define d
Definition: KDChartAbstractCartesianDiagram.cpp:54
KDChart::AbstractCartesianDiagram::referenceDiagramOffset
virtual QPointF referenceDiagramOffset() const
Definition: KDChartAbstractCartesianDiagram.cpp:158

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/