1 /****************************************************************************
2 ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB. All rights reserved.
4 ** This file is part of the KD Gantt library.
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.
11 ** Licensees holding valid commercial KD Gantt licenses may use this file in
12 ** accordance with the KD Gantt Commercial License Agreement provided with
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.
18 ** See http://www.kdab.net/kdgantt for
19 ** information about KD Gantt Commercial License Agreements.
21 ** Contact info@kdab.net if any conditions of this
22 ** licensing are not clear to you.
24 **********************************************************************/
25 #include "kdganttconstraint.h"
26 #include "kdganttconstraint_p.h"
30 using namespace KDGantt
;
32 /*!\class KDGantt::Constraint
34 * \brief A class used to represent a dependency.
36 * Instances of this class represent a dependency between the
37 * data items pointed to by a start-QModelIndex and an
41 /*!\enum KDGantt::Constraint::Type
42 * This enum is unused for now.
45 /*!\enum KDGantt::Constraint::ConstraintDataRole
46 * Data roles used when specifying the pen to draw constraints with.
50 Constraint::Private::Private()
55 Constraint::Private::Private( const Private
& other
)
56 : QSharedData( other
)
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
70 * Actually enforcing hard constraints is the responsibility of
71 * the AbstractGrid subclass used in the view.
73 Constraint::Constraint( const QModelIndex
& idx1
, const QModelIndex
& idx2
, Type type
)
79 Q_ASSERT_X( idx1
!= idx2
|| !idx1
.isValid(), "Constraint::Constraint", "cannot create a constraint with idx1 == idx2" );
82 /*! Copy-Constructor. */
83 Constraint::Constraint( const Constraint
& other
)
89 Constraint::~Constraint()
93 /*! Assignment operator. */
94 Constraint
& Constraint::operator=( const Constraint
& other
)
100 /*! This is unused for now. */
101 Constraint::Type
Constraint::type() const
106 /*! \returns The dependency index */
107 QModelIndex
Constraint::startIndex() const
112 /*! \returns The constrained index */
113 QModelIndex
Constraint::endIndex() const
118 /*! \returns The data associated with this index for the specified role.
119 * \param role The role to fetch the data for.
120 * \sa ConstraintDataRole
122 QVariant
Constraint::data( int role
) const
124 return d
->data
.value( role
);
127 /*! Set data on this index for the specified role.
128 * \param role The role to set the data for.
129 * \param value The data to set on the index.
130 * \sa ConstraintDataRole
132 void Constraint::setData( int role
, const QVariant
& value
)
134 d
->data
.insert( role
, value
);
137 /*! Compare two Constraint objects. Two Constraints are equal
138 * if the have the same start and end indexes
140 bool Constraint::operator==( const Constraint
& other
) const
142 if ( d
== other
.d
) return true;
143 return ( *d
).equals( *( other
.d
) );
147 uint
Constraint::hash() const
149 return ::qHash( d
->start
) ^ ::qHash( d
->end
) ^ ::qHash( static_cast<uint
>( d
->type
) );
152 #ifndef QT_NO_DEBUG_STREAM
154 QDebug
operator<<( QDebug dbg
, const Constraint
& c
)
156 return c
.debug( dbg
);
159 QDebug
Constraint::debug( QDebug dbg
) const
161 dbg
<< "KDGantt::Constraint[ start="<<d
->start
<<" end="<<d
->end
<<"]";
165 #endif /* QT_NO_DEBUG_STREAM */
167 #ifndef KDAB_NO_UNIT_TESTS
169 #include <QStandardItemModel>
171 #include "unittest/test.h"
173 KDAB_SCOPED_UNITTEST_SIMPLE( KDGantt
, Constraint
, "test" )
175 QStandardItemModel
dummyModel( 100, 100 );
176 QModelIndex idx1
= dummyModel
.index( 7, 17, QModelIndex() );
177 QModelIndex idx2
= dummyModel
.index( 42, 17, QModelIndex() );
179 Constraint c1
= Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft
);
180 Constraint c2
= Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft
);
182 Constraint
c4( idx1
, idx2
);
183 Constraint
c5( idx2
, idx1
);
185 assertTrue( c1
==c2
);
186 assertEqual( qHash( c1
), qHash( c2
) );
187 assertTrue( c1
==c3
);
188 assertEqual( qHash( c1
), qHash( c3
) );
189 assertTrue( c2
==c3
);
190 assertEqual( qHash( c2
), qHash( c3
) );
192 assertFalse( c2
==c4
);
193 assertNotEqual( qHash( c2
), qHash( c4
) );
195 assertFalse( c4
==c5
);
197 assertEqual( c3
.type(), Constraint::TypeSoft
);
199 dummyModel
.removeRow( 8 );
200 assertFalse( c4
==c5
);
203 #endif /* KDAB_NO_UNIT_TESTS */