File indexing completed on 2025-01-05 05:14:40
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #pragma once 0008 0009 #include <QAbstractItemModel> 0010 0011 #include <QIcon> 0012 0013 class TreeModel : public QAbstractItemModel 0014 { 0015 Q_OBJECT 0016 0017 public: 0018 struct Node { 0019 QString title; 0020 QList<Node *> childs; 0021 Node *parent; 0022 struct Feature *feature; 0023 int row{}; 0024 QStringList data; 0025 QString key; 0026 QString prefix; 0027 #ifdef TREEMODEL_NODE_DATA_TYPE 0028 TREEMODEL_NODE_DATA_TYPE metaData; 0029 #endif 0030 Node() 0031 : parent(nullptr) 0032 , feature(nullptr) 0033 { 0034 } 0035 explicit Node(Node *parent) 0036 : parent(parent) 0037 , feature(nullptr) 0038 { 0039 row = parent->childs.count(); 0040 } 0041 0042 ~Node() 0043 { 0044 qDeleteAll(childs); 0045 } 0046 0047 Node *createChild() 0048 { 0049 auto ch = new Node; 0050 ch->parent = this; 0051 ch->row = childs.count(); 0052 childs.append(ch); 0053 return ch; 0054 } 0055 Node *find(const QString &title) 0056 { 0057 auto i = childs.begin(); 0058 while (i != childs.end()) { 0059 if ((*i)->title == title) 0060 return *i; 0061 ++i; 0062 } 0063 return nullptr; 0064 } 0065 0066 void clear() 0067 { 0068 qDeleteAll(childs); 0069 childs.clear(); 0070 } 0071 }; 0072 0073 explicit TreeModel(QObject *parent = nullptr); 0074 0075 QModelIndex index(const Node *node, int col) const; 0076 int rowCount(const QModelIndex &parent) const override; 0077 int columnCount(const QModelIndex &parent) const override; 0078 QVariant data(const QModelIndex &index, int role) const override; 0079 QModelIndex index(int row, int column, const QModelIndex &parent) const override; 0080 QModelIndex parent(const QModelIndex &child) const override; 0081 QVariant headerData(int section, Qt::Orientation orientation, int role) const override; 0082 QStringList rootData() const; 0083 QStringList data(const QModelIndex &index) const; 0084 QString fullPath(const QModelIndex &index) const; 0085 QString key(const QModelIndex &index) const; 0086 QString section(const QModelIndex &index) const; 0087 void sortItems(); 0088 0089 void addData(const QStringList &data, const QString &prefix = QString(), bool split = true); 0090 0091 const QString &separator() const; 0092 void setSeparator(const QString &newSeparator); 0093 0094 bool lastPartAsData() const; 0095 void setLastPartAsData(bool newLastPartAsData); 0096 0097 const QIcon &defaultIcon() const; 0098 void setDefaultIcon(const QIcon &newDefaultIcon); 0099 0100 void clear(); 0101 0102 bool showRoot() const; 0103 void setShowRoot(bool newDefaultRoot); 0104 0105 protected: 0106 Node *mRootNode = nullptr; 0107 0108 Node *createPath(const QStringList &path); 0109 Node *find(QStringList &path, Node *node = nullptr); 0110 void getFullPath(QString &path, Node *node) const; 0111 void sortNode(Node *node); 0112 0113 private: 0114 QString mSeparator{QStringLiteral("/")}; 0115 bool mLastPartAsData{false}; 0116 QIcon mDefaultIcon; 0117 bool mShowRoot{false}; 0118 };