12 #include "kdganttconstraintmodel_p.h"
30 ConstraintModel::Private::Private()
34 void ConstraintModel::Private::addConstraintToIndex(
const QModelIndex &idx,
const Constraint &c)
36 IndexType::iterator it = indexMap.find(idx);
37 while (it != indexMap.end() && it.key() == idx) {
44 indexMap.insert(idx, c);
47 void ConstraintModel::Private::removeConstraintFromIndex(
const QModelIndex &idx,
const Constraint &c)
49 IndexType::iterator it = indexMap.find(idx);
50 while (it != indexMap.end() && it.key() == idx) {
52 it = indexMap.erase(it);
84 void ConstraintModel::init()
89 struct compare_constraint_indexes_to
91 compare_constraint_indexes_to(
const Constraint &c)
97 return m_c.compareIndexes(c);
111 QList<Constraint>::iterator it = std::find_if(
d->constraints.begin(),
112 d->constraints.end(),
113 compare_constraint_indexes_to(c));
115 if (it ==
d->constraints.end()) {
116 d->constraints.push_back(c);
118 d->addConstraintToIndex(c.
endIndex(), c);
120 }
else if ((*it).dataMap() != c.
dataMap()) {
123 d->constraints.push_back(c);
125 d->addConstraintToIndex(c.
endIndex(), c);
141 for (
int i = 0; i <
d->constraints.count(); i++) {
143 d->constraints.removeAt(i);
150 d->removeConstraintFromIndex(c.
endIndex(), c);
173 QSet<Constraint> orphans;
174 for (
const Constraint& c, qAsConst(
d->constraints)) {
178 d->constraints.subtract( orphans );
188 return d->constraints;
197 assert(!idx.isValid() ||
d->indexMap.isEmpty() || !
d->indexMap.keys().front().model() || idx.model() ==
d->indexMap.keys().front().model());
198 if (!idx.isValid()) {
200 QSet<Constraint> result;
205 return result.values();
207 QList<Constraint> result;
233 for (
int i = 0; i <
d->constraints.count(); i++)
240 #ifndef QT_NO_DEBUG_STREAM
244 dbg <<
"KDGantt::ConstraintModel[ " <<
static_cast<const QObject *
>(&model) <<
": [\n";
247 dbg <<
"\t" << c <<
"\n";
257 #ifndef KDAB_NO_UNIT_TESTS
259 #include <QStandardItemModel>
261 #include "unittest/test.h"
263 std::ostream &
operator<<(std::ostream &os,
const QModelIndex &idx)
268 os << str.toLatin1().constData();
270 os << str.toStdString();
277 QStandardItemModel dummyModel(100, 100);
280 QModelIndex invalidIndex;
281 assertEqual(invalidIndex, invalidIndex);
291 QPersistentModelIndex idx1 = dummyModel.index(7, 17, QModelIndex());
292 QPersistentModelIndex idx2 = dummyModel.index(42, 17, QModelIndex());
314 dummyModel.removeRow(8);
316 dummyModel.removeRow(7);
322 #include "moc_kdganttconstraintmodel.cpp"
The ConstraintModel keeps track of the interdependencies between gantt items in a View.
virtual void addConstraint(const Constraint &c)
Adds the constraint c to this ConstraintModel If the Constraint c is already in this ConstraintModel,...
void clear()
Removes all Constraints from this model The signal constraintRemoved(const Constraint&) is emitted fo...
~ConstraintModel() override
Destroys this ConstraintModel.
QList< Constraint > constraintsForIndex(const QModelIndex &) const
QList< Constraint > constraints() const
ConstraintModel(QObject *parent=nullptr)
Constructor.
bool hasConstraint(const Constraint &c) const
Returns true if a Constraint with start s and end e exists, otherwise false.
void constraintRemoved(const KDGantt::Constraint &)
virtual bool removeConstraint(const Constraint &c)
Removes the Constraint c from this ConstraintModel.
void constraintAdded(const KDGantt::Constraint &)
A class used to represent a dependency.
QModelIndex endIndex() const
bool compareIndexes(const Constraint &other) const
QModelIndex startIndex() const
QMap< int, QVariant > dataMap() const
QDebug operator<<(QDebug dbg, const KDGantt::ConstraintModel &model)
KDAB_SCOPED_UNITTEST_SIMPLE(KDGantt, ConstraintModel, "test")