-
Notifications
You must be signed in to change notification settings - Fork 3
/
tab.cpp
312 lines (257 loc) · 9.34 KB
/
tab.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <QApplication>
#include <QDir>
#include <QMessageBox>
#include <QSettings>
#include <QSplitter>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QToolBar>
#include <Qsci/qscilexercpp.h>
#include <Qsci/qscilexer.h>
#include <cmath>
#include "loader.h"
#include "tab.h"
#include "canvas.h"
Tab::Tab(QWidget *parent)
: QWidget(parent), code(new QsciScintilla()), lexer(new QsciLexerCPP()),
canvas(new Canvas(
[] {
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setVersion(2, 1);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
return format;
}(),
this)),
toolbar(new QToolBar(this)), console(new QTextEdit()),
v_splitter(new QSplitter()), h_splitter(new QSplitter())
{
code->setWrapMode(QsciScintilla::WrapWord);
code->setWrapVisualFlags(QsciScintilla::WrapFlagByBorder);
code->setIndentationsUseTabs(false);
code->setTabWidth(2);
code->setIndentationGuides(true);
code->setAutoIndent(true);
code->setCaretLineVisible(true);
code->setMarginType(1, QsciScintilla::NumberMargin);
code->setLexer(lexer);
connect(code, &QsciScintilla::copyAvailable,
[=](const bool available) { emit copyAvailable(available); });
connect(code, &QsciScintilla::linesChanged, [=] {
const auto lines = code->lines();
const auto digits = std::floor(std::log10(lines) + 1);
code->setMarginWidth(1, QString("0").repeated(digits+1));
});
toolbar->addAction(tr("Orthographic"),
[=] { canvas->view_orthographic(); });
toolbar->addAction(tr("Perspective"), [=] { canvas->view_perspective(); });
toolbar->addAction(tr("Reset Cam"), canvas, &Canvas::reset_cam);
toolbar->addAction(
tr("Front"), [=] { canvas->setCameraAngle(Canvas::Direction::Front); });
toolbar->addAction(
tr("Back"), [=] { canvas->setCameraAngle(Canvas::Direction::Back); });
toolbar->addAction(tr("Top"),
[=] { canvas->setCameraAngle(Canvas::Direction::Top); });
toolbar->addAction(tr("Bottom"), [=] {
canvas->setCameraAngle(Canvas::Direction::Bottom);
});
toolbar->addAction(
tr("Left"), [=] { canvas->setCameraAngle(Canvas::Direction::Left); });
toolbar->addAction(
tr("Right"), [=] { canvas->setCameraAngle(Canvas::Direction::Right); });
auto preview_and_controls = new QWidget();
auto preview_layout = new QVBoxLayout();
preview_layout->setContentsMargins(0, 0, 0, 0);
preview_layout->addWidget(canvas);
preview_layout->addWidget(toolbar);
preview_and_controls->setLayout(preview_layout);
console->setReadOnly(true);
console->setAcceptRichText(true);
v_splitter->setOrientation(Qt::Vertical);
v_splitter->addWidget(preview_and_controls);
v_splitter->addWidget(console);
v_splitter->setSizes({200, 200});
h_splitter->addWidget(code);
h_splitter->addWidget(v_splitter);
h_splitter->setSizes({200, 200});
QGridLayout *layout = new QGridLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(h_splitter);
setLayout(layout);
process.setProgram("extopenscad");
connect(&process,
QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[=](int exitCode, QProcess::ExitStatus exitStatus) {
log(process.readAllStandardOutput());
logError(process.readAllStandardError());
if (exitStatus == QProcess::NormalExit && exitCode == 0) {
load_stl(stl.fileName(), reload);
reload = true;
log("Rendering done.");
} else {
logError("Rendering failed.");
}
canvas->set_status("");
});
setFocusPolicy(Qt::StrongFocus);
setFocusProxy(code);
code->setFocus();
}
void Tab::log(const QString &str) const { console->append(str); }
void Tab::logError(const QString &str) const
{
console->setTextColor(Qt::red);
log(str);
console->setTextColor(Qt::black);
}
bool Tab::hasModifiedCode() const { return code->isModified(); }
bool Tab::hasSelectedCode() const { return code->hasSelectedText(); }
void Tab::newFile()
{
console->clear();
reload = false;
}
bool Tab::hasFile() const { return !curFile.isEmpty(); }
void Tab::setFileName(const QString &filename)
{
curFile = filename;
emit fileNameChanged(curFile);
}
const QString &Tab::fileName() const { return curFile; }
std::pair<bool, QString> Tab::writeFile(const QString &fileName) const
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly)) {
return std::make_pair(false, file.errorString());
}
QTextStream out(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
out << code->text();
QApplication::restoreOverrideCursor();
return std::make_pair(true, "");
}
void Tab::call_implicitcad(const QString &inputFile, const QString outputFile,
const float resolution, const QString &format)
{
if (process.state() == QProcess::ProcessState::Running) {
log("Renderer already running.");
return;
}
auto args = QStringList{inputFile, "-f", format, "-o", outputFile};
if (resolution > 0) {
args.append("-r");
args.append(QString::number(resolution));
}
qDebug() << args;
process.setArguments(args);
process.start();
process.waitForStarted();
}
static const QString err_bad_stl{
"<b>Error:</b><br>"
"This <code>.stl</code> file is invalid or corrupted.<br>"
"Please export it from the original source, verify, and retry."};
static const QString err_empty_mesh{
"<b>Error:</b><br>"
"This file is syntactically correct<br>but contains no triangles."};
static const QString err_missing_file{"<b>Error:</b><br>"
"The target file is missing.<br>"};
static const QString warn_confusing_stl{
"<b>Warning:</b><br>"
"This <code>.stl</code> file begins with <code>solid </code>but appears to "
"be a binary file.<br>"
"<code>fstl</code> loaded it, but other programs may be confused by this "
"file."};
void Tab::load_stl(const QString &fileName, const bool reload)
{
//canvas->set_status("Loading " + filename);
Loader* loader = new Loader(this, fileName, reload);
connect(loader, &Loader::got_mesh,
canvas, &Canvas::load_mesh);
//
// QMessageBox::critical(this, "Error",
connect(
loader, &Loader::error_bad_stl, this, [=] { logError(err_bad_stl); },
Qt::QueuedConnection);
connect(
loader, &Loader::error_empty_mesh, this,
[=] { logError(err_empty_mesh); }, Qt::QueuedConnection);
connect(
loader, &Loader::error_missing_file, this,
[=] { logError(err_missing_file); }, Qt::QueuedConnection);
connect(
loader, &Loader::warning_confusing_stl, this,
[=] { logError(warn_confusing_stl); }, Qt::QueuedConnection);
// TODO: maybe we can re-use the loader obj
connect(loader, &Loader::finished,
loader, &Loader::deleteLater);
//connect(loader, &Loader::finished,
// this, &Window::enable_open);
//connect(loader, &Loader::finished,
// canvas, &Canvas::clear_status);
loader->start();
}
bool Tab::save()
{
const auto ret = writeFile(curFile);
if (!ret.first) {
QMessageBox::warning(this, tr("Application"),
tr("Cannot write file %1:\n%2.")
.arg(curFile)
.arg(ret.second));
return false;
}
code->setModified(false);
{
QSettings settings("ImplicitCAD", "ExplicitCAD");
if (settings.value("autorender", false).toBool()) {
preview();
}
}
return true;
}
bool Tab::open(const QString& fileName) {
QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
QMessageBox::warning(this, tr("Application"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
QTextStream in(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
code->setText(in.readAll());
QApplication::restoreOverrideCursor();
code->setModified(false);
setFileName(fileName);
return true;
}
void Tab::preview(const float res) {
if (process.state() == QProcess::ProcessState::Running) {
log("Renderer already running.");
return;
}
if (!stl.isOpen()) {
// this actually creates the temporary filename
stl.open();
}
const QString tempfilename = QDir::tempPath() + "/explicitcadtemp.escad";
const auto ret = writeFile(tempfilename);
if (!ret.first) {
// TODO: Display user massage failure
return;
}
canvas->set_status("Rendering preview …");
call_implicitcad(tempfilename, stl.fileName());
}
void Tab::render(const QString &fileName, const float res)
{
// TODO save if 'curFile' has been modified or is empty …
call_implicitcad(curFile, fileName, res);
}
void Tab::cut() { code->cut(); }
void Tab::copy() { code->copy(); }
void Tab::paste() { code->paste(); }