kdganttconstraint.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002  ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB.  All rights reserved.
00003  **
00004  ** This file is part of the KD Gantt library.
00005  **
00006  ** This file may be distributed and/or modified under the terms of the
00007  ** GNU General Public License version 2 as published by the Free Software
00008  ** Foundation and appearing in the file LICENSE.GPL included in the
00009  ** packaging of this file.
00010  **
00011  ** Licensees holding valid commercial KD Gantt licenses may use this file in
00012  ** accordance with the KD Gantt Commercial License Agreement provided with
00013  ** the Software.
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  ** See http://www.kdab.net/kdgantt for
00019  **   information about KD Gantt Commercial License Agreements.
00020  **
00021  ** Contact info@kdab.net if any conditions of this
00022  ** licensing are not clear to you.
00023  **
00024  **********************************************************************/
00025 #include "kdganttconstraint.h"
00026 #include "kdganttconstraint_p.h"
00027 
00028 #include <QDateTime>
00029 
00030 using namespace KDGantt;
00031 
00050 Constraint::Private::Private()
00051     : type( TypeSoft ),
00052       relationType( FinishStart )
00053 {
00054 }
00055 
00056 Constraint::Private::Private( const Private& other )
00057     : QSharedData( other )
00058 {
00059     start=other.start;
00060     end=other.end;
00061     type=other.type;
00062     relationType=other.relationType;
00063 }
00064 
00078 Constraint::Constraint( const QModelIndex& idx1,  const QModelIndex& idx2, Constraint::Type type, Constraint::RelationType relationType )
00079     : d( new Private )
00080 {
00081     d->start=idx1;
00082     d->end=idx2;
00083     d->type=type;
00084     d->relationType=relationType;
00085     Q_ASSERT_X( idx1 != idx2 || !idx1.isValid(), "Constraint::Constraint", "cannot create a constraint with idx1 == idx2" );
00086 }
00087 
00089 Constraint::Constraint( const Constraint& other )
00090     : d( other.d )
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 /* QT_NO_DEBUG_STREAM */
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 /* KDAB_NO_UNIT_TESTS */

Generated on Thu Mar 4 23:19:13 2010 for KD Chart 2 by  doxygen 1.5.4