kdganttconstraintproxy.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL included.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #include "kdganttconstraintproxy.h"
00024 #include "kdganttconstraintmodel.h"
00025 
00026 #include <QAbstractProxyModel>
00027 
00028 using namespace KDGantt;
00029 
00034 ConstraintProxy::ConstraintProxy( QObject* parent )
00035     : QObject( parent )
00036 {
00037 }
00038 
00039 ConstraintProxy::~ConstraintProxy()
00040 {
00041 }
00042 
00043 void ConstraintProxy::setSourceModel( ConstraintModel* src )
00044 {
00045     if ( m_source ) disconnect( m_source );
00046     m_source = src;
00047 
00048     copyFromSource();
00049 
00050     connect( m_source, SIGNAL( constraintAdded( const KDGantt::Constraint& ) ),
00051              this, SLOT( slotSourceConstraintAdded( const KDGantt::Constraint& ) ) );
00052     connect( m_source, SIGNAL( constraintRemoved( const KDGantt::Constraint& ) ),
00053              this, SLOT( slotSourceConstraintRemoved( const KDGantt::Constraint& ) ) );
00054 }
00055 
00056 void ConstraintProxy::setDestinationModel( ConstraintModel* dest )
00057 {
00058     if ( m_destination ) disconnect( m_destination );
00059     m_destination = dest;
00060 
00061     copyFromSource();
00062 
00063     connect( m_destination, SIGNAL( constraintAdded( const KDGantt::Constraint& ) ),
00064              this, SLOT( slotDestinationConstraintAdded( const KDGantt::Constraint& ) ) );
00065     connect( m_destination, SIGNAL( constraintRemoved( const KDGantt::Constraint& ) ),
00066              this, SLOT( slotDestinationConstraintRemoved( const KDGantt::Constraint& ) ) );
00067 }
00068 
00069 void ConstraintProxy::setProxyModel( QAbstractProxyModel* proxy )
00070 {
00071     if ( m_proxy == proxy ) return;
00072     if ( m_proxy  ) disconnect( m_proxy );
00073     m_proxy = proxy;
00074     if ( m_proxy ) {
00075         connect( m_proxy, SIGNAL( layoutChanged() ), this, SLOT( slotLayoutChanged() ) );
00076         connect( m_proxy, SIGNAL( modelReset() ), this, SLOT( slotLayoutChanged() ) );
00077     }
00078 }
00079 
00080 ConstraintModel* ConstraintProxy::sourceModel() const { return m_source; }
00081 ConstraintModel* ConstraintProxy::destinationModel() const { return m_destination; }
00082 QAbstractProxyModel* ConstraintProxy::proxyModel() const { return m_proxy; }
00083 
00084 
00085 void ConstraintProxy::copyFromSource()
00086 {
00087     if ( m_destination ) {
00088         m_destination->clear();
00089         if ( !m_source ) return;
00090         QList<Constraint> lst = m_source->constraints();
00091         Q_FOREACH( const Constraint& c, lst )
00092         {
00093            Constraint temp( m_proxy->mapFromSource( c.startIndex() ), m_proxy->mapFromSource( c.endIndex() ) );
00094            temp.setData(Constraint::ValidConstraintPen, c.data(Constraint::ValidConstraintPen));
00095            temp.setData(Constraint::InvalidConstraintPen, c.data(Constraint::InvalidConstraintPen));
00096            m_destination->addConstraint( temp );
00097         }
00098     }
00099 }
00100 
00101 void ConstraintProxy::slotSourceConstraintAdded( const KDGantt::Constraint& c )
00102 {
00103     if ( m_destination )
00104     {
00105         Constraint temp( m_proxy->mapFromSource( c.startIndex() ), m_proxy->mapFromSource( c.endIndex() ), c.type(), c.relationType() );
00106         temp.setData(Constraint::ValidConstraintPen, c.data(Constraint::ValidConstraintPen));
00107         temp.setData(Constraint::InvalidConstraintPen, c.data(Constraint::InvalidConstraintPen));
00108         m_destination->addConstraint( temp );
00109     }
00110 }
00111 
00112 void ConstraintProxy::slotSourceConstraintRemoved( const KDGantt::Constraint& c )
00113 {
00114     if ( m_destination )
00115     {
00116         Constraint temp( m_proxy->mapFromSource( c.startIndex() ), m_proxy->mapFromSource( c.endIndex() ), c.type(), c.relationType() );
00117         temp.setData(Constraint::ValidConstraintPen, c.data(Constraint::ValidConstraintPen));
00118         temp.setData(Constraint::InvalidConstraintPen, c.data(Constraint::InvalidConstraintPen));
00119         m_destination->removeConstraint( temp );
00120     }
00121 }
00122 
00123 void ConstraintProxy::slotDestinationConstraintAdded( const KDGantt::Constraint& c )
00124 {
00125     if ( m_source )
00126     {
00127         Constraint temp( m_proxy->mapToSource( c.startIndex() ), m_proxy->mapToSource( c.endIndex() ), c.type(), c.relationType() );
00128         temp.setData(Constraint::ValidConstraintPen, c.data(Constraint::ValidConstraintPen));
00129         temp.setData(Constraint::InvalidConstraintPen, c.data(Constraint::InvalidConstraintPen));
00130         m_source->addConstraint( temp );
00131     }
00132 }
00133 
00134 void ConstraintProxy::slotDestinationConstraintRemoved( const KDGantt::Constraint& c )
00135 {
00136     if ( m_source )
00137     {
00138         Constraint temp( m_proxy->mapToSource( c.startIndex() ), m_proxy->mapToSource( c.endIndex() ), c.type(), c.relationType() );
00139         temp.setData(Constraint::ValidConstraintPen, c.data(Constraint::ValidConstraintPen));
00140         temp.setData(Constraint::InvalidConstraintPen, c.data(Constraint::InvalidConstraintPen));
00141         m_source->removeConstraint( temp );
00142     }
00143 }
00144 
00145 void ConstraintProxy::slotLayoutChanged()
00146 {
00147     copyFromSource();
00148 }
00149 
00150 #include "moc_kdganttconstraintproxy.cpp"