KChart

KChartAbstractCartesianDiagram.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 "KChartAbstractCartesianDiagram.h"
10 #include "KChartAbstractCartesianDiagram_p.h"
11 
12 #include "KChartMath_p.h"
13 
14 
15 using namespace KChart;
16 
17 AbstractCartesianDiagram::Private::Private()
18  : referenceDiagram( nullptr )
19 {
20 }
21 
22 AbstractCartesianDiagram::Private::~Private()
23 {
24 }
25 
27 {
28  if ( other == this ) return true;
29  if ( ! other ) {
30  return false;
31  }
32  return // compare the base class
33  ( static_cast<const AbstractDiagram*>(this)->compare( other ) ) &&
34  // compare own properties
35  (referenceDiagram() == other->referenceDiagram()) &&
36  ((! referenceDiagram()) || (referenceDiagramOffset() == other->referenceDiagramOffset()));
37 }
38 
39 
40 #define d d_func()
41 
42 AbstractCartesianDiagram::AbstractCartesianDiagram ( QWidget* parent, CartesianCoordinatePlane* plane )
43  : AbstractDiagram ( new Private(), parent, plane )
44 {
45  init();
46 }
47 
48 KChart::AbstractCartesianDiagram::~AbstractCartesianDiagram()
49 {
50  for ( CartesianAxis* axis : qAsConst(d->axesList) ) {
51  axis->deleteObserver( this );
52  }
53  d->axesList.clear();
54 }
55 
56 void AbstractCartesianDiagram::init()
57 {
58  d->compressor.setModel( attributesModel() );
59  // NOTE: These signals are disconnected elsewhere (e.g: Plotter) so be careful to make any changes.
60  connect( this, SIGNAL(layoutChanged(KChart::AbstractDiagram*)),
61  &d->compressor, SLOT(slotDiagramLayoutChanged(KChart::AbstractDiagram*)) );
62  connect( this, SIGNAL(attributesModelAboutToChange(KChart::AttributesModel*,KChart::AttributesModel*)),
63  this, SLOT(connectAttributesModel(KChart::AttributesModel*)) );
64 
65  if ( d->plane ) {
66  connect( d->plane, SIGNAL(viewportCoordinateSystemChanged()),
67  this, SIGNAL(viewportCoordinateSystemChanged()) );
68  }
69 }
70 
72 {
73  if ( !d->axesList.contains( axis ) ) {
74  d->axesList.append( axis );
75  axis->createObserver( this );
76  layoutPlanes();
77  }
78 }
79 
81 {
82  const int idx = d->axesList.indexOf( axis );
83  if ( idx != -1 )
84  d->axesList.takeAt( idx );
85  axis->deleteObserver( this );
86  axis->setParentWidget( nullptr );
87  layoutPlanes();
88 }
89 
91 {
92  return d->axesList;
93 }
94 
96 {
98  if ( plane ) {
99  plane->layoutPlanes();
100  }
101 }
102 
104 {
105  if ( coordinatePlane() ) {
106  disconnect( attributesModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
107  coordinatePlane(), SLOT(relayout()) );
108  disconnect( attributesModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
109  coordinatePlane(), SLOT(relayout()) );
110  disconnect( attributesModel(), SIGNAL(columnsRemoved(QModelIndex,int,int)),
111  coordinatePlane(), SLOT(relayout()) );
112  disconnect( attributesModel(), SIGNAL(columnsInserted(QModelIndex,int,int)),
113  coordinatePlane(), SLOT(relayout()) );
115  }
116 
118  if ( plane ) {
119  // Readjust the layout when the dataset count changes
120  connect( attributesModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
121  plane, SLOT(relayout()) );
122  connect( attributesModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
123  plane, SLOT(relayout()) );
124  connect( attributesModel(), SIGNAL(columnsRemoved(QModelIndex,int,int)),
125  plane, SLOT(relayout()) );
126  connect( attributesModel(), SIGNAL(columnsInserted(QModelIndex,int,int)),
127  plane, SLOT(relayout()) );
128  connect( plane, SIGNAL(viewportCoordinateSystemChanged()),
129  this, SIGNAL(viewportCoordinateSystemChanged()) );
130  connect( plane, SIGNAL(viewportCoordinateSystemChanged()), this, SLOT(update()) );
131  }
132 }
133 
135 {
136  d->referenceDiagram = diagram;
137  d->referenceDiagramOffset = offset;
138 }
139 
141 {
142  return d->referenceDiagram;
143 }
144 
146 {
147  return d->referenceDiagramOffset;
148 }
149 
150 void AbstractCartesianDiagram::setRootIndex( const QModelIndex& index )
151 {
152  d->compressor.setRootIndex( attributesModel()->mapFromSource( index ) );
154 }
155 
156 void AbstractCartesianDiagram::setModel( QAbstractItemModel* m )
157 {
158  if ( m == model() ) {
159  return;
160  }
162 }
163 
165 {
166  if ( model == attributesModel() ) {
167  return;
168  }
170 }
171 
172 void AbstractCartesianDiagram::connectAttributesModel( AttributesModel* newModel )
173 {
174  // The compressor must receive model signals before the diagram because the diagram will ask the
175  // compressor for data upon receiving dataChanged() et al. from the model, at which point the
176  // compressor must be up to date already.
177  // Starting from Qt 4.6, the invocation order of slots is guaranteed to be equal to connection
178  // order (and probably was before).
179  // This is our opportunity to connect to the AttributesModel before the AbstractDiagram does.
180 
181  // ### A better design would be to properly recognize that the compressor is the real data interface
182  // for Cartesian diagrams and make diagrams listen to updates from the *compressor*, not the model.
183  // However, this would change the outside interface of AbstractCartesianDiagram which would be bad.
184  // So we're stuck with the complication of this slot and the corresponding signal.
185  d->compressor.setModel( newModel );
186 }
Class only listed here to document inheritance of some KChart classes.
void createObserver(AbstractDiagram *diagram)
AbstractDiagram defines the interface for diagram classes.
virtual void addAxis(CartesianAxis *axis)
Add the axis to the diagram.
bool compare(const AbstractAxis *other) const
Returns true if both axes have the same settings.
bool compare(const AbstractCartesianDiagram *other) const
Returns true if both diagrams have the same settings.
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
The class for cartesian axes.
void layoutPlanes()
Calling layoutPlanes() on the plane triggers the global KChart::Chart::slotLayoutPlanes() ...
virtual void setAttributesModel(AttributesModel *model)
Associate an AttributesModel with this diagram.
virtual void setReferenceDiagram(AbstractCartesianDiagram *diagram, const QPointF &offset=QPointF())
Makes this diagram use another diagram diagram as reference diagram with relative offset offset...
Base class for diagrams based on a cartesian coordianate system.
Base class common for all coordinate planes, CartesianCoordinatePlane, PolarCoordinatePlane, TernaryCoordinatePlane.
virtual void setParentWidget(QWidget *widget)
Inform the item about its widget: This enables the item, to trigger that widget&#39;s update...
A proxy model used for decorating data with attributes.
virtual KChart::CartesianAxisList axes() const
virtual void setCoordinatePlane(AbstractCoordinatePlane *plane)
Set the coordinate plane associated with the diagram.
void setAttributesModel(AttributesModel *model) override
Associate an AttributesModel with this diagram.
void setCoordinatePlane(AbstractCoordinatePlane *plane) override
QCA_EXPORT void init()
virtual void takeAxis(CartesianAxis *axis)
Removes the axis from the diagram, without deleting it.
void setRootIndex(const QModelIndex &idx) override
Set the root index in the model, where the diagram starts referencing data for display.
void deleteObserver(AbstractDiagram *diagram)
virtual void layoutPlanes()
Triggers layouting of all coordinate planes on the current chart.
void setModel(QAbstractItemModel *model) override
Associate a model with the diagram.
Global namespace.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
virtual AbstractCartesianDiagram * referenceDiagram() const
const AbstractCoordinatePlane * coordinatePlane() const
Convenience function, returns the coordinate plane, in which this axis is used.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jul 29 2021 22:36:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.