Skip to content

Commit

Permalink
Working tree view.
Browse files Browse the repository at this point in the history
  • Loading branch information
ASxa86 committed Feb 4, 2025
1 parent 81ebf2f commit 9c59d93
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 15 deletions.
72 changes: 57 additions & 15 deletions app/editor/Main.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import QtQuick
import QtQuick.Controls
// import QtQuick.Controls.Style
import aspire

Window {
Expand All @@ -11,13 +12,65 @@ Window {
visible: true
color: Qt.rgba(0.55, 0.55, 0.55, 1.0)

Component {
id: source

Item {
Rectangle {
color: "red"
x: 50
y: 50
width: 32
height: 32

Text {
text: "red"
color: "white"
font.pointSize: 12
}
}

Rectangle {
color: "green"
x: 120
y: 120
width: 64
height: 32

Rectangle {
color: "white"
height: 16
width: 16
radius: 8
x: 90
}

Rectangle {
color: "white"
height: 16
width: 16
radius: 8
x: -30
}
}
}
}

SplitView {
anchors.fill: parent

Item {
TreeView {
SplitView.preferredWidth: window.width * 0.25

selectionModel: ItemSelectionModel {
}

model: ModelTreeItem {
item: loader.item
}

delegate: TreeViewDelegate {
}
}

ItemView {
Expand Down Expand Up @@ -62,20 +115,9 @@ Window {
width: 1280
height: 720

Rectangle {
color: "red"
x: 50
y: 50
width: 32
height: 32
}

Rectangle {
color: "green"
x: 120
y: 120
width: 64
height: 32
Loader {
id: loader
sourceComponent: source
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions module/aspire/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ qt_add_qml_module(${PROJECT_NAME}
FactoryComponent.h
ItemView.cpp
ItemView.h
ModelTreeItem.cpp
ModelTreeItem.h
QML_FILES
${QML_SINGLETONS}
RESOURCES
Expand Down
52 changes: 52 additions & 0 deletions module/aspire/ModelTreeItem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <aspire/ModelTreeItem.h>

#include <private/qqmlmetatype_p.h>
#include <QQuickItem>

using aspire::ModelTreeItem;

ModelTreeItem::ModelTreeItem(QObject* parent) : QStandardItemModel{parent}
{
QObject::connect(this, &ModelTreeItem::itemChanged, this, &ModelTreeItem::reload);
}

void ModelTreeItem::setItem(QObject* x) noexcept
{
if(this->item == x)
{
return;
}

this->item = x;
this->itemChanged(this->item);
}

QObject* ModelTreeItem::getItem() const noexcept
{
return this->item;
}

void ModelTreeItem::reload()
{
this->clear();
this->insertRow(0, this->load(this->item));
}

QStandardItem* ModelTreeItem::load(QObject* x)
{
auto* qitem = qobject_cast<QQuickItem*>(x);

if(qitem == nullptr)
{
return nullptr;
}

auto* node = new QStandardItem(qitem->metaObject()->className());

for(auto* child : qitem->childItems())
{
node->appendRow(this->load(child));
}

return node;
}
28 changes: 28 additions & 0 deletions module/aspire/ModelTreeItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <QtQml/qqmlregistration.h>
#include <QStandardItemModel>
#include <aspire/export.hxx>

namespace aspire
{
class ASPIRE_EXPORT ModelTreeItem : public QStandardItemModel
{
Q_OBJECT
Q_PROPERTY(QObject* item READ getItem WRITE setItem NOTIFY itemChanged)
QML_ELEMENT

public:
explicit ModelTreeItem(QObject* parent = nullptr);
void setItem(QObject* x) noexcept;
QObject* getItem() const noexcept;

signals:
void itemChanged(QObject*);

private:
void reload();
QStandardItem* load(QObject*);
QObject* item{};
};
}

0 comments on commit 9c59d93

Please sign in to comment.