mainguiwindow.cpp 23.6 KB
Newer Older
1
#include "mainguiwindow.h"
2
#include "ui_mainguiwindow.h"
3
#include "crazyFlyZoneTab.h"
4
5
#include "myGraphicsScene.h"
#include "myGraphicsView.h"
6
7
8
9
10

#include <QObject>
#include <QDoubleSpinBox>
#include <QTextEdit>
#include <QString>
11
#include <QMetaType>
12
#include <QDir>
13
#include <regex>
14

15
16
#ifdef CATKIN_MAKE
#include "d_fall_pps/UnlabeledMarker.h"
17
#include "d_fall_pps/CMRead.h"
18
#include "d_fall_pps/CrazyflieEntry.h"
19
20
#endif

21
#include <string>
22

23
#define N_MAX_CRAZYFLIES           20 // protection number
24

roangel's avatar
roangel committed
25
#ifdef CATKIN_MAKE
26
using namespace d_fall_pps;
roangel's avatar
roangel committed
27
#endif
28

29
30
31
32
MainGUIWindow::MainGUIWindow(int argc, char **argv, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainGUIWindow)
{
33
34
35
    #ifdef CATKIN_MAKE
    _rosNodeThread = new rosNodeThread(argc, argv, "/ViconDataPublisher/ViconData");
    #endif
36
37
38
    ui->setupUi(this);
    _init();
}
39

40

41
42
43
44
45
MainGUIWindow::~MainGUIWindow()
{
    delete ui;
}

46
47
48
49
50
51
52
53
54
55
56
57
58
59
int MainGUIWindow::getTabIndexFromName(QString name)
{
    int found_name = -1;
    for(int i = 0; i < ui->tabWidget->count(); i++)
    {
        qDebug("name: %s", name.toStdString().c_str());
        qDebug("tabText: %s", ui->tabWidget->tabText(i).toStdString().c_str());
        if(name == ui->tabWidget->tabText(i))
        {
            found_name = i;
        }
    }
    return found_name;
}
60

roangel's avatar
roangel committed
61
void MainGUIWindow::doNumCrazyFlyZonesChanged(int n)
62
{
roangel's avatar
roangel committed
63
    // tabs number management, maybe do it in a different way so we dont have to remove and add everything?
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    // first check if size of tabs is greater than size of vector or viceversa. Have we removed or added a zone?
    qDebug("tabWidgetCount : %d", ui->tabWidget->count());
    if(ui->tabWidget->count() > scene->crazyfly_zones.size())
    {
        // we removed one crazyfly_zone, n means index of the one we removed. Look for that index tab and remove it
        QString qstr = "CrazyFly ";
        qstr.append(QString::number(n+1));
        if(scene->crazyfly_zones.size() == 0)
        {
            ui->tabWidget->clear();
        }
        int found_index = getTabIndexFromName(qstr);
        if(found_index != -1)
        {
            ui->tabWidget->removeTab(found_index);
        }

        //  now unlink it from table also:
roangel's avatar
roangel committed
82
        #ifdef CATKIN_MAKE
83
84
85
86
        if(cf_linker->isCFZoneLinked(n))
        {
            cf_linker->unlink_cf_zone(n);
        }
roangel's avatar
roangel committed
87
        #endif
88
89
    }
    else if(ui->tabWidget->count() < scene->crazyfly_zones.size())
90
    {
91
        // we added one crazyfly_zone, n means index of the new one. New tab will be labeld index + 1
92
        QString qstr = "CrazyFly ";
93
94
95
        qstr.append(QString::number(n+1));
        crazyFlyZoneTab* widget = new crazyFlyZoneTab(n);
        ui->tabWidget->insertTab(n, widget, qstr);
96
97
        connect(widget, SIGNAL(centerButtonClickedSignal(int)), this, SLOT(centerViewIndex(int)));
    }
98
99
100
101
102
103
104
105
    // for (int i = 0; i < n; i++)
    // {
    //     QString qstr = "CrazyFly ";
    //     qstr.append(QString::number(i+1));
    //     crazyFlyZoneTab* widget = new crazyFlyZoneTab(i);
    //     ui->tabWidget->addTab(widget, qstr);
    //     connect(widget, SIGNAL(centerButtonClickedSignal(int)), this, SLOT(centerViewIndex(int)));
    // }
roangel's avatar
roangel committed
106

107
    updateComboBoxesCFZones();
108
109
110
111
}

