kdgantt
kdganttconstraint.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "kdganttconstraint.h"
00024 #include "kdganttconstraint_p.h"
00025
00026 #include <QDateTime>
00027
00028 using namespace KDGantt;
00029
00048 Constraint::Private::Private()
00049 : type( TypeSoft ),
00050 relationType( FinishStart )
00051 {
00052 }
00053
00054 Constraint::Private::Private( const Private& other )
00055 : QSharedData( other )
00056 {
00057 start=other.start;
00058 end=other.end;
00059 type=other.type;
00060 relationType=other.relationType;
00061 }
00062
00076 Constraint::Constraint( const QModelIndex& idx1, const QModelIndex& idx2, Constraint::Type type, Constraint::RelationType relationType )
00077 : d( new Private )
00078 {
00079 d->start=idx1;
00080 d->end=idx2;
00081 d->type=type;
00082 d->relationType=relationType;
00083 Q_ASSERT_X( idx1 != idx2 || !idx1.isValid(), "Constraint::Constraint", "cannot create a constraint with idx1 == idx2" );
00084
00085 }
00086
00088 Constraint::Constraint( const Constraint& other )
00089 : d( other.d )
00090 {
00091
00092 }
00093
00095 Constraint::~Constraint()
00096 {
00097 }
00098
00100 Constraint& Constraint::operator=( const Constraint& other )
00101 {
00102 d = other.d;
00103 return *this;
00104 }
00105
00107 Constraint::Type Constraint::type() const
00108 {
00109 return d->type;
00110 }
00111
00113 Constraint::RelationType Constraint::relationType() const
00114 {
00115 return d->relationType;
00116 }
00117
00119 QModelIndex Constraint::startIndex() const
00120 {
00121 return d->start;
00122 }
00123
00125 QModelIndex Constraint::endIndex() const
00126 {
00127 return d->end;
00128 }
00129
00134 QVariant Constraint::data( int role ) const
00135 {
00136 return d->data.value( role );
00137 }
00138
00144 void Constraint::setData( int role, const QVariant& value )
00145 {
00146 d->data.insert( role, value );
00147 }
00148
00152 bool Constraint::operator==( const Constraint& other ) const
00153 {
00154 if ( d == other.d ) return true;
00155 return ( *d ).equals( *( other.d ) );
00156 }
00157
00159 uint Constraint::hash() const
00160 {
00161 return ::qHash( d->start ) ^ ::qHash( d->end ) ^ ::qHash( static_cast<uint>( d->type ) );
00162 }
00163
00164 #ifndef QT_NO_DEBUG_STREAM
00165
00166 QDebug operator<<( QDebug dbg, const Constraint& c )
00167 {
00168 return c.debug( dbg );
00169 }
00170
00171 QDebug Constraint::debug( QDebug dbg ) const
00172 {
00173 dbg << "KDGantt::Constraint[ start=" << d->start << "end=" << d->end << "relationType=" << d->relationType << "]";
00174 return dbg;
00175 }
00176
00177 #endif
00178
00179 #ifndef KDAB_NO_UNIT_TESTS
00180
00181 #include <QStandardItemModel>
00182
00183 #include "unittest/test.h"
00184
00185 KDAB_SCOPED_UNITTEST_SIMPLE( KDGantt, Constraint, "test" )
00186 {
00187 QStandardItemModel dummyModel( 100, 100 );
00188 QModelIndex idx1 = dummyModel.index( 7, 17, QModelIndex() );
00189 QModelIndex idx2 = dummyModel.index( 42, 17, QModelIndex() );
00190
00191 Constraint c1 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
00192 Constraint c2 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
00193 Constraint c3 = c2;
00194 Constraint c4( idx1, idx2 );
00195 Constraint c5( idx2, idx1 );
00196
00197 assertTrue( c1==c2 );
00198 assertEqual( qHash( c1 ), qHash( c2 ) );
00199 assertTrue( c1==c3 );
00200 assertEqual( qHash( c1 ), qHash( c3 ) );
00201 assertTrue( c2==c3 );
00202 assertEqual( qHash( c2 ), qHash( c3 ) );
00203
00204 assertFalse( c2==c4 );
00205 assertNotEqual( qHash( c2 ), qHash( c4 ) );
00206
00207 assertFalse( c4==c5 );
00208
00209 assertEqual( c3.type(), Constraint::TypeSoft );
00210
00211 dummyModel.removeRow( 8 );
00212 assertFalse( c4==c5 );
00213 }
00214
00215 #endif