diff --git a/src/presentation/contextpagemodel.cpp b/src/presentation/contextpagemodel.cpp --- a/src/presentation/contextpagemodel.cpp +++ b/src/presentation/contextpagemodel.cpp @@ -68,7 +68,8 @@ void ContextPageModel::removeItem(const QModelIndex &index) { QVariant data = index.data(QueryTreeModel::ObjectRole); - auto task = data.value(); + auto artifact = data.value(); + auto task = artifact.objectCast(); const auto job = m_contextRepository->dissociate(m_context, task); installHandler(job, tr("Cannot remove task %1 from context %2").arg(task->title()).arg(m_context->name())); } diff --git a/src/presentation/noteinboxpagemodel.cpp b/src/presentation/noteinboxpagemodel.cpp --- a/src/presentation/noteinboxpagemodel.cpp +++ b/src/presentation/noteinboxpagemodel.cpp @@ -52,7 +52,9 @@ void NoteInboxPageModel::removeItem(const QModelIndex &index) { QVariant data = index.data(QueryTreeModel::ObjectRole); - auto note = data.value(); + auto artifact = data.value(); + auto note = artifact.objectCast(); + Q_ASSERT(note); const auto job = m_noteRepository->remove(note); installHandler(job, tr("Cannot remove note %1 from Inbox").arg(note->title())); } diff --git a/src/presentation/querytreenode.h b/src/presentation/querytreenode.h --- a/src/presentation/querytreenode.h +++ b/src/presentation/querytreenode.h @@ -28,12 +28,37 @@ #include +// Qt5 TODO, shouldn't be needed anymore, QVariant will do the right thing +#include "domain/note.h" +#include "domain/task.h" + #include "domain/queryresultinterface.h" #include "querytreemodelbase.h" namespace Presentation { +// Qt5 TODO, shouldn't be needed anymore, QVariant will do the right thing +namespace Internal { + template + QVariant variantFromValue(const T &object) + { + return QVariant::fromValue(object); + } + + template<> + inline QVariant variantFromValue(const Domain::Note::Ptr &task) + { + return QVariant::fromValue(task.staticCast()); + } + + template<> + inline QVariant variantFromValue(const Domain::Task::Ptr ¬e) + { + return QVariant::fromValue(note.staticCast()); + } +} + template class QueryTreeNode : public QueryTreeNodeBase { @@ -84,7 +109,7 @@ QVariant data(int role) const Q_DECL_OVERRIDE { if (role == QueryTreeModelBase::ObjectRole) - return QVariant::fromValue(m_item); + return Internal::variantFromValue(m_item); return m_dataFunction(m_item, role); } diff --git a/src/presentation/tagpagemodel.cpp b/src/presentation/tagpagemodel.cpp --- a/src/presentation/tagpagemodel.cpp +++ b/src/presentation/tagpagemodel.cpp @@ -68,7 +68,9 @@ void TagPageModel::removeItem(const QModelIndex &index) { QVariant data = index.data(QueryTreeModel::ObjectRole); - auto note = data.value(); + auto artifact = data.value(); + auto note = artifact.objectCast(); + Q_ASSERT(note); const auto job = m_tagRepository->dissociate(m_tag, note); installHandler(job, tr("Cannot remove note %1 from tag %2").arg(note->title()).arg(m_tag->name())); } diff --git a/tests/units/presentation/querytreemodeltest.cpp b/tests/units/presentation/querytreemodeltest.cpp --- a/tests/units/presentation/querytreemodeltest.cpp +++ b/tests/units/presentation/querytreemodeltest.cpp @@ -719,6 +719,42 @@ QCOMPARE(data.value(), provider->data().at(1)); } + void shouldProvideUnderlyingTaskAsArtifact() + { + // GIVEN + auto provider = Domain::QueryResultProvider::Ptr::create(); + foreach (const auto &task, createTasks()) + provider->append(task); + + auto queryGenerator = [&](const Domain::Task::Ptr &artifact) { + if (!artifact) + return Domain::QueryResult::create(provider); + else + return Domain::QueryResult::Ptr(); + }; + auto flagsFunction = [](const Domain::Task::Ptr &) { + return Qt::NoItemFlags; + }; + auto dataFunction = [](const Domain::Task::Ptr &, int) { + return QVariant(); + }; + auto setDataFunction = [](const Domain::Task::Ptr &, const QVariant &, int) { + return false; + }; + Presentation::QueryTreeModel model(queryGenerator, flagsFunction, dataFunction, setDataFunction); + new ModelTest(&model); + + // WHEN + const QModelIndex index = model.index(1, 0); + const QVariant data = index.data(Presentation::QueryTreeModel::ObjectRole); + + // THEN + QVERIFY(data.isValid()); + // Note it says Artifact and *not* Task here, it should up-cast automatically + QVERIFY(!data.value().isNull()); + QCOMPARE(data.value(), provider->data().at(1).staticCast()); + } + void shouldCreateMimeData() { // GIVEN