]> git.sur5r.net Git - fstl/blob - src/window.cpp
Imported Upstream version 0.9.2
[fstl] / src / window.cpp
1 #include <QMenuBar>
2 #include <QMessageBox>
3 #include <QFileDialog>
4
5 #include "window.h"
6 #include "canvas.h"
7 #include "loader.h"
8
9 Window::Window(QWidget *parent) :
10     QMainWindow(parent),
11     open_action(new QAction("Open", this)),
12     about_action(new QAction("About", this)),
13     quit_action(new QAction("Quit", this))
14
15 {
16     setWindowTitle("fstl");
17     setAcceptDrops(true);
18
19     QFile styleFile(":/qt/style.qss");
20     styleFile.open( QFile::ReadOnly );
21     setStyleSheet(styleFile.readAll());
22
23     QGLFormat format;
24     format.setVersion(2, 1);
25     format.setSampleBuffers(true);
26
27     canvas = new Canvas(format, this);
28     setCentralWidget(canvas);
29
30     open_action->setShortcut(QKeySequence::Open);
31     QObject::connect(open_action, &QAction::triggered,
32                      this, &Window::on_open);
33
34     quit_action->setShortcut(QKeySequence::Quit);
35     QObject::connect(quit_action, &QAction::triggered,
36                      this, &Window::close);
37
38     QObject::connect(about_action, &QAction::triggered,
39                      this, &Window::on_about);
40
41     auto file_menu = menuBar()->addMenu("File");
42     file_menu->addAction(open_action);
43     file_menu->addAction(quit_action);
44
45     auto help_menu = menuBar()->addMenu("Help");
46     help_menu->addAction(about_action);
47
48     resize(600, 400);
49 }
50
51 void Window::on_open()
52 {
53     QString filename = QFileDialog::getOpenFileName(
54                 this, "Load .stl file", QString(), "*.stl");
55     if (not filename.isNull())
56     {
57         load_stl(filename);
58     }
59 }
60
61 void Window::on_about()
62 {
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>");
71 }
72
73 void Window::on_ascii_stl()
74 {
75     QMessageBox::critical(this, "Error",
76                           "<b>Error:</b><br>"
77                           "Cannot open ASCII <code>.stl</code> file<br>"
78                           "Please convert to binary <code>.stl</code> and retry");
79 }
80
81 void Window::on_bad_stl()
82 {
83     QMessageBox::critical(this, "Error",
84                           "<b>Error:</b><br>"
85                           "This <code>.stl</code> file is invalid or corrupted.<br>"
86                           "Please export it from the original source, verify, and retry.");
87 }
88
89 void Window::enable_open()
90 {
91     open_action->setEnabled(true);
92 }
93
94 void Window::disable_open()
95 {
96     open_action->setEnabled(false);
97 }
98
99 bool Window::load_stl(const QString& filename)
100 {
101     if (!open_action->isEnabled())  return false;
102
103     canvas->set_status("Loading " + filename);
104
105     Loader* loader = new Loader(this, filename);
106     connect(loader, &Loader::started,
107               this, &Window::disable_open);
108
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);
115
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);
122
123     if (filename[0] != ':')
124     {
125         connect(loader, &Loader::loaded_file,
126                   this, &Window::setWindowTitle);
127     }
128
129     loader->start();
130     return true;
131 }
132
133 void Window::dragEnterEvent(QDragEnterEvent *event)
134 {
135     if (event->mimeData()->hasUrls())
136     {
137         auto urls = event->mimeData()->urls();
138         if (urls.size() == 1 && urls.front().path().endsWith(".stl"))
139             event->acceptProposedAction();
140     }
141 }
142
143 void Window::dropEvent(QDropEvent *event)
144 {
145     load_stl(event->mimeData()->urls().front().toLocalFile());
146 }