kdganttconstraint.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2011 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.txt 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       start( other.start ),
00057       end( other.end ),
00058       type( other.type ),
00059       relationType( other.relationType ),
00060       data( other.data )
00061 {
00062 }
00063 
00077 Constraint::Constraint( const QModelIndex& idx1,
00078                         const QModelIndex& idx2,
00079                         Constraint::Type type,
00080                         Constraint::RelationType relationType,
00081                         const QMap<int, QVariant>& dataMap )
00082     : d( new Private )
00083 {
00084     d->start=idx1;
00085     d->end=idx2;
00086     d->type=type;
00087     d->relationType=relationType;
00088     d->data=dataMap;
00089     Q_ASSERT_X( idx1 != idx2 || !idx1.isValid(), "Constraint::Constraint", "cannot create a constraint with idx1 == idx2" );
00090 }
00091 
00093 Constraint::Constraint()
00094     : d( new Private )
00095 {
00096 }
00097 
00099 Constraint::Constraint( const Constraint& other )
00100     : d( other.d )
00101 {
00102 }
00103 
00105 Constraint::~Constraint()
00106 {
00107 }
00108 
00110 Constraint& Constraint::operator=( const Constraint& other )
00111 {
00112     d = other.d;
00113     return *this;
00114 }
00115 
00117 Constraint::Type Constraint::type() const
00118 {
00119     return d->type;
00120 }
00121 
00123 Constraint::RelationType Constraint::relationType() const
00124 {
00125     return d->relationType;
00126 }
00127 
00129 QModelIndex Constraint::startIndex() const
00130 {
00131     return d->start;
00132 }
00133 
00135 QModelIndex Constraint::endIndex() const
00136 {
00137     return d->end;
00138 }
00139 
00144 QVariant Constraint::data( int role ) const
00145 {
00146     return d->data.value( role );
00147 }
00148 
00154 void Constraint::setData( int role, const QVariant& value )
00155 {
00156     d->data.insert( role, value );
00157 }
00158 
00162 void Constraint::setDataMap( const QMap< int, QVariant >& datamap )
00163 {
00164     d->data = datamap;
00165 }
00166 
00169 QMap< int, QVariant > Constraint::dataMap() const
00170 {
00171     return d->data;
00172 }
00173 
00174 
00175 bool Constraint::compareIndexes(const Constraint& other) const
00176 {
00177     return (d->start==other.startIndex() || (!d->start.isValid() && !other.startIndex().isValid()))
00178         && (d->end==other.endIndex() || (!d->end.isValid() && !other.endIndex().isValid()));
00179 }
00180 
00184 bool Constraint::operator==( const Constraint& other ) const
00185 {
00186     if ( d == other.d ) return true;
00187     return ( *d ).equals( *( other.d ) );
00188 }
00189 
00191 uint Constraint::hash() const
00192 {
00193     return ::qHash( d->start ) ^ ::qHash( d->end ) ^ ::qHash( static_cast<uint>( d->type ) );
00194 }
00195 
00196 #ifndef QT_NO_DEBUG_STREAM
00197 
00198 QDebug operator<<( QDebug dbg, const Constraint& c )
00199 {
00200     return c.debug( dbg );
00201 }
00202 
00203 QDebug Constraint::debug( QDebug dbg ) const
00204 {
00205     dbg << "KDGantt::Constraint[ start=" << d->start << "end=" << d->end << "relationType=" << d->relationType << "], data=" << d->data;
00206     return dbg;
00207 }
00208 
00209 #endif /* QT_NO_DEBUG_STREAM */
00210 
00211 #ifndef KDAB_NO_UNIT_TESTS
00212 
00213 #include <QStandardItemModel>
00214 
00215 #include "unittest/test.h"
00216 
00217 KDAB_SCOPED_UNITTEST_SIMPLE( KDGantt, Constraint, "test" )
00218 {
00219     QStandardItemModel dummyModel( 100, 100 );
00220     QModelIndex idx1 = dummyModel.index( 7, 17, QModelIndex() );
00221     QModelIndex idx2 = dummyModel.index( 42, 17, QModelIndex() );
00222 
00223     Constraint c1 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
00224     Constraint c2 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
00225     Constraint c3 = c2;
00226     Constraint c4( idx1, idx2 );
00227     Constraint c5( idx2, idx1 );
00228 
00229     assertTrue( c1==c2 );
00230     assertEqual( qHash( c1 ), qHash( c2 ) );
00231     assertTrue( c1==c3 );
00232     assertEqual( qHash( c1 ), qHash( c3 ) );
00233     assertTrue( c2==c3 );
00234     assertEqual( qHash( c2 ), qHash( c3 ) );
00235 
00236     assertFalse( c2==c4 );
00237     assertNotEqual( qHash( c2 ), qHash( c4 ) );
00238 
00239     assertFalse( c4==c5 );
00240 
00241     assertEqual( c3.type(), Constraint::TypeSoft );
00242 
00243     dummyModel.removeRow( 8 );
00244     assertFalse( c4==c5 );
00245 }
00246 
00247 #endif /* KDAB_NO_UNIT_TESTS */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/