diff --git a/Component.cpp b/Component.cpp index 8727989..f6afbb7 100644 --- a/Component.cpp +++ b/Component.cpp @@ -26,8 +26,20 @@ void Component::setProp(const QString & key, const QVariant& value) } } +std::vector Component::getPropKeys() +{ + std::vector res; + for (auto& pair : defaultProp) { + res.push_back(pair.first); + } + res.insert(res.end(), propertyKeys.begin(), propertyKeys.end()); + return res; +} + void Component::defProp(const QString & key, const QVariant & init) { assert(properties.count(key) == 0); + assert(defaultProp.count(key) == 0); properties[key] = init; + propertyKeys.push_back(key); } diff --git a/Component.h b/Component.h index 9ecbe67..2f92c3f 100644 --- a/Component.h +++ b/Component.h @@ -1,5 +1,6 @@ #pragma once +#include #include "nesteddefs.h" #include "qvariant.h" @@ -10,9 +11,11 @@ class Component { protected: std::map properties; + std::vector propertyKeys; public: HierarchyObject* hierarchyObject; + QString name = "undefined"; virtual void onInit() {}; virtual void onUpdate() {}; @@ -23,6 +26,8 @@ class Component { QVariant getProp(const QString& key); // 改变属性的值 void setProp(const QString& key, const QVariant& value); + // 获取所有键 + std::vector getPropKeys(); protected: // 定义属性 diff --git a/HierarchyModel.cpp b/HierarchyModel.cpp index 35da5b8..8df6f07 100644 --- a/HierarchyModel.cpp +++ b/HierarchyModel.cpp @@ -192,7 +192,7 @@ QVariant HierarchyModel::data(const QModelIndex & index, int role) const } bool HierarchyModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) { - qDebug() << "setData: " << value << "|" << role; + //qDebug() << "setData: " << value << "|" << role; HierarchyObject * item; diff --git a/HierarchyObject.cpp b/HierarchyObject.cpp index da80003..d27a4ad 100644 --- a/HierarchyObject.cpp +++ b/HierarchyObject.cpp @@ -1,6 +1,7 @@ #include "HierarchyObject.h" #include "HierarchyModel.h" #include "Component.h" +#include "qdebug.h" HierarchyObject * HierarchyObject::getChildren(const QString & name) { @@ -24,7 +25,7 @@ HierarchyObject::HierarchyObject(const QString & name, HierarchyObject* parent) { transform = glm::identity(); this->name = name; - this->parent = parent; + this->parentObj = parent; this->enabled = true; } @@ -55,14 +56,14 @@ HierarchyObject* HierarchyObject::popChild(int index) { Q_ASSERT(index < children.size() && index >= 0); HierarchyObject* child = children[index]; children.erase(children.begin() + index); - child->parent = NULL; + child->parentObj = NULL; return child; } void HierarchyObject::insertChild(int index, HierarchyObject* child) { Q_ASSERT(index <= children.size() && index >= 0); children.insert(children.begin() + index, child); - child->parent = this; + child->parentObj = this; } void HierarchyObject::moveChild(int oldIndex, int newIndex) { @@ -119,6 +120,91 @@ void HierarchyObject::callRecursively(const std::function= components.size() || row < 0) return QModelIndex(); + return createIndex(row, column, nullptr); + } + else { + // 鏍硅妭鐐瑰氨鏄痗omponent锛岃繑鍥炲睘鎬ч」 + if (parent.row() >= components.size() || parent.row() < 0) return QModelIndex(); + return createIndex(row, column, components[parent.row()]); + } +} + +QModelIndex HierarchyObject::parent(const QModelIndex & index) const +{ + if (!index.isValid()) //浼犲叆鑺傜偣鏄牴鑺傜偣 + return QModelIndex(); + + if (index.internalPointer() == nullptr) { + // 鏄痗omponent + return QModelIndex(); + } + + // 鍓╀綑鎯呭喌涓瀹氭槸灞炴ч」 + Component* p = static_cast(index.internalPointer()); + int row = std::find(components.begin(), components.end(), p) - components.begin(); + return createIndex(row, 0, nullptr); +} + +int HierarchyObject::rowCount(const QModelIndex & parent) const +{ + if (!parent.isValid()) //浼犲叆鑺傜偣鏄牴鑺傜偣 + return components.size(); + + if (parent.internalPointer() == nullptr) { + // 鏄痗omponent + return components[parent.row()]->getPropKeys().size(); + } + // property鏈韩娌℃湁瀛愯妭鐐 + return 0; +} + +int HierarchyObject::columnCount(const QModelIndex & parent) const +{ + if (!parent.isValid()) //浼犲叆鑺傜偣鏄牴鑺傜偣 + return 1; + + if (parent.internalPointer() == nullptr) { + // 鏄痗omponent锛 涓嬮潰鐨刾roperty鏈変袱鍒 + return 2; + } + // property + return 0; +} + +QVariant HierarchyObject::data(const QModelIndex & index, int role) const +{ + if (!index.isValid()) //浼犲叆鑺傜偣鏄牴鑺傜偣 + return QVariant(); + + if (role != Qt::DisplayRole && role != Qt::EditRole) + return QVariant(); + + if (index.internalPointer() == nullptr) { + // 鏄痗omponent 杩斿洖鍏跺悕绉 + return QVariant(components[index.row()]->name); + } + // property + Component* c = static_cast(index.internalPointer()); + if (index.column() == 0) { + // 杩斿洖灞炴у悕 + return c->getPropKeys()[index.row()]; + } + else { + // 杩斿洖灞炴у + return c->getProp(c->getPropKeys()[index.row()]); + } +} + HierarchyObject::~HierarchyObject() { for (auto component : components) { @@ -130,3 +216,49 @@ HierarchyObject::~HierarchyObject() delete child; } } + +bool HierarchyObject::setData(const QModelIndex & index, const QVariant & value, int role) +{ + if (!index.isValid()) return false; + + if (index.internalPointer() == nullptr) { + // 鏄痗omponent 涓嶅厑璁镐慨鏀 + return false; + } + // property + Component* c = static_cast(index.internalPointer()); + if (index.column() == 0) { + // 灞炴у悕涓嶅厑璁镐慨鏀 + return false; + } + else { + // 淇敼灞炴у + qDebug() << "setData: " << value << "|" << role; + c->setProp(c->getPropKeys()[index.row()], value); + return true; + } +} + +Qt::ItemFlags HierarchyObject::flags(const QModelIndex & index) const +{ + if (!index.isValid()) return Qt::ItemIsSelectable | Qt::ItemIsEnabled; + + if (index.internalPointer() == nullptr) { + // 鏄痗omponent 涓嶅厑璁镐慨鏀 + return Qt::ItemIsSelectable | Qt::ItemIsEnabled; + } + // property + if (index.column() == 0) { + // 灞炴у悕涓嶅厑璁镐慨鏀 + return Qt::ItemIsSelectable | Qt::ItemIsEnabled; + } + else { + // 灞炴у + return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled; + } +} + +QVariant HierarchyObject::headerData(int section, Qt::Orientation orientation, int role) const +{ + return QVariant(QString("123")); +} diff --git a/HierarchyObject.h b/HierarchyObject.h index 05bad86..65b8871 100644 --- a/HierarchyObject.h +++ b/HierarchyObject.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -17,12 +18,12 @@ #include "nesteddefs.h" -class HierarchyObject { +class HierarchyObject : public QAbstractItemModel { private: std::vector children; std::vector components; - HierarchyObject* parent = NULL; + HierarchyObject* parentObj = NULL; public: glm::mat4 transform; @@ -40,7 +41,7 @@ class HierarchyObject { // 鏈妭鐐规寕杞界殑缁勪欢鏁伴噺 int componentsCount() { return components.size(); } // 鑾峰彇鐖惰妭鐐 - HierarchyObject* getParent() { return parent; } + HierarchyObject* getParent() { return parentObj; } // ctor HierarchyObject(const QString& name, HierarchyObject* parent = NULL); @@ -91,11 +92,26 @@ class HierarchyObject { // 閬嶅巻瀛愭爲 void callRecursively(const std::function& func, bool requireEnabled = false); - // 浠庡満鏅Щ闄 - void Remove(); - // 鎽ф瘉瀛愭爲 - void Destroy(); + // -------------瀹炵幇QAbstractItemModel鐨勫繀閫夋帴鍙----------------- + // 鑾峰彇缁欏畾鍏冪礌鐨勭row涓瓙鍏冪礌 + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; + // 鑾峰彇缁欏畾鍏冪礌鐨勭埗鍏冪礌 + QModelIndex parent(const QModelIndex &index) const; + // 鑾峰彇鏌愬厓绱犵殑瀛愯妭鐐规暟 + int rowCount(const QModelIndex &parent = QModelIndex()) const; + // 2 + int columnCount(const QModelIndex &parent = QModelIndex()) const; + // 鍙栧嚭鏁版嵁 + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + + // ----------------------閲嶅啓鍙夊嚱鏁---------------------------- + // 鎺ユ敹缂栬緫name浜嬩欢 + bool setData(const QModelIndex & index, const QVariant & value, int role); + // 杩斿洖鎸囧畾鑺傜偣鐨勫悇绉嶆樉绀哄睘鎬 + Qt::ItemFlags flags(const QModelIndex &index) const; + // 杩斿洖鏍囬 + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; }; diff --git a/mainwindow.cpp b/mainwindow.cpp index 5ff8d41..ad374fc 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -146,6 +146,13 @@ MainWindow::~MainWindow() delete hierarchy; } +#define PRINT_MAT4(t)\ +qDebug() << t[0][0] << t[0][1] << t[0][2] << t[0][3];\ +qDebug() << t[1][0] << t[1][1] << t[1][2] << t[1][3];\ +qDebug() << t[2][0] << t[2][1] << t[2][2] << t[2][3];\ +qDebug() << t[3][0] << t[3][1] << t[3][2] << t[3][3]; + + void MainWindow::ObjectSelected(const QItemSelection& selected, const QItemSelection& deselected) { QModelIndexList selectedIndices = selected.indexes(); @@ -284,32 +291,6 @@ void MainWindow::ObjectSelected(const QItemSelection& selected, const QItemSelec } } - -//void MainWindow::btn_slot1() -//{ -// -// qDebug() << ui->lineEdit->text(); -// qDebug() << ui->lineEdit_2->text(); -// qDebug() << ui->lineEdit_3->text(); -//} -//void MainWindow::btn_slot2() -//{ -// float scalex, scaley, scalez; -// scalex = QString(ui->lineEdit_6->text()).toFloat(); -// scaley = QString(ui->lineEdit_7->text()).toFloat(); -// scalez = QString(ui->lineEdit_10->text()).toFloat(); -// //scaleChange(scalex, scaley, scalez, NULL); -// qDebug() << ui->lineEdit_6->text(); -// qDebug() << ui->lineEdit_7->text(); -// qDebug() << ui->lineEdit_10->text(); -//} -//void MainWindow::btn_slot3() -//{ -// -// qDebug() << ui->lineEdit_8->text(); -// qDebug() << ui->lineEdit_9->text(); -// qDebug() << ui->lineEdit_11->text(); -//} void MainWindow::onEdited() { // 缂栬緫瀹屾寜涓嬪洖杞︿簡 @@ -353,39 +334,13 @@ void MainWindow::onEdited() {0,0,1,translationZ}, {0,0,0,1}, })); - t = rotation; - - qDebug() << t[0][0] << t[0][1] << t[0][2] << t[0][3]; - qDebug() << t[1][0] << t[1][1] << t[1][2] << t[1][3]; - qDebug() << t[2][0] << t[2][1] << t[2][2] << t[2][3]; - qDebug() << t[3][0] << t[3][1] << t[3][2] << t[3][3]; - t = scale; - - qDebug() << t[0][0] << t[0][1] << t[0][2] << t[0][3]; - qDebug() << t[1][0] << t[1][1] << t[1][2] << t[1][3]; - qDebug() << t[2][0] << t[2][1] << t[2][2] << t[2][3]; - qDebug() << t[3][0] << t[3][1] << t[3][2] << t[3][3]; - t = translation; - qDebug() << t[0][0] << t[0][1] << t[0][2] << t[0][3]; - qDebug() << t[1][0] << t[1][1] << t[1][2] << t[1][3]; - qDebug() << t[2][0] << t[2][1] << t[2][2] << t[2][3]; - qDebug() << t[3][0] << t[3][1] << t[3][2] << t[3][3]; + PRINT_MAT4(rotation); + PRINT_MAT4(scale); + PRINT_MAT4(translation); if (hierarchy->lastSelected) { - hierarchy->lastSelected->transform = rotation * scale; - t = hierarchy->lastSelected->transform; - qDebug() << t[0][0] << t[0][1] << t[0][2] << t[0][3]; - qDebug() << t[1][0] << t[1][1] << t[1][2] << t[1][3]; - qDebug() << t[2][0] << t[2][1] << t[2][2] << t[2][3]; - qDebug() << t[3][0] << t[3][1] << t[3][2] << t[3][3]; - hierarchy->lastSelected->transform = translation*hierarchy->lastSelected->transform; - t = hierarchy->lastSelected->transform; - - qDebug() << t[0][0] << t[0][1] << t[0][2] << t[0][3]; - qDebug() << t[1][0] << t[1][1] << t[1][2] << t[1][3]; - qDebug() << t[2][0] << t[2][1] << t[2][2] << t[2][3]; - qDebug() << t[3][0] << t[3][1] << t[3][2] << t[3][3]; + hierarchy->lastSelected->transform = translation * rotation * scale; } //scaleChange(scalex, scaley, scalez, nullptr);