-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmainwindow.cpp
461 lines (368 loc) · 12.8 KB
/
mainwindow.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QFileDialog>
#include <QSettings>
#include <QMessageBox>
#include <iostream>
#include <QDir>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gcodeview.h"
#include "gcodeviewapplication.h"
//#include "slicingdialog.h"
#include "mgl/miracle.h"
#include "mgl/configuration.h"
using namespace mgl;
using namespace std;
class Progress : public ProgressBar
{
QLabel &taskLabel; QProgressBar& progress;
public:
Progress(QLabel *taskLabelp, QProgressBar* progressp)
:taskLabel(*taskLabelp), progress(*progressp)
{
}
void onTick(const char* taskName, unsigned int count, unsigned int tick)
{
if(tick==0)
{
taskLabel.setText(taskName);
progress.setMinimum(0);
progress.setMaximum(count);
}
progress.setValue(tick+1);
qApp->processEvents();
// cout << taskName << " tick: " << tick << "/" << count << endl;
}
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Build the 'recent files' menu
for (int i = 0; i < MaxRecentFiles; ++i) {
recentFileActs[i] = new QAction(this);
recentFileActs[i]->setVisible(false);
connect(recentFileActs[i], SIGNAL(triggered()),
this, SLOT(openRecentFile()));
}
QMenu* recentFilesMenu = ui->menuBar->findChild<QMenu *>("menuRecent_Files");
if (recentFilesMenu != NULL) {
for (int i = 0; i < MaxRecentFiles; ++i)
recentFilesMenu->addAction(recentFileActs[i]);
}
// Reload the recent file menu
updateRecentFileActions();
// Reload the 'windows' menu
updateWindowMenu();
ui->progressBar->setValue(0);
QString config = QDir::currentPath()+"/miracle.config";
QFile f(config);
if(f.exists(config))
{
ui->lineEditConfigFile->setText(config);
}
string title = "Miracle-Grue ";
title += mgl::getMiracleGrueVersionStr().c_str();
this->setWindowTitle(title.c_str());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionLoad_GCode_triggered()
{
QString fileName;
{
fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"",//"/home",
tr("3D Models (*.stl);;GCode (*.gcode)") // tr("GCode (*.gcode)")
);
}
if(fileName.size())
{
GCodeViewApplication::LoadFile(fileName);
setCurrentFile(fileName);
}
}
// TODO: Move this to the app class
void MainWindow::setCurrentFile(const QString &fileName)
{
curFile = fileName;
setWindowFilePath(curFile);
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
files.removeAll(fileName);
files.prepend(fileName);
while (files.size() > MaxRecentFiles)
files.removeLast();
settings.setValue("recentFileList", files);
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
if (mainWin)
mainWin->updateRecentFileActions();
}
}
void MainWindow::updateRecentFileActions()
{
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
for (int i = 0; i < numRecentFiles; ++i) {
QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
recentFileActs[i]->setText(text);
recentFileActs[i]->setData(files[i]);
recentFileActs[i]->setVisible(true);
}
for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
recentFileActs[j]->setVisible(false);
// TODO: Disable 'recent files' menu if there aren't any.
// separatorAct->setVisible(numRecentFiles > 0);
}
void MainWindow::updateWindowMenu()
{
QMenu* windowMenu = ui->menuBar->findChild<QMenu *>("menuWindow");
if (windowMenu) {
// TODO: Does this cause a memory leak?
windowMenu->clear();
// Re-add the windows
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
if (mainWin) {
QAction* action = new QAction(this);
action->setText(mainWin->windowTitle());
windowMenu->addAction(action);
// windowMenu->addAction(recentFileActs[i]);
}
}
}
}
QString MainWindow::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
void MainWindow::openRecentFile()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action)
GCodeViewApplication::LoadFile(action->data().toString());
}
bool MainWindow::hasFile() {
return ui->graphicsView->hasModel();
}
void MainWindow::on_LayerHeight_sliderMoved(int position)
{
// TODO: where /should/ this signal go?
ui->graphicsView->setMaximumVisibleLayer(position);
ui->LayerMin->setMaximum(ui->graphicsView->model.getMapSize() );
// display the current layer height.
}
void MainWindow::on_LayerMin_valueChanged(int value)
{
ui->graphicsView->setMinimumVisibleLayer(value);
}
void MainWindow::on_zoomIn_clicked()
{
ui->graphicsView->zoom(1.1);
}
void MainWindow::on_zoomOut_clicked()
{
ui->graphicsView->zoom(.9);
}
void MainWindow::on_actionClose_triggered()
{
// Close this window.
this->close();
// TODO: Can the application get a signal when this happens, instead of sending it explicitly here?
}
void MainWindow::on_panLeft_clicked()
{
ui->graphicsView->panX(1.0);
}
void MainWindow::on_panRight_clicked()
{
ui->graphicsView->panX(-1.0);
}
void MainWindow::on_panUp_clicked()
{
ui->graphicsView->panY(-1.0);
}
void MainWindow::on_panDown_clicked()
{
ui->graphicsView->panY(1.0);
}
void MainWindow::on_buttonConfigBrowse_clicked()
{
QString fileName;
fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Miracle-Grue configurations (*.config)") );
ui->lineEditConfigFile->setText(fileName);
}
//void MainWindow::on_button3dModelBrowse_clicked()
//{
// QString fileName;
// string modelFile = ui->lineEdit3dModelFile->text().toStdString();
// MyComputer computer;
// string dir = computer.fileSystem.ExtractDirectory(modelFile.c_str());
// fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(dir.c_str()), tr("3D models (*.stl)") );
// ui->lineEdit3dModelFile->setText(fileName);
// sliceModelAndCreateToolPaths(fileName.toAscii());
//}
void MainWindow::sliceModelAndCreateToolPaths(const char* modelpath)
{
try
{
cout << "Output file: ";
MyComputer computer;
string configFileName = ui->lineEditConfigFile->text().toStdString();
string modelFile = modelpath; //ui->lineEdit3dModelFile->text().toStdString();
string gcodeFile = computer.fileSystem.ChangeExtension(computer.fileSystem.ExtractFilename(modelFile.c_str()).c_str(), ".gcode" );
cout << gcodeFile << endl;
string scadFile = computer.fileSystem.ChangeExtension(computer.fileSystem.ExtractFilename(modelFile.c_str()).c_str(), ".scad" );
cout << scadFile << endl;
//configFileName += "/miracle.config";
mgl::Configuration config;
cout << "loading config: " << configFileName << endl;
config.readFromFile(configFileName.c_str());
GCoderConfig gcoderCfg;
loadGCoderConfigFromFile(config, gcoderCfg);
SlicerConfig slicerCfg;
loadSlicerConfigFromFile(config, slicerCfg);
RegionerConfig regionerCfg;
loadRegionerConfigFromFile(config, regionerCfg);
PatherConfig patherCfg;
loadPatherConfigFromFile(config, patherCfg);
ExtruderConfig extruderCfg;
loadExtruderConfigFromFile(config, extruderCfg);
int shells = ui->spinBoxShells->value();
double density = atof(ui->lineEditDensity->text().toAscii());
regionerCfg.infillDensity = density;
regionerCfg.nbOfShells = shells;
cout << "Shells " << regionerCfg.nbOfShells << endl;
cout << "Density " << regionerCfg.infillDensity << endl;
Progress progress(ui->label_task, ui->progressBar);
Tomograph tomograph; // = ui->graphicsView->model.tomograph;
LayerLoops layerloops;
RegionList regions; // = ui->graphicsView->model.regions;
std::vector<mgl::SliceData> slices;
LayerPaths layerpaths;
Meshy mesh;
mesh.readStlFile(modelFile.c_str());
mesh.alignToPlate();
Limits limits = mesh.readLimits();
Grid grid;
Segmenter segmenter(slicerCfg.firstLayerZ, slicerCfg.layerH);
segmenter.tablaturize(mesh);
Slicer slicer(slicerCfg, &progress);
slicer.generateLoops(segmenter, layerloops);
cout << "Slicer done" << endl;
Regioner regioner(regionerCfg, &progress);
regioner.generateSkeleton(layerloops, layerloops.layerMeasure, regions,
limits, grid);
cout << "Regioner done" << endl;
Pather pather(patherCfg, &progress);
pather.generatePaths(extruderCfg, regions,
layerloops.layerMeasure, grid, layerpaths);
cout << "Pather done" << endl;
ui->graphicsView->loadSliceData(layerloops, regions, grid, layerpaths);
ui->LayerHeight->setMaximum(ui->graphicsView->model.getMapSize());
ui->LayerMin->setMaximum(ui->graphicsView->model.getMapSize());
ui->LayerMin->setValue(0);
ui->LayerHeight->setValue(0);
// things we need to remember
ui->graphicsView->model.layerMeasure = tomograph.layerMeasure;
ui->graphicsView->model.slices = slices;
ui->graphicsView->model.slicePaths = layerpaths;
ui->graphicsView->model.gcoderCfg = gcoderCfg;
ui->graphicsView->model.modelFile = modelFile;
}
catch(mgl::Exception &mixup)
{
cout << "ERROR: " << mixup.error << endl;
QMessageBox &box = *new QMessageBox();
box.setText(mixup.error.c_str());
box.show();
}
catch(...)
{
cout << "ERROR" << endl;
}
}
void MainWindow::loadFile(const QString &fileName)
{
setWindowTitle(strippedName(fileName));
QAction* closeMenu = ui->menuBar->findChild<QAction *>("actionClose");
if (closeMenu) {
closeMenu->setText("Close \"" + fileName + "\"");
}
std::string x(fileName.toAscii());
size_t dot_index = x.find_last_of('.');
size_t x_size = x.size();
std::string ex = x.substr(dot_index, x_size-1);
if( (ex == ".stl") || (ex == ".STL") )
{
cout << "STL!!!" << ex << endl;
sliceModelAndCreateToolPaths(fileName.toAscii());
}
else
{
ui->graphicsView->loadModel(fileName);
ui->LayerHeight->setMaximum(ui->graphicsView->model.getMapSize() );
ui->LayerMin->setMaximum(ui->graphicsView->model.getMapSize() );
ui->LayerMin->setValue(0 );
ui->LayerHeight->setValue(0 );
}
}
void MainWindow::on_actionSave_gcode_triggered()
{
Progress progress(ui->label_task, ui->progressBar);
cout << "hello on_actionSave_gcode_triggered save menu!" << endl;
QString filename = QFileDialog::getSaveFileName(this, tr("Export GCode"), QDir::currentPath(), tr("GCode File (*.gcode)"));
cout << "SAVE into " << filename.toStdString() << endl;
ui->graphicsView->exportModel(filename, &progress);
}
void MainWindow::on_checkBoxSurfs_toggled(bool checked)
{
cout << "Toggle surfs" << endl;
ui->graphicsView->toggleSurfs(checked);
}
void MainWindow::on_checkBoxRoofs_toggled(bool checked)
{
cout << "Toggle roofs " <<checked << endl;
ui->graphicsView->toggleRoofs(checked);
}
void MainWindow::on_checkBoxFloors_toggled(bool checked)
{
cout << "Toggle floors " <<checked << endl;
ui->graphicsView->toggleFLoors(checked);
}
void MainWindow::on_checkBoxLoops_toggled(bool checked)
{
cout << "Toggle loops " <<checked << endl;
ui->graphicsView->toggleLoops(checked);
}
void MainWindow::on_checkBoxInfills_toggled(bool checked)
{
cout << "Toggle infills " <<checked << endl;
ui->graphicsView->toggleInfills(checked);
}
void MainWindow::on_actionOpen_3D_model_triggered()
{
cout << "OPEN!" << endl;
QString fileName;
{
fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"",//"/home",
tr("GCode (*.stl)") // tr("3D Models (*.stl);;GCode (*.gcode)")
);
}
if(fileName.size())
{
GCodeViewApplication::LoadFile(fileName);
setCurrentFile(fileName);
}
}
void MainWindow::on_pushButtonSlice_clicked()
{
sliceModelAndCreateToolPaths(ui->graphicsView->model.modelFile.c_str());
}