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

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Mario Bensi <mbensi@ipsquad.net>
0003    SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org>
0004    * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005    */
0006 
0007 
0008 
0009 #ifndef PRESENTATION_QUERYTREEMODEL_H
0010 #define PRESENTATION_QUERYTREEMODEL_H
0011 
0012 #include "querytreenode.h"
0013 
0014 #include <functional>
0015 #include <algorithm>
0016 
0017 namespace Presentation {
0018 
0019 template<typename ItemType, typename AdditionalInfo = int>
0020 class QueryTreeModel : public QueryTreeModelBase
0021 {
0022 public:
0023     typedef typename QueryTreeNode<ItemType, AdditionalInfo>::QueryGenerator QueryGenerator;
0024     typedef typename QueryTreeNode<ItemType, AdditionalInfo>::FlagsFunction FlagsFunction;
0025     typedef typename QueryTreeNode<ItemType, AdditionalInfo>::DataFunction DataFunction;
0026     typedef typename QueryTreeNode<ItemType, AdditionalInfo>::SetDataFunction SetDataFunction;
0027     typedef typename QueryTreeNode<ItemType, AdditionalInfo>::DropFunction DropFunction;
0028     typedef std::function<QMimeData*(const QList<ItemType> &)> DragFunction;
0029     using FetchAdditionalInfoFunction = std::function<AdditionalInfo(const QModelIndex &index, ItemType)>;
0030     using NodeType = QueryTreeNode<ItemType, AdditionalInfo>;
0031 
0032     explicit QueryTreeModel(const QueryGenerator &queryGenerator,
0033                             const FlagsFunction &flagsFunction,
0034                             const DataFunction &dataFunction,
0035                             const SetDataFunction &setDataFunction,
0036                             QObject *parent = nullptr)
0037         : QueryTreeModelBase(new QueryTreeNode<ItemType, AdditionalInfo>(ItemType(), nullptr, this,
0038                                                          queryGenerator, flagsFunction,
0039                                                          dataFunction, setDataFunction),
0040                              parent)
0041     {
0042     }
0043 
0044     explicit QueryTreeModel(const QueryGenerator &queryGenerator,
0045                             const FlagsFunction &flagsFunction,
0046                             const DataFunction &dataFunction,
0047                             const SetDataFunction &setDataFunction,
0048                             const DropFunction &dropFunction,
0049                             const DragFunction &dragFunction,
0050                             const FetchAdditionalInfoFunction &fetchAdditionalInfoFunction,
0051                             QObject *parent = nullptr)
0052         : QueryTreeModelBase(new QueryTreeNode<ItemType, AdditionalInfo>(ItemType(), nullptr, this,
0053                                                          queryGenerator, flagsFunction,
0054                                                          dataFunction, setDataFunction,
0055                                                          dropFunction),
0056                              parent),
0057           m_dragFunction(dragFunction),
0058           m_fetchAdditionalInfoFunction(fetchAdditionalInfoFunction)
0059     {
0060     }
0061 
0062 protected:
0063     QMimeData *createMimeData(const QModelIndexList &indexes) const override
0064     {
0065         if (m_dragFunction) {
0066             QList<ItemType> items;
0067             std::transform(indexes.begin(), indexes.end(),
0068                            std::back_inserter(items),
0069                            [this](const QModelIndex &index) {
0070                                return itemAtIndex(index);
0071                            });
0072             return m_dragFunction(items);
0073         } else {
0074             return nullptr;
0075         }
0076     }
0077 
0078 
0079     void fetchAdditionalInfo(const QModelIndex &index) override
0080     {
0081         if (m_fetchAdditionalInfoFunction) {
0082             auto theNode = node(index);
0083             if (!theNode->hasAdditionalInfo())
0084                 theNode->setAdditionalInfo(m_fetchAdditionalInfoFunction(index, theNode->item()));
0085         }
0086     }
0087 
0088     ItemType itemAtIndex(const QModelIndex &index) const
0089     {
0090         return node(index)->item();
0091     }
0092 
0093     NodeType *node(const QModelIndex &index) const
0094     {
0095         return static_cast<NodeType *>(nodeFromIndex(index));
0096     }
0097 
0098 private:
0099     DragFunction m_dragFunction;
0100     FetchAdditionalInfoFunction m_fetchAdditionalInfoFunction;
0101 };
0102 
0103 }
0104 
0105 #endif // PRESENTATION_QUERYTREEMODEL_H