File indexing completed on 2024-05-12 15:54:21

0001 /*
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KGantt library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "kganttconstraint.h"
0021 #include "kganttconstraint_p.h"
0022 
0023 #include <QDateTime>
0024 
0025 using namespace KGantt;
0026 
0027 
0028 Constraint::Private::Private()
0029     : type( TypeSoft ),
0030       relationType( FinishStart )
0031 {
0032 }
0033 
0034 Constraint::Private::Private( const Private& other )
0035     : QSharedData( other ),
0036       start( other.start ),
0037       end( other.end ),
0038       type( other.type ),
0039       relationType( other.relationType ),
0040       data( other.data )
0041 {
0042 }
0043 
0044 Constraint::Constraint( const QModelIndex& idx1,
0045                         const QModelIndex& idx2,
0046                         Constraint::Type type,
0047                         Constraint::RelationType relationType,
0048                         const Constraint::DataMap& datamap )
0049     : d( new Private )
0050 {
0051     d->start=idx1;
0052     d->end=idx2;
0053     d->type=type;
0054     d->relationType=relationType;
0055     d->data=datamap;
0056     Q_ASSERT_X( idx1 != idx2 || !idx1.isValid(), "Constraint::Constraint", "cannot create a constraint with idx1 == idx2" );
0057 }
0058 
0059 Constraint::Constraint()
0060     : d( new Private )
0061 {
0062 }
0063 
0064 Constraint::Constraint( const Constraint& other )
0065     : d( other.d )
0066 {
0067 }
0068 
0069 Constraint::~Constraint()
0070 {
0071 }
0072 
0073 Constraint& Constraint::operator=( const Constraint& other )
0074 {
0075     d = other.d;
0076     return *this;
0077 }
0078 
0079 Constraint::Type Constraint::type() const
0080 {
0081     return d->type;
0082 }
0083 
0084 Constraint::RelationType Constraint::relationType() const
0085 {
0086     return d->relationType;
0087 }
0088 
0089 QModelIndex Constraint::startIndex() const
0090 {
0091     return d->start;
0092 }
0093 
0094 QModelIndex Constraint::endIndex() const
0095 {
0096     return d->end;
0097 }
0098 
0099 QVariant Constraint::data( int role ) const
0100 {
0101     return d->data.value( role );
0102 }
0103 
0104 void Constraint::setData( int role, const QVariant& value )
0105 {
0106     d->data.insert( role, value );
0107 }
0108 
0109 void Constraint::setDataMap( const QMap< int, QVariant >& datamap )
0110 {
0111     d->data = datamap;
0112 }
0113 
0114 QMap< int, QVariant > Constraint::dataMap() const
0115 {
0116     return d->data;
0117 }
0118 
0119 bool Constraint::compareIndexes(const Constraint& other) const
0120 {
0121     return (d->start==other.startIndex() || (!d->start.isValid() && !other.startIndex().isValid()))
0122         && (d->end==other.endIndex() || (!d->end.isValid() && !other.endIndex().isValid()));
0123 }
0124 
0125 bool Constraint::operator==( const Constraint& other ) const
0126 {
0127     if ( d == other.d ) return true;
0128     return ( *d ).equals( *( other.d ) );
0129 }
0130 
0131 uint Constraint::hash() const
0132 {
0133     return ::qHash( d->start ) ^ ::qHash( d->end ) ^ ::qHash( static_cast<uint>( d->type ) );
0134 }
0135 
0136 #ifndef QT_NO_DEBUG_STREAM
0137 
0138 QDebug operator<<( QDebug dbg, const Constraint& c )
0139 {
0140     return c.debug( dbg );
0141 }
0142 
0143 QDebug Constraint::debug( QDebug dbg ) const
0144 {
0145     dbg << "KGantt::Constraint[ start=" << d->start << "end=" << d->end << "relationType=" << d->relationType << "], data=" << d->data;
0146     return dbg;
0147 }
0148 
0149 #endif /* QT_NO_DEBUG_STREAM */
0150 
0151 #ifndef KDAB_NO_UNIT_TESTS
0152 
0153 #include <QStandardItemModel>
0154 
0155 #include "unittest/test.h"
0156 
0157 KDAB_SCOPED_UNITTEST_SIMPLE( KGantt, Constraint, "test" )
0158 {
0159     QStandardItemModel dummyModel( 100, 100 );
0160     QModelIndex idx1 = dummyModel.index( 7, 17, QModelIndex() );
0161     QModelIndex idx2 = dummyModel.index( 42, 17, QModelIndex() );
0162 
0163     Constraint c1 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
0164     Constraint c2 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
0165     Constraint c3 = c2;
0166     Constraint c4( idx1, idx2 );
0167     Constraint c5( idx2, idx1 );
0168 
0169     assertTrue( c1==c2 );
0170     assertEqual( qHash( c1 ), qHash( c2 ) );
0171     assertTrue( c1==c3 );
0172     assertEqual( qHash( c1 ), qHash( c3 ) );
0173     assertTrue( c2==c3 );
0174     assertEqual( qHash( c2 ), qHash( c3 ) );
0175 
0176     assertFalse( c2==c4 );
0177     assertNotEqual( qHash( c2 ), qHash( c4 ) );
0178 
0179     assertFalse( c4==c5 );
0180 
0181     assertEqual( c3.type(), Constraint::TypeSoft );
0182 
0183     dummyModel.removeRow( 8 );
0184     assertFalse( c4==c5 );
0185 }
0186 
0187 #endif /* KDAB_NO_UNIT_TESTS */