mod_geo-tp/src/main_window.cpp
2021-09-20 20:36:29 +02:00

57 lines
1.6 KiB
C++

#include "main_window.h"
#include <QApplication>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
:QMainWindow(parent),
mesh_viewer(this),
toolbar(this) {
connect(&mesh_viewer, &MeshViewer::initialized, this, &MainWindow::meshViewerInitialized);
setCentralWidget(&mesh_viewer);
addToolBar(Qt::RightToolBarArea, &toolbar);
open_action = toolbar.addAction("Ouvrir…", [&](){
open(QFileDialog::getOpenFileName(this, "Ouvrir un fichier OBJ"));
});
// toolbar_actions.append(toolbar.addAction("Fractionner", [&](){
// QVector<QPair<MyMesh::Point, MyMesh>> fragments = shatter(mesh);
// mesh_viewer.removeOpenGLMesh(glm);
// for (auto &[pos, fragment] : fragments) {
// fragment.triangulate();
// QMatrix4x4 mat;
// float scale = 1.2;
// mat.translate(pos[0] * scale, pos[1] * scale, pos[2] * scale);
// mesh_viewer.addOpenGLMeshFromOpenMesh(&fragment, mat);
// }
// }));
open_action->setEnabled(false);
for (QAction *a : toolbar_actions) {
a->setEnabled(false);
}
}
void MainWindow::open(const QString &path) {
if (!OpenMesh::IO::read_mesh(mesh, path.toUtf8().constData())) {
qWarning() << "Failed to read" << path;
return;
}
for (const VertexHandle &vh : mesh.vertices()) {
mesh.set_color(vh, MyMesh::Color(.5, .5, .5));
}
if (glm != nullptr) mesh_viewer.removeOpenGLMesh(glm);
glm = mesh_viewer.addOpenGLMeshFromOpenMesh(&mesh);
for (QAction *a : toolbar_actions) {
a->setEnabled(true);
}
}
void MainWindow::meshViewerInitialized() {
if (qApp->arguments().size() == 2) {
open(qApp->arguments().at(1));
}
open_action->setEnabled(true);
}