Backport r950340 | aacid | 2009-04-06 23:21:18 +0200 (Mon, 06 Apr 2009) | 4 lines
[kdepim.git] / kdgantt / kdganttconstraint.cpp
blob4c6dab8b25a942c40d1c9619492e954385e20206
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 used under the terms of the GNU General Public
7 ** License versions 2.0 or 3.0 as published by the Free Software
8 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
9 ** included in the packaging of this file. Alternatively you may (at
10 ** your option) use any later version of the GNU General Public
11 ** License if such license has been publicly approved by
12 ** Klarälvdalens Datakonsult AB (or its successors, if any).
13 **
14 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
15 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
16 ** A PARTICULAR PURPOSE. Klarälvdalens Datakonsult AB reserves all rights
17 ** not expressly granted herein.
18 **
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 **********************************************************************/
23 #include "kdganttconstraint.h"
24 #include "kdganttconstraint_p.h"
26 #include <QDateTime>
28 using namespace KDGantt;
30 /*!\class KDGantt::Constraint
31 *\ingroup KDGantt
32 * \brief A class used to represent a dependency.
34 * Instances of this class represent a dependency between the
35 * data items pointed to by a start-QModelIndex and an
36 * end-QModelIndex.
39 /*!\enum KDGantt::Constraint::Type
40 * This enum is unused for now.
43 /*!\enum KDGantt::Constraint::ConstraintDataRole
44 * Data roles used when specifying the pen to draw constraints with.
45 * \sa setData
48 Constraint::Private::Private()
49 : type( TypeSoft ),
50 relationType( FinishStart )
54 Constraint::Private::Private( const Private& other )
55 : QSharedData( other )
57 start=other.start;
58 end=other.end;
59 type=other.type;
60 relationType=other.relationType;
63 /*! Constructor. Creates a dependency for \a idx2 on \a idx1.
64 * \param type controls if the constraint is a soft one that
65 * is allowed to be broken (ie, go backwards in time) or a hard
66 * constraint that will not allow the user to move an item so
67 * that the constraint would have to go backwards. The default is
68 * TypeSoft.
70 * Actually enforcing hard constraints is the responsibility of
71 * the AbstractGrid subclass used in the view.
73 * \param relationType defines how the tasks depends on each other.
74 * relationType can be FinishStart (default), FinishFinish, StartStart or StartFinish.
76 Constraint::Constraint( const QModelIndex& idx1, const QModelIndex& idx2, Constraint::Type type, Constraint::RelationType relationType )
77 : d( new Private )
79 d->start=idx1;
80 d->end=idx2;
81 d->type=type;
82 d->relationType=relationType;
83 Q_ASSERT_X( idx1 != idx2 || !idx1.isValid(), "Constraint::Constraint", "cannot create a constraint with idx1 == idx2" );
84 //qDebug()<<"Constraint::Constraint"<<*this;
87 /*! Copy-Constructor. */
88 Constraint::Constraint( const Constraint& other )
89 : d( other.d )
91 //qDebug()<<"Constraint::Constraint"<<*this<<other;
94 /*! Destructor */
95 Constraint::~Constraint()
99 /*! Assignment operator. */
100 Constraint& Constraint::operator=( const Constraint& other )
102 d = other.d;
103 return *this;
106 /*! This is unused for now. */
107 Constraint::Type Constraint::type() const
109 return d->type;
112 /*! This is unused for now. */
113 Constraint::RelationType Constraint::relationType() const
115 return d->relationType;
118 /*! \returns The dependency index */
119 QModelIndex Constraint::startIndex() const
121 return d->start;
124 /*! \returns The constrained index */
125 QModelIndex Constraint::endIndex() const
127 return d->end;
130 /*! \returns The data associated with this index for the specified role.
131 * \param role The role to fetch the data for.
132 * \sa ConstraintDataRole
134 QVariant Constraint::data( int role ) const
136 return d->data.value( role );
139 /*! Set data on this index for the specified role.
140 * \param role The role to set the data for.
141 * \param value The data to set on the index.
142 * \sa ConstraintDataRole
144 void Constraint::setData( int role, const QVariant& value )
146 d->data.insert( role, value );
149 /*! Compare two Constraint objects. Two Constraints are equal
150 * if the have the same start and end indexes
152 bool Constraint::operator==( const Constraint& other ) const
154 if ( d == other.d ) return true;
155 return ( *d ).equals( *( other.d ) );
158 /*!\internal*/
159 uint Constraint::hash() const
161 return ::qHash( d->start ) ^ ::qHash( d->end ) ^ ::qHash( static_cast<uint>( d->type ) );
164 #ifndef QT_NO_DEBUG_STREAM
166 QDebug operator<<( QDebug dbg, const Constraint& c )
168 return c.debug( dbg );
171 QDebug Constraint::debug( QDebug dbg ) const
173 dbg << "KDGantt::Constraint[ start=" << d->start << "end=" << d->end << "relationType=" << d->relationType << "]";
174 return dbg;
177 #endif /* QT_NO_DEBUG_STREAM */
179 #ifndef KDAB_NO_UNIT_TESTS
181 #include <QStandardItemModel>
183 #include "unittest/test.h"
185 KDAB_SCOPED_UNITTEST_SIMPLE( KDGantt, Constraint, "test" )
187 QStandardItemModel dummyModel( 100, 100 );
188 QModelIndex idx1 = dummyModel.index( 7, 17, QModelIndex() );
189 QModelIndex idx2 = dummyModel.index( 42, 17, QModelIndex() );
191 Constraint c1 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
192 Constraint c2 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
193 Constraint c3 = c2;
194 Constraint c4( idx1, idx2 );
195 Constraint c5( idx2, idx1 );
197 assertTrue( c1==c2 );
198 assertEqual( qHash( c1 ), qHash( c2 ) );
199 assertTrue( c1==c3 );
200 assertEqual( qHash( c1 ), qHash( c3 ) );
201 assertTrue( c2==c3 );
202 assertEqual( qHash( c2 ), qHash( c3 ) );
204 assertFalse( c2==c4 );
205 assertNotEqual( qHash( c2 ), qHash( c4 ) );
207 assertFalse( c4==c5 );
209 assertEqual( c3.type(), Constraint::TypeSoft );
211 dummyModel.removeRow( 8 );
212 assertFalse( c4==c5 );
215 #endif /* KDAB_NO_UNIT_TESTS */