/* * SPDX-FileCopyrightText: 2014 Mario Bensi SPDX-FileCopyrightText: 2014 Kevin Ottens * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL */ #ifndef PRESENTATION_QUERYTREEMODEL_H #define PRESENTATION_QUERYTREEMODEL_H #include "querytreenode.h" #include #include namespace Presentation { template class QueryTreeModel : public QueryTreeModelBase { public: typedef typename QueryTreeNode::QueryGenerator QueryGenerator; typedef typename QueryTreeNode::FlagsFunction FlagsFunction; typedef typename QueryTreeNode::DataFunction DataFunction; typedef typename QueryTreeNode::SetDataFunction SetDataFunction; typedef typename QueryTreeNode::DropFunction DropFunction; typedef std::function &)> DragFunction; using FetchAdditionalInfoFunction = std::function; using NodeType = QueryTreeNode; explicit QueryTreeModel(const QueryGenerator &queryGenerator, const FlagsFunction &flagsFunction, const DataFunction &dataFunction, const SetDataFunction &setDataFunction, QObject *parent = nullptr) : QueryTreeModelBase(new QueryTreeNode(ItemType(), nullptr, this, queryGenerator, flagsFunction, dataFunction, setDataFunction), parent) { } explicit QueryTreeModel(const QueryGenerator &queryGenerator, const FlagsFunction &flagsFunction, const DataFunction &dataFunction, const SetDataFunction &setDataFunction, const DropFunction &dropFunction, const DragFunction &dragFunction, const FetchAdditionalInfoFunction &fetchAdditionalInfoFunction, QObject *parent = nullptr) : QueryTreeModelBase(new QueryTreeNode(ItemType(), nullptr, this, queryGenerator, flagsFunction, dataFunction, setDataFunction, dropFunction), parent), m_dragFunction(dragFunction), m_fetchAdditionalInfoFunction(fetchAdditionalInfoFunction) { } protected: QMimeData *createMimeData(const QModelIndexList &indexes) const override { if (m_dragFunction) { QList items; std::transform(indexes.begin(), indexes.end(), std::back_inserter(items), [this](const QModelIndex &index) { return itemAtIndex(index); }); return m_dragFunction(items); } else { return nullptr; } } void fetchAdditionalInfo(const QModelIndex &index) override { if (m_fetchAdditionalInfoFunction) { auto theNode = node(index); if (!theNode->hasAdditionalInfo()) theNode->setAdditionalInfo(m_fetchAdditionalInfoFunction(index, theNode->item())); } } ItemType itemAtIndex(const QModelIndex &index) const { return node(index)->item(); } NodeType *node(const QModelIndex &index) const { return static_cast(nodeFromIndex(index)); } private: DragFunction m_dragFunction; FetchAdditionalInfoFunction m_fetchAdditionalInfoFunction; }; } #endif // PRESENTATION_QUERYTREEMODEL_H