diff --git a/src/widgets/3dview/CMakeLists.txt b/src/widgets/3dview/CMakeLists.txt index 2ddc732..3727faf 100644 --- a/src/widgets/3dview/CMakeLists.txt +++ b/src/widgets/3dview/CMakeLists.txt @@ -1,25 +1,26 @@ set(3d_SRCS axisgnomonentity.cpp + bedproperties.cpp cameracontroller.cpp fileloader.cpp gcodeto4d.cpp gridmesh.cpp linemesh.cpp linemeshgeometry.cpp viewer3d.cpp ) qt5_add_resources(3dfiles_RCS viewer3d.qrc) add_library(Atelier3D STATIC ${3d_SRCS} ${3dfiles_RCS}) target_link_libraries(Atelier3D Qt5::Core Qt5::Qml Qt5::Quick Qt5::Widgets Qt5::3DCore Qt5::3DExtras Qt5::3DRender Qt5::3DInput ) diff --git a/src/widgets/3dview/SceneEntity.qml b/src/widgets/3dview/SceneEntity.qml index 71f5e28..e976370 100644 --- a/src/widgets/3dview/SceneEntity.qml +++ b/src/widgets/3dview/SceneEntity.qml @@ -1,77 +1,89 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2017-2018> Author: Patrick José Pereira - patrickjp@kde.org 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 . */ import QtQuick 2.7 import Qt3D.Core 2.0 import Qt3D.Render 2.0 import Qt3D.Input 2.0 import Qt3D.Extras 2.0 import Atelier 1.0 Entity { id: sceneRoot property string currentFile + BedProperties { + id: bedProperties + } + Camera { id: camera fieldOfView: 45 position: Qt.vector3d( 10.0, -10.0, 35.0 ) upVector: Qt.vector3d( 0.0, 0.85, 0.75 ) viewCenter: Qt.vector3d( 10.0, 10.0, 0.0 ) } CameraController { camera: camera } components: [ RenderSettings { activeFrameGraph: ForwardRenderer { camera: camera frustumCulling: false } }, InputSettings { } ] Entity { 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) + } ] } Entity { id: lineEntity components: [ PhongMaterial { ambient: "darkGreen" }, LineMesh { readonly property string currentFile: sceneRoot.currentFile onCurrentFileChanged: readAndRun(currentFile) } ] } AxisGnomonEntity { scale: 0.05 position: Qt.vector2d(0.075, 0.075) } } diff --git a/src/widgets/3dview/bedproperties.cpp b/src/widgets/3dview/bedproperties.cpp new file mode 100644 index 0000000..a06f987 --- /dev/null +++ b/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); + } +} diff --git a/src/widgets/3dview/gridmesh.h b/src/widgets/3dview/bedproperties.h similarity index 64% copy from src/widgets/3dview/gridmesh.h copy to src/widgets/3dview/bedproperties.h index 9a52824..a14a225 100644 --- a/src/widgets/3dview/gridmesh.h +++ b/src/widgets/3dview/bedproperties.h @@ -1,36 +1,47 @@ /* 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 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 . */ #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; }; diff --git a/src/widgets/3dview/gridmesh.cpp b/src/widgets/3dview/gridmesh.cpp index b15cad1..83e8675 100644 --- a/src/widgets/3dview/gridmesh.cpp +++ b/src/widgets/3dview/gridmesh.cpp @@ -1,56 +1,81 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2017-2018> Author: Patrick José Pereira - patrickjp@kde.org 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 #include #include #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); } diff --git a/src/widgets/3dview/gridmesh.h b/src/widgets/3dview/gridmesh.h index 9a52824..4f8d73a 100644 --- a/src/widgets/3dview/gridmesh.h +++ b/src/widgets/3dview/gridmesh.h @@ -1,36 +1,48 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2017-2018> Author: Patrick José Pereira - patrickjp@kde.org 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 . */ #pragma once #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; }; diff --git a/src/widgets/3dview/viewer3d.cpp b/src/widgets/3dview/viewer3d.cpp index 4d12972..96e5a28 100644 --- a/src/widgets/3dview/viewer3d.cpp +++ b/src/widgets/3dview/viewer3d.cpp @@ -1,93 +1,96 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2017> Author: Patrick José Pereira - patrickjp@kde.org Chris Rizzitello - rizzitello@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 #include #include #include #include #include #include #include #include #include "axisgnomonentity.h" +#include "bedproperties.h" #include "cameracontroller.h" #include "gridmesh.h" #include "viewer3d.h" #include "linemesh.h" Viewer3D::Viewer3D(QWidget *parent) : QWidget(parent) , _lineMesh(new LineMesh) { Q_INIT_RESOURCE(viewer3d); qmlRegisterType("Atelier", 1, 0, "AxisGnomonEntity"); 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; mainLayout->addWidget(QWidget::createWindowContainer(_view)); QObject *item = _view->rootObject(); //Connect the drop pass from the QML part. connect(item, SIGNAL(droppedUrls(QVariant)), this, SLOT(dropCatch(QVariant))); this->setLayout(mainLayout); } Viewer3D::~Viewer3D() { } void Viewer3D::dropCatch(const QVariant &var) { emit droppedUrls(var.value >()); } void Viewer3D::drawModel(QString file) { QObject *object = _view->rootObject(); QObject *fileName = object->findChild(QStringLiteral("fileName")); fileName->setProperty("text", QVariant(file)); } void Viewer3D::setBedSize(const QSize &newBedSize) { if (newBedSize != _bedSize) { _bedSize = newBedSize; emit bedSizeChanged(_bedSize); } } QSize Viewer3D::bedSize() { return _bedSize; }