• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kdgantt2

  • sources
  • kde-4.12
  • kdepim
  • kdgantt2
kdganttconstraintmodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB. All rights reserved.
3  **
4  ** This file is part of the KD Gantt library.
5  **
6  ** This file may be distributed and/or modified under the terms of the
7  ** GNU General Public License version 2 as published by the Free Software
8  ** Foundation and appearing in the file LICENSE.GPL included in the
9  ** packaging of this file.
10  **
11  ** Licensees holding valid commercial KD Gantt licenses may use this file in
12  ** accordance with the KD Gantt Commercial License Agreement provided with
13  ** the Software.
14  **
15  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17  **
18  ** See http://www.kdab.net/kdgantt for
19  ** information about KD Gantt Commercial License Agreements.
20  **
21  ** Contact info@kdab.net if any conditions of this
22  ** licensing are not clear to you.
23  **
24  **********************************************************************/
25 #include "kdganttconstraintmodel.h"
26 #include "kdganttconstraintmodel_p.h"
27 
28 #include <QDebug>
29 
30 #include <cassert>
31 
32 using namespace KDGantt;
33 
42 ConstraintModel::Private::Private()
43 {
44 }
45 
46 void ConstraintModel::Private::addConstraintToIndex( const QModelIndex& idx, const Constraint& c )
47 {
48  IndexType::iterator it = indexMap.find(idx);
49  while (it != indexMap.end() && it.key() == idx) {
50  // Check if we already have this
51  if ( *it == c ) return;
52  ++it;
53  }
54 
55  indexMap.insert( idx, c );
56 }
57 
58 void ConstraintModel::Private::removeConstraintFromIndex( const QModelIndex& idx, const Constraint& c )
59 {
60  IndexType::iterator it = indexMap.find(idx);
61  while (it != indexMap.end() && it.key() == idx) {
62  if ( *it == c ) {
63  it =indexMap.erase( it );
64  } else {
65  ++it;
66  }
67  }
68 }
69 
72 ConstraintModel::ConstraintModel( QObject* parent )
73  : QObject( parent ), _d( new Private )
74 {
75  init();
76 }
77 
79 ConstraintModel::ConstraintModel( Private* d_ptr, QObject* parent )
80  : QObject( parent ), _d( d_ptr )
81 {
82  init();
83 }
84 
86 ConstraintModel::~ConstraintModel()
87 {
88  delete _d;
89 }
90 
91 #define d d_func()
92 
93 void ConstraintModel::init()
94 {
95 }
96 
101 void ConstraintModel::addConstraint( const Constraint& c )
102 {
103  //int size = d->constraints.size();
104  bool hasConstraint = d->constraints.contains( c );
105  //d->constraints.insert( c );
106  //if ( size != d->constraints.size() ) {
107  if ( !hasConstraint ) {
108  d->constraints.push_back( c );
109  d->addConstraintToIndex( c.startIndex(), c );
110  d->addConstraintToIndex( c.endIndex(), c );
111  emit constraintAdded( c );
112  }
113 }
114 
122 bool ConstraintModel::removeConstraint( const Constraint& c )
123 {
124  //qDebug() << "ConstraintModel::removeConstraint("<<c<<") from "<< d->constraints;
125  bool rc = d->constraints.removeAll( c );
126  //bool rc = d->constraints.remove( c );
127  if ( rc ) {
128  d->removeConstraintFromIndex( c.startIndex(), c );
129  d->removeConstraintFromIndex( c.endIndex(), c );
130  emit constraintRemoved( c );
131  }
132  return rc;
133 }
134 
139 void ConstraintModel::clear()
140 {
141  QList<Constraint> lst = constraints();
142  Q_FOREACH( const Constraint& c, lst ) {
143  removeConstraint( c );
144  }
145 }
146 
148 void ConstraintModel::cleanup()
149 {
150 #if 0
151  QSet<Constraint> orphans;
152  Q_FOREACH( const Constraint& c, d->constraints ) {
153  if ( !c.startIndex().isValid() || !c.endIndex().isValid() ) orphans.insert( c );
154  }
155  //qDebug() << "Constraint::cleanup() found" << orphans << "orphans";
156  d->constraints.subtract( orphans );
157 #endif
158 }
159 
163 QList<Constraint> ConstraintModel::constraints() const
164 {
165  //return d->constraints.toList();
166  return d->constraints;
167 }
168 
172 QList<Constraint> ConstraintModel::constraintsForIndex( const QModelIndex& idx ) const
173 {
174  assert( !idx.isValid() || d->indexMap.isEmpty() || !d->indexMap.keys().front().model() || idx.model() == d->indexMap.keys().front().model() );
175  if ( !idx.isValid() ) {
176  // Because of a Qt bug we need to treat this as a special case
177  QSet<Constraint> result;
178  Q_FOREACH( Constraint c, d->constraints ) {
179  if ( !c.startIndex().isValid() || !c.endIndex().isValid() ) result.insert( c );
180  }
181  return result.toList();
182  } else {
183  QList<Constraint> result;
184  Q_FOREACH( Constraint c, d->constraints ) {
185  if ( c.startIndex() == idx || c.endIndex() == idx ) result.push_back( c );
186  }
187  return result;
188  }
189 
190  //return d->indexMap.values( idx );
191 }
192 
196 bool ConstraintModel::hasConstraint( const Constraint& c ) const
197 {
198  /*
199  // Because of a Qt bug we have to search like this
200  Q_FOREACH( Constraint c2, d->constraints ) {
201  if ( c==c2 ) return true;
202  }
203  return false;
204  */
205  return d->constraints.contains( c );
206 }
207 
208 #ifndef QT_NO_DEBUG_STREAM
209 
210 QDebug operator<<( QDebug dbg, const KDGantt::ConstraintModel& model )
211 {
212  dbg << "KDGantt::ConstraintModel[ " << static_cast<const QObject*>( &model ) << ":"
213  << model.constraints() << "]";
214  return dbg;
215 }
216 
217 #endif /* QT_NO_DEBUG_STREAM */
218 
219 #undef d
220 
221 #ifndef KDAB_NO_UNIT_TESTS
222 
223 #include <QStandardItemModel>
224 
225 #include "unittest/test.h"
226 
227 std::ostream& operator<<( std::ostream& os, const QModelIndex& idx )
228 {
229  QString str;
230  QDebug( &str )<<idx;
231 #ifdef QT_NO_STL
232  os<<str.toLatin1().constData();
233 #else
234  os<<str.toStdString();
235 #endif
236  return os;
237 }
238 
239 KDAB_SCOPED_UNITTEST_SIMPLE( KDGantt, ConstraintModel, "test" )
240 {
241  QStandardItemModel dummyModel( 100, 100 );
242  ConstraintModel model;
243 
244  QModelIndex invalidIndex;
245  assertEqual( invalidIndex, invalidIndex );
246 
247  assertEqual( model.constraints().count(), 0 );
248 
249  model.addConstraint( Constraint( QModelIndex(), QModelIndex() ) );
250  assertEqual( model.constraints().count(), 1 );
251 
252  model.addConstraint( Constraint( QModelIndex(), QModelIndex() ) );
253  assertEqual( model.constraints().count(), 1 );
254 
255  QPersistentModelIndex idx1 = dummyModel.index( 7, 17, QModelIndex() );
256  QPersistentModelIndex idx2 = dummyModel.index( 42, 17, QModelIndex() );
257 
258  model.addConstraint( Constraint( idx1, idx2 ) );
259  assertEqual( model.constraints().count(), 2 );
260  assertTrue( model.hasConstraint( Constraint( idx1, idx2 ) ) );
261 
262  assertEqual( model.constraintsForIndex( QModelIndex() ).count(), 1 );
263 
264  assertEqual( model.constraints().count(), 2 );
265  model.removeConstraint( Constraint( QModelIndex(), QModelIndex() ) );
266  assertEqual( model.constraints().count(), 1 );
267  assertFalse( model.hasConstraint( Constraint( QModelIndex(), QModelIndex() ) ) );
268 
269  model.removeConstraint( Constraint( QModelIndex(), QModelIndex() ) );
270  assertEqual( model.constraints().count(), 1 );
271 
272  model.removeConstraint( Constraint( idx1, idx2 ) );
273  assertEqual( model.constraints().count(), 0 );
274  assertFalse( model.hasConstraint( Constraint( idx1, idx2 ) ) );
275 
276  model.addConstraint( Constraint( idx1, idx2 ) );
277  assertTrue( model.hasConstraint( Constraint( idx1, idx2 ) ) );
278  dummyModel.removeRow( 8 );
279  assertTrue( model.hasConstraint( Constraint( idx1, idx2 ) ) );
280  dummyModel.removeRow( 7 );
281  assertTrue( model.hasConstraint( Constraint( idx1, idx2 ) ) );
282 }
283 
284 #endif /* KDAB_NO_UNIT_TESTS */
285 
286 #include "moc_kdganttconstraintmodel.cpp"
KDGantt::ConstraintModel::Private
Definition: kdganttconstraintmodel_p.h:35
KDGantt::Constraint::endIndex
QModelIndex endIndex() const
Definition: kdganttconstraint.cpp:113
KDGantt::ConstraintModel::Private::Private
Private()
Definition: kdganttconstraintmodel.cpp:42
KDGantt::ConstraintModel::constraintAdded
void constraintAdded(const Constraint &)
KDGantt::Constraint::startIndex
QModelIndex startIndex() const
Definition: kdganttconstraint.cpp:107
KDGantt::ConstraintModel::cleanup
void cleanup()
Definition: kdganttconstraintmodel.cpp:148
KDGantt::ConstraintModel::Private::addConstraintToIndex
void addConstraintToIndex(const QModelIndex &idx, const Constraint &c)
Definition: kdganttconstraintmodel.cpp:46
QObject
KDAB_SCOPED_UNITTEST_SIMPLE
KDAB_SCOPED_UNITTEST_SIMPLE(KDGantt, ConstraintModel,"test")
Definition: kdganttconstraintmodel.cpp:239
operator<<
QDebug operator<<(QDebug dbg, const KDGantt::ConstraintModel &model)
Definition: kdganttconstraintmodel.cpp:210
kdganttconstraintmodel_p.h
KDGantt::ConstraintModel::clear
void clear()
Definition: kdganttconstraintmodel.cpp:139
KDGantt::ConstraintModel::constraints
QList< Constraint > constraints() const
Definition: kdganttconstraintmodel.cpp:163
KDGantt::ConstraintModel::~ConstraintModel
virtual ~ConstraintModel()
Definition: kdganttconstraintmodel.cpp:86
KDGantt::ConstraintModel::ConstraintModel
ConstraintModel(QObject *parent=0)
Definition: kdganttconstraintmodel.cpp:72
KDGantt::Constraint
A class used to represent a dependency.
Definition: kdganttconstraint.h:38
KDGantt::ConstraintModel::Private::removeConstraintFromIndex
void removeConstraintFromIndex(const QModelIndex &idx, const Constraint &c)
Definition: kdganttconstraintmodel.cpp:58
KDGantt::ConstraintModel::addConstraint
void addConstraint(const Constraint &c)
Definition: kdganttconstraintmodel.cpp:101
KDGantt::ConstraintModel::hasConstraint
bool hasConstraint(const Constraint &c) const
Definition: kdganttconstraintmodel.cpp:196
d
#define d
Definition: kdganttconstraintmodel.cpp:91
KDGantt::ConstraintModel::removeConstraint
bool removeConstraint(const Constraint &c)
Definition: kdganttconstraintmodel.cpp:122
kdganttconstraintmodel.h
KDGantt::ConstraintModel::constraintRemoved
void constraintRemoved(const Constraint &)
KDGantt::ConstraintModel::constraintsForIndex
QList< Constraint > constraintsForIndex(const QModelIndex &) const
Definition: kdganttconstraintmodel.cpp:172
KDGantt::ConstraintModel
Definition: kdganttconstraintmodel.h:35
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:05 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdgantt2

Skip menu "kdgantt2"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal