-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglmdichild.cpp
249 lines (229 loc) · 6.82 KB
/
glmdichild.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright (c) 2014 Andranik Abrahamyan
#include <QtGui/QtGui>
#include <exception>
#include "glmdichild.h"
GLMdiChild::GLMdiChild(QWidget *parent) : GLWidget(parent)
{
m_stlFile = new StlFile();
setAttribute(Qt::WA_DeleteOnClose);
m_isUntitled = true;
}
GLMdiChild::~GLMdiChild()
{
delete m_stlFile;
}
void GLMdiChild::newFile()
{
static int sequenceNumber = 1;
m_isUntitled = true;
m_curFile = tr("untitled%1.stl").arg(sequenceNumber++);
setWindowTitle(m_curFile);
}
bool GLMdiChild::loadFile(const QString &fileName)
{
try {
QApplication::setOverrideCursor(Qt::WaitCursor);
// Open the file and make an object from its content
std::string utf8_fileName = fileName.toUtf8().constData();
if (!m_stlFile->open(utf8_fileName)) {
::std::cerr << "The file " << utf8_fileName << " not a correct STL file." << ::std::endl;
return false;
}
makeObjectFromStlFile(m_stlFile);
updateGL();
setCurrentFile(fileName);
QApplication::restoreOverrideCursor();
return true;
} catch (::std::bad_alloc) {
QApplication::restoreOverrideCursor();
QMessageBox msgBox;
msgBox.setText("Problem allocating memory.");
msgBox.exec();
return false;
} catch (StlFile::wrong_header_size) {
QApplication::restoreOverrideCursor();
QMessageBox msgBox;
msgBox.setText("The file " + fileName + " has a wrong size.");
msgBox.exec();
return false;
} catch (StlFile::error_opening_file) { // ::std::ios_base::failure
QApplication::restoreOverrideCursor();
QMessageBox msgBox;
msgBox.setText("The file " + fileName + " could not be opened.");
msgBox.exec();
return false;
} catch (...) {
QApplication::restoreOverrideCursor();
QMessageBox msgBox;
msgBox.setText("Error unknown.");
msgBox.exec();
return false;
}
}
bool GLMdiChild::save()
{
if (m_isUntitled)
return saveAs();
else
return saveFile(m_curFile);
}
bool GLMdiChild::saveAsSphere()
{
try {
QApplication::setOverrideCursor(Qt::WaitCursor);
// Write the current object into a file
std::string utf8_fileName = m_curFile.toUtf8().constData();
utf8_fileName.erase (utf8_fileName.end() -3, utf8_fileName.end());
utf8_fileName.append ("sph");
m_stlFile->writeAsSphere(utf8_fileName);
QApplication::restoreOverrideCursor();
//setCurrentFile(fileName);
return true;
} catch (StlFile::error_opening_file) { // ::std::ios_base::failure
QApplication::restoreOverrideCursor();
QMessageBox msgBox;
msgBox.setText("Unable to write as sphere in " + m_curFile + ".");
msgBox.exec();
return false;
} catch (...) {
QApplication::restoreOverrideCursor();
QMessageBox msgBox;
msgBox.setText("Error unknown.");
msgBox.exec();
return false;
}
}
bool GLMdiChild::saveAsNormalFrequency()
{
try {
QApplication::setOverrideCursor(Qt::WaitCursor);
// Write the current object into a file
std::string utf8_fileName = m_curFile.toUtf8().constData();
utf8_fileName.erase (utf8_fileName.end() -3, utf8_fileName.end());
utf8_fileName.append ("nfr");
m_stlFile->m_stlSphere->writeAsNormalFrequency(utf8_fileName);
QApplication::restoreOverrideCursor();
//setCurrentFile(fileName);
return true;
} catch (StlFile::error_opening_file) { // ::std::ios_base::failure
QApplication::restoreOverrideCursor();
QMessageBox msgBox;
msgBox.setText("Unable to write as sphere in " + m_curFile + ".");
msgBox.exec();
return false;
} catch (...) {
QApplication::restoreOverrideCursor();
QMessageBox msgBox;
msgBox.setText("Error unknown.");
msgBox.exec();
return false;
}
}
bool GLMdiChild::saveAs()
{
if (!m_isUntitled) {
QString filterBin = tr("STL Files, binary (*.stl)");
QString filterAscii = tr("STL Files, ASCII (*.stl)");
QString filterAll = tr("All files (*.*)");
QString filterSel;
// Set the current file type as default
if (m_stlFile->getStats().type == ASCII)
filterSel = filterAscii;
else
filterSel = filterBin;
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), m_curFile,
filterBin + ";;" + filterAscii + ";;" + filterAll, &filterSel);
if (fileName.isEmpty())
return false;
// Change the current file type to the one chosen by the user
if (filterSel == filterBin)
m_stlFile->setFormat(BINARY);
else if (filterSel == filterAscii)
m_stlFile->setFormat(ASCII);
// Save the file
return saveFile(fileName);
}
return false;
}
bool GLMdiChild::saveFile(const QString &fileName) {
try {
QApplication::setOverrideCursor(Qt::WaitCursor);
// Write the current object into a file
std::string utf8_fileName = fileName.toUtf8().constData();
m_stlFile->write(utf8_fileName);
QApplication::restoreOverrideCursor();
setCurrentFile(fileName);
return true;
} catch (StlFile::error_opening_file) { // ::std::ios_base::failure
QApplication::restoreOverrideCursor();
QMessageBox msgBox;
msgBox.setText("Unable to write in " + fileName + ".");
msgBox.exec();
return false;
} catch (...) {
QApplication::restoreOverrideCursor();
QMessageBox msgBox;
msgBox.setText("Error unknown.");
msgBox.exec();
return false;
}
}
bool GLMdiChild::saveImage() {
QFileInfo fi(m_curFile);
QString imFile = fi.path() + "/" + fi.completeBaseName() + ".png";
QString filterPng = tr("PNG Files (*.png)");
QString filterBmp = tr("BMP Files (*.bmp)");
QString filterAll = tr("All files (*.*)");
QString filterSel = filterPng; // Default image type is PNG
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Image"), imFile,
filterPng + ";;" + filterBmp + ";;" + filterAll, &filterSel);
if (fileName.isEmpty())
return false;
QString format = "png"; // Default image type is PNG if none was specified
if (filterSel == filterBmp || QFileInfo(fileName).suffix() == ".bmp")
format = "bmp";
QApplication::setOverrideCursor(Qt::WaitCursor);
QImage screenshot = this->grabFrameBuffer();
screenshot.save(fileName, format.toAscii());
QApplication::restoreOverrideCursor();
return true;
}
QString GLMdiChild::userFriendlyCurrentFile()
{
return strippedName(m_curFile);
}
void GLMdiChild::closeEvent(QCloseEvent *event) {
// m_stlFile->close();
// m_stlSphere->close();
if (maybeSave())
event->accept();
else
event->ignore();
}
void GLMdiChild::mousePressEvent(QMouseEvent *event)
{
// Emit a signal if a mouse button is pressed on this widget
emit mouseButtonPressed(event->buttons());
GLWidget::mousePressEvent(event);
}
void GLMdiChild::mouseReleaseEvent(QMouseEvent *event)
{
// Emit a signal if a mouse button is released on this widget
emit mouseButtonReleased(event->button());
GLWidget::mouseReleaseEvent(event);
}
bool GLMdiChild::maybeSave()
{
return true;
}
void GLMdiChild::setCurrentFile(const QString &fileName)
{
m_curFile = QFileInfo(fileName).canonicalFilePath();
m_isUntitled = false;
setWindowModified(false);
setWindowTitle(userFriendlyCurrentFile() + "[*]");
}
QString GLMdiChild::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}