#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");
+ setAcceptDrops(true);
+
+ QFile styleFile(":/qt/style.qss");
+ styleFile.open( QFile::ReadOnly );
+ setStyleSheet(styleFile.readAll());
QGLFormat format;
format.setVersion(2, 1);
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);
void Window::on_about()
{
- QMessageBox::about(this, "About fstl", "<b>fstl</b><br><br>"
- "A fast viewer for <code>.stl</code> files.<br>"
- "<a href=\"https://github.com/mkeeter/fstl\">https://github.com/mkeeter/fstl</a><br><br>"
- "© 2014 Matthew Keeter<br>"
- "<a href=\"mailto:matt.j.keeter@gmail.com\">matt.j.keeter@gmail.com</a><br>");
+ QMessageBox::about(this, "",
+ "<p align=\"center\"><b>fstl</b></p>"
+ "<p>A fast viewer for <code>.stl</code> files.<br>"
+ "<a href=\"https://github.com/mkeeter/fstl\""
+ " style=\"color: #93a1a1;\">https://github.com/mkeeter/fstl</a></p>"
+ "<p>© 2014 Matthew Keeter<br>"
+ "<a href=\"mailto:matt.j.keeter@gmail.com\""
+ " 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;
+}
+
+void Window::dragEnterEvent(QDragEnterEvent *event)
+{
+ if (event->mimeData()->hasUrls())
+ {
+ auto urls = event->mimeData()->urls();
+ if (urls.size() == 1 && urls.front().path().endsWith(".stl"))
+ event->acceptProposedAction();
+ }
+}
+
+void Window::dropEvent(QDropEvent *event)
+{
+ load_stl(event->mimeData()->urls().front().toLocalFile());
}