From 54470644f121f8b5775ac9969af1640714e25118 Mon Sep 17 00:00:00 2001 From: papush! Date: Thu, 18 Nov 2021 14:55:03 +0100 Subject: [PATCH] cleanup initialization --- src/main.cpp | 20 +++----------------- src/mesh_processor.cpp | 23 ++++++++++++++++++----- src/mesh_viewer.cpp | 1 + src/mesh_viewer.h | 2 ++ 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index afd7067..91fe2e8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,31 +18,17 @@ int main(int argc, char *argv[]) { MeshProcessor *mesh_processor = nullptr; MainWindow main_window; MeshViewer *mesh_viewer = &main_window.mesh_viewer; - QObject::connect(mesh_viewer, &MeshViewer::initialized, - [&]() { - if (mesh_processor) { - mesh_viewer->addMesh(mesh_processor->mesh); - } - }); - if (argc > 2) { qWarning("Utilisation : %s [MAILLAGE]", argv[0]); return 1; } else if (argc == 2) { mesh_processor = new MeshProcessor(argv[1], *mesh_viewer); - QObject::connect(mesh_viewer, &MeshViewer::clicked, - mesh_processor, &MeshProcessor::click); } QObject::connect(&main_window, &MainWindow::open, [&](const QString &path) { - if (mesh_processor) { - mesh_viewer->removeMesh(mesh_processor->mesh); - delete mesh_processor; - } - mesh_processor = new MeshProcessor(path, *mesh_viewer); - mesh_viewer->addMesh(mesh_processor->mesh); - QObject::connect(mesh_viewer, &MeshViewer::clicked, - mesh_processor, &MeshProcessor::click); + if (mesh_processor) delete mesh_processor; + mesh_processor = new MeshProcessor(path, + *mesh_viewer); }); main_window.show(); return app.exec(); diff --git a/src/mesh_processor.cpp b/src/mesh_processor.cpp index c79e0dc..d9610a4 100644 --- a/src/mesh_processor.cpp +++ b/src/mesh_processor.cpp @@ -35,16 +35,29 @@ MeshProcessor::MeshProcessor(const QString &path, MeshViewer &mesh_viewer) return; } mesh.update_normals(); - courbure = new Courbures(mesh); - courbure->compute_KH(); - courbure->set_K_colors(); - // mesh.holes = findHoles(mesh); - // fillHoles(); + // courbure = new Courbures(mesh); + // courbure->compute_KH(); + // courbure->set_K_colors(); + mesh.holes = findHoles(mesh); + fillHoles(); // smooth(mesh); + if (mesh_viewer.isInitialized()) { + mesh_viewer.addMesh(mesh); + } else { + connect(&mesh_viewer, &MeshViewer::initialized, + [&]() { mesh_viewer.addMesh(mesh); }); + } + connect(&mesh_viewer, &MeshViewer::clicked, this, &MeshProcessor::click); } MeshProcessor::~MeshProcessor() { + if (mesh_viewer.isInitialized()) { + mesh_viewer.removeMesh(mesh); + } else { + connect(&mesh_viewer, &MeshViewer::initialized, + [&]() { mesh_viewer.removeMesh(mesh); }); + } if (courbure) delete courbure; } diff --git a/src/mesh_viewer.cpp b/src/mesh_viewer.cpp index 62f5020..bd32d5f 100644 --- a/src/mesh_viewer.cpp +++ b/src/mesh_viewer.cpp @@ -64,6 +64,7 @@ void MeshViewer::initializeGL() { glf->glEnable(GL_MULTISAMPLE); qDebug("MeshViewer: initialization complete"); + is_initialized = true; emit initialized(); } diff --git a/src/mesh_viewer.h b/src/mesh_viewer.h index 67a41ea..609fe85 100644 --- a/src/mesh_viewer.h +++ b/src/mesh_viewer.h @@ -35,6 +35,7 @@ class MeshViewer : public QOpenGLWidget { QMatrix4x4 view = trans * rot; QPoint mouse_pos; void updateViewMatrix(); + bool is_initialized = false; public: MeshViewer(QWidget *parent=nullptr); @@ -42,6 +43,7 @@ public: void initializeGL() override; void resizeGL(int w, int h) override; void paintGL() override; + constexpr bool isInitialized() { return is_initialized; } public slots: void addMesh(const MyMesh &mesh);