]> git.sur5r.net Git - fstl/blobdiff - src/window.cpp
Added check for stl corruption
[fstl] / src / window.cpp
index 90517e4b0aa24e786b9323051f457a0d1c5cf0b4..2d809bfb0610c8a3924213148528d9693c377c22 100644 (file)
@@ -7,11 +7,15 @@
 #include "loader.h"
 
 Window::Window(QWidget *parent) :
-    QMainWindow(parent)
+    QMainWindow(parent),
+    open_action(new QAction("Open", this)),
+    about_action(new QAction("About", this)),
+    quit_action(new QAction("Quit", this))
+
 {
     setWindowTitle("fstl");
 
-    QFile styleFile( ":/style.qss" );
+    QFile styleFile(":/qt/style.qss");
     styleFile.open( QFile::ReadOnly );
     setStyleSheet(styleFile.readAll());
 
@@ -22,19 +26,16 @@ Window::Window(QWidget *parent) :
     canvas = new Canvas(format, this);
     setCentralWidget(canvas);
 
-    open_action = new QAction("Open", this);
     open_action->setShortcut(QKeySequence::Open);
-    QObject::connect(open_action, SIGNAL(triggered()),
-                     this, SLOT(on_open()));
+    QObject::connect(open_action, &QAction::triggered,
+                     this, &Window::on_open);
 
-    quit_action = new QAction("Quit", this);
     quit_action->setShortcut(QKeySequence::Quit);
-    QObject::connect(quit_action, SIGNAL(triggered()),
-                     this, SLOT(close()));
+    QObject::connect(quit_action, &QAction::triggered,
+                     this, &Window::close);
 
-    about_action = new QAction("About", this);
-    QObject::connect(about_action, SIGNAL(triggered()),
-                     this, SLOT(on_about()));
+    QObject::connect(about_action, &QAction::triggered,
+                     this, &Window::on_about);
 
     auto file_menu = menuBar()->addMenu("File");
     file_menu->addAction(open_action);
@@ -68,31 +69,62 @@ void Window::on_about()
         "   style=\"color: #93a1a1;\">matt.j.keeter@gmail.com</a></p>");
 }
 
+void Window::on_ascii_stl()
+{
+    QMessageBox::critical(this, "Error",
+                          "<b>Error:</b><br>"
+                          "Cannot open ASCII <code>.stl</code> file<br>"
+                          "Please convert to binary <code>.stl</code> and retry");
+}
 
-void Window::enable_open_action()
+void Window::on_bad_stl()
 {
-    open_action->setEnabled(true);
+    QMessageBox::critical(this, "Error",
+                          "<b>Error:</b><br>"
+                          "This <code>.stl</code> file is invalid or corrupted.<br>"
+                          "Please export it from the original source, verify, and retry.");
 }
 
+void Window::enable_open()
+{
+    open_action->setEnabled(true);
+}
 
-void Window::disable_open_action()
+void Window::disable_open()
 {
     open_action->setEnabled(false);
 }
 
-
-void Window::load_stl(const QString &filename)
+bool Window::load_stl(const QString& filename)
 {
+    if (!open_action->isEnabled())  return false;
+
+    canvas->set_status("Loading " + filename);
+
     Loader* loader = new Loader(this, filename);
-    connect(loader, SIGNAL(started()),
-            this, SLOT(disable_open_action()));
-    connect(loader, SIGNAL(got_mesh(Mesh*)),
-            canvas, SLOT(load_mesh(Mesh*)));
-    connect(loader, SIGNAL(finished()),
-            loader, SLOT(deleteLater()));
-    connect(loader, SIGNAL(finished()),
-            this, SLOT(enable_open_action()));
-    connect(loader, SIGNAL(loaded_file(QString)),
-            this, SLOT(setWindowTitle(QString)));
+    connect(loader, &Loader::started,
+              this, &Window::disable_open);
+
+    connect(loader, &Loader::got_mesh,
+            canvas, &Canvas::load_mesh);
+    connect(loader, &Loader::error_ascii_stl,
+              this, &Window::on_ascii_stl);
+    connect(loader, &Loader::error_bad_stl,
+              this, &Window::on_bad_stl);
+
+    connect(loader, &Loader::finished,
+            loader, &Loader::deleteLater);
+    connect(loader, &Loader::finished,
+              this, &Window::enable_open);
+    connect(loader, &Loader::finished,
+            canvas, &Canvas::clear_status);
+
+    if (filename[0] != ':')
+    {
+        connect(loader, &Loader::loaded_file,
+                  this, &Window::setWindowTitle);
+    }
+
     loader->start();
+    return true;
 }