File indexing completed on 2024-05-12 04:20:39
0001 /* 0002 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KGantt library. 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "kganttconstraint.h" 0010 #include "kganttconstraint_p.h" 0011 0012 #include <QDateTime> 0013 0014 using namespace KGantt; 0015 0016 0017 Constraint::Private::Private() 0018 : type( TypeSoft ), 0019 relationType( FinishStart ) 0020 { 0021 } 0022 0023 Constraint::Private::Private( const Private& other ) 0024 : QSharedData( other ), 0025 start( other.start ), 0026 end( other.end ), 0027 type( other.type ), 0028 relationType( other.relationType ), 0029 data( other.data ) 0030 { 0031 } 0032 0033 Constraint::Constraint( const QModelIndex& idx1, 0034 const QModelIndex& idx2, 0035 Constraint::Type type, 0036 Constraint::RelationType relationType, 0037 const Constraint::DataMap& datamap ) 0038 : d( new Private ) 0039 { 0040 d->start=idx1; 0041 d->end=idx2; 0042 d->type=type; 0043 d->relationType=relationType; 0044 d->data=datamap; 0045 Q_ASSERT_X( idx1 != idx2 || !idx1.isValid(), "Constraint::Constraint", "cannot create a constraint with idx1 == idx2" ); 0046 } 0047 0048 Constraint::Constraint() 0049 : d( new Private ) 0050 { 0051 } 0052 0053 Constraint::Constraint( const Constraint& other ) 0054 : d( other.d ) 0055 { 0056 } 0057 0058 Constraint::~Constraint() 0059 { 0060 } 0061 0062 Constraint& Constraint::operator=( const Constraint& other ) 0063 { 0064 d = other.d; 0065 return *this; 0066 } 0067 0068 Constraint::Type Constraint::type() const 0069 { 0070 return d->type; 0071 } 0072 0073 Constraint::RelationType Constraint::relationType() const 0074 { 0075 return d->relationType; 0076 } 0077 0078 QModelIndex Constraint::startIndex() const 0079 { 0080 return d->start; 0081 } 0082 0083 QModelIndex Constraint::endIndex() const 0084 { 0085 return d->end; 0086 } 0087 0088 QVariant Constraint::data( int role ) const 0089 { 0090 return d->data.value( role ); 0091 } 0092 0093 void Constraint::setData( int role, const QVariant& value ) 0094 { 0095 d->data.insert( role, value ); 0096 } 0097 0098 void Constraint::setDataMap( const QMap< int, QVariant >& datamap ) 0099 { 0100 d->data = datamap; 0101 } 0102 0103 QMap< int, QVariant > Constraint::dataMap() const 0104 { 0105 return d->data; 0106 } 0107 0108 bool Constraint::compareIndexes(const Constraint& other) const 0109 { 0110 return (d->start==other.startIndex() || (!d->start.isValid() && !other.startIndex().isValid())) 0111 && (d->end==other.endIndex() || (!d->end.isValid() && !other.endIndex().isValid())); 0112 } 0113 0114 bool Constraint::operator==( const Constraint& other ) const 0115 { 0116 if ( d == other.d ) return true; 0117 return ( *d ).equals( *( other.d ) ); 0118 } 0119 0120 uint Constraint::hash() const 0121 { 0122 return ::qHash( d->start ) ^ ::qHash( d->end ) ^ ::qHash( static_cast<uint>( d->type ) ); 0123 } 0124 0125 #ifndef QT_NO_DEBUG_STREAM 0126 0127 QDebug operator<<( QDebug dbg, const Constraint& c ) 0128 { 0129 return c.debug( dbg ); 0130 } 0131 0132 QDebug Constraint::debug( QDebug dbg ) const 0133 { 0134 dbg << "KGantt::Constraint[ start=" << d->start << "end=" << d->end << "relationType=" << d->relationType << "], data=" << d->data; 0135 return dbg; 0136 } 0137 0138 #endif /* QT_NO_DEBUG_STREAM */ 0139 0140 #ifndef KDAB_NO_UNIT_TESTS 0141 0142 #include <QStandardItemModel> 0143 0144 #include "unittest/test.h" 0145 0146 KDAB_SCOPED_UNITTEST_SIMPLE( KGantt, Constraint, "test" ) 0147 { 0148 QStandardItemModel dummyModel( 100, 100 ); 0149 QModelIndex idx1 = dummyModel.index( 7, 17, QModelIndex() ); 0150 QModelIndex idx2 = dummyModel.index( 42, 17, QModelIndex() ); 0151 0152 Constraint c1 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft ); 0153 Constraint c2 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft ); 0154 Constraint c3 = c2; 0155 Constraint c4( idx1, idx2 ); 0156 Constraint c5( idx2, idx1 ); 0157 0158 assertTrue( c1==c2 ); 0159 assertEqual( qHash( c1 ), qHash( c2 ) ); 0160 assertTrue( c1==c3 ); 0161 assertEqual( qHash( c1 ), qHash( c3 ) ); 0162 assertTrue( c2==c3 ); 0163 assertEqual( qHash( c2 ), qHash( c3 ) ); 0164 0165 assertFalse( c2==c4 ); 0166 assertNotEqual( qHash( c2 ), qHash( c4 ) ); 0167 0168 assertFalse( c4==c5 ); 0169 0170 assertEqual( c3.type(), Constraint::TypeSoft ); 0171 0172 dummyModel.removeRow( 8 ); 0173 assertFalse( c4==c5 ); 0174 } 0175 0176 #endif /* KDAB_NO_UNIT_TESTS */