FluentUI/src/FluTreeModel.h

123 lines
3.8 KiB
C
Raw Normal View History

2023-09-15 19:11:55 +08:00
#ifndef FLUTREEMODEL_H
#define FLUTREEMODEL_H
#include <QObject>
#include <QAbstractTableModel>
2023-09-17 20:36:33 +08:00
#include <QJsonArray>
2024-04-09 20:53:52 +08:00
#include <QVariant>
2023-09-15 19:11:55 +08:00
#include <QtQml/qqml.h>
2023-09-17 20:36:33 +08:00
#include "stdafx.h"
2024-02-27 12:23:24 +08:00
/**
2024-04-03 11:19:35 +08:00
* @brief The FluTreeNode class
2024-02-27 12:23:24 +08:00
*/
2024-04-03 11:19:35 +08:00
class FluTreeNode : public QObject{
2023-09-17 20:36:33 +08:00
Q_OBJECT
2024-04-09 20:53:52 +08:00
Q_PROPERTY(QVariantMap data READ data CONSTANT)
2023-09-17 20:36:33 +08:00
Q_PROPERTY(int depth READ depth CONSTANT)
Q_PROPERTY(bool isExpanded READ isExpanded CONSTANT)
2023-09-22 17:35:02 +08:00
Q_PROPERTY(bool checked READ checked CONSTANT)
2023-09-17 20:36:33 +08:00
public:
2024-04-03 11:19:35 +08:00
explicit FluTreeNode(QObject *parent = nullptr);
2023-09-17 20:36:33 +08:00
Q_INVOKABLE int depth(){return _depth;};
Q_INVOKABLE bool isExpanded(){return _isExpanded;};
2024-04-09 20:53:52 +08:00
Q_INVOKABLE QVariantMap data(){return _data;};
2023-09-17 20:36:33 +08:00
Q_INVOKABLE bool hasChildren(){ return !_children.isEmpty();};
2023-09-18 18:10:27 +08:00
Q_INVOKABLE bool hasNextNodeByIndex(int index){
2024-04-03 11:19:35 +08:00
FluTreeNode* p = this;
2023-09-18 18:10:27 +08:00
for(int i=0;i<(_depth - index -1);i++){
p = p->_parent;
}
if(p->_parent->_children.indexOf(p) == p->_parent->_children.count()-1){
return false;
}
return true;
}
2023-09-22 17:35:02 +08:00
Q_INVOKABLE bool checked(){
if(!hasChildren()){
return _checked;
}
foreach (auto item, _children) {
if(!item->checked()){
return false;
}
}
return true;
};
2023-09-17 20:36:33 +08:00
Q_INVOKABLE bool hideLineFooter(){
if(_parent){
auto childIndex = _parent->_children.indexOf(this);
if(childIndex==_parent->_children.count()-1){
return true;
}
if(_parent->_children.at(childIndex+1)->hasChildren()){
return true;
}
return false;
}
return false;
};
bool isShown(){
auto p = _parent;
while (p) {
if(!p->_isExpanded){
return false;
}
p = p->_parent;
}
return true;
}
public:
QString _title="";
int _depth=0;
2023-09-22 17:35:02 +08:00
bool _checked = false;
2023-09-17 20:36:33 +08:00
bool _isExpanded=true;
2024-04-09 20:53:52 +08:00
QVariantMap _data;
2024-04-03 11:19:35 +08:00
QList<FluTreeNode*> _children;
FluTreeNode* _parent = nullptr;
2023-09-17 20:36:33 +08:00
};
2023-09-15 19:11:55 +08:00
2023-09-19 23:41:56 +08:00
class FluTreeModel : public QAbstractItemModel
2023-09-15 19:11:55 +08:00
{
Q_OBJECT
2023-09-17 20:36:33 +08:00
Q_PROPERTY_AUTO(int,dataSourceSize)
2024-04-03 11:19:35 +08:00
Q_PROPERTY_AUTO(QList<FluTreeNode*>,selectionModel)
2024-04-09 20:53:52 +08:00
Q_PROPERTY_AUTO(QList<QVariantMap>,columnSource)
2023-09-15 19:11:55 +08:00
QML_NAMED_ELEMENT(FluTreeModel)
QML_ADDED_IN_MINOR_VERSION(1)
public:
2024-04-09 20:53:52 +08:00
enum TreeModelRoles {
RowModel = 0x0101,
ColumnModel = 0x0102
};
2023-09-15 19:11:55 +08:00
explicit FluTreeModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;
2023-09-19 23:41:56 +08:00
QModelIndex parent(const QModelIndex &child) const override;
QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const override;
2023-09-15 19:11:55 +08:00
Q_INVOKABLE void removeRows(int row,int count);
2024-04-03 11:19:35 +08:00
Q_INVOKABLE void insertRows(int row,QList<FluTreeNode*> data);
2023-09-15 19:11:55 +08:00
Q_INVOKABLE QObject* getRow(int row);
2024-04-09 20:53:52 +08:00
Q_INVOKABLE void setRow(int row,QVariantMap data);
2024-04-03 11:19:35 +08:00
Q_INVOKABLE void setData(QList<FluTreeNode*> data);
2023-09-17 20:36:33 +08:00
Q_INVOKABLE void setDataSource(QList<QMap<QString,QVariant>> data);
Q_INVOKABLE void collapse(int row);
Q_INVOKABLE void expand(int row);
2024-04-03 11:19:35 +08:00
Q_INVOKABLE FluTreeNode* getNode(int row);
2023-09-18 18:10:27 +08:00
Q_INVOKABLE void refreshNode(int row);
2024-03-31 21:52:06 +08:00
Q_INVOKABLE void checkRow(int row,bool checked);
2023-09-19 00:31:49 +08:00
Q_INVOKABLE bool hitHasChildrenExpanded(int row);
Q_INVOKABLE void allExpand();
Q_INVOKABLE void allCollapse();
2023-09-15 19:11:55 +08:00
private:
2024-04-03 11:19:35 +08:00
QList<FluTreeNode*> _rows;
QList<FluTreeNode*> _dataSource;
FluTreeNode* _root = nullptr;
2023-09-15 19:11:55 +08:00
};
#endif // FLUTREEMODEL_H