9 Window::Window(QWidget *parent) :
11 open_action(new QAction("Open", this)),
12 about_action(new QAction("About", this)),
13 quit_action(new QAction("Quit", this))
16 setWindowTitle("fstl");
19 QFile styleFile(":/qt/style.qss");
20 styleFile.open( QFile::ReadOnly );
21 setStyleSheet(styleFile.readAll());
24 format.setVersion(2, 1);
25 format.setSampleBuffers(true);
27 canvas = new Canvas(format, this);
28 setCentralWidget(canvas);
30 open_action->setShortcut(QKeySequence::Open);
31 QObject::connect(open_action, &QAction::triggered,
32 this, &Window::on_open);
34 quit_action->setShortcut(QKeySequence::Quit);
35 QObject::connect(quit_action, &QAction::triggered,
36 this, &Window::close);
38 QObject::connect(about_action, &QAction::triggered,
39 this, &Window::on_about);
41 auto file_menu = menuBar()->addMenu("File");
42 file_menu->addAction(open_action);
43 file_menu->addAction(quit_action);
45 auto help_menu = menuBar()->addMenu("Help");
46 help_menu->addAction(about_action);
51 void Window::on_open()
53 QString filename = QFileDialog::getOpenFileName(
54 this, "Load .stl file", QString(), "*.stl");
55 if (not filename.isNull())
61 void Window::on_about()
63 QMessageBox::about(this, "",
64 "<p align=\"center\"><b>fstl</b></p>"
65 "<p>A fast viewer for <code>.stl</code> files.<br>"
66 "<a href=\"https://github.com/mkeeter/fstl\""
67 " style=\"color: #93a1a1;\">https://github.com/mkeeter/fstl</a></p>"
68 "<p>© 2014 Matthew Keeter<br>"
69 "<a href=\"mailto:matt.j.keeter@gmail.com\""
70 " style=\"color: #93a1a1;\">matt.j.keeter@gmail.com</a></p>");
73 void Window::on_ascii_stl()
75 QMessageBox::critical(this, "Error",
77 "Cannot open ASCII <code>.stl</code> file<br>"
78 "Please convert to binary <code>.stl</code> and retry");
81 void Window::on_bad_stl()
83 QMessageBox::critical(this, "Error",
85 "This <code>.stl</code> file is invalid or corrupted.<br>"
86 "Please export it from the original source, verify, and retry.");
89 void Window::enable_open()
91 open_action->setEnabled(true);
94 void Window::disable_open()
96 open_action->setEnabled(false);
99 bool Window::load_stl(const QString& filename)
101 if (!open_action->isEnabled()) return false;
103 canvas->set_status("Loading " + filename);
105 Loader* loader = new Loader(this, filename);
106 connect(loader, &Loader::started,
107 this, &Window::disable_open);
109 connect(loader, &Loader::got_mesh,
110 canvas, &Canvas::load_mesh);
111 connect(loader, &Loader::error_ascii_stl,
112 this, &Window::on_ascii_stl);
113 connect(loader, &Loader::error_bad_stl,
114 this, &Window::on_bad_stl);
116 connect(loader, &Loader::finished,
117 loader, &Loader::deleteLater);
118 connect(loader, &Loader::finished,
119 this, &Window::enable_open);
120 connect(loader, &Loader::finished,
121 canvas, &Canvas::clear_status);
123 if (filename[0] != ':')
125 connect(loader, &Loader::loaded_file,
126 this, &Window::setWindowTitle);
133 void Window::dragEnterEvent(QDragEnterEvent *event)
135 if (event->mimeData()->hasUrls())
137 auto urls = event->mimeData()->urls();
138 if (urls.size() == 1 && urls.front().path().endsWith(".stl"))
139 event->acceptProposedAction();
143 void Window::dropEvent(QDropEvent *event)
145 load_stl(event->mimeData()->urls().front().toLocalFile());