File indexing completed on 2024-05-12 15:54:15

0001 /*
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include <KChartDiagramObserver.h>
0021 
0022 #include <KChartAbstractDiagram.h>
0023 #include <KChartAttributesModel.h>
0024 #include "KChartMath_p.h"
0025 
0026 #include <QDebug>
0027 
0028 using namespace KChart;
0029 
0030 DiagramObserver::DiagramObserver( AbstractDiagram * diagram, QObject* parent )
0031     : QObject( parent ), m_diagram( diagram )
0032 {
0033     if ( m_diagram ) {
0034         connect( m_diagram, SIGNAL(destroyed(QObject*)), SLOT(slotDestroyed(QObject*)));
0035         connect( m_diagram, SIGNAL(aboutToBeDestroyed()), SLOT(slotAboutToBeDestroyed()));
0036         connect( m_diagram, SIGNAL(modelsChanged()), SLOT(slotModelsChanged()));
0037     }
0038     init();
0039 }
0040 
0041 DiagramObserver::~DiagramObserver()
0042 {
0043 }
0044 
0045 const AbstractDiagram* DiagramObserver::diagram() const
0046 {
0047     return m_diagram;
0048 }
0049 
0050 AbstractDiagram* DiagramObserver::diagram()
0051 {
0052     return m_diagram;
0053 }
0054 
0055 
0056 void DiagramObserver::init()
0057 {
0058     if ( !m_diagram )
0059         return;
0060 
0061     if ( m_model )
0062         disconnect(m_model);
0063 
0064     if ( m_attributesmodel )
0065         disconnect(m_attributesmodel);
0066 
0067     const bool con = connect( m_diagram, SIGNAL(viewportCoordinateSystemChanged()), this, SLOT(slotDataChanged()) );
0068     Q_ASSERT( con );
0069     Q_UNUSED( con )
0070     connect( m_diagram, SIGNAL(dataHidden()), SLOT(slotDataHidden()) );
0071 
0072     if ( m_diagram->model() ) {
0073         connect( m_diagram->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
0074                  SLOT(slotDataChanged(QModelIndex,QModelIndex)));
0075         connect( m_diagram->model(), SIGNAL(rowsInserted(QModelIndex,int,int)),
0076                  SLOT(slotDataChanged()));
0077         connect( m_diagram->model(), SIGNAL(columnsInserted(QModelIndex,int,int)),
0078                  SLOT(slotDataChanged()));
0079         connect( m_diagram->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
0080                  SLOT(slotDataChanged()));
0081         connect( m_diagram->model(), SIGNAL(columnsRemoved(QModelIndex,int,int)),
0082                  SLOT(slotDataChanged()));
0083         connect( m_diagram->model(), SIGNAL(modelReset()),
0084                  SLOT(slotDataChanged()));
0085         connect( m_diagram->model(), SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
0086                  SLOT(slotHeaderDataChanged(Qt::Orientation,int,int)));
0087     }
0088 
0089     if ( m_diagram->attributesModel() )
0090         connect( m_diagram->attributesModel(), SIGNAL(attributesChanged(QModelIndex,QModelIndex)),
0091                  SLOT(slotAttributesChanged(QModelIndex,QModelIndex)));
0092     m_model = m_diagram->model();
0093     m_attributesmodel = m_diagram->attributesModel();
0094 }
0095 
0096 
0097 void DiagramObserver::slotDestroyed(QObject*)
0098 {
0099     //qDebug() << this << "emits signal\n"
0100     //        "    Q_EMIT diagramDestroyed(" <<  m_diagram << ")";
0101     AbstractDiagram* diag = m_diagram;
0102     disconnect( m_diagram, nullptr, this, nullptr);
0103     m_diagram = nullptr;
0104     Q_EMIT diagramDestroyed( diag );
0105 }
0106 
0107 void DiagramObserver::slotAboutToBeDestroyed()
0108 {
0109     Q_EMIT diagramAboutToBeDestroyed( m_diagram );
0110 }
0111 
0112 void DiagramObserver::slotModelsChanged()
0113 {
0114     init();
0115     slotDataChanged();
0116     slotAttributesChanged();
0117 }
0118 
0119 void DiagramObserver::slotHeaderDataChanged(Qt::Orientation,int,int)
0120 {
0121     //qDebug() << "DiagramObserver::slotHeaderDataChanged()";
0122     Q_EMIT diagramDataChanged( m_diagram );
0123 }
0124 
0125 void DiagramObserver::slotDataChanged(QModelIndex,QModelIndex)
0126 {
0127     slotDataChanged();
0128 }
0129 
0130 void DiagramObserver::slotDataChanged()
0131 {
0132     //qDebug() << "DiagramObserver::slotDataChanged()";
0133     Q_EMIT diagramDataChanged( m_diagram );
0134 }
0135 
0136 void DiagramObserver::slotDataHidden()
0137 {
0138     //qDebug() << "DiagramObserver::slotDataHidden()";
0139     Q_EMIT diagramDataHidden( m_diagram );
0140 }
0141 
0142 void DiagramObserver::slotAttributesChanged(QModelIndex,QModelIndex)
0143 {
0144     slotAttributesChanged();
0145 }
0146 
0147 void DiagramObserver::slotAttributesChanged()
0148 {
0149     //qDebug() << "DiagramObserver::slotAttributesChanged()";
0150     Q_EMIT diagramAttributesChanged( m_diagram );
0151 }
0152