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

0001 /*
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KGantt 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 "kganttconstraintproxy.h"
0021 #include "kganttconstraintmodel.h"
0022 
0023 #include <QAbstractProxyModel>
0024 
0025 using namespace KGantt;
0026 
0027 
0028 
0029 ConstraintProxy::ConstraintProxy( QObject* parent )
0030     : QObject( parent )
0031 {
0032 }
0033 
0034 ConstraintProxy::~ConstraintProxy()
0035 {
0036 }
0037 
0038 void ConstraintProxy::setSourceModel( ConstraintModel* src )
0039 {
0040     if ( m_source ) m_source->disconnect( this );
0041     m_source = src;
0042 
0043     copyFromSource();
0044 
0045     connect( m_source, SIGNAL(constraintAdded(KGantt::Constraint)),
0046              this, SLOT(slotSourceConstraintAdded(KGantt::Constraint)) );
0047     connect( m_source, SIGNAL(constraintRemoved(KGantt::Constraint)),
0048              this, SLOT(slotSourceConstraintRemoved(KGantt::Constraint)) );
0049 }
0050 
0051 void ConstraintProxy::setDestinationModel( ConstraintModel* dest )
0052 {
0053     if ( m_destination ) m_destination->disconnect( this );
0054     m_destination = dest;
0055 
0056     copyFromSource();
0057 
0058     connect( m_destination, SIGNAL(constraintAdded(KGantt::Constraint)),
0059              this, SLOT(slotDestinationConstraintAdded(KGantt::Constraint)) );
0060     connect( m_destination, SIGNAL(constraintRemoved(KGantt::Constraint)),
0061              this, SLOT(slotDestinationConstraintRemoved(KGantt::Constraint)) );
0062 }
0063 
0064 void ConstraintProxy::setProxyModel( QAbstractProxyModel* proxy )
0065 {
0066     if ( m_proxy == proxy ) return;
0067     if ( m_proxy ) m_proxy->disconnect( this );
0068     m_proxy = proxy;
0069     if ( m_proxy ) {
0070         connect( m_proxy, SIGNAL(layoutChanged()), this, SLOT(slotLayoutChanged()) );
0071         connect( m_proxy, SIGNAL(modelReset()), this, SLOT(slotLayoutChanged()) );
0072     }
0073 }
0074 
0075 ConstraintModel* ConstraintProxy::sourceModel() const { return m_source; }
0076 ConstraintModel* ConstraintProxy::destinationModel() const { return m_destination; }
0077 QAbstractProxyModel* ConstraintProxy::proxyModel() const { return m_proxy; }
0078 
0079 
0080 void ConstraintProxy::copyFromSource()
0081 {
0082     if ( m_destination ) {
0083         m_destination->clear();
0084         if ( !m_source ) return;
0085         const QList<Constraint> lst = m_source->constraints();
0086         for( const Constraint& c : lst )
0087         {
0088            Constraint temp( m_proxy->mapFromSource( c.startIndex() ), m_proxy->mapFromSource( c.endIndex() ),
0089                             c.type(), c.relationType(), c.dataMap() );
0090            m_destination->addConstraint( temp );
0091         }
0092     }
0093 }
0094 
0095 void ConstraintProxy::slotSourceConstraintAdded( const KGantt::Constraint& c )
0096 {
0097     if ( m_destination )
0098     {
0099         Constraint temp( m_proxy->mapFromSource( c.startIndex() ), m_proxy->mapFromSource( c.endIndex() ),
0100                          c.type(), c.relationType(), c.dataMap() );
0101         m_destination->addConstraint( temp );
0102     }
0103 }
0104 
0105 void ConstraintProxy::slotSourceConstraintRemoved( const KGantt::Constraint& c )
0106 {
0107     if ( m_destination )
0108     {
0109         Constraint temp( m_proxy->mapFromSource( c.startIndex() ), m_proxy->mapFromSource( c.endIndex() ),
0110                          c.type(), c.relationType(), c.dataMap() );
0111         m_destination->removeConstraint( temp );
0112     }
0113 }
0114 
0115 void ConstraintProxy::slotDestinationConstraintAdded( const KGantt::Constraint& c )
0116 {
0117     if ( m_source )
0118     {
0119         Constraint temp( m_proxy->mapToSource( c.startIndex() ), m_proxy->mapToSource( c.endIndex() ),
0120                          c.type(), c.relationType(), c.dataMap() );
0121         m_source->addConstraint( temp );
0122     }
0123 }
0124 
0125 void ConstraintProxy::slotDestinationConstraintRemoved( const KGantt::Constraint& c )
0126 {
0127     if ( m_source )
0128     {
0129         Constraint temp( m_proxy->mapToSource( c.startIndex() ), m_proxy->mapToSource( c.endIndex() ),
0130                          c.type(), c.relationType(), c.dataMap() );
0131         m_source->removeConstraint( temp );
0132     }
0133 }
0134 
0135 void ConstraintProxy::slotLayoutChanged()
0136 {
0137     copyFromSource();
0138 }
0139 
0140 #include "moc_kganttconstraintproxy.cpp"