void MainGUIWindow::_init()
{
112
113
114
115
116
117
118
119
    // initialize checkboxes, spinboxes,....
    ui->scaleSpinBox->setRange(0.1, 100);
    ui->scaleSpinBox->setSingleStep(0.1);
    ui->scaleSpinBox->setValue(1);

    ui->checkBox_vicon_crazyflies->setChecked(false);
    ui->scaleSpinBox->setEnabled(false);

120

121
    ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
122
123
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
124

125
126
127
128
129
    // error messages
    // ui->err_message_cf->hide();
    // ui->err_message_cf_zone->hide();
    // ui->err_message_student_id->hide();

roangel's avatar
roangel committed
130
131
132
   ui->err_message_cf->setStyleSheet("QLabel { color : red; }");
   ui->err_message_cf_zone->setStyleSheet("QLabel { color : red; }");
   ui->err_message_student_id->setStyleSheet("QLabel { color : red; }");
133

roangel's avatar
roangel committed
134
135
136
   ui->err_message_cf->clear();
   ui->err_message_cf_zone->clear();
   ui->err_message_student_id->clear();
137

138
139
    // initialize table_links
    ui->table_links->setColumnCount(3);
140

141
142
143
144
145
    QFont fnt;
    fnt.setPointSize(7);
    ui->table_links->horizontalHeader()->setFont(fnt);

    ui->table_links->horizontalHeader()->setDefaultSectionSize(90);
146
    ui->table_links->verticalHeader()->setDefaultSectionSize(20);
147
148
149
150
151
152
153
154
155
156
157

    const int rowCount = ui->table_links->rowCount();
    const int columnCount = ui->table_links->columnCount();
    for(int i = 0; i < rowCount; ++i)
    {
    	for(int j = 0; j < columnCount; ++j)
        {
    		QTableWidgetItem* selectedItem = ui->table_links->item(i, j);
    		selectedItem->setFont(fnt);
    	}
    }
roangel's avatar
roangel committed
158
    ui->table_links->setSelectionBehavior(QAbstractItemView::SelectRows);
159
160
161
    QStringList horizontal_header;
    horizontal_header << "Student ID" << "CrazyFly" << "CrazyFly Zone";
    ui->table_links->setHorizontalHeaderLabels(horizontal_header);
162

163
    // scene
164
    scene = new myGraphicsScene(ui->frame_drawing);
165
    scene->setSceneRect(-100 * FROM_METERS_TO_UNITS, -100 * FROM_METERS_TO_UNITS, 200 * FROM_METERS_TO_UNITS, 200 * FROM_METERS_TO_UNITS);
166

167
    ui->graphicsView->setScene(scene);
168

169
    // after scene is created, create CFlinker
170
    #ifdef CATKIN_MAKE
171
    cf_linker = new CFLinker(ui, &crazyflies_vector, &scene->crazyfly_zones);
172
    #endif
173
    // connections
174
    QObject::connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(doTabClosed(int)));
roangel's avatar
roangel committed
175
    QObject::connect(scene, SIGNAL(numCrazyFlyZonesChanged(int)), this, SLOT(doNumCrazyFlyZonesChanged(int)));
176
    QObject::connect(scene, SIGNAL(crazyFlyZoneSelected(int)), this, SLOT(setTabIndex(int)));
177
178
    QObject::connect(scene, SIGNAL(modeChanged(int)), this, SLOT(transitionToMode(int)));
    QObject::connect(scene, SIGNAL(numTablePiecesChanged(int)), this, SLOT(handleTablePiecesNumChanged(int)));
roangel's avatar
roangel committed
179
180

    ui->checkBox_vicon_highlight_markers->setEnabled(false);
181

roangel's avatar
roangel committed
182
    #ifdef CATKIN_MAKE
183
    _rosNodeThread->init();
184
    qRegisterMetaType<ptrToMessage>("ptrToMessage");
185
    QObject::connect(_rosNodeThread, SIGNAL(newViconData(const ptrToMessage&)), this, SLOT(updateNewViconData(const ptrToMessage&)));
186
    QObject::connect(cf_linker, SIGNAL(updateComboBoxes()), this, SLOT(updateComboBoxes()));
roangel's avatar
roangel committed
187
    #endif
188
}
189

190
191
192
void MainGUIWindow::doTabClosed(int tab_index)
{
    QString name = ui->tabWidget->tabText(tab_index);
roangel's avatar
roangel committed
193
    #ifdef CATKIN_MAKE
roangel's avatar
roangel committed
194
195
    int cf_zone_index = cf_linker->getCFZoneIndexFromName(name);
    scene->removeCrazyFlyZone(cf_zone_index);
roangel's avatar
roangel committed
196
    #endif
197
198
199
200
201
202
203
204
205
}

void MainGUIWindow::setTabIndex(int index)
{
    QString qstr = "CrazyFly ";
    qstr.append(QString::number(index + 1));
    ui->tabWidget->setCurrentIndex(getTabIndexFromName(qstr));
}

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
void MainGUIWindow::updateComboBoxes()
{
    updateComboBoxesCFs();
    updateComboBoxesCFZones();
}
void MainGUIWindow::updateComboBoxesCFs()
{
    #ifdef CATKIN_MAKE
    ui->comboBoxCFs->clear();
    for(int i = 0; i < crazyflies_vector.size(); i++)
    {
        if(!cf_linker->isCFLinked(crazyflies_vector[i]->getName()))
        {
            QString qstr = QString::fromStdString(crazyflies_vector[i]->getName());
            ui->comboBoxCFs->addItem(qstr);
        }
    }
    #endif
}

void MainGUIWindow::updateComboBoxesCFZones()
{
    ui->comboBoxCFZones->clear();
    #ifdef CATKIN_MAKE
    for(int i = 0; i < scene->crazyfly_zones.size(); i++)
    {
        if(!cf_linker->isCFZoneLinked(scene->crazyfly_zones[i]->getIndex()))
        {
234
            int cf_zone_index = scene->crazyfly_zones[i]->getIndex();
235
            QString qstr = "CrazyFlyZone ";
236
            qstr.append(QString::number(cf_zone_index + 1));
237
238
239
240
241
242
243
            ui->comboBoxCFZones->addItem(qstr);
        }
    }
    #endif
}


roangel's avatar
roangel committed
244
#ifdef CATKIN_MAKE
245
void MainGUIWindow::updateNewViconData(const ptrToMessage& p_msg) //connected to newViconData, from node
246
{
247

248
249
    // update Markers

250
    if(p_msg->markers.size() < markers_vector.size()) // some markers have dissapeared, received stuff is smaller than what we have
251
252
253
    {
        for(int i = p_msg->markers.size(); i < markers_vector.size(); i++)
        {
254
            scene->removeItem(markers_vector[i]); // remove objects from scene
255
            // ROS_INFO_STREAM("element index: " << i << " removed");
256
        }
257
        markers_vector.erase(markers_vector.begin() + p_msg->markers.size(), markers_vector.end()); //delete them
258
259
    }

260
    // ROS_INFO_STREAM("markers.size: " << p_msg->markers.size());
261

262
263
264
265
    for(int i = 0; i < p_msg->markers.size(); i++) // here, or new markers message is equal to current messages, or greater (some new markers)
    {
        if(i >= markers_vector.size()) //some new markers coming
        {
266
            // ROS_INFO_STREAM("element index: " << i << " added");
267
            Marker* tmp_p_marker = new Marker(&(p_msg->markers[i]));
268
269
            markers_vector.push_back(tmp_p_marker); // what happens with the new indexes? check if this is correct

270
            if(ui->checkBox_vicon_markers->checkState() == Qt::Checked) //only if markers checkbox info is checked..
271
272
273
274
275
276
277
            {
                scene->addItem(markers_vector[i]);
                if(ui->checkBox_vicon_highlight_markers->checkState() == Qt::Checked)
                {
                    markers_vector[i]->setHighlighted();
                }
            }
278
279
280
        }
        else
        {
281
            // ROS_INFO_STREAM("element index: " << i << " moved, already existed");
282
            markers_vector[i]->updateMarker(&(p_msg->markers[i]));
283
284
        }
    }
285
286

    // update Crazyflies
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
    // also: what happens if we dont go through one of the names? we need to remove that crazyfly
    int crazyfly_vector_size_before = crazyflies_vector.size(); //initial size of vector
    // in this loop, add new ones and update old ones
    for(int i = 0; i < p_msg->crazyflies.size(); i++)
    {
        bool name_found = false; // for each iteration, name_found starts in false
        int index_name_found;
        for(int j = 0; j < crazyfly_vector_size_before; j++)
        {
            if(crazyflies_vector[j]->getName() == p_msg->crazyflies[i].crazyflieName)
            {
                name_found = true; // name found. This can only happen once per i-iteration, names are unique
                index_name_found = j; // index in already existing vector, to update it later (really needed?)
            }
        }

        if(name_found)
        {
            crazyflies_vector[index_name_found]->updateCF(&(p_msg->crazyflies[i]));
        }
307
        else                    //name not found, newly arrived, add it to the vector
308
309
        {
            crazyFly* tmp_p_crazyfly = new crazyFly(&(p_msg->crazyflies[i]));
310
311
312
313
314
315
            crazyflies_vector.push_back(tmp_p_crazyfly);
        }

        if(ui->checkBox_vicon_crazyflies->checkState() == Qt::Checked)
        {
            for(int i = 0; i < crazyflies_vector.size(); i++) //check for occlussion
316
            {
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
                if(crazyflies_vector[i]->isOccluded())
                {
                    ROS_INFO("===================OCCLUDED");
                    if(crazyflies_vector[i]->isAddedToScene())
                    {
                        scene->removeItem(crazyflies_vector[i]);
                        crazyflies_vector[i]->setAddedToScene(false);
                    }
                }
                else
                {
                    if(!crazyflies_vector[i]->isAddedToScene())
                    {
                        scene->addItem(crazyflies_vector[i]);
                        crazyflies_vector[i]->setAddedToScene(true);
                    }
                }
334
            }
335
336
        }
    }
337

338
    // in this loop, clean the ones that are not present anymore. UPDATE: this will apparently only happen when we tick and untick in Vicon
339
    int crazyfly_vector_size_after = crazyflies_vector.size();
340

341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
    for(int j = 0; j < crazyfly_vector_size_after; j++)
    {
        bool name_found = false;
        for(int i = 0; i < p_msg->crazyflies.size(); i++)
        {
            if(crazyflies_vector[j]->getName() == p_msg->crazyflies[i].crazyflieName)
            {
                name_found = true;
            }
        }
        if(!name_found)
        {
            scene->removeItem(crazyflies_vector[j]);
            crazyflies_vector.erase(crazyflies_vector.begin() + j);
        }
    }
357
}
roangel's avatar
roangel committed
358
#endif
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


void MainGUIWindow::on_removeTable_clicked()
{
    if(scene->getMode() == myGraphicsScene::mode_table)
    {
        scene->removeTable();
    }
}

void MainGUIWindow::transitionToMode(int mode)
{
    switch(mode)
    {
        case myGraphicsScene::mode_table:
        {
            ui->removeTable->setDisabled(false);
            break;
        }
        case myGraphicsScene::mode_crazyfly_zones:
        {
            ui->removeTable->setDisabled(true);
            break;
        }
    }
}

void MainGUIWindow::on_radioButton_table_mode_toggled(bool checked)
{
    switch(scene->getMode())
    {
        case myGraphicsScene::mode_table:
        {
            // already in the mode we want, do nothing
            break;
        }
        case myGraphicsScene::mode_crazyfly_zones:
        {
            scene->setMode(myGraphicsScene::mode_table);
            break;
        }
        case myGraphicsScene::mode_locked:
        {
            scene->setMode(myGraphicsScene::mode_table);
            break;
        }
    }

}


void MainGUIWindow::on_radioButton_crazyfly_zones_mode_toggled(bool checked)
{
    switch(scene->getMode())
    {
        case myGraphicsScene::mode_table:
        {
            scene->setMode(myGraphicsScene::mode_crazyfly_zones);
            break;
        }
        case myGraphicsScene::mode_crazyfly_zones:
        {
            // already in the mode we want, do nothing
            break;
        }
        case myGraphicsScene::mode_locked:
        {
            scene->setMode(myGraphicsScene::mode_crazyfly_zones);
            break;
        }
    }
}

void MainGUIWindow::handleTablePiecesNumChanged(int newNum)
{
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
}

void MainGUIWindow::on_radioButton_lock_mode_toggled(bool checked)
{
    switch(scene->getMode())
    {
        case myGraphicsScene::mode_table:
        {
            scene->setMode(myGraphicsScene::mode_locked);
            break;
        }
        case myGraphicsScene::mode_crazyfly_zones:
        {
            scene->setMode(myGraphicsScene::mode_locked);
            break;
        }
        case myGraphicsScene::mode_locked:
        {
            break;
        }
    }
}

void MainGUIWindow::on_checkBox_grid_toggled(bool checked)
{
    scene->setGrid(checked);
}

void MainGUIWindow::on_checkBox_table_toggled(bool checked)
{
    if(checked)
    {
        scene->showTable();
    }
    else
    {
        scene->hideTable();
    }
}

void MainGUIWindow::on_checkBox_crazyfly_zones_toggled(bool checked)
{
    if(checked)
    {
        scene->showCrazyFlyZones();
    }
    else
    {
        scene->hideCrazyFlyZones();
    }
}

void MainGUIWindow::on_tabWidget_currentChanged(int index)
{
489
490
    // this index is tab index. Need to go to cf index
    QString name = ui->tabWidget->tabText(index);
roangel's avatar
roangel committed
491
    #ifdef CATKIN_MAKE
492
493
    int cf_index = cf_linker->getCFZoneIndexFromName(name);
    scene->setSelectedCrazyFlyZone(cf_index);
roangel's avatar
roangel committed
494
    #endif
495
496
497
498
499
500
501
502
503
504
505
506
507
508
}

void MainGUIWindow::centerViewIndex(int index)
{
    ui->graphicsView->fitInView(scene->getRectFCrazyFlyZone(index), Qt::KeepAspectRatio);
    ui->graphicsView->scale(0.95, 0.95); // A bit back zoom, so we can see everything better
}


void MainGUIWindow::on_pushButton_fitAll_clicked()
{
    ui->graphicsView->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
    ui->graphicsView->scale(0.95, 0.95); // A bit back zoom, so we can see everything better
}
509
510
511
512
513

void MainGUIWindow::on_checkBox_vicon_markers_toggled(bool checked)
{
    if(checked)
    {
514
        #ifdef CATKIN_MAKE
515
516
517
518
        for(int i = 0; i < markers_vector.size(); i++)
        {
            scene->addItem(markers_vector[i]);
        }
519
        #endif
520
        ui->checkBox_vicon_highlight_markers->setCheckable(true);
roangel's avatar
roangel committed
521
        ui->checkBox_vicon_highlight_markers->setEnabled(true);
522
523
524
    }
    else
    {
525
        #ifdef CATKIN_MAKE
526
527
528
529
        for(int i = 0; i < markers_vector.size(); i++)
        {
            scene->removeItem(markers_vector[i]);
        }
530
        #endif
531
532
        ui->checkBox_vicon_highlight_markers->setChecked(false);
        ui->checkBox_vicon_highlight_markers->setCheckable(false);
roangel's avatar
roangel committed
533
        ui->checkBox_vicon_highlight_markers->setEnabled(false);
534
535
536
537
538
539
540
    }
}

void MainGUIWindow::on_checkBox_vicon_highlight_markers_toggled(bool checked)
{
    if(checked)
    {
541
        #ifdef CATKIN_MAKE
542
543
544
545
        for(int i = 0; i < markers_vector.size(); i++)
        {
            markers_vector[i]->setHighlighted();
        }
546
        #endif
547
548
549
    }
    else
    {
550
        #ifdef CATKIN_MAKE
551
552
553
554
        for(int i = 0; i < markers_vector.size(); i++)
        {
            markers_vector[i]->clearHighlighted();
        }
555
556
557
558
559
560
561
562
563
564
565
        #endif
    }
}

void MainGUIWindow::on_checkBox_vicon_crazyflies_toggled(bool checked)
{
    if(checked)
    {
        #ifdef CATKIN_MAKE
        for(int i = 0; i < crazyflies_vector.size(); i++)
        {
566
567
568
569
570
            if(!crazyflies_vector[i]->isAddedToScene())
            {
                scene->addItem(crazyflies_vector[i]);
                crazyflies_vector[i]->setAddedToScene(true);
            }
571
572
573
574
575
576
577
578
579
        }
        #endif
        ui->scaleSpinBox->setEnabled(true);
    }
    else
    {
        #ifdef CATKIN_MAKE
        for(int i = 0; i < crazyflies_vector.size(); i++)
        {
580
581
582
583
584
            if(crazyflies_vector[i]->isAddedToScene())
            {
                scene->removeItem(crazyflies_vector[i]);
                crazyflies_vector[i]->setAddedToScene(false);
            }
585
586
587
588
589
590
591
592
593
594
595
596
        }
        #endif
        ui->scaleSpinBox->setEnabled(false);
    }
}

void MainGUIWindow::on_scaleSpinBox_valueChanged(double arg1)
{
    #ifdef CATKIN_MAKE
    for(int i = 0; i < crazyflies_vector.size(); i++)
    {
        crazyflies_vector[i]->setScaleCFs(arg1);
597
    }
598
    #endif
599
}
600
601
602

void MainGUIWindow::on_refresh_cfs_button_clicked()
{
603
    updateComboBoxesCFs();
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
}

void MainGUIWindow::on_refresh_student_ids_button_clicked()
{
    #ifdef CATKIN_MAKE
    ui->list_discovered_student_ids->clear();

    // \/(\d)\/PPSClient
    ros::V_string v_str;
    ros::master::getNodes(v_str);
    for(int i = 0; i < v_str.size(); i++)
    {
        std::string s = v_str[i];
        std::smatch m;
        std::regex e ("\\/(\\d)\\/PPSClient");

        // std::regex e("\\/PPSClien(.)");

        // while(std::regex_search(s, m, e))
        // {
        //     for (int i = 0; i < m.size(); i++)
        //     {
        //         ROS_INFO("FOUND: %s", m[i].str().c_str());
        //         // std::cout << "FOUND" << m[i] << "\n";
        //     }
        //     s = m.suffix().str();
        // }

        if(std::regex_search(s, m, e))
        {
            // ROS_INFO("===============================================FOUND: %s", m[1].str().c_str()); // one because we are interested ONLY in the first match
            std::string found_string = m[1].str();
            ui->list_discovered_student_ids->addItem(found_string.c_str());
        }
    }
    #endif
}
641
642
643
644
645
646



void MainGUIWindow::on_link_button_clicked()
{
    #ifdef CATKIN_MAKE
647
648
649
650
651

    bool error = false;
    if(ui->comboBoxCFs->count() == 0)
    {
        // plot error message
652
        ui->err_message_cf->setText("CF box is empty");
653
654
        error = true;
    }
655
656
657
658
    else
    {
        ui->err_message_cf->clear();
    }
659
660
661
    if(ui->comboBoxCFZones->count() == 0)
    {
        // plot error message
662
        ui->err_message_cf_zone->setText("CFZone box is empty");
663
664
        error = true;
    }
665
666
667
668
    else
    {
        ui->err_message_cf_zone->clear();
    }
669

670
    if(cf_linker->isStudentIDLinked(ui->spinBox_student_ids->value()))
671
    {
672
673
674
675
676
677
678
        // plot error message
        ui->err_message_student_id->setText("This StudentID has already been linked");
        error = true;
    }
    else
    {
        ui->err_message_student_id->clear();
679
680
681
682
    }

    if(!error)
    {
683
        cf_linker->link(ui->spinBox_student_ids->value(), cf_linker->getCFZoneIndexFromName(ui->comboBoxCFZones->currentText()), ui->comboBoxCFs->currentText().toStdString());
684
    }
685
686
    #endif
}
687
688
689
690

void MainGUIWindow::on_unlink_button_clicked()
{
    #ifdef CATKIN_MAKE
691
    cf_linker->unlink_selection();
692
693
    #endif
}
694
695
696
697

void MainGUIWindow::on_save_in_DB_button_clicked()
{
    // we need to update and then save?
698
    CrazyflieDB tmp_db;
699
    for(int i = 0; i < cf_linker->links.size(); i++)
700
    {
701
702
703
        CrazyflieEntry tmp_entry;
        ROS_INFO("for loop iteration");
        tmp_entry.crazyflieContext.crazyflieName = cf_linker->links[i].cf_name;
704
705
        tmp_entry.crazyflieContext.localArea.crazyfly_zone_index = cf_linker->links[i].cf_zone_index;
        tmp_entry.studentID = cf_linker->links[i].student_id;
706

707
708
709
710
711
712
713
714
715
716
717
718
        for(int j = 0; j < scene->crazyfly_zones.size(); j++)
        {
            if(cf_linker->links[i].cf_zone_index == scene->crazyfly_zones[j]->getIndex())
            {
                QRectF rect = scene->crazyfly_zones[j]->rect();
                tmp_entry.crazyflieContext.localArea.xmin = rect.x() * FROM_UNITS_TO_METERS;
                tmp_entry.crazyflieContext.localArea.xmax = (rect.x() + rect.width()) * FROM_UNITS_TO_METERS;
                tmp_entry.crazyflieContext.localArea.ymin = (rect.y() - rect.height()) * FROM_UNITS_TO_METERS;
                tmp_entry.crazyflieContext.localArea.ymax = rect.y() * FROM_UNITS_TO_METERS;
            }
        }
        tmp_db.crazyflieEntries.push_back(tmp_entry);
719
    }
720
721
722
723

    m_data_base = tmp_db;

    ROS_INFO_STREAM("database:\n" << m_data_base);
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
}

void MainGUIWindow::on_load_from_DB_button_clicked()
{
    // need to reload and then read?
    CMRead getDBCall;
    _rosNodeThread->m_read_db_client.waitForExistence(ros::Duration(-1));
    if(_rosNodeThread->m_read_db_client.call(getDBCall))
    {
        m_data_base = getDBCall.response.crazyflieDB;
		ROS_INFO_STREAM("database:\n" << m_data_base);
        // TODO: update links table
        cf_linker->clear_all_links();
        for(int i = 0; i < m_data_base.crazyflieEntries.size(); i++)
        {
            std::string cf_name = m_data_base.crazyflieEntries[i].crazyflieContext.crazyflieName;
            int cf_zone_index = m_data_base.crazyflieEntries[i].crazyflieContext.localArea.crazyfly_zone_index;
            // we should first create the cf zones that are in the database?
742
743
744
            bool cf_zone_exists;
            qreal width = m_data_base.crazyflieEntries[i].crazyflieContext.localArea.xmax - m_data_base.crazyflieEntries[i].crazyflieContext.localArea.xmin;
            qreal height = m_data_base.crazyflieEntries[i].crazyflieContext.localArea.ymax - m_data_base.crazyflieEntries[i].crazyflieContext.localArea.ymin;
745
746
747
748
            QRectF tmp_rect(m_data_base.crazyflieEntries[i].crazyflieContext.localArea.xmin * FROM_METERS_TO_UNITS,
                            m_data_base.crazyflieEntries[i].crazyflieContext.localArea.ymax * FROM_METERS_TO_UNITS,
                            width * FROM_METERS_TO_UNITS,
                            height * FROM_METERS_TO_UNITS);
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
            int found_j;
            for(int j = 0; j < scene->crazyfly_zones.size(); j++)
            {
                if(cf_zone_index == scene->crazyfly_zones[j]->getIndex())
                {
                    cf_zone_exists = true;
                    found_j = j;
                    break;
                }
            }
            if(!cf_zone_exists)
            {
                scene->addCFZone(tmp_rect, cf_zone_index);
            }
            else
            {
                scene->crazyfly_zones[found_j]->setRect(tmp_rect);
                scene->crazyfly_zones[found_j]->rectSizeChanged();
            }
768
769
770
771
772
773
774
775
776
            int student_id = m_data_base.crazyflieEntries[i].studentID;
            cf_linker->link(student_id, cf_zone_index, cf_name);
        }
    }
    else
    {
        ROS_ERROR("Failed to read DB");
    }
}