Index: src/widgets/3dview/CMakeLists.txt =================================================================== --- src/widgets/3dview/CMakeLists.txt +++ src/widgets/3dview/CMakeLists.txt @@ -1,5 +1,6 @@ set(3d_SRCS axisgnomonentity.cpp + bedproperties.cpp cameracontroller.cpp fileloader.cpp gcodeto4d.cpp Index: src/widgets/3dview/SceneEntity.qml =================================================================== --- src/widgets/3dview/SceneEntity.qml +++ src/widgets/3dview/SceneEntity.qml @@ -31,6 +31,10 @@ id: sceneRoot property string currentFile + BedProperties { + id: bedProperties + } + Camera { id: camera fieldOfView: 45 @@ -55,7 +59,15 @@ id: gridEntity components: [ PhongMaterial { ambient: "darkBlue" }, - GridMesh {} + GridMesh { + meshResolution: Qt.size(Math.floor(bedProperties.width / 10), + Math.floor(bedProperties.depth / 10)) + }, + Transform { + scale3D: Qt.vector3d(bedProperties.width / 10, + bedProperties.depth / 10, + 1) + } ] } Index: src/widgets/3dview/bedproperties.h =================================================================== --- src/widgets/3dview/bedproperties.h +++ src/widgets/3dview/bedproperties.h @@ -1,7 +1,6 @@ /* Atelier KDE Printer Host for 3D Printing - Copyright (C) <2017-2018> - Author: Patrick José Pereira - patrickjp@kde.org - Kevin Ottens - ervin@kde.org + Copyright (C) <2018> + Author: Kevin Ottens - ervin@kde.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -23,14 +22,26 @@ #pragma once #include -#include -#include -class GridMesh : public Qt3DRender::QGeometryRenderer +class BedProperties : public QObject { Q_OBJECT - + Q_PROPERTY(int width READ width NOTIFY widthChanged) + Q_PROPERTY(int depth READ depth NOTIFY depthChanged) public: - explicit GridMesh(Qt3DCore::QNode *parent = nullptr); - ~GridMesh(); + explicit BedProperties(QObject *parent = nullptr); + ~BedProperties(); + + int width() const; + int depth() const; + +signals: + void widthChanged(int width); + void depthChanged(int depth); + +private: + void updateBedSize(const QSize &size); + + int m_width; + int m_depth; }; Index: src/widgets/3dview/bedproperties.cpp =================================================================== --- /dev/null +++ src/widgets/3dview/bedproperties.cpp @@ -0,0 +1,76 @@ +/* Atelier KDE Printer Host for 3D Printing + Copyright (C) <2018> + Author: Kevin Ottens - ervin@kde.org + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License or any later version accepted by the membership of + KDE e.V. (or its successor approved by the membership of KDE + e.V.), which shall act as a proxy defined in Section 14 of + version 3 of the license. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "bedproperties.h" + +#include "viewer3d.h" + +#include +#include + +BedProperties::BedProperties(QObject *parent) + : QObject(parent) + , m_width(200) + , m_depth(200) +{ + QTimer::singleShot(0, [=] { + auto context = qmlContext(this); + if (!context) { + return; + } + + auto viewer = context->contextProperty("viewer3d").value(); + if (!viewer) { + return; + } + + updateBedSize(viewer->bedSize()); + connect(viewer, &Viewer3D::bedSizeChanged, + this, &BedProperties::updateBedSize); + }); +} + +BedProperties::~BedProperties() +{ +} + +int BedProperties::width() const +{ + return m_width; +} + +int BedProperties::depth() const +{ + return m_depth; +} + +void BedProperties::updateBedSize(const QSize &size) +{ + if (size.width() != m_width) { + m_width = size.width(); + emit widthChanged(m_width); + } + + if (size.height() != m_depth) { + m_depth = size.height(); + emit depthChanged(m_depth); + } +} Index: src/widgets/3dview/gridmesh.h =================================================================== --- src/widgets/3dview/gridmesh.h +++ src/widgets/3dview/gridmesh.h @@ -25,12 +25,24 @@ #include #include #include +#include class GridMesh : public Qt3DRender::QGeometryRenderer { Q_OBJECT - + Q_PROPERTY(QSize meshResolution READ meshResolution WRITE setMeshResolution NOTIFY meshResolutionChanged) public: explicit GridMesh(Qt3DCore::QNode *parent = nullptr); ~GridMesh(); + + QSize meshResolution() const; + +public slots: + void setMeshResolution(const QSize &meshResolution); + +signals: + void meshResolutionChanged(const QSize &meshResolution); + +private: + QSize m_meshResolution; }; Index: src/widgets/3dview/gridmesh.cpp =================================================================== --- src/widgets/3dview/gridmesh.cpp +++ src/widgets/3dview/gridmesh.cpp @@ -26,31 +26,56 @@ #include "gridmesh.h" #include "linemeshgeometry.h" -GridMesh::GridMesh(Qt3DCore::QNode *parent) : Qt3DRender::QGeometryRenderer(parent) +GridMesh::GridMesh(Qt3DCore::QNode *parent) + : Qt3DRender::QGeometryRenderer(parent) { setInstanceCount(1); setIndexOffset(0); setFirstInstance(0); setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines); + setMeshResolution(QSize(20, 20)); +} + +GridMesh::~GridMesh() +{ +} + +QSize GridMesh::meshResolution() const +{ + return m_meshResolution; +} + +void GridMesh::setMeshResolution(const QSize &meshResolution) +{ + if (meshResolution == m_meshResolution) + return; - QSize meshResolution(20, 20); QVector vertices; - for (int x = 0; x <= meshResolution.width(); x++) { - vertices.append(QVector3D(x, 0, 0)); - vertices.append(QVector3D(x, meshResolution.width(), 0)); + const float dx = 1.0f / static_cast(meshResolution.width()); + for (int col = 0; col <= meshResolution.width(); col++) { + const float x = col * dx; + vertices.append(QVector3D(x, 0.0f, 0.0f)); + vertices.append(QVector3D(x, 1.0f, 0.0f)); } - for (int y = 0; y <= meshResolution.height(); y++) { - vertices.append(QVector3D(0, y, 0)); - vertices.append(QVector3D(meshResolution.height(), y, 0)); + const float dy = 1.0f / static_cast(meshResolution.height()); + for (int row = 0; row <= meshResolution.height(); row++) { + const float y = row * dy; + vertices.append(QVector3D(0.0f, y, 0.0f)); + vertices.append(QVector3D(1.0f, y, 0.0f)); } - auto geometry = new LineMeshGeometry(vertices, this); - setVertexCount(geometry->vertexCount()); - setGeometry(geometry); -} + if (geometry()) { + auto geom = geometry(); + setGeometry(nullptr); + delete geom; + } -GridMesh::~GridMesh() -{ + auto geom = new LineMeshGeometry(vertices, this); + setVertexCount(geom->vertexCount()); + setGeometry(geom); + + m_meshResolution = meshResolution; + emit meshResolutionChanged(m_meshResolution); } Index: src/widgets/3dview/viewer3d.cpp =================================================================== --- src/widgets/3dview/viewer3d.cpp +++ src/widgets/3dview/viewer3d.cpp @@ -30,6 +30,7 @@ #include #include "axisgnomonentity.h" +#include "bedproperties.h" #include "cameracontroller.h" #include "gridmesh.h" #include "viewer3d.h" @@ -45,14 +46,16 @@ qmlRegisterType("Atelier", 1, 0, "CameraController"); qmlRegisterType("Atelier", 1, 0, "GridMesh"); qmlRegisterType("Atelier", 1, 0, "LineMesh"); + qmlRegisterType("Atelier", 1, 0, "BedProperties"); _view = new QQuickView(&_engine, nullptr); auto format = QSurfaceFormat(); format.setVersion(3, 1); format.setProfile(QSurfaceFormat::CoreProfile); _view->setFormat(format); + _view->rootContext()->setContextProperty("viewer3d", this); _view->setResizeMode(QQuickView::SizeRootObjectToView); _view->setSource(QUrl(QStringLiteral("qrc:/viewer3d.qml"))); QHBoxLayout *mainLayout = new QHBoxLayout;