-
Notifications
You must be signed in to change notification settings - Fork 334
/
main.cpp
265 lines (221 loc) · 8.21 KB
/
main.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
// This source file is part of appleseed.
// Visit http://appleseedhq.net/ for additional information and resources.
//
// This software is released under the MIT license.
//
// Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// appleseed.studio headers.
#include "commandlinehandler.h"
#include "mainwindow/mainwindow.h"
// appleseed.shared headers.
#include "application/application.h"
#include "application/superlogger.h"
// appleseed.foundation headers.
#include "foundation/core/appleseed.h"
#include "foundation/platform/path.h"
#include "foundation/utility/log.h"
#include "foundation/utility/preprocessor.h"
// appleseed.main headers.
#include "main/allocator.h"
// Qt headers.
#include <QApplication>
#include <QLocale>
#include <QMessageBox>
#include <QString>
#include <QTextStream>
// boost headers.
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
// Standard headers.
#include <cstdlib>
#include <fstream>
#include <locale>
#include <sstream>
#include <string>
using namespace appleseed::studio;
using namespace appleseed::shared;
using namespace boost;
using namespace foundation;
using namespace std;
namespace
{
void display_incorrect_installation_error()
{
// We need the path to the application's executable to construct the error message.
const filesystem::path executable_path(Path::get_executable_path());
// Construct the error message.
const string informative_text =
"Specifically, it was expected that " + executable_path.filename().string() + " would "
"reside in a " + filesystem::path("bin/").make_preferred().string() + " subdirectory "
"inside the main directory of the application, but it appears not to be the case "
"(" + executable_path.filename().string() +
" seems to be located in " + executable_path.parent_path().string() + ").";
// Display a message box.
QMessageBox msgbox;
msgbox.setWindowTitle("Application Incorrectly Installed");
msgbox.setIcon(QMessageBox::Critical);
msgbox.setText(
"The application failed to start because it is not properly installed. "
"Please reinstall the application.");
msgbox.setInformativeText(QString::fromStdString(informative_text));
msgbox.setStandardButtons(QMessageBox::Ok);
msgbox.setDefaultButton(QMessageBox::Ok);
msgbox.exec();
}
void check_installation()
{
if (!Application::is_correctly_installed())
{
display_incorrect_installation_error();
exit(EXIT_FAILURE);
}
}
bool load_file(const string& filename, string& contents)
{
ifstream file(filename.c_str());
if (!file.is_open())
return false;
stringstream sstr;
sstr << file.rdbuf();
contents = sstr.str();
return true;
}
void display_stylesheet_load_error(const string& stylesheet_path)
{
QMessageBox msgbox;
msgbox.setWindowTitle("Failed to Load Default Stylesheet");
msgbox.setIcon(QMessageBox::Warning);
msgbox.setText(
QString(
"The stylesheet %1 could not be loaded.\n\n"
"The application will use the default style.")
.arg(QString::fromStdString(stylesheet_path)));
msgbox.setStandardButtons(QMessageBox::Ok);
msgbox.setDefaultButton(QMessageBox::Ok);
msgbox.exec();
}
void display_stylesheet_process_error(
const string& stylesheet_path,
const string& error_message,
const size_t error_location)
{
QMessageBox msgbox;
msgbox.setWindowTitle("Failed to Process Default Stylesheet");
msgbox.setIcon(QMessageBox::Warning);
msgbox.setText(
QString("An error occurred while processing the stylesheet %1.\n\n"
"The application will use the default style.")
.arg(QString::fromStdString(stylesheet_path)));
msgbox.setDetailedText(
QString("Line %1: %3.")
.arg(error_location)
.arg(QString::fromStdString(error_message)));
msgbox.setStandardButtons(QMessageBox::Ok);
msgbox.setDefaultButton(QMessageBox::Ok);
msgbox.exec();
}
bool load_stylesheet(const string& stylesheet_path, string& stylesheet)
{
if (!load_file(stylesheet_path, stylesheet))
{
display_stylesheet_load_error(stylesheet_path);
return false;
}
Preprocessor preprocessor;
#if defined __APPLE__
preprocessor.define_symbol("__APPLE__");
#elif defined _WIN32
preprocessor.define_symbol("_WIN32");
#endif
preprocessor.process(stylesheet.c_str());
if (preprocessor.failed())
{
display_stylesheet_process_error(
stylesheet_path,
preprocessor.get_error_message(),
preprocessor.get_error_location());
return false;
}
stylesheet = preprocessor.get_processed_text();
return true;
}
void set_default_stylesheet(QApplication& application)
{
if (Application::is_correctly_installed())
{
// Build the path to the default stylesheet file.
const filesystem::path stylesheet_path =
filesystem::path(Application::get_root_path())
/ "stylesheets"
/ "default.qss";
// Load and apply the stylesheet.
string stylesheet;
if (load_stylesheet(stylesheet_path.string(), stylesheet))
application.setStyleSheet(QString::fromStdString(stylesheet));
}
}
void configure_application(QApplication& application)
{
application.setAttribute(Qt::AA_DontUseNativeMenuBar, true);
set_default_stylesheet(application);
}
}
//
// Entry point of appleseed.studio.
//
int main(int argc, char *argv[])
{
start_memory_tracking();
QApplication application(argc, argv);
QApplication::setOrganizationName("appleseedhq");
QApplication::setOrganizationDomain("appleseedhq.net");
QApplication::setApplicationName("appleseed.studio");
QApplication::setApplicationVersion(Appleseed::get_lib_version());
// The locale must be set after the construction of QApplication.
QLocale::setDefault(QLocale::C);
setlocale(LC_ALL, "C");
check_installation();
SuperLogger logger;
#ifdef _WIN32
logger.set_log_target(create_string_log_target());
#endif
CommandLineHandler cl;
cl.parse(argc, const_cast<const char**>(argv), logger);
configure_application(application);
appleseed::studio::MainWindow window;
if (!cl.m_filename.values().empty())
{
const QString filename = QString::fromStdString(cl.m_filename.values().front());
if (cl.m_render.is_set())
{
const QString configuration = QString::fromStdString(cl.m_render.values()[0]);
window.open_and_render_project(filename, configuration);
}
else
{
window.open_project(filename);
}
}
window.show();
return application.exec();
}