-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
247 lines (206 loc) · 7.74 KB
/
mainwindow.cpp
File metadata and controls
247 lines (206 loc) · 7.74 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "exporttexturesdialog.h"
#include "scenetablemodel.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <QDialog>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <iostream>
#include "project.h"
#define RECENT_MESHES_SETTING "recent/meshes"
#define MAX_RECENT_PATHS 10
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QCoreApplication::setOrganizationName("Paint Bug");
updateRecentMeshesMenu();
ui->sceneTable->setModel(new SceneTableModel());
connect(ui->newProjectAction, SIGNAL(triggered(bool)), this, SLOT(onNewProjectClicked(bool)));
connect(ui->saveProjectAction, SIGNAL(triggered(bool)), this, SLOT(onSaveProjectClicked(bool)));
connect(ui->importMeshAction, SIGNAL(triggered(bool)), this, SLOT(onImportMeshClicked(bool)));
connect(ui->recentMeshMenu, SIGNAL(triggered(QAction*)), this, SLOT(onImportRecentMeshClicked(QAction*)));
connect(ui->exportTexturesAction, SIGNAL(triggered(bool)), this, SLOT(onExportTexturesClicked(bool)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
// TODO: handle keys that bubble up
}
void MainWindow::onNewProjectClicked(bool c)
{
Project::activeProject()->reset();
}
void MainWindow::onSaveProjectClicked(bool c)
{
QString filePath = QFileDialog::getSaveFileName(this, "Save Project", QString(), "Projects (*.pbug)");
if (!filePath.isNull()) {
if (!filePath.endsWith(".pbug")) {
filePath = filePath + ".pbug";
}
QJsonObject rootObj;
rootObj.insert("file_version", "1");
Project *project = Project::activeProject();
QJsonArray meshArr;
QVectorIterator<Mesh*> meshes = project->meshes();
while (meshes.hasNext()) {
Mesh *mesh = meshes.next();
meshArr.append(mesh->serialize());
}
rootObj.insert("meshes", meshArr);
QJsonDocument doc;
doc.setObject(rootObj);
QString contents = doc.toJson();
QFile projFile(filePath);
if (projFile.exists()) {
QMessageBox msgBox;
msgBox.setText("Are you sure you want to overwrite this file?");
//msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
if (ret == QMessageBox::Save) {
//saveFile(projFile, contents);
}
} else {
// saveFile(projFile, contents);
}
//std::cout << doc.toJson().toStdString() << std::endl;
}
}
//void MainWindow::saveFile(QFile projFile, QString contents)
//{
// if (projFile.open(QIODevice::ReadWrite))
// {
// QTextStream stream(&projFile);
// stream << contents << endl;
// projFile.close();
// } else {
// QMessageBox::warning(this, "Error", "Unable to save project");
// }
//}
void MainWindow::onImportMeshClicked(bool c)
{
QString filePath = QFileDialog::getOpenFileName(this, "Import Mesh", "", "Meshes (*.obj)");
if (!filePath.isNull()) {
importMesh(filePath);
}
}
void MainWindow::onImportRecentMeshClicked(QAction *event)
{
QString filePath = event->text();
importMesh(filePath);
}
void MainWindow::onExportTexturesClicked(bool c)
{
ExportTexturesDialog dialog;
dialog.exec();
}
void MainWindow::importMesh(QString filePath)
{
std::cout << "importing mesh file: " << filePath << std::endl;
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(filePath.toStdString(),
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
// If the import failed, report it
if(!scene) {
std::cerr << "error importing mesh: " << importer.GetErrorString() << std::endl;
showImportError();
} else if (!scene->HasMeshes()) {
std::cerr << "error importing mesh: no meshes found" << std::endl;
showImportError();
} else {
bool meshFailure = false;
for (int meshIndex = 0; meshIndex < scene->mNumMeshes; meshIndex++) {
aiMesh* mMesh = scene->mMeshes[meshIndex];
if (!mMesh->HasPositions()) {
std::cerr << "error importing mesh (no positions detected): " << mMesh->mName.C_Str() << std::endl;
meshFailure = true;
} else if (!mMesh->HasFaces()) {
std::cerr << "error importing mesh (no faces detected): " << mMesh->mName.C_Str() << std::endl;
} else if (!mMesh->HasTextureCoords(0)) {
std::cerr << "error importing mesh (no texture coordinates detected): " << mMesh->mName.C_Str() << std::endl;
meshFailure = true;
} else {
Mesh* m = new Mesh();
for (int vertIndex = 0; vertIndex < mMesh->mNumVertices; vertIndex++) {
aiVector3D v = mMesh->mVertices[vertIndex];
m->addVertex(v.x, v.y, v.z);
}
for (int uvIndex = 0; uvIndex < mMesh->mNumVertices; uvIndex++) {
aiVector3D uv = mMesh->mTextureCoords[0][uvIndex];
m->addUV(uv.x, uv.y);
}
for (int faceIndex = 0; faceIndex < mMesh->mNumFaces; faceIndex++) {
m->addTriangle(mMesh->mFaces[faceIndex].mIndices[0],
mMesh->mFaces[faceIndex].mIndices[1],
mMesh->mFaces[faceIndex].mIndices[2]);
}
m->setMeshName(mMesh->mName.C_Str());
m->setGeometryPath(filePath);
Project* project = Project::activeProject();
project->addMesh(m);
}
}
if (meshFailure) {
QMessageBox::warning(this, tr("Error"),
tr("At least one mesh was unable to be imported. See log for more details"),
QMessageBox::Ok);
}
QList<QString> recentMeshPaths = getRecentMeshPaths();
int currentIndex = recentMeshPaths.indexOf(filePath);
if (currentIndex > -1) {
recentMeshPaths.removeAt(currentIndex);
}
recentMeshPaths.prepend(filePath);
while (recentMeshPaths.count() > MAX_RECENT_PATHS) {
recentMeshPaths.removeLast();
}
QSettings settings;
settings.setValue(RECENT_MESHES_SETTING, QVariant(recentMeshPaths));
settings.sync();
updateRecentMeshesMenu();
}
}
void MainWindow::showImportError()
{
QMessageBox::warning(this, tr("Error"),
tr("Unable to import mesh file. See log for more details"),
QMessageBox::Ok);
}
QList<QString> MainWindow::getRecentMeshPaths()
{
QSettings settings;
QList<QVariant> storedList = settings.value(RECENT_MESHES_SETTING).toList();
QList<QString> list;
foreach(QVariant v, storedList) {
list << v.value<QString>();
}
return list;
}
void MainWindow::updateRecentMeshesMenu()
{
ui->recentMeshMenu->clear();
QList<QString> recentMeshPaths = getRecentMeshPaths();
foreach (QString recentMeshPath, recentMeshPaths) {
QFileInfo fileInfo(recentMeshPath);
if (fileInfo.exists() && fileInfo.isFile()) {
ui->recentMeshMenu->addAction(recentMeshPath);
}
}
ui->recentMeshMenu->setEnabled(recentMeshPaths.count() > 0);
}