mirror of
https://github.com/zhuzichu520/FluentUI.git
synced 2025-07-06 18:55:58 +08:00
update
This commit is contained in:
@ -1,75 +1,80 @@
|
||||
#pragma clang diagnostic push
|
||||
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
|
||||
#pragma ide diagnostic ignored "google-default-arguments"
|
||||
|
||||
#include "FluTreeModel.h"
|
||||
|
||||
#include <QMetaEnum>
|
||||
#include <utility>
|
||||
|
||||
FluTreeNode::FluTreeNode(QObject *parent): QObject{parent}{
|
||||
FluTreeNode::FluTreeNode(QObject *parent) : QObject{parent} {
|
||||
}
|
||||
|
||||
FluTreeModel::FluTreeModel(QObject *parent): QAbstractItemModel{parent}{
|
||||
dataSourceSize(0);
|
||||
FluTreeModel::FluTreeModel(QObject *parent) : QAbstractItemModel{parent} {
|
||||
_dataSourceSize = 0;
|
||||
}
|
||||
|
||||
QModelIndex FluTreeModel::parent(const QModelIndex &child) const{
|
||||
return QModelIndex();
|
||||
QModelIndex FluTreeModel::parent(const QModelIndex &child) const {
|
||||
return {};
|
||||
}
|
||||
|
||||
QModelIndex FluTreeModel::index(int row, int column,const QModelIndex &parent) const{
|
||||
QModelIndex FluTreeModel::index(int row, int column, const QModelIndex &parent) const {
|
||||
if (!hasIndex(row, column, parent) || parent.isValid())
|
||||
return QModelIndex();
|
||||
return {};
|
||||
return createIndex(row, column, _rows.at(row));
|
||||
}
|
||||
|
||||
int FluTreeModel::rowCount(const QModelIndex &parent) const {
|
||||
return _rows.count();
|
||||
};
|
||||
}
|
||||
|
||||
int FluTreeModel::columnCount(const QModelIndex &parent) const {
|
||||
return this->_columnSource.size();
|
||||
};
|
||||
}
|
||||
|
||||
QVariant FluTreeModel::data(const QModelIndex &index, int role) const {
|
||||
switch (role) {
|
||||
case TreeModelRoles::RowModel:
|
||||
return QVariant::fromValue(_rows.at(index.row()));
|
||||
case TreeModelRoles::ColumnModel:
|
||||
return QVariant::fromValue(_columnSource.at(index.column()));
|
||||
default:
|
||||
break;
|
||||
case TreeModelRoles::RowModel:
|
||||
return QVariant::fromValue(_rows.at(index.row()));
|
||||
case TreeModelRoles::ColumnModel:
|
||||
return QVariant::fromValue(_columnSource.at(index.column()));
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QVariant();
|
||||
};
|
||||
return {};
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> FluTreeModel::roleNames() const {
|
||||
return {
|
||||
{TreeModelRoles::RowModel, "rowModel"},
|
||||
{TreeModelRoles::ColumnModel, "columnModel"}
|
||||
{TreeModelRoles::RowModel, "rowModel"},
|
||||
{TreeModelRoles::ColumnModel, "columnModel"}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
void FluTreeModel::setData(QList<FluTreeNode*> data){
|
||||
void FluTreeModel::setData(QList<FluTreeNode *> data) {
|
||||
beginResetModel();
|
||||
_rows = data;
|
||||
_rows = std::move(data);
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void FluTreeModel::removeRows(int row,int count){
|
||||
if (row < 0 || row + count > _rows.size() || count==0)
|
||||
void FluTreeModel::removeRows(int row, int count) {
|
||||
if (row < 0 || row + count > _rows.size() || count == 0)
|
||||
return;
|
||||
beginRemoveRows(QModelIndex(),row, row + count - 1);
|
||||
QList<FluTreeNode*> firstPart = _rows.mid(0,row);
|
||||
QList<FluTreeNode*> secondPart = _rows.mid(row + count);
|
||||
beginRemoveRows(QModelIndex(), row, row + count - 1);
|
||||
QList<FluTreeNode *> firstPart = _rows.mid(0, row);
|
||||
QList<FluTreeNode *> secondPart = _rows.mid(row + count);
|
||||
_rows.clear();
|
||||
_rows.append(firstPart);
|
||||
_rows.append(secondPart);
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void FluTreeModel::insertRows(int row,QList<FluTreeNode*> data){
|
||||
if (row < 0 || row > _rows.size() || data.size() == 0)
|
||||
return;;
|
||||
void FluTreeModel::insertRows(int row, const QList<FluTreeNode *> &data) {
|
||||
if (row < 0 || row > _rows.size() || data.empty())
|
||||
return;
|
||||
beginInsertRows(QModelIndex(), row, row + data.size() - 1);
|
||||
QList<FluTreeNode*> firstPart = _rows.mid(0, row);
|
||||
QList<FluTreeNode*> secondPart = _rows.mid(row);
|
||||
QList<FluTreeNode *> firstPart = _rows.mid(0, row);
|
||||
QList<FluTreeNode *> secondPart = _rows.mid(row);
|
||||
_rows.clear();
|
||||
_rows.append(firstPart);
|
||||
_rows.append(data);
|
||||
@ -77,83 +82,83 @@ void FluTreeModel::insertRows(int row,QList<FluTreeNode*> data){
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
QObject* FluTreeModel::getRow(int row){
|
||||
QObject *FluTreeModel::getRow(int row) {
|
||||
return _rows.at(row);
|
||||
}
|
||||
|
||||
void FluTreeModel::setRow(int row,QVariantMap data){
|
||||
_rows.at(row)->_data = data;
|
||||
Q_EMIT dataChanged(index(row,0),index(row,columnCount()-1));
|
||||
void FluTreeModel::setRow(int row, QVariantMap data) {
|
||||
_rows.at(row)->_data = std::move(data);
|
||||
Q_EMIT dataChanged(index(row, 0), index(row, columnCount() - 1));
|
||||
}
|
||||
|
||||
void FluTreeModel::checkRow(int row,bool checked){
|
||||
void FluTreeModel::checkRow(int row, bool checked) {
|
||||
auto itemData = _rows.at(row);
|
||||
if(itemData->hasChildren()){
|
||||
QList<FluTreeNode*> stack = itemData->_children;
|
||||
if (itemData->hasChildren()) {
|
||||
QList<FluTreeNode *> stack = itemData->_children;
|
||||
std::reverse(stack.begin(), stack.end());
|
||||
while (stack.count() > 0) {
|
||||
auto item = stack.at(stack.count()-1);
|
||||
auto item = stack.at(stack.count() - 1);
|
||||
stack.pop_back();
|
||||
if(!item->hasChildren()){
|
||||
if (!item->hasChildren()) {
|
||||
item->_checked = checked;
|
||||
}
|
||||
QList<FluTreeNode*> children = item->_children;
|
||||
if(!children.isEmpty()){
|
||||
QList<FluTreeNode *> children = item->_children;
|
||||
if (!children.isEmpty()) {
|
||||
std::reverse(children.begin(), children.end());
|
||||
foreach (auto c, children) {
|
||||
stack.append(c);
|
||||
}
|
||||
foreach (auto c, children) {
|
||||
stack.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(itemData->_checked == checked){
|
||||
} else {
|
||||
if (itemData->_checked == checked) {
|
||||
return;
|
||||
}
|
||||
itemData->_checked = checked;
|
||||
}
|
||||
Q_EMIT dataChanged(index(0,0),index(rowCount()-1,0));
|
||||
QList<FluTreeNode*> data;
|
||||
foreach (auto item, _dataSource) {
|
||||
if(!item->hasChildren()){
|
||||
if(item->_checked){
|
||||
data.append(item);
|
||||
Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0));
|
||||
QList<FluTreeNode *> data;
|
||||
foreach (auto item, _dataSource) {
|
||||
if (!item->hasChildren()) {
|
||||
if (item->_checked) {
|
||||
data.append(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
selectionModel(data);
|
||||
}
|
||||
|
||||
void FluTreeModel::setDataSource(QList<QMap<QString,QVariant>> data){
|
||||
void FluTreeModel::setDataSource(QList<QMap<QString, QVariant>> data) {
|
||||
_dataSource.clear();
|
||||
if(_root){
|
||||
if (_root) {
|
||||
delete _root;
|
||||
_root = nullptr;
|
||||
}
|
||||
_root = new FluTreeNode(this);
|
||||
std::reverse(data.begin(), data.end());
|
||||
while (data.count() > 0) {
|
||||
auto item = data.at(data.count()-1);
|
||||
auto item = data.at(data.count() - 1);
|
||||
data.pop_back();
|
||||
FluTreeNode* node = new FluTreeNode(this);
|
||||
auto *node = new FluTreeNode(this);
|
||||
node->_depth = item.value("__depth").toInt();
|
||||
node->_parent = item.value("__parent").value<FluTreeNode*>();
|
||||
node->_parent = item.value("__parent").value<FluTreeNode *>();
|
||||
node->_data = item;
|
||||
node->_isExpanded = true;
|
||||
if(node->_parent){
|
||||
if (node->_parent) {
|
||||
node->_parent->_children.append(node);
|
||||
}else{
|
||||
} else {
|
||||
node->_parent = _root;
|
||||
_root->_children.append(node);
|
||||
}
|
||||
_dataSource.append(node);
|
||||
if (item.contains("children")) {
|
||||
QList<QVariant> children = item.value("children").toList();
|
||||
if(!children.isEmpty()){
|
||||
if (!children.isEmpty()) {
|
||||
std::reverse(children.begin(), children.end());
|
||||
for (int i = 0; i < children.count(); ++i) {
|
||||
for (int i = 0; i <= children.count() - 1; ++i) {
|
||||
auto child = children.at(i).toMap();
|
||||
child.insert("__depth",item.value("__depth").toInt(0)+1);
|
||||
child.insert("__parent",QVariant::fromValue(node));
|
||||
child.insert("__depth", item.value("__depth").toInt(nullptr) + 1);
|
||||
child.insert("__parent", QVariant::fromValue(node));
|
||||
data.append(child);
|
||||
}
|
||||
}
|
||||
@ -165,108 +170,111 @@ void FluTreeModel::setDataSource(QList<QMap<QString,QVariant>> data){
|
||||
dataSourceSize(_dataSource.size());
|
||||
}
|
||||
|
||||
void FluTreeModel::collapse(int row){
|
||||
if(!_rows.at(row)->_isExpanded){
|
||||
void FluTreeModel::collapse(int row) {
|
||||
if (!_rows.at(row)->_isExpanded) {
|
||||
return;
|
||||
}
|
||||
_rows.at(row)->_isExpanded = false;
|
||||
Q_EMIT dataChanged(index(row,0),index(row,0));
|
||||
Q_EMIT dataChanged(index(row, 0), index(row, 0));
|
||||
auto modelData = _rows.at(row);
|
||||
int removeCount = 0;
|
||||
for(int i=row+1;i<_rows.count();i++){
|
||||
for (int i = row + 1; i < _rows.count(); i++) {
|
||||
auto obj = _rows[i];
|
||||
if(obj->_depth<=modelData->_depth){
|
||||
if (obj->_depth <= modelData->_depth) {
|
||||
break;
|
||||
}
|
||||
removeCount = removeCount + 1;
|
||||
}
|
||||
removeRows(row+1,removeCount);
|
||||
removeRows(row + 1, removeCount);
|
||||
}
|
||||
|
||||
void FluTreeModel::expand(int row){
|
||||
if(_rows.at(row)->_isExpanded){
|
||||
void FluTreeModel::expand(int row) {
|
||||
if (_rows.at(row)->_isExpanded) {
|
||||
return;
|
||||
}
|
||||
_rows.at(row)->_isExpanded = true;
|
||||
Q_EMIT dataChanged(index(row,0),index(row,0));
|
||||
Q_EMIT dataChanged(index(row, 0), index(row, 0));
|
||||
auto modelData = _rows.at(row);
|
||||
QList<FluTreeNode*> insertData;
|
||||
QList<FluTreeNode*> stack = modelData->_children;
|
||||
QList<FluTreeNode *> insertData;
|
||||
QList<FluTreeNode *> stack = modelData->_children;
|
||||
std::reverse(stack.begin(), stack.end());
|
||||
while (stack.count() > 0) {
|
||||
auto item = stack.at(stack.count()-1);
|
||||
auto item = stack.at(stack.count() - 1);
|
||||
stack.pop_back();
|
||||
if(item->isShown()){
|
||||
if (item->isShown()) {
|
||||
insertData.append(item);
|
||||
}
|
||||
QList<FluTreeNode*> children = item->_children;
|
||||
if(!children.isEmpty()){
|
||||
QList<FluTreeNode *> children = item->_children;
|
||||
if (!children.isEmpty()) {
|
||||
std::reverse(children.begin(), children.end());
|
||||
foreach (auto c, children) {
|
||||
stack.append(c);
|
||||
}
|
||||
foreach (auto c, children) {
|
||||
stack.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
insertRows(row+1,insertData);
|
||||
insertRows(row + 1, insertData);
|
||||
}
|
||||
|
||||
bool FluTreeModel::hitHasChildrenExpanded(int row){
|
||||
bool FluTreeModel::hitHasChildrenExpanded(int row) {
|
||||
auto itemData = _rows.at(row);
|
||||
if(itemData->hasChildren() && itemData->_isExpanded){
|
||||
if (itemData->hasChildren() && itemData->_isExpanded) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void FluTreeModel::refreshNode(int row){
|
||||
Q_EMIT dataChanged(index(row,0),index(row,0));
|
||||
};
|
||||
void FluTreeModel::refreshNode(int row) {
|
||||
Q_EMIT dataChanged(index(row, 0), index(row, 0));
|
||||
}
|
||||
|
||||
FluTreeNode* FluTreeModel::getNode(int row){
|
||||
FluTreeNode *FluTreeModel::getNode(int row) {
|
||||
return _rows.at(row);
|
||||
}
|
||||
|
||||
void FluTreeModel::allExpand(){
|
||||
void FluTreeModel::allExpand() {
|
||||
beginResetModel();
|
||||
QList<FluTreeNode*> data;
|
||||
QList<FluTreeNode*> stack = _root->_children;
|
||||
QList<FluTreeNode *> data;
|
||||
QList<FluTreeNode *> stack = _root->_children;
|
||||
std::reverse(stack.begin(), stack.end());
|
||||
while (stack.count() > 0) {
|
||||
auto item = stack.at(stack.count()-1);
|
||||
auto item = stack.at(stack.count() - 1);
|
||||
stack.pop_back();
|
||||
if(item->hasChildren()){
|
||||
if (item->hasChildren()) {
|
||||
item->_isExpanded = true;
|
||||
}
|
||||
data.append(item);
|
||||
QList<FluTreeNode*> children = item->_children;
|
||||
if(!children.isEmpty()){
|
||||
QList<FluTreeNode *> children = item->_children;
|
||||
if (!children.isEmpty()) {
|
||||
std::reverse(children.begin(), children.end());
|
||||
foreach (auto c, children) {
|
||||
stack.append(c);
|
||||
}
|
||||
foreach (auto c, children) {
|
||||
stack.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
_rows = data;
|
||||
endResetModel();
|
||||
}
|
||||
void FluTreeModel::allCollapse(){
|
||||
|
||||
void FluTreeModel::allCollapse() {
|
||||
beginResetModel();
|
||||
QList<FluTreeNode*> stack = _root->_children;
|
||||
QList<FluTreeNode *> stack = _root->_children;
|
||||
std::reverse(stack.begin(), stack.end());
|
||||
while (stack.count() > 0) {
|
||||
auto item = stack.at(stack.count()-1);
|
||||
auto item = stack.at(stack.count() - 1);
|
||||
stack.pop_back();
|
||||
if(item->hasChildren()){
|
||||
if (item->hasChildren()) {
|
||||
item->_isExpanded = false;
|
||||
}
|
||||
QList<FluTreeNode*> children = item->_children;
|
||||
if(!children.isEmpty()){
|
||||
QList<FluTreeNode *> children = item->_children;
|
||||
if (!children.isEmpty()) {
|
||||
std::reverse(children.begin(), children.end());
|
||||
foreach (auto c, children) {
|
||||
stack.append(c);
|
||||
}
|
||||
foreach (auto c, children) {
|
||||
stack.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
_rows = _root->_children;
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
#pragma clang diagnostic pop
|
Reference in New Issue
Block a user