File indexing completed on 2025-01-19 04:56:39

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org>
0003  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004  */
0005 
0006 
0007 #include "inboxpagemodel.h"
0008 
0009 #include <QMimeData>
0010 
0011 #include <KLocalizedString>
0012 
0013 #include "presentation/querytreemodel.h"
0014 
0015 using namespace Presentation;
0016 
0017 InboxPageModel::InboxPageModel(const Domain::TaskQueries::Ptr &taskQueries,
0018                                const Domain::TaskRepository::Ptr &taskRepository,
0019                                QObject *parent)
0020     : PageModel(parent),
0021       m_taskQueries(taskQueries),
0022       m_taskRepository(taskRepository)
0023 {
0024 }
0025 
0026 Domain::Task::Ptr InboxPageModel::addItem(const QString &title, const QModelIndex &parentIndex)
0027 {
0028     const auto parentData = parentIndex.data(QueryTreeModelBase::ObjectRole);
0029     const auto parentTask = parentData.value<Domain::Task::Ptr>();
0030 
0031     auto task = Domain::Task::Ptr::create();
0032     task->setTitle(title);
0033     const auto job = parentTask ? m_taskRepository->createChild(task, parentTask)
0034                    : m_taskRepository->create(task);
0035     installHandler(job, i18n("Cannot add task %1 in Inbox", title));
0036 
0037     return task;
0038 }
0039 
0040 void InboxPageModel::removeItem(const QModelIndex &index)
0041 {
0042     QVariant data = index.data(QueryTreeModelBase::ObjectRole);
0043     auto task = data.value<Domain::Task::Ptr>();
0044     Q_ASSERT(task);
0045     const auto job = m_taskRepository->remove(task);
0046     installHandler(job, i18n("Cannot remove task %1 from Inbox", task->title()));
0047 }
0048 
0049 void InboxPageModel::promoteItem(const QModelIndex &index)
0050 {
0051     QVariant data = index.data(QueryTreeModelBase::ObjectRole);
0052     auto task = data.value<Domain::Task::Ptr>();
0053     Q_ASSERT(task);
0054     const auto job = m_taskRepository->promoteToProject(task);
0055     installHandler(job, i18n("Cannot promote task %1 to be a project", task->title()));
0056 }
0057 
0058 QAbstractItemModel *InboxPageModel::createCentralListModel()
0059 {
0060     auto query = [this](const Domain::Task::Ptr &task) -> Domain::QueryResultInterface<Domain::Task::Ptr>::Ptr {
0061         if (!task)
0062             return m_taskQueries->findInboxTopLevel();
0063         else
0064             return m_taskQueries->findChildren(task);
0065     };
0066 
0067     auto flags = [](const Domain::Task::Ptr &) {
0068         return Qt::ItemIsSelectable
0069              | Qt::ItemIsEnabled
0070              | Qt::ItemIsEditable
0071              | Qt::ItemIsDragEnabled
0072              | Qt::ItemIsUserCheckable
0073              | Qt::ItemIsDropEnabled;
0074     };
0075 
0076     auto data = [](const Domain::Task::Ptr &task, int role, const TaskExtraDataPtr &info) -> QVariant {
0077         return defaultTaskData(task, role, info);
0078     };
0079 
0080     auto fetchAdditionalInfo = [this](const QModelIndex &index, const Domain::Task::Ptr &task) {
0081         return fetchTaskExtraData(m_taskQueries, TaskExtraPart::DataSource, index, task);
0082     };
0083 
0084     auto setData = [this](const Domain::Task::Ptr &task, const QVariant &value, int role) {
0085         if (role != Qt::EditRole && role != Qt::CheckStateRole) {
0086             return false;
0087         }
0088 
0089         const auto currentTitle = task->title();
0090         if (role == Qt::EditRole)
0091             task->setTitle(value.toString());
0092         else
0093             task->setDone(value.toInt() == Qt::Checked);
0094 
0095         const auto job = m_taskRepository->update(task);
0096         installHandler(job, i18n("Cannot modify task %1 in Inbox", currentTitle));
0097         return true;
0098     };
0099 
0100     auto drop = [this](const QMimeData *mimeData, Qt::DropAction, const Domain::Task::Ptr &parentTask) {
0101         if (!mimeData->hasFormat(QStringLiteral("application/x-zanshin-object")))
0102             return false;
0103 
0104         auto droppedTasks = mimeData->property("objects").value<Domain::Task::List>();
0105         if (droppedTasks.isEmpty())
0106             return false;
0107 
0108         foreach(const auto &childTask, droppedTasks) {
0109             if (parentTask) {
0110                 const auto job = m_taskRepository->associate(parentTask, childTask);
0111                 installHandler(job, i18n("Cannot move task %1 as sub-task of %2", childTask->title(), parentTask->title()));
0112             } else {
0113                 const auto job = m_taskRepository->dissociate(childTask);
0114                 installHandler(job, i18n("Cannot deparent task %1 from its parent", childTask->title()));
0115             }
0116         }
0117 
0118         return true;
0119     };
0120 
0121     auto drag = [](const Domain::Task::List &tasks) -> QMimeData* {
0122         if (tasks.isEmpty())
0123             return nullptr;
0124 
0125         auto data = new QMimeData;
0126         data->setData(QStringLiteral("application/x-zanshin-object"), "object");
0127         data->setProperty("objects", QVariant::fromValue(tasks));
0128         return data;
0129     };
0130 
0131     return new QueryTreeModel<Domain::Task::Ptr, TaskExtraDataPtr>(query, flags, data, setData, drop, drag, fetchAdditionalInfo, this);
0132 }
0133 
0134 #include "moc_inboxpagemodel.cpp"