KChart

KChartDiagramObserver.cpp
1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include <KChartDiagramObserver.h>
21 
22 #include <KChartAbstractDiagram.h>
23 #include <KChartAttributesModel.h>
24 #include "KChartMath_p.h"
25 
26 #include <QDebug>
27 
28 using namespace KChart;
29 
31  : QObject( parent ), m_diagram( diagram )
32 {
33  if ( m_diagram ) {
34  connect( m_diagram, SIGNAL(destroyed(QObject*)), SLOT(slotDestroyed(QObject*)));
35  connect( m_diagram, SIGNAL(aboutToBeDestroyed()), SLOT(slotAboutToBeDestroyed()));
36  connect( m_diagram, SIGNAL(modelsChanged()), SLOT(slotModelsChanged()));
37  }
38  init();
39 }
40 
41 DiagramObserver::~DiagramObserver()
42 {
43 }
44 
45 const AbstractDiagram* DiagramObserver::diagram() const
46 {
47  return m_diagram;
48 }
49 
50 AbstractDiagram* DiagramObserver::diagram()
51 {
52  return m_diagram;
53 }
54 
55 
56 void DiagramObserver::init()
57 {
58  if ( !m_diagram )
59  return;
60 
61  if ( m_model )
62  disconnect(m_model);
63 
64  if ( m_attributesmodel )
65  disconnect(m_attributesmodel);
66 
67  const bool con = connect( m_diagram, SIGNAL(viewportCoordinateSystemChanged()), this, SLOT(slotDataChanged()) );
68  Q_ASSERT( con );
69  Q_UNUSED( con )
70  connect( m_diagram, SIGNAL(dataHidden()), SLOT(slotDataHidden()) );
71 
72  if ( m_diagram->model() ) {
73  connect( m_diagram->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
74  SLOT(slotDataChanged(QModelIndex,QModelIndex)));
75  connect( m_diagram->model(), SIGNAL(rowsInserted(QModelIndex,int,int)),
76  SLOT(slotDataChanged()));
77  connect( m_diagram->model(), SIGNAL(columnsInserted(QModelIndex,int,int)),
78  SLOT(slotDataChanged()));
79  connect( m_diagram->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
80  SLOT(slotDataChanged()));
81  connect( m_diagram->model(), SIGNAL(columnsRemoved(QModelIndex,int,int)),
82  SLOT(slotDataChanged()));
83  connect( m_diagram->model(), SIGNAL(modelReset()),
84  SLOT(slotDataChanged()));
85  connect( m_diagram->model(), SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
86  SLOT(slotHeaderDataChanged(Qt::Orientation,int,int)));
87  }
88 
89  if ( m_diagram->attributesModel() )
90  connect( m_diagram->attributesModel(), SIGNAL(attributesChanged(QModelIndex,QModelIndex)),
91  SLOT(slotAttributesChanged(QModelIndex,QModelIndex)));
92  m_model = m_diagram->model();
93  m_attributesmodel = m_diagram->attributesModel();
94 }
95 
96 
97 void DiagramObserver::slotDestroyed(QObject*)
98 {
99  //qDebug() << this << "emits signal\n"
100  // " emit diagramDestroyed(" << m_diagram << ")";
101  AbstractDiagram* diag = m_diagram;
102  disconnect( m_diagram, nullptr, this, nullptr);
103  m_diagram = nullptr;
104  emit diagramDestroyed( diag );
105 }
106 
107 void DiagramObserver::slotAboutToBeDestroyed()
108 {
109  emit diagramAboutToBeDestroyed( m_diagram );
110 }
111 
112 void DiagramObserver::slotModelsChanged()
113 {
114  init();
115  slotDataChanged();
116  slotAttributesChanged();
117 }
118 
119 void DiagramObserver::slotHeaderDataChanged(Qt::Orientation,int,int)
120 {
121  //qDebug() << "DiagramObserver::slotHeaderDataChanged()";
122  emit diagramDataChanged( m_diagram );
123 }
124 
125 void DiagramObserver::slotDataChanged(QModelIndex,QModelIndex)
126 {
127  slotDataChanged();
128 }
129 
130 void DiagramObserver::slotDataChanged()
131 {
132  //qDebug() << "DiagramObserver::slotDataChanged()";
133  emit diagramDataChanged( m_diagram );
134 }
135 
136 void DiagramObserver::slotDataHidden()
137 {
138  //qDebug() << "DiagramObserver::slotDataHidden()";
139  emit diagramDataHidden( m_diagram );
140 }
141 
142 void DiagramObserver::slotAttributesChanged(QModelIndex,QModelIndex)
143 {
144  slotAttributesChanged();
145 }
146 
147 void DiagramObserver::slotAttributesChanged()
148 {
149  //qDebug() << "DiagramObserver::slotAttributesChanged()";
150  emit diagramAttributesChanged( m_diagram );
151 }
152 
AbstractDiagram defines the interface for diagram classes.
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
void diagramDestroyed(AbstractDiagram *diagram)
This signal is emitted immediately before the diagram is being destroyed.
void diagramAttributesChanged(AbstractDiagram *diagram)
This signal is emitted whenever the attributes of the diagram change.
virtual AttributesModel * attributesModel() const
Returns the AttributesModel, that is used by this diagram.
Class only listed here to document inheritance of some KChart classes.
DiagramObserver(AbstractDiagram *diagram, QObject *parent=nullptr)
Constructs a new observer observing the given diagram.
void diagramAboutToBeDestroyed(AbstractDiagram *diagram)
Emitted when a diagram is being destroyed, but before its data is invalidated.
Orientation
QAbstractItemModel * model() const const
void diagramDataHidden(AbstractDiagram *diagram)
This signal is emitted whenever any of the data of the diagram was set (un)hidden.
Global namespace.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void destroyed(QObject *obj)
void diagramDataChanged(AbstractDiagram *diagram)
This signal is emitted whenever the data of the diagram changes.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Sep 17 2020 22:36:39 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.