kdganttconstraint.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL included.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
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 
00087 Constraint::Constraint( const Constraint& other )
00088     : d( other.d )
00089 {
00090 }
00091 
00093 Constraint::~Constraint()
00094 {
00095 }
00096 
00098 Constraint& Constraint::operator=( const Constraint& other )
00099 {
00100     d = other.d;
00101     return *this;
00102 }
00103 
00105 Constraint::Type Constraint::type() const
00106 {
00107     return d->type;
00108 }
00109 
00111 Constraint::RelationType Constraint::relationType() const
00112 {
00113     return d->relationType;
00114 }
00115 
00117 QModelIndex Constraint::startIndex() const
00118 {
00119     return d->start;
00120 }
00121 
00123 QModelIndex Constraint::endIndex() const
00124 {
00125     return d->end;
00126 }
00127 
00132 QVariant Constraint::data( int role ) const
00133 {
00134     return d->data.value( role );
00135 }
00136 
00142 void Constraint::setData( int role, const QVariant& value )
00143 {
00144     d->data.insert( role, value );
00145 }
00146 
00150 bool Constraint::operator==( const Constraint& other ) const
00151 {
00152     if ( d == other.d ) return true;
00153     return ( *d ).equals( *( other.d ) );
00154 }
00155 
00157 uint Constraint::hash() const
00158 {
00159     return ::qHash( d->start ) ^ ::qHash( d->end ) ^ ::qHash( static_cast<uint>( d->type ) );
00160 }
00161 
00162 #ifndef QT_NO_DEBUG_STREAM
00163 
00164 QDebug operator<<( QDebug dbg, const Constraint& c )
00165 {
00166     return c.debug( dbg );
00167 }
00168 
00169 QDebug Constraint::debug( QDebug dbg ) const
00170 {
00171     dbg << "KDGantt::Constraint[ start=" << d->start << "end=" << d->end << "relationType=" << d->relationType << "]";
00172     return dbg;
00173 }
00174 
00175 #endif /* QT_NO_DEBUG_STREAM */
00176 
00177 #ifndef KDAB_NO_UNIT_TESTS
00178 
00179 #include <QStandardItemModel>
00180 
00181 #include "unittest/test.h"
00182 
00183 KDAB_SCOPED_UNITTEST_SIMPLE( KDGantt, Constraint, "test" )
00184 {
00185     QStandardItemModel dummyModel( 100, 100 );
00186     QModelIndex idx1 = dummyModel.index( 7, 17, QModelIndex() );
00187     QModelIndex idx2 = dummyModel.index( 42, 17, QModelIndex() );
00188 
00189     Constraint c1 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
00190     Constraint c2 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
00191     Constraint c3 = c2;
00192     Constraint c4( idx1, idx2 );
00193     Constraint c5( idx2, idx1 );
00194 
00195     assertTrue( c1==c2 );
00196     assertEqual( qHash( c1 ), qHash( c2 ) );
00197     assertTrue( c1==c3 );
00198     assertEqual( qHash( c1 ), qHash( c3 ) );
00199     assertTrue( c2==c3 );
00200     assertEqual( qHash( c2 ), qHash( c3 ) );
00201 
00202     assertFalse( c2==c4 );
00203     assertNotEqual( qHash( c2 ), qHash( c4 ) );
00204 
00205     assertFalse( c4==c5 );
00206 
00207     assertEqual( c3.type(), Constraint::TypeSoft );
00208 
00209     dummyModel.removeRow( 8 );
00210     assertFalse( c4==c5 );
00211 }
00212 
00213 #endif /* KDAB_NO_UNIT_TESTS */