KGantt

kganttconstraintproxy.cpp
1 /*
2  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KGantt library.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "kganttconstraintproxy.h"
10 #include "kganttconstraintmodel.h"
11 
12 #include <QAbstractProxyModel>
13 
14 using namespace KGantt;
15 
16 
17 
18 ConstraintProxy::ConstraintProxy( QObject* parent )
19  : QObject( parent )
20 {
21 }
22 
23 ConstraintProxy::~ConstraintProxy()
24 {
25 }
26 
27 void ConstraintProxy::setSourceModel( ConstraintModel* src )
28 {
29  if ( m_source ) m_source->disconnect( this );
30  m_source = src;
31 
32  copyFromSource();
33 
34  connect( m_source, SIGNAL(constraintAdded(KGantt::Constraint)),
35  this, SLOT(slotSourceConstraintAdded(KGantt::Constraint)) );
36  connect( m_source, SIGNAL(constraintRemoved(KGantt::Constraint)),
37  this, SLOT(slotSourceConstraintRemoved(KGantt::Constraint)) );
38 }
39 
40 void ConstraintProxy::setDestinationModel( ConstraintModel* dest )
41 {
42  if ( m_destination ) m_destination->disconnect( this );
43  m_destination = dest;
44 
45  copyFromSource();
46 
47  connect( m_destination, SIGNAL(constraintAdded(KGantt::Constraint)),
48  this, SLOT(slotDestinationConstraintAdded(KGantt::Constraint)) );
49  connect( m_destination, SIGNAL(constraintRemoved(KGantt::Constraint)),
50  this, SLOT(slotDestinationConstraintRemoved(KGantt::Constraint)) );
51 }
52 
53 void ConstraintProxy::setProxyModel( QAbstractProxyModel* proxy )
54 {
55  if ( m_proxy == proxy ) return;
56  if ( m_proxy ) m_proxy->disconnect( this );
57  m_proxy = proxy;
58  if ( m_proxy ) {
59  connect( m_proxy, SIGNAL(layoutChanged()), this, SLOT(slotLayoutChanged()) );
60  connect( m_proxy, SIGNAL(modelReset()), this, SLOT(slotLayoutChanged()) );
61  }
62 }
63 
64 ConstraintModel* ConstraintProxy::sourceModel() const { return m_source; }
65 ConstraintModel* ConstraintProxy::destinationModel() const { return m_destination; }
66 QAbstractProxyModel* ConstraintProxy::proxyModel() const { return m_proxy; }
67 
68 
69 void ConstraintProxy::copyFromSource()
70 {
71  if ( m_destination ) {
72  m_destination->clear();
73  if ( !m_source ) return;
74  const QList<Constraint> lst = m_source->constraints();
75  for( const Constraint& c : lst )
76  {
77  Constraint temp( m_proxy->mapFromSource( c.startIndex() ), m_proxy->mapFromSource( c.endIndex() ),
78  c.type(), c.relationType(), c.dataMap() );
79  m_destination->addConstraint( temp );
80  }
81  }
82 }
83 
84 void ConstraintProxy::slotSourceConstraintAdded( const KGantt::Constraint& c )
85 {
86  if ( m_destination )
87  {
88  Constraint temp( m_proxy->mapFromSource( c.startIndex() ), m_proxy->mapFromSource( c.endIndex() ),
89  c.type(), c.relationType(), c.dataMap() );
90  m_destination->addConstraint( temp );
91  }
92 }
93 
94 void ConstraintProxy::slotSourceConstraintRemoved( const KGantt::Constraint& c )
95 {
96  if ( m_destination )
97  {
98  Constraint temp( m_proxy->mapFromSource( c.startIndex() ), m_proxy->mapFromSource( c.endIndex() ),
99  c.type(), c.relationType(), c.dataMap() );
100  m_destination->removeConstraint( temp );
101  }
102 }
103 
104 void ConstraintProxy::slotDestinationConstraintAdded( const KGantt::Constraint& c )
105 {
106  if ( m_source )
107  {
108  Constraint temp( m_proxy->mapToSource( c.startIndex() ), m_proxy->mapToSource( c.endIndex() ),
109  c.type(), c.relationType(), c.dataMap() );
110  m_source->addConstraint( temp );
111  }
112 }
113 
114 void ConstraintProxy::slotDestinationConstraintRemoved( const KGantt::Constraint& c )
115 {
116  if ( m_source )
117  {
118  Constraint temp( m_proxy->mapToSource( c.startIndex() ), m_proxy->mapToSource( c.endIndex() ),
119  c.type(), c.relationType(), c.dataMap() );
120  m_source->removeConstraint( temp );
121  }
122 }
123 
124 void ConstraintProxy::slotLayoutChanged()
125 {
126  copyFromSource();
127 }
128 
129 #include "moc_kganttconstraintproxy.cpp"
RelationType relationType() const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
A class used to represent a dependency.
QModelIndex endIndex() const
Global namespace.
QModelIndex startIndex() const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QMap< int, QVariant > dataMap() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 22:32:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.