KD Chart 2  [rev.2.7]
kdganttconstraint.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #include "kdganttconstraint.h"
24 #include "kdganttconstraint_p.h"
25 
26 #include <QDateTime>
27 
28 using namespace KDGantt;
29 
48 Constraint::Private::Private()
49  : type( TypeSoft ),
50  relationType( FinishStart )
51 {
52 }
53 
54 Constraint::Private::Private( const Private& other )
55  : QSharedData( other ),
56  start( other.start ),
57  end( other.end ),
58  type( other.type ),
59  relationType( other.relationType ),
60  data( other.data )
61 {
62 }
63 
77 Constraint::Constraint( const QModelIndex& idx1,
78  const QModelIndex& idx2,
79  Constraint::Type type,
80  Constraint::RelationType relationType,
81  const Constraint::DataMap& datamap )
82  : d( new Private )
83 {
84  d->start=idx1;
85  d->end=idx2;
86  d->type=type;
87  d->relationType=relationType;
88  d->data=datamap;
89  Q_ASSERT_X( idx1 != idx2 || !idx1.isValid(), "Constraint::Constraint", "cannot create a constraint with idx1 == idx2" );
90 }
91 
94  : d( new Private )
95 {
96 }
97 
100  : d( other.d )
101 {
102 }
103 
106 {
107 }
108 
111 {
112  d = other.d;
113  return *this;
114 }
115 
118 {
119  return d->type;
120 }
121 
124 {
125  return d->relationType;
126 }
127 
129 QModelIndex Constraint::startIndex() const
130 {
131  return d->start;
132 }
133 
135 QModelIndex Constraint::endIndex() const
136 {
137  return d->end;
138 }
139 
144 QVariant Constraint::data( int role ) const
145 {
146  return d->data.value( role );
147 }
148 
154 void Constraint::setData( int role, const QVariant& value )
155 {
156  d->data.insert( role, value );
157 }
158 
163 {
164  d->data = datamap;
165 }
166 
170 {
171  return d->data;
172 }
173 
174 
175 bool Constraint::compareIndexes(const Constraint& other) const
176 {
177  return (d->start==other.startIndex() || (!d->start.isValid() && !other.startIndex().isValid()))
178  && (d->end==other.endIndex() || (!d->end.isValid() && !other.endIndex().isValid()));
179 }
180 
184 bool Constraint::operator==( const Constraint& other ) const
185 {
186  if ( d == other.d ) return true;
187  return ( *d ).equals( *( other.d ) );
188 }
189 
191 uint Constraint::hash() const
192 {
193  return ::qHash( d->start ) ^ ::qHash( d->end ) ^ ::qHash( static_cast<uint>( d->type ) );
194 }
195 
196 #ifndef QT_NO_DEBUG_STREAM
197 
198 QDebug operator<<( QDebug dbg, const Constraint& c )
199 {
200  return c.debug( dbg );
201 }
202 
203 QDebug Constraint::debug( QDebug dbg ) const
204 {
205  dbg << "KDGantt::Constraint[ start=" << d->start << "end=" << d->end << "relationType=" << d->relationType << "], data=" << d->data;
206  return dbg;
207 }
208 
209 #endif /* QT_NO_DEBUG_STREAM */
210 
211 #ifndef KDAB_NO_UNIT_TESTS
212 
213 #include <QStandardItemModel>
214 
215 #include "unittest/test.h"
216 
218 {
219  QStandardItemModel dummyModel( 100, 100 );
220  QModelIndex idx1 = dummyModel.index( 7, 17, QModelIndex() );
221  QModelIndex idx2 = dummyModel.index( 42, 17, QModelIndex() );
222 
223  Constraint c1 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
224  Constraint c2 = Constraint( QModelIndex(), QModelIndex(), Constraint::TypeSoft );
225  Constraint c3 = c2;
226  Constraint c4( idx1, idx2 );
227  Constraint c5( idx2, idx1 );
228 
229  assertTrue( c1==c2 );
230  assertEqual( qHash( c1 ), qHash( c2 ) );
231  assertTrue( c1==c3 );
232  assertEqual( qHash( c1 ), qHash( c3 ) );
233  assertTrue( c2==c3 );
234  assertEqual( qHash( c2 ), qHash( c3 ) );
235 
236  assertFalse( c2==c4 );
237  assertNotEqual( qHash( c2 ), qHash( c4 ) );
238 
239  assertFalse( c4==c5 );
240 
242 
243  dummyModel.removeRow( 8 );
244  assertFalse( c4==c5 );
245 }
246 
247 #endif /* KDAB_NO_UNIT_TESTS */
QDebug debug(QDebug dbg) const
void setDataMap(const QMap< int, QVariant > &datamap)
void setData(int role, const QVariant &value)
bool operator==(const Constraint &other) const
QVariant data(int role) const
A class used to represent a dependency.
QDebug operator<<(QDebug dbg, const Constraint &c)
#define assertNotEqual(x, y)
Definition: test.h:44
KDAB_SCOPED_UNITTEST_SIMPLE(KDGantt, Constraint,"test")
RelationType relationType() const
Constraint & operator=(const Constraint &other)
QModelIndex endIndex() const
uint qHash(const Constraint &c)
#define assertFalse(x)
Definition: test.h:42
QMap< int, QVariant > dataMap() const
bool compareIndexes(const Constraint &other) const
QModelIndex startIndex() const
#define assertTrue(x)
Definition: test.h:41
#define assertEqual(x, y)
Definition: test.h:43

Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/

https://www.kdab.com/development-resources/qt-tools/kd-chart/