Skip to content

Commit

Permalink
Working item saving.
Browse files Browse the repository at this point in the history
  • Loading branch information
ASxa86 committed Feb 17, 2025
1 parent 1eb7fb0 commit ce04bfa
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 49 deletions.
25 changes: 25 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22621.0",
"compilerPath": "cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64",
"configurationProvider": "ms-vscode.cmake-tools",
"compileCommands": [
"${workspaceFolder}/build/compile_commands.json"
]
}
],
"version": 4
}
54 changes: 9 additions & 45 deletions app/editor/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,6 @@ 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

Expand All @@ -77,7 +33,7 @@ Window {

SplitView.preferredWidth: window.width * 0.5

sourceComponent: source
source: "file:///D:/dev/aspire/app/editor/Test.qml"

Text {
anchors.left: parent.left
Expand All @@ -92,6 +48,14 @@ Window {
}
}
}

focus: true
Keys.onPressed: (event) => {
if (event.key === Qt.Key_S && event.modifiers & Qt.ControlModifier) {
console.log("Saving...");
view.save("file:test.qml");
}
}
}

Item {
Expand Down
41 changes: 41 additions & 0 deletions app/editor/Test.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import QtQuick

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
}
}
}
9 changes: 9 additions & 0 deletions module/aspire/Aspire.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <aspire/Aspire.h>

using aspire::Aspire;

void Aspire::writeFile(QObject* object, const QUrl& url) const
{
const auto file = url.toLocalFile();
aspire::WriteFileQML(object, file.toStdString());
}
23 changes: 23 additions & 0 deletions module/aspire/Aspire.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <aspire/WriteFileQML.h>
#include <qqmlintegration.h>
#include <QObject>
#include <QUrl>
#include <aspire/export.hxx>


namespace aspire
{
class ASPIRE_EXPORT Aspire : public QObject
{
Q_OBJECT
QML_SINGLETON
QML_ELEMENT

public:
Q_INVOKABLE void writeFile(QObject* object, const QUrl& url) const;

private:
};
}
2 changes: 2 additions & 0 deletions module/aspire/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ qt_add_qml_module(${PROJECT_NAME}
URI ${PROJECT_NAME}
VERSION 1.0
SOURCES
Aspire.cpp
Aspire.h
FactoryComponent.cpp
FactoryComponent.h
# ItemView.cpp
Expand Down
10 changes: 9 additions & 1 deletion module/aspire/FactoryComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,15 @@ auto FactoryComponent::findQmlName(const QMetaObject* x) const -> std::string_vi
return {};
}

const auto foundIt = this->qmlTypeMap.find(x->className());
auto foundIt = this->qmlTypeMap.find(x->className());

// Attempt to use the object's superclass if it can't be found in the qml map.
// This is to handle the situation where a QML object may be the root node of a qml file
// which qt will treat as a type. This won't show up in the registration.
if(foundIt == std::end(this->qmlTypeMap))
{
foundIt = this->qmlTypeMap.find(x->superClass()->className());
}

if(foundIt == std::end(this->qmlTypeMap))
{
Expand Down
20 changes: 19 additions & 1 deletion module/aspire/ItemView.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import QtQuick
import Qt.labs.animation
import aspire

Item {
id: view
Expand All @@ -11,6 +12,10 @@ Item {
property alias item: loader.item
property alias zoom: container.scale

function save(file) {
Aspire.writeFile(view.item, file);
}

Rectangle {
id: container

Expand Down Expand Up @@ -90,6 +95,10 @@ Item {

Loader {
id: loader

onLoaded: {
console.log("Loaded: ", loader.source);
}
}
}

Expand Down Expand Up @@ -119,6 +128,10 @@ Item {
property point point: Qt.point(0, 0)

onPressed: (event) => {
if (event.button != Qt.LeftButton) {
return;
}

if (root === null) {
return;
}
Expand All @@ -135,6 +148,9 @@ Item {
return;
}

// To enable smooth movement at any scale,
// we map the event position to the item's parent
// and calculate a delta to apply.
let p = current.parent;
const start = p.mapFromItem(mouseArea, point);
const end = p.mapFromItem(mouseArea, Qt.point(event.x, event.y));
Expand All @@ -153,8 +169,11 @@ Item {
for (let i = 0; i < parent.children.length; i++) {
let child = parent.children[i];

// Perform a depth first search to intersect with children
// that are rendered in front of their parents first.
intersect(child, point);

// If an intersection as occurred, let's exit.
if (current !== null) {
return;
}
Expand All @@ -165,7 +184,6 @@ Item {
current = child;
return;
}

}
}
}
Expand Down
2 changes: 0 additions & 2 deletions module/aspire/WriteFileQML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace
{
auto WriteProperties(QObject& x, std::string& tabs, std::ofstream& ofs) -> void
{
QQmlMetaType type;

const auto* metaObject = x.metaObject();

if(metaObject == nullptr)
Expand Down

0 comments on commit ce04bfa

Please sign in to comment.