KD Chart API Documentation  3.1
kdganttconstraint.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Chart library.
4 **
5 ** SPDX-FileCopyrightText: 2001 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
11 #include "kdganttconstraint.h"
12 #include "kdganttconstraint_p.h"
13 
14 #include <QDateTime>
15 
16 using namespace KDGantt;
17 
36 Constraint::Private::Private()
37 {
38 }
39 
40 Constraint::Private::Private(const Private &other)
41  : QSharedData(other)
42  , start(other.start)
43  , end(other.end)
44  , type(other.type)
45  , relationType(other.relationType)
46  , data(other.data)
47 {
48 }
49 
63 Constraint::Constraint(const QModelIndex &idx1,
64  const QModelIndex &idx2,
65  Constraint::Type type,
66  Constraint::RelationType relationType,
67  const DataMap &datamap)
68  : d(new Private)
69 {
70  d->start = idx1;
71  d->end = idx2;
72  d->type = type;
73  d->relationType = relationType;
74  d->data = datamap;
75  Q_ASSERT_X(idx1 != idx2 || !idx1.isValid(), "Constraint::Constraint", "cannot create a constraint with idx1 == idx2");
76 }
77 
80  : d(new Private)
81 {
82 }
83 
86  : d(other.d)
87 {
88 }
89 
92 {
93 }
94 
97 {
98  d = other.d;
99  return *this;
100 }
101 
104 {
105  return d->type;
106 }
107 
110 {
111  return d->relationType;
112 }
113 
115 QModelIndex Constraint::startIndex() const
116 {
117  return d->start;
118 }
119 
121 QModelIndex Constraint::endIndex() const
122 {
123  return d->end;
124 }
125 
130 QVariant Constraint::data(int role) const
131 {
132  return d->data.value(role);
133 }
134 
140 void Constraint::setData(int role, const QVariant &value)
141 {
142  d->data.insert(role, value);
143 }
144 
149 {
150  d->data = datamap;
151 }
152 
156 {
157  return d->data;
158 }
159 
160 bool Constraint::compareIndexes(const Constraint &other) const
161 {
162  return (d->start == other.startIndex() || (!d->start.isValid() && !other.startIndex().isValid()))
163  && (d->end == other.endIndex() || (!d->end.isValid() && !other.endIndex().isValid()));
164 }
165 
169 bool Constraint::operator==(const Constraint &other) const
170 {
171  if (d == other.d)
172  return true;
173  return (*d).equals(*(other.d));
174 }
175 
177 uint Constraint::hash() const
178 {
179  return ::qHash(d->start) ^ ::qHash(d->end) ^ ::qHash(static_cast<uint>(d->type));
180 }
181 
182 #ifndef QT_NO_DEBUG_STREAM
183 
184 QDebug operator<<(QDebug dbg, const Constraint &c)
185 {
186  return c.debug(dbg);
187 }
188 
189 QDebug Constraint::debug(QDebug dbg) const
190 {
191  dbg << "KDGantt::Constraint[ start=" << d->start << "end=" << d->end << "relationType=" << d->relationType << "], data=" << d->data;
192  return dbg;
193 }
194 
195 #endif /* QT_NO_DEBUG_STREAM */
196 
197 #ifndef KDAB_NO_UNIT_TESTS
198 
199 #include <QStandardItemModel>
200 
201 #include "unittest/test.h"
202 
204 {
205  QStandardItemModel dummyModel(100, 100);
206  QModelIndex idx1 = dummyModel.index(7, 17, QModelIndex());
207  QModelIndex idx2 = dummyModel.index(42, 17, QModelIndex());
208 
209  Constraint c1 = Constraint(QModelIndex(), QModelIndex(), Constraint::TypeSoft);
210  Constraint c2 = Constraint(QModelIndex(), QModelIndex(), Constraint::TypeSoft);
211  Constraint c3 = c2;
212  Constraint c4(idx1, idx2);
213  Constraint c5(idx2, idx1);
214 
215  assertTrue(c1 == c2);
216  assertEqual(qHash(c1), qHash(c2));
217  assertTrue(c1 == c3);
218  assertEqual(qHash(c1), qHash(c3));
219  assertTrue(c2 == c3);
220  assertEqual(qHash(c2), qHash(c3));
221 
222  assertFalse(c2 == c4);
223  assertNotEqual(qHash(c2), qHash(c4));
224 
225  assertFalse(c4 == c5);
226 
227  assertEqual(c3.type(), Constraint::TypeSoft);
228 
229  dummyModel.removeRow(8);
230  assertFalse(c4 == c5);
231 }
232 
233 #endif /* KDAB_NO_UNIT_TESTS */
A class used to represent a dependency.
QVariant data(int role) const
void setDataMap(const QMap< int, QVariant > &datamap)
Set data on this constraint to the keys/values in datamap.
QDebug debug(QDebug dbg) const
Type
This enum is unused for now.
QModelIndex endIndex() const
Constraint & operator=(const Constraint &other)
Assignment operator.
Type type() const
This is unused for now.
bool operator==(const Constraint &other) const
Compare two Constraint objects.
bool compareIndexes(const Constraint &other) const
RelationType relationType() const
This is unused for now.
QModelIndex startIndex() const
Constraint()
Default constructor, created an invalid constraint.
QMap< int, QVariant > dataMap() const
void setData(int role, const QVariant &value)
Set data on this index for the specified role.
KDAB_SCOPED_UNITTEST_SIMPLE(KDGantt, Constraint, "test")
QDebug operator<<(QDebug dbg, const Constraint &c)
uint qHash(const Constraint &c)

© 2001 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/
Generated by doxygen 1.9.1