Skip to content

Commit

Permalink
Merge pull request #32 from xdedss/dev-lzr
Browse files Browse the repository at this point in the history
hobject实现QAbstractItemModel
  • Loading branch information
xdedss authored Jul 15, 2021
2 parents caddcac + f92ebc8 commit 3523d73
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 67 deletions.
12 changes: 12 additions & 0 deletions Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ void Component::setProp(const QString & key, const QVariant& value)
}
}

std::vector<QString> Component::getPropKeys()
{
std::vector<QString> 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);
}
5 changes: 5 additions & 0 deletions Component.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <set>

#include "nesteddefs.h"
#include "qvariant.h"
Expand All @@ -10,9 +11,11 @@ class Component {

protected:
std::map<QString, QVariant> properties;
std::vector<QString> propertyKeys;

public:
HierarchyObject* hierarchyObject;
QString name = "undefined";

virtual void onInit() {};
virtual void onUpdate() {};
Expand All @@ -23,6 +26,8 @@ class Component {
QVariant getProp(const QString& key);
// 改变属性的值
void setProp(const QString& key, const QVariant& value);
// 获取所有键
std::vector<QString> getPropKeys();

protected:
// 定义属性
Expand Down
2 changes: 1 addition & 1 deletion HierarchyModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
138 changes: 135 additions & 3 deletions HierarchyObject.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "HierarchyObject.h"
#include "HierarchyModel.h"
#include "Component.h"
#include "qdebug.h"

HierarchyObject * HierarchyObject::getChildren(const QString & name)
{
Expand All @@ -24,7 +25,7 @@ HierarchyObject::HierarchyObject(const QString & name, HierarchyObject* parent)
{
transform = glm::identity<glm::mat4>();
this->name = name;
this->parent = parent;
this->parentObj = parent;
this->enabled = true;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -119,6 +120,91 @@ void HierarchyObject::callRecursively(const std::function<void(HierarchyObject*)
}
}



// ---------abstract item model--------------------


QModelIndex HierarchyObject::index(int row, int column, const QModelIndex & parent) const
{
if (!parent.isValid()) {
// 根节点index,返回component标题项
if (row >= components.size() || row < 0) return QModelIndex();
return createIndex(row, column, nullptr);
}
else {
// 根节点就是component,返回属性项
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) {
// 是component
return QModelIndex();
}

// 剩余情况一定是属性项
Component* p = static_cast<Component*>(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) {
// 是component
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) {
// 是component, 下面的property有两列
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) {
// 是component 返回其名称
return QVariant(components[index.row()]->name);
}
// property
Component* c = static_cast<Component*>(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) {
Expand All @@ -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) {
// 是component 不允许修改
return false;
}
// property
Component* c = static_cast<Component*>(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) {
// 是component 不允许修改
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"));
}
30 changes: 23 additions & 7 deletions HierarchyObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <qpoint.h>
#include <qstring.h>
#include <qabstractitemmodel.h>

#include <vector>
#include <set>
Expand All @@ -17,12 +18,12 @@
#include "nesteddefs.h"


class HierarchyObject {
class HierarchyObject : public QAbstractItemModel {

private:
std::vector<HierarchyObject*> children;
std::vector<Component*> components;
HierarchyObject* parent = NULL;
HierarchyObject* parentObj = NULL;

public:
glm::mat4 transform;
Expand All @@ -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);
Expand Down Expand Up @@ -91,11 +92,26 @@ class HierarchyObject {
// 遍历子树
void callRecursively(const std::function<void(HierarchyObject*)>& 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;

};

Expand Down
67 changes: 11 additions & 56 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
{
// 编辑完按下回车了
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3523d73

Please sign in to comment.