FluentUI/src/FluTreeModel.h

152 lines
3.9 KiB
C
Raw Normal View History

2024-04-11 14:51:43 +08:00
#pragma once
2023-09-15 19:11:55 +08:00
#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-06-28 17:09:23 +08:00
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-11 14:51:43 +08:00
class FluTreeNode : public QObject {
2024-06-28 17:09:23 +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);
2024-04-11 14:51:43 +08:00
2024-06-28 17:09:23 +08:00
[[nodiscard]] Q_INVOKABLE int depth() const {
return _depth;
};
2024-04-11 15:27:15 +08:00
2024-06-28 17:09:23 +08:00
[[nodiscard]] Q_INVOKABLE bool isExpanded() const {
return _isExpanded;
};
2024-04-11 15:27:15 +08:00
2024-06-28 17:09:23 +08:00
[[nodiscard]] Q_INVOKABLE QVariantMap data() const {
return _data;
};
2024-04-11 15:27:15 +08:00
2024-06-28 17:09:23 +08:00
[[nodiscard]] Q_INVOKABLE bool hasChildren() const {
return !_children.isEmpty();
};
2024-04-11 15:27:15 +08:00
2024-04-11 14:51:43 +08:00
Q_INVOKABLE bool hasNextNodeByIndex(int index) {
FluTreeNode *p = this;
for (int i = 0; i <= _depth - index - 1; i++) {
2023-09-18 18:10:27 +08:00
p = p->_parent;
}
2024-04-11 14:51:43 +08:00
if (p->_parent->_children.indexOf(p) == p->_parent->_children.count() - 1) {
2023-09-18 18:10:27 +08:00
return false;
}
return true;
}
2024-04-11 14:51:43 +08:00
2024-04-11 15:27:15 +08:00
[[nodiscard]] Q_INVOKABLE bool checked() const {
2024-04-11 14:51:43 +08:00
if (!hasChildren()) {
2023-09-22 17:35:02 +08:00
return _checked;
}
2024-04-11 14:51:43 +08:00
for (int i = 0; i <= _children.size() - 1; ++i) {
auto item = _children.at(i);
if (!item->checked()) {
2023-09-22 17:35:02 +08:00
return false;
}
}
return true;
};
2024-04-28 20:22:05 +08:00
2024-04-11 14:51:43 +08:00
Q_INVOKABLE bool hideLineFooter() {
if (_parent) {
auto childIndex = _parent->_children.indexOf(this);
if (childIndex == _parent->_children.count() - 1) {
2023-09-17 20:36:33 +08:00
return true;
}
2024-04-11 14:51:43 +08:00
if (_parent->_children.at(childIndex + 1)->hasChildren()) {
2023-09-17 20:36:33 +08:00
return true;
}
return false;
}
return false;
};
2024-04-11 14:51:43 +08:00
2024-04-11 15:27:15 +08:00
[[nodiscard]] bool isShown() const {
2023-09-17 20:36:33 +08:00
auto p = _parent;
while (p) {
2024-04-11 14:51:43 +08:00
if (!p->_isExpanded) {
2023-09-17 20:36:33 +08:00
return false;
}
p = p->_parent;
}
return true;
}
2024-04-11 14:51:43 +08:00
2023-09-17 20:36:33 +08:00
public:
2024-04-11 14:51:43 +08:00
QString _title = "";
int _depth = 0;
2023-09-22 17:35:02 +08:00
bool _checked = false;
2024-04-11 14:51:43 +08:00
bool _isExpanded = true;
2024-04-09 20:53:52 +08:00
QVariantMap _data;
2024-04-11 14:51:43 +08:00
QList<FluTreeNode *> _children;
FluTreeNode *_parent = nullptr;
2023-09-17 20:36:33 +08:00
};
2023-09-15 19:11:55 +08:00
2024-04-28 18:19:21 +08:00
class FluTreeModel : public QAbstractTableModel {
2024-06-28 17:09:23 +08:00
Q_OBJECT
Q_PROPERTY_AUTO(int, dataSourceSize)
Q_PROPERTY_AUTO(QList<QVariantMap>, columnSource)
2023-09-15 19:11:55 +08:00
QML_NAMED_ELEMENT(FluTreeModel)
public:
2024-06-28 17:09:23 +08:00
enum TreeModelRoles { RowModel = 0x0101, ColumnModel = 0x0102 };
2024-04-11 14:51:43 +08:00
2023-09-15 19:11:55 +08:00
explicit FluTreeModel(QObject *parent = nullptr);
2024-04-11 14:51:43 +08:00
2024-04-11 15:27:15 +08:00
[[nodiscard]] int rowCount(const QModelIndex &parent = {}) const override;
2024-04-11 14:51:43 +08:00
2024-04-11 15:27:15 +08:00
[[nodiscard]] int columnCount(const QModelIndex &parent = {}) const override;
2024-04-11 14:51:43 +08:00
2024-06-28 17:09:23 +08:00
[[nodiscard]] QVariant data(const QModelIndex &index,
int role = Qt::DisplayRole) const override;
2024-04-11 14:51:43 +08:00
2024-04-11 15:27:15 +08:00
[[nodiscard]] QHash<int, QByteArray> roleNames() const override;
2024-04-11 14:51:43 +08:00
Q_INVOKABLE void removeRows(int row, int count);
Q_INVOKABLE void insertRows(int row, const QList<FluTreeNode *> &data);
Q_INVOKABLE QObject *getRow(int row);
Q_INVOKABLE void setRow(int row, QVariantMap data);
Q_INVOKABLE void setData(QList<FluTreeNode *> data);
Q_INVOKABLE void setDataSource(QList<QMap<QString, QVariant>> data);
2023-09-17 20:36:33 +08:00
Q_INVOKABLE void collapse(int row);
2024-04-11 14:51:43 +08:00
2023-09-17 20:36:33 +08:00
Q_INVOKABLE void expand(int row);
2024-04-11 14:51:43 +08:00
Q_INVOKABLE FluTreeNode *getNode(int row);
2023-09-18 18:10:27 +08:00
Q_INVOKABLE void refreshNode(int row);
2024-04-11 14:51:43 +08:00
Q_INVOKABLE void checkRow(int row, bool checked);
2023-09-19 00:31:49 +08:00
Q_INVOKABLE bool hitHasChildrenExpanded(int row);
2024-04-11 14:51:43 +08:00
2023-09-19 00:31:49 +08:00
Q_INVOKABLE void allExpand();
2024-04-11 14:51:43 +08:00
2023-09-19 00:31:49 +08:00
Q_INVOKABLE void allCollapse();
2024-04-11 14:51:43 +08:00
2024-04-14 23:58:15 +08:00
Q_INVOKABLE QVariant selectionModel();
2023-09-15 19:11:55 +08:00
private:
2024-04-11 14:51:43 +08:00
QList<FluTreeNode *> _rows;
QList<FluTreeNode *> _dataSource;
FluTreeNode *_root = nullptr;
2023-09-15 19:11:55 +08:00
};