This commit is contained in:
朱子楚\zhuzi 2024-04-03 11:19:35 +08:00
parent bf5bedc9ed
commit a95916ab03
3 changed files with 46 additions and 46 deletions

View File

@ -8,14 +8,14 @@ import "../component"
FluWindowDialog { FluWindowDialog {
id:window id:window
title:qsTr("FluentUI Initalizr") title:qsTr("FluentUI Initializr")
width: 600 width: 600
height: 400 height: 400
contentDelegate:Component{ contentDelegate:Component{
Item{ Item{
Connections{ Connections{
target: InitalizrHelper target: InitializrHelper
function onError(message){ function onError(message){
showError(message) showError(message)
} }
@ -27,7 +27,7 @@ FluWindowDialog {
FluText{ FluText{
id:text_title id:text_title
text:qsTr("FluentUI Initalizr") text:qsTr("FluentUI Initializr")
font: FluTextStyle.Title font: FluTextStyle.Title
anchors{ anchors{
left: parent.left left: parent.left
@ -102,7 +102,7 @@ FluWindowDialog {
width: 120 width: 120
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
onClicked: { onClicked: {
InitalizrHelper.generate(text_box_name.text,text_box_path.text) InitializrHelper.generate(text_box_name.text,text_box_path.text)
} }
} }
} }

View File

@ -2,7 +2,7 @@
#include <QMetaEnum> #include <QMetaEnum>
FluNode::FluNode(QObject *parent): QObject{parent}{ FluTreeNode::FluTreeNode(QObject *parent): QObject{parent}{
} }
FluTreeModel::FluTreeModel(QObject *parent): QAbstractItemModel{parent}{ FluTreeModel::FluTreeModel(QObject *parent): QAbstractItemModel{parent}{
@ -41,7 +41,7 @@ QHash<int, QByteArray> FluTreeModel::roleNames() const {
return { {Qt::DisplayRole, "dataModel"} }; return { {Qt::DisplayRole, "dataModel"} };
}; };
void FluTreeModel::setData(QList<FluNode*> data){ void FluTreeModel::setData(QList<FluTreeNode*> data){
beginResetModel(); beginResetModel();
_rows = data; _rows = data;
endResetModel(); endResetModel();
@ -51,20 +51,20 @@ void FluTreeModel::removeRows(int row,int count){
if (row < 0 || row + count > _rows.size() || count==0) if (row < 0 || row + count > _rows.size() || count==0)
return; return;
beginRemoveRows(QModelIndex(),row, row + count - 1); beginRemoveRows(QModelIndex(),row, row + count - 1);
QList<FluNode*> firstPart = _rows.mid(0,row); QList<FluTreeNode*> firstPart = _rows.mid(0,row);
QList<FluNode*> secondPart = _rows.mid(row + count); QList<FluTreeNode*> secondPart = _rows.mid(row + count);
_rows.clear(); _rows.clear();
_rows.append(firstPart); _rows.append(firstPart);
_rows.append(secondPart); _rows.append(secondPart);
endRemoveRows(); endRemoveRows();
} }
void FluTreeModel::insertRows(int row,QList<FluNode*> data){ void FluTreeModel::insertRows(int row,QList<FluTreeNode*> data){
if (row < 0 || row > _rows.size() || data.size() == 0) if (row < 0 || row > _rows.size() || data.size() == 0)
return;; return;;
beginInsertRows(QModelIndex(), row, row + data.size() - 1); beginInsertRows(QModelIndex(), row, row + data.size() - 1);
QList<FluNode*> firstPart = _rows.mid(0, row); QList<FluTreeNode*> firstPart = _rows.mid(0, row);
QList<FluNode*> secondPart = _rows.mid(row); QList<FluTreeNode*> secondPart = _rows.mid(row);
_rows.clear(); _rows.clear();
_rows.append(firstPart); _rows.append(firstPart);
_rows.append(data); _rows.append(data);
@ -79,7 +79,7 @@ QObject* FluTreeModel::getRow(int row){
void FluTreeModel::checkRow(int row,bool checked){ void FluTreeModel::checkRow(int row,bool checked){
auto itemData = _rows.at(row); auto itemData = _rows.at(row);
if(itemData->hasChildren()){ if(itemData->hasChildren()){
QList<FluNode*> stack = itemData->_children; QList<FluTreeNode*> stack = itemData->_children;
std::reverse(stack.begin(), stack.end()); std::reverse(stack.begin(), stack.end());
while (stack.count() > 0) { while (stack.count() > 0) {
auto item = stack.at(stack.count()-1); auto item = stack.at(stack.count()-1);
@ -87,7 +87,7 @@ void FluTreeModel::checkRow(int row,bool checked){
if(!item->hasChildren()){ if(!item->hasChildren()){
item->_checked = checked; item->_checked = checked;
} }
QList<FluNode*> children = item->_children; QList<FluTreeNode*> children = item->_children;
if(!children.isEmpty()){ if(!children.isEmpty()){
std::reverse(children.begin(), children.end()); std::reverse(children.begin(), children.end());
foreach (auto c, children) { foreach (auto c, children) {
@ -101,8 +101,8 @@ void FluTreeModel::checkRow(int row,bool checked){
} }
itemData->_checked = checked; itemData->_checked = checked;
} }
Q_EMIT layoutChanged(QList<QPersistentModelIndex>(),QAbstractItemModel::VerticalSortHint); Q_EMIT layoutChanged();
QList<FluNode*> data; QList<FluTreeNode*> data;
foreach (auto item, _dataSource) { foreach (auto item, _dataSource) {
if(!item->hasChildren()){ if(!item->hasChildren()){
if(item->_checked){ if(item->_checked){
@ -119,16 +119,16 @@ void FluTreeModel::setDataSource(QList<QMap<QString,QVariant>> data){
delete _root; delete _root;
_root = nullptr; _root = nullptr;
} }
_root = new FluNode(this); _root = new FluTreeNode(this);
std::reverse(data.begin(), data.end()); std::reverse(data.begin(), data.end());
while (data.count() > 0) { while (data.count() > 0) {
auto item = data.at(data.count()-1); auto item = data.at(data.count()-1);
data.pop_back(); data.pop_back();
FluNode* node = new FluNode(this); FluTreeNode* node = new FluTreeNode(this);
node->_title = item.value("title").toString(); node->_title = item.value("title").toString();
node->_key = item.value("key").toString(); node->_key = item.value("key").toString();
node->_depth = item.value("__depth").toInt(); node->_depth = item.value("__depth").toInt();
node->_parent = item.value("__parent").value<FluNode*>(); node->_parent = item.value("__parent").value<FluTreeNode*>();
node->_isExpanded = true; node->_isExpanded = true;
if(node->_parent){ if(node->_parent){
node->_parent->_children.append(node); node->_parent->_children.append(node);
@ -181,8 +181,8 @@ void FluTreeModel::expand(int row){
_rows.at(row)->_isExpanded = true; _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); auto modelData = _rows.at(row);
QList<FluNode*> insertData; QList<FluTreeNode*> insertData;
QList<FluNode*> stack = modelData->_children; QList<FluTreeNode*> stack = modelData->_children;
std::reverse(stack.begin(), stack.end()); std::reverse(stack.begin(), stack.end());
while (stack.count() > 0) { while (stack.count() > 0) {
auto item = stack.at(stack.count()-1); auto item = stack.at(stack.count()-1);
@ -190,7 +190,7 @@ void FluTreeModel::expand(int row){
if(item->isShown()){ if(item->isShown()){
insertData.append(item); insertData.append(item);
} }
QList<FluNode*> children = item->_children; QList<FluTreeNode*> children = item->_children;
if(!children.isEmpty()){ if(!children.isEmpty()){
std::reverse(children.begin(), children.end()); std::reverse(children.begin(), children.end());
foreach (auto c, children) { foreach (auto c, children) {
@ -242,7 +242,7 @@ void FluTreeModel::dragAndDrop(int dragIndex,int dropIndex,bool isDropTopArea){
Q_EMIT layoutAboutToBeChanged(); Q_EMIT layoutAboutToBeChanged();
if(dragItem->_parent == dropItem->_parent){ if(dragItem->_parent == dropItem->_parent){
QList<FluNode*>* children = &(dragItem->_parent->_children); QList<FluTreeNode*>* children = &(dragItem->_parent->_children);
int srcIndex = children->indexOf(dragItem); int srcIndex = children->indexOf(dragItem);
int destIndex = children->indexOf(dropItem); int destIndex = children->indexOf(dropItem);
if(dropIndex > dragIndex){ if(dropIndex > dragIndex){
@ -260,14 +260,14 @@ void FluTreeModel::dragAndDrop(int dragIndex,int dropIndex,bool isDropTopArea){
} }
children->move(srcIndex,targetIndex); children->move(srcIndex,targetIndex);
}else{ }else{
QList<FluNode*>* srcChildren = &(dragItem->_parent->_children); QList<FluTreeNode*>* srcChildren = &(dragItem->_parent->_children);
QList<FluNode*>* destChildren = &(dropItem->_parent->_children); QList<FluTreeNode*>* destChildren = &(dropItem->_parent->_children);
int srcIndex = srcChildren->indexOf(dragItem); int srcIndex = srcChildren->indexOf(dragItem);
int destIndex = destChildren->indexOf(dropItem); int destIndex = destChildren->indexOf(dropItem);
dragItem->_depth = dropItem->_depth; dragItem->_depth = dropItem->_depth;
dragItem->_parent = dropItem->_parent; dragItem->_parent = dropItem->_parent;
if(dragItem->hasChildren()){ if(dragItem->hasChildren()){
QList<FluNode*> stack = dragItem->_children; QList<FluTreeNode*> stack = dragItem->_children;
foreach (auto node, stack) { foreach (auto node, stack) {
node->_depth = dragItem->_depth+1; node->_depth = dragItem->_depth+1;
} }
@ -275,7 +275,7 @@ void FluTreeModel::dragAndDrop(int dragIndex,int dropIndex,bool isDropTopArea){
while (stack.count() > 0) { while (stack.count() > 0) {
auto item = stack.at(stack.count()-1); auto item = stack.at(stack.count()-1);
stack.pop_back(); stack.pop_back();
QList<FluNode*> children = item->_children; QList<FluTreeNode*> children = item->_children;
if(!children.isEmpty()){ if(!children.isEmpty()){
std::reverse(children.begin(), children.end()); std::reverse(children.begin(), children.end());
foreach (auto c, children) { foreach (auto c, children) {
@ -302,7 +302,7 @@ void FluTreeModel::dragAndDrop(int dragIndex,int dropIndex,bool isDropTopArea){
destChildren->insert(targetIndex,dragItem); destChildren->insert(targetIndex,dragItem);
} }
changePersistentIndex(index(qMin(dragIndex,dropIndex),0),index(qMax(dragIndex,dropIndex),0)); changePersistentIndex(index(qMin(dragIndex,dropIndex),0),index(qMax(dragIndex,dropIndex),0));
Q_EMIT layoutChanged(QList<QPersistentModelIndex>(),QAbstractItemModel::VerticalSortHint); Q_EMIT layoutChanged();
} }
@ -318,14 +318,14 @@ void FluTreeModel::refreshNode(int row){
Q_EMIT dataChanged(index(row,0),index(row,0)); Q_EMIT dataChanged(index(row,0),index(row,0));
}; };
FluNode* FluTreeModel::getNode(int row){ FluTreeNode* FluTreeModel::getNode(int row){
return _rows.at(row); return _rows.at(row);
} }
void FluTreeModel::allExpand(){ void FluTreeModel::allExpand(){
beginResetModel(); beginResetModel();
QList<FluNode*> data; QList<FluTreeNode*> data;
QList<FluNode*> stack = _root->_children; QList<FluTreeNode*> stack = _root->_children;
std::reverse(stack.begin(), stack.end()); std::reverse(stack.begin(), stack.end());
while (stack.count() > 0) { while (stack.count() > 0) {
auto item = stack.at(stack.count()-1); auto item = stack.at(stack.count()-1);
@ -334,7 +334,7 @@ void FluTreeModel::allExpand(){
item->_isExpanded = true; item->_isExpanded = true;
} }
data.append(item); data.append(item);
QList<FluNode*> children = item->_children; QList<FluTreeNode*> children = item->_children;
if(!children.isEmpty()){ if(!children.isEmpty()){
std::reverse(children.begin(), children.end()); std::reverse(children.begin(), children.end());
foreach (auto c, children) { foreach (auto c, children) {
@ -347,7 +347,7 @@ void FluTreeModel::allExpand(){
} }
void FluTreeModel::allCollapse(){ void FluTreeModel::allCollapse(){
beginResetModel(); beginResetModel();
QList<FluNode*> stack = _root->_children; QList<FluTreeNode*> stack = _root->_children;
std::reverse(stack.begin(), stack.end()); std::reverse(stack.begin(), stack.end());
while (stack.count() > 0) { while (stack.count() > 0) {
auto item = stack.at(stack.count()-1); auto item = stack.at(stack.count()-1);
@ -355,7 +355,7 @@ void FluTreeModel::allCollapse(){
if(item->hasChildren()){ if(item->hasChildren()){
item->_isExpanded = false; item->_isExpanded = false;
} }
QList<FluNode*> children = item->_children; QList<FluTreeNode*> children = item->_children;
if(!children.isEmpty()){ if(!children.isEmpty()){
std::reverse(children.begin(), children.end()); std::reverse(children.begin(), children.end());
foreach (auto c, children) { foreach (auto c, children) {

View File

@ -8,9 +8,9 @@
#include "stdafx.h" #include "stdafx.h"
/** /**
* @brief The FluNode class * @brief The FluTreeNode class
*/ */
class FluNode : public QObject{ class FluTreeNode : public QObject{
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString key READ key CONSTANT) Q_PROPERTY(QString key READ key CONSTANT)
Q_PROPERTY(QString title READ title CONSTANT) Q_PROPERTY(QString title READ title CONSTANT)
@ -18,14 +18,14 @@ class FluNode : public QObject{
Q_PROPERTY(bool isExpanded READ isExpanded CONSTANT) Q_PROPERTY(bool isExpanded READ isExpanded CONSTANT)
Q_PROPERTY(bool checked READ checked CONSTANT) Q_PROPERTY(bool checked READ checked CONSTANT)
public: public:
explicit FluNode(QObject *parent = nullptr); explicit FluTreeNode(QObject *parent = nullptr);
Q_INVOKABLE QString key(){return _key;}; Q_INVOKABLE QString key(){return _key;};
Q_INVOKABLE QString title(){return _title;}; Q_INVOKABLE QString title(){return _title;};
Q_INVOKABLE int depth(){return _depth;}; Q_INVOKABLE int depth(){return _depth;};
Q_INVOKABLE bool isExpanded(){return _isExpanded;}; Q_INVOKABLE bool isExpanded(){return _isExpanded;};
Q_INVOKABLE bool hasChildren(){ return !_children.isEmpty();}; Q_INVOKABLE bool hasChildren(){ return !_children.isEmpty();};
Q_INVOKABLE bool hasNextNodeByIndex(int index){ Q_INVOKABLE bool hasNextNodeByIndex(int index){
FluNode* p = this; FluTreeNode* p = this;
for(int i=0;i<(_depth - index -1);i++){ for(int i=0;i<(_depth - index -1);i++){
p = p->_parent; p = p->_parent;
} }
@ -75,15 +75,15 @@ public:
int _depth=0; int _depth=0;
bool _checked = false; bool _checked = false;
bool _isExpanded=true; bool _isExpanded=true;
QList<FluNode*> _children; QList<FluTreeNode*> _children;
FluNode* _parent = nullptr; FluTreeNode* _parent = nullptr;
}; };
class FluTreeModel : public QAbstractItemModel class FluTreeModel : public QAbstractItemModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY_AUTO(int,dataSourceSize) Q_PROPERTY_AUTO(int,dataSourceSize)
Q_PROPERTY_AUTO(QList<FluNode*>,selectionModel) Q_PROPERTY_AUTO(QList<FluTreeNode*>,selectionModel)
QML_NAMED_ELEMENT(FluTreeModel) QML_NAMED_ELEMENT(FluTreeModel)
QML_ADDED_IN_MINOR_VERSION(1) QML_ADDED_IN_MINOR_VERSION(1)
public: public:
@ -96,23 +96,23 @@ public:
QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const override; QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const override;
Q_INVOKABLE void removeRows(int row,int count); Q_INVOKABLE void removeRows(int row,int count);
Q_INVOKABLE void insertRows(int row,QList<FluNode*> data); Q_INVOKABLE void insertRows(int row,QList<FluTreeNode*> data);
Q_INVOKABLE QObject* getRow(int row); Q_INVOKABLE QObject* getRow(int row);
Q_INVOKABLE void setData(QList<FluNode*> data); Q_INVOKABLE void setData(QList<FluTreeNode*> data);
Q_INVOKABLE void setDataSource(QList<QMap<QString,QVariant>> data); Q_INVOKABLE void setDataSource(QList<QMap<QString,QVariant>> data);
Q_INVOKABLE void collapse(int row); Q_INVOKABLE void collapse(int row);
Q_INVOKABLE void expand(int row); Q_INVOKABLE void expand(int row);
Q_INVOKABLE void dragAndDrop(int dragIndex,int dropIndex,bool isDropTopArea); Q_INVOKABLE void dragAndDrop(int dragIndex,int dropIndex,bool isDropTopArea);
Q_INVOKABLE FluNode* getNode(int row); Q_INVOKABLE FluTreeNode* getNode(int row);
Q_INVOKABLE void refreshNode(int row); Q_INVOKABLE void refreshNode(int row);
Q_INVOKABLE void checkRow(int row,bool checked); Q_INVOKABLE void checkRow(int row,bool checked);
Q_INVOKABLE bool hitHasChildrenExpanded(int row); Q_INVOKABLE bool hitHasChildrenExpanded(int row);
Q_INVOKABLE void allExpand(); Q_INVOKABLE void allExpand();
Q_INVOKABLE void allCollapse(); Q_INVOKABLE void allCollapse();
private: private:
QList<FluNode*> _rows; QList<FluTreeNode*> _rows;
QList<FluNode*> _dataSource; QList<FluTreeNode*> _dataSource;
FluNode* _root = nullptr; FluTreeNode* _root = nullptr;
}; };
#endif // FLUTREEMODEL_H #endif // FLUTREEMODEL_H