Repository navigation
Expand file tree
/
Copy pathconstructar.cpp
More file actions
506 lines (404 loc) · 18.5 KB
/
Copy pathconstructar.cpp
File metadata and controls
506 lines (404 loc) · 18.5 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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include "constructar.h"
#include "ui_constructar.h"
//Class constructor
ConstructAR::ConstructAR(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ConstructAR)
{
//Setting the UI pointer, registering meta types in order to be able to pass them as arguments in signals and slots
ui->setupUi(this);
qRegisterMetaType< cv::Mat >("cv::Mat");
qRegisterMetaType< std::string >("std::string");
qRegisterMetaType< bool >("bool");
//Calling the init() function
init();
}
ConstructAR::~ConstructAR()
{
delete ui;
}
//INIT FUNCTION
void ConstructAR::init(){
//Pixmap object holding the picture of the program background
QPixmap bkgnd(":/Icons/background.jpg");
//QPalette objects used for setting different color properties of different widgets
QPalette mainWindowPalette, projectTitlePalette, infoLabelPalette;
//Loading the specified images as QImages
initialBG = QImage(":/Icons/initialBG.jpg");
loadingBG = QImage(":/Icons/loadingBG.jpg");
initialDB = QImage(":/Icons/initialDB.jpg");
//Set the background of the main window to the previously loaded image
mainWindowPalette.setBrush(QPalette::Window, bkgnd);
//Update the palettes of the main window to apply changes
this->setPalette(mainWindowPalette);
//Show window in full screen
QMainWindow::showMaximized();
//Modify the color of the QPalette properties of the specified palettes
//Window - holds the background color
//WindowText - holds the color of the text written in the widget
projectTitlePalette.setColor(QPalette::Window, QColor(Qt::white));
infoLabelPalette.setColor(QPalette::Window, QColor(Qt::gray));
infoLabelPalette.setColor(QPalette::WindowText, QColor(Qt::white));
//Update the specified palettes and set welcoming text and tip in the specified widgets
ui->projectTitle->setPalette(projectTitlePalette);
ui->infoLabel->setPalette(infoLabelPalette);
ui->projectTitle->setText(" Welcome to ConstructAR!");
ui->infoLabel->setText(" Tip: You can choose to either build a mosaic by clicking on New file, or rebuild a mosaic by clicking on Open file.");
//Set the picture of the frame label to the initial image
ui->frameLabel->setPixmap(QPixmap::fromImage(initialBG));
//Hide extra widgets, not needed in the welcoming screen and disable share and save files, because no mosaic is built yet
ui->dbLabel->hide();
ui->ebLabel->hide();
ui->frLabel->hide();
ui->finalResultLabel->hide();
ui->expectedBlockLabel->hide();
ui->detectedBlockLabel->hide();
ui->progressBar->hide();
ui->progressLabel->hide();
ui->shareFile->setDisabled(true);
ui->saveFile->setDisabled(true);
ui->reset->setDisabled(true);
//Hide the mosaic description widgets
toggleMosaicDescription(false);
//update the creator thread pointer to point to a newly defined QThread object
creatorThread = new QThread;
//Update theManual pointer
theManual = new TheManual();
//Set theManual object to be run by creatorThread
theManual->moveToThread(creatorThread);
//Connect all the signals and slots
//First argument - the object the signal belongs to
//Second argument - the signal itself
//Third argument - the object, the slot belongs to
//fourth argument - the slot itself
connect(this, SIGNAL(startTheManual(std::string, std::string, bool)), theManual, SLOT(startTheManual(std::string, std::string, bool)));
connect(theManual, SIGNAL(blockDetected(bool, bool, bool)), this, SLOT(blockDetected(bool, bool, bool)));
connect(theManual, SIGNAL(on_mosaic_loaded(int)), this, SLOT(on_mosaic_loaded(int)));
connect(theManual, SIGNAL(on_step_changed(int)), this, SLOT(on_step_changed(int)));
connect(theManual, SIGNAL(on_description_done()), this, SLOT(on_description_done()));
connect(theManual, SIGNAL(frameFinished(cv::Mat)), this, SLOT(updateFrameLabel(cv::Mat)));
connect(theManual, SIGNAL(createdExpectedBlockPath(QString, QString, QString)), this, SLOT(updateExpectedBlockLabel(QString, QString, QString)));
connect(theManual, SIGNAL(createdDetectedBlockPath(QString, bool)), this, SLOT(updateDetectedBlockLabel(QString, bool)));
connect(creatorThread, SIGNAL(started()), theManual, SLOT(runManual()));
connect(theManual, SIGNAL(finished()), creatorThread, SLOT(quit()));
connect(theManual, SIGNAL(finished()), theManual, SLOT(deleteLater()));
connect(creatorThread, SIGNAL(finished()), creatorThread, SLOT(deleteLater()));
//Start the thread which holds theManual object
creatorThread->start();
}
//WHEN SAVE BUTTON IS CLICKED
void ConstructAR::on_newFile_clicked()
{
//Set the path for the save file by prompting the user to specify a location from a dialog
QString fileSavePath = QFileDialog::getSaveFileName(this, tr("Choose save destination"), "", "Lego Mosaic(*.xml)");
//If the newly created path is not empty
if(!fileSavePath.isEmpty()){
//Construct the path for the .xml and .jpg paths and emit startTheManual signal
QString mosaicPictureSavePath;
QFileInfo fileInfo(fileSavePath);
mosaicName = fileInfo.baseName();
mosaicPictureSavePath = fileInfo.path() + "/" + fileInfo.completeBaseName() + ".jpg";
std::string stdMosaicPictureSavePath = mosaicPictureSavePath.toStdString();
std::string stdFilePath = fileSavePath.toStdString();
emit startTheManual(stdFilePath, stdMosaicPictureSavePath, true);
showCreateUI();
}
}
//WHEN OPEN FILE BUTTON IS CLICKED
void ConstructAR::on_openFile_clicked()
{
//Prompt the user to select a file for opening via a dialog and get its path
QString filePath = QFileDialog::getOpenFileName(
this,
tr("Open Mosaic"),
"",
"Lego Mosaic(*.xml)"
);
if(!filePath.isEmpty()){
//Store the loading picture and the initial picture for the detectedBlock label in QImages
QImage loadingBG(":/Icons/loadingBG.jpg"), initialDB(":/Icons/initialDB.jpg");
//Get the name of the file from its path and store it in mosaicName
QFileInfo fileInfo(filePath);
QString mosaicPictureLoadPath;
mosaicName = fileInfo.baseName();
//Construct the path for the .xml and .jpg files and store them in std::string variables
mosaicPictureLoadPath = fileInfo.path() + "/" + fileInfo.completeBaseName() + ".jpg";
std::string stdMosaicPictureLoadPath = mosaicPictureLoadPath.toStdString();
std::string stdFilePath = filePath.toStdString();
//Load the picture of the mosaic from its path in its QImage
loadedMosaic = QImage(mosaicPictureLoadPath);
//Emit signal
emit startTheManual(stdFilePath, stdMosaicPictureLoadPath, false);
showRecreateUI();
}
}
//SLOT FUNCTION EXECUTED ON EACH FRAME CAPTURE
void ConstructAR::updateFrameLabel(cv::Mat frame){
//Take the frame, convert it from BGR to RGB and store it in rgbCamFrame object
cv::Mat rgbCamFrame;
cv::cvtColor(frame, rgbCamFrame, cv::COLOR_BGR2RGB);
//Construct a QImage object from the Mat object in order to be able to apply it to a Qt widget (label)
QImage qCamFrame = QImage(rgbCamFrame.data, rgbCamFrame.cols, rgbCamFrame.rows, rgbCamFrame.step, QImage::Format_RGB888);
//Update the label with the current frame
ui->frameLabel->setPixmap(QPixmap::fromImage(qCamFrame));
}
//SLOT FUNCTION UPDATING THE EXPECTED BLOCK PICTURE AND TEXT
void ConstructAR::updateExpectedBlockLabel(QString path, QString blockType, QString blockColor){
//Load the picture from its path to a QImage
QImage expectedBlock = QImage(path);
//Update the label which holds the picture with the new picture
ui->expectedBlockLabel->setPixmap(QPixmap::fromImage(expectedBlock));
//update the text of the label used to explain what is the picture actually showing
ui->ebLabel->setText("Next step: " + blockType + ", " + blockColor);
}
//SLOT FUNCTION UPDATING THE DETECTED BLOCK PICTURE AND TEXT
void ConstructAR::updateDetectedBlockLabel(QString _path, bool _isBlockRemoved){
dbPath = _path;
isBlockRemoved = _isBlockRemoved;
QTimer::singleShot(1000, this, SLOT(refreshDetectedBlockLabel()));
}
void ConstructAR::refreshDetectedBlockLabel(){
//Load picture and update label
QImage detectedBlock = QImage(dbPath);
ui->detectedBlockLabel->setPixmap(QPixmap::fromImage(detectedBlock));
//Update text above picture label to indicate if block is placed or removed
if(isBlockRemoved){
ui->dbLabel->setText("Block removed: ");
}
else{
ui->dbLabel->setText("Block placed: ");
}
}
//FUNCTION HANDLING KEYPRESS EVENTS, USED FOR DEBUGGING
void ConstructAR::keyPressEvent(QKeyEvent *event)
{
//Check which key is pressed and update the booleans in theManual object accordingly
if(event->key() == Qt::Key_S)
{
emit startTheManual();
}
else if(event->key() == Qt::Key_W){
theManual->willSaveMosaic = true;
}
else if(event->key() == Qt::Key_R){
theManual->willLoadMosaic = true;
}
else if(event->key() == Qt::Key_C){
theManual->willClearVector = true;
}
else if(event->key() == Qt::Key_T){
theManual->willSortBlocks = true;
}
else if(event->key() == Qt::Key_D){
emit startTheManual("LegoModel.xml", "LegoModel.jpg", false);
}
else if(event->key() == Qt::Key_Q){
theManual->willExit = true;
this->destroy();
QApplication::quit();
return;
}
}
//WHEN SAVE FILE BUTTON IS CLICKED
void ConstructAR::on_saveFile_clicked()
{
//The only action required is to update the boolean in theManual, the rest is done by theManual object itself
theManual->willSaveMosaic = true;
}
//WHEN RESET BUTTON IS CLICKED
void ConstructAR::on_reset_clicked()
{
//Update the content of the infoLabel based on whether user is creating or recreating a mosaic
if(!theManual->isCreating){
ui->infoLabel->setText(" Tip: Place a block with a matching type and color in the specified position to start rebuilding.");
}
else{
ui->infoLabel->setText(" Tip: Place a block on the plate to start building.");
}
if(theManual->isCreating){
//Clear widget content
ui->blocksList->clear();
ui->totalNumberSteps->clear();
}
//Update boolean
theManual->willClearVector = true;
}
//UPDATES UI FOR CREATING A MOSAIC
void ConstructAR::showCreateUI(){
//Update text content of labels, enable required buttons, show required widgets and hide widgets that are not needed
ui->projectTitle->setText(" Creating project | " + mosaicName);
ui->infoLabel->setText(" Tip: Place a block on the plate to start building.");
ui->detectedBlockLabel->setGeometry(createMdetectedBlockLabelGeometry);
ui->dbLabel->setGeometry(createMdbLabelGeometry);
ui->frameLabel->setPixmap(QPixmap::fromImage(loadingBG));
ui->detectedBlockLabel->setPixmap(QPixmap::fromImage(initialDB));
ui->ebLabel->hide();
ui->expectedBlockLabel->hide();
ui->frLabel->hide();
ui->finalResultLabel->hide();
ui->detectedBlockLabel->show();
ui->dbLabel->show();
ui->saveFile->setEnabled(true);
ui->reset->setEnabled(true);
ui->shareFile->setEnabled(true);
ui->blocksList->clear();
ui->totalNumberSteps->clear();
}
//UPDATES UI FOR RECREATING A MOSAIC
void ConstructAR::showRecreateUI(){
//Update text content of labels, enable required buttons, show required widgets and hide widgets that are not needed
ui->projectTitle->setText(" Recreating project | " + mosaicName);
ui->infoLabel->setText(" Tip: Place a block with a matching type and color in the specified position to start rebuilding.");
ui->finalResultLabel->setPixmap(QPixmap::fromImage(loadedMosaic));
ui->saveFile->setDisabled(true);
ui->reset->setEnabled(true);
ui->dbLabel->setGeometry(recreateMdbLabelGeometry);
ui->detectedBlockLabel->setGeometry(recreateMdetectedBlockLabelGeometry);
ui->frameLabel->setPixmap(QPixmap::fromImage(loadingBG));
ui->detectedBlockLabel->setPixmap(QPixmap::fromImage(initialDB));
ui->progressBar->show();
ui->progressLabel->show();
ui->frLabel->show();
ui->finalResultLabel->show();
ui->detectedBlockLabel->show();
ui->dbLabel->show();
ui->expectedBlockLabel->show();
ui->ebLabel->show();
ui->blocksList->clear();
ui->totalNumberSteps->clear();
}
//WHEN SHARE FILE IS CLICKED
void ConstructAR::on_shareFile_clicked()
{
//Open default mail app with updated subject field and example email recipient
QDesktopServices::openUrl(QUrl::fromEncoded("mailto:yourrecipient@mail.com?subject=Try%20my%20Lego%20Mosaic"));
}
//WHEN EXIT BUTTON IS CLICKED
void ConstructAR::on_exit_clicked()
{
//Update boolean
theManual->willExit = true;
//Destroy the UI window
this->destroy();
//Quit complete application
QApplication::quit();
return;
}
//SLOT FUNCTION EXECUTED WHEN BLOCK IS DETECTED
void ConstructAR::blockDetected(bool isCorrect, bool isRemoved, bool isCreating){
//Update infoLabel content based on whether block is correct, incorrect, placed or removed
if(isCreating){
if(!isRemoved){
ui->infoLabel->setText(" Tip: Block placed, build your mosaic and save it to see details.");
}
else{
ui->infoLabel->setText(" Tip: You removed a block, you can continue building.");
}
}
else{
if(isRemoved){
ui->infoLabel->setText(" Tip: You removed a block, you can continue rebuilding.");
}
else if(theManual->isMosaicRecreated){
ui->infoLabel->setText(" Tip: Mosaic successfully rebuilt.");
}
else{
if (isCorrect) {
ui->infoLabel->setText(" Tip: Correct block placed. You can proceed to the next step.");
}
else{
ui->infoLabel->setText(" Tip: Incorrect block placed. Please remove it and place a block with matching type and color in the specified position.");
}
}
}
}
//SLOT FUNCTION EXECUTED WHEN USER MAKES A STEP
void ConstructAR::on_step_changed(int step){
//Update the value of the progress bar with the current step
QString progressString;
ui->progressBar->setValue(step);
//Construct the text for the progress bar label
if(!theManual->isMosaicRecreated){
if(step > 9){
progressString = "Step " + QString::number(step+1) + QString::fromStdString(" of ") + QString::number(ui->progressBar->maximum());
}
else{
progressString = " Step " + QString::number(step+1) + QString::fromStdString(" of ") + QString::number(ui->progressBar->maximum());
}
//Update label text
ui->progressLabel->setText(progressString);
}
}
//SLOT FUNCTION
void ConstructAR::on_mosaic_loaded(int maximum){
//Set maximum value for progress bar (total number of steps)
ui->progressBar->setMaximum(maximum);
//Update label text and show details for mosaic
ui->progressLabel->setText("Step 1 of " + QString::number(maximum));
toggleMosaicDescription(true);
}
//SLOT FUNCTION WHICH EXECUTES AFTER A MOSAIC IS SAVED AND DESCRIPTION IS CREATED
void ConstructAR::on_description_done(){
//Show the details of the mosaic to the user
toggleMosaicDescription(true, theManual->isCreating);
}
//The function which handles the display of the mosaic description
void ConstructAR::toggleMosaicDescription(bool show, bool isCreating){
//If it should hide the description or show it
if(!show){
ui->blocksList->hide();
ui->blocksUsedNeeded->hide();
ui->detailsFrame->hide();
ui->detailsLabel->hide();
ui->totalNumberSteps->hide();
ui->totalNumberStepsLabel->hide();
}
else{
QString blockListString;
std::vector<LegoBlock> theDescription = theManual->theMosaicDescription;
//Compile the description text from the vector of LegoBlock objects
for(size_t i=0;i<theDescription.size(); i++){
LegoBlock currentBlock = theDescription[i];
int occurrences = currentBlock.occurrences;
QString listLine;
if( i == theDescription.size() - 1 ){
listLine = "(" + QString::number(occurrences) + "x) " + QString::fromStdString(currentBlock.type) + ", " + QString::fromStdString(currentBlock.color) + ".";
}
else if(i % 2 == 1){
listLine = "(" + QString::number(occurrences) + "x) " + QString::fromStdString(currentBlock.type) + ", " + QString::fromStdString(currentBlock.color) + ";\n";
}
else{
listLine = "(" + QString::number(occurrences) + "x) " + QString::fromStdString(currentBlock.type) + ", " + QString::fromStdString(currentBlock.color) + "; ";
}
blockListString += listLine;
}
//Update widgets based on whether the user is creating or recreating mosaic
if(isCreating){
ui->blocksList->setGeometry(createMblockListGeometry);
ui->blocksUsedNeeded->setGeometry(createMblocksUsedNeededGeometry);
ui->detailsFrame->setGeometry(createMdetailsFrameGeometry);
ui->detailsLabel->setGeometry(createMdetailsLabelGeometry);
ui->totalNumberSteps->setGeometry(createMtotalNumberStepsGeometry);
ui->totalNumberStepsLabel->setGeometry(createMtotalNumberStepsLabelGeometry);
ui->blocksUsedNeeded->setText("Blocks used: ");
ui->totalNumberSteps->setText(QString::number(theManual->theMosaic.size()) + " blocks");
}
else{
ui->blocksList->setGeometry(recreateMblockListGeometry);
ui->blocksUsedNeeded->setGeometry(recreateMblocksUsedNeededGeometry);
ui->detailsFrame->setGeometry(recreateMdetailsFrameGeometry);
ui->detailsLabel->setGeometry(recreateMdetailsLabelGeometry);
ui->totalNumberSteps->setGeometry(recreateMtotalNumberStepsGeometry);
ui->totalNumberStepsLabel->setGeometry(recreateMtotalNumberStepsLabelGeometry);
ui->blocksUsedNeeded->setText("Blocks needed: ");
ui->totalNumberSteps->setText(QString::number(theManual->theLoadedMosaic.size()) + " steps");
}
ui->blocksList->setText(blockListString);
ui->blocksList->show();
ui->blocksUsedNeeded->show();
ui->detailsFrame->show();
ui->detailsLabel->show();
ui->totalNumberSteps->show();
ui->totalNumberStepsLabel->show();
}
}