mainguiwindow.cpp 8.92 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

14
#include <string>
15

16
#define N_MAX_CRAZYFLIES           20 // protection number
17

roangel's avatar
roangel committed
18
#ifdef CATKIN_MAKE
19
using namespace d_fall_pps;
roangel's avatar
roangel committed
20
#endif
21

22
#ifdef CATKIN_MAKE
23
MainGUIWindow::MainGUIWindow(int argc, char **argv, QWidget *parent) :
24
    QMainWindow(parent),
25
26
    ui(new Ui::MainGUIWindow)//,
    // _rosNodeThread(argc, argv, "/ViconDataPublisher/ViconData")
27
{
muelmarc's avatar
merging    
muelmarc committed
28
    _rosNodeThread = new rosNodeThread(argc, argv, "/ViconDataPublisher/ViconData");
29
30
31
32

    ui->setupUi(this);
    _init();
}
33
34
35
36
37
38
39
40
41
42
#else
MainGUIWindow::MainGUIWindow(int argc, char **argv, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainGUIWindow)
{

    ui->setupUi(this);
    _init();
}
#endif
43

44

45
46
47
48
49
50
51
52
53
54
MainGUIWindow::~MainGUIWindow()
{
    delete ui;
}

void MainGUIWindow::set_tabs(int n)
{
    ui->tabWidget->clear();
    for (int i = 0; i < n; i++)
    {
55
56
        QString qstr = "CrazyFly ";
        qstr.append(QString::number(i+1));
57
58
59
60
61
62
63
64
65
        crazyFlyZoneTab* widget = new crazyFlyZoneTab(i);
        ui->tabWidget->addTab(widget, qstr);
        connect(widget, SIGNAL(centerButtonClickedSignal(int)), this, SLOT(centerViewIndex(int)));
    }
}

void MainGUIWindow::_init()
{

66
67
    ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);

68
    scene = new myGraphicsScene(ui->frame_drawing);
69
    scene->setSceneRect(-100 * FROM_METERS_TO_UNITS, -100 * FROM_METERS_TO_UNITS, 200 * FROM_METERS_TO_UNITS, 200 * FROM_METERS_TO_UNITS);
70

71
    ui->graphicsView->setScene(scene);
72
73
    crazyFly_1 = new crazyFly(QPointF(50,50));
    scene->addItem(crazyFly_1);
74

75
76
77
78
79
80
    QObject::connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), scene, SLOT(removeCrazyFlyZone(int)));
    QObject::connect(scene, SIGNAL(numCrazyFlyZonesChanged(int)), this, SLOT(set_tabs(int)));
    QObject::connect(ui->tabWidget, SIGNAL(currentChanged(int)), scene, SLOT(setSelectedCrazyFlyZone(int)));
    QObject::connect(scene, SIGNAL(crazyFlyZoneSelected(int)), ui->tabWidget, SLOT(setCurrentIndex(int)));
    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
81
82

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

roangel's avatar
roangel committed
84
    #ifdef CATKIN_MAKE
85
    _rosNodeThread->init();
86
87
    qRegisterMetaType<ptrToMessage>("ptrToMessage");
    QObject::connect(_rosNodeThread, SIGNAL(newViconData(const ptrToMessage&)), this, SLOT(setPosMarkers(const ptrToMessage&)));
roangel's avatar
roangel committed
88
    #endif
89
}
90

roangel's avatar
roangel committed
91
#ifdef CATKIN_MAKE
92
void MainGUIWindow::setPosMarkers(const ptrToMessage& p_msg) //connected to newViconData, from node
93
{
94

95
    if(p_msg->markers.size() < markers_vector.size()) // some markers have dissapeared, received stuff is smaller than what we have
96
97
98
    {
        for(int i = p_msg->markers.size(); i < markers_vector.size(); i++)
        {
99
100
            scene->removeItem(markers_vector[i]); // remove objects from scene
            ROS_INFO_STREAM("element index: " << i << " removed");
101
        }
102
        markers_vector.erase(markers_vector.begin() + p_msg->markers.size(), markers_vector.end()); //delete them
103
104
    }

105
106
    ROS_INFO_STREAM("markers.size: " << p_msg->markers.size());

107
108
109
110
    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
        {
111
            ROS_INFO_STREAM("element index: " << i << " added");
112
            QPointF p(p_msg->markers[i].x * FROM_MILIMETERS_TO_UNITS, p_msg->markers[i].y * FROM_MILIMETERS_TO_UNITS);
113
            Marker* tmp_p_marker = new Marker(scene->mapFromWorldToScene(p));
114
115
116
117
118
119
120
121
122
123
            markers_vector.push_back(tmp_p_marker); // what happens with the new indexes? check if this is correct

            if(ui->checkBox_vicon_markers->checkState() == Qt::Checked)                                    //only if markers checkbox info is checked..
            {
                scene->addItem(markers_vector[i]);
                if(ui->checkBox_vicon_highlight_markers->checkState() == Qt::Checked)
                {
                    markers_vector[i]->setHighlighted();
                }
            }
124
125
126
        }
        else
        {
127
            ROS_INFO_STREAM("element index: " << i << " moved, already existed");
128
            markers_vector[i]->setPosMarker(scene->mapFromWorldToScene(QPointF(p_msg->markers[i].x * FROM_MILIMETERS_TO_UNITS, p_msg->markers[i].y * FROM_MILIMETERS_TO_UNITS)));
129
130
        }
    }
131
}
roangel's avatar
roangel committed
132
#endif
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


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)
{
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
}

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)
{
    if(index >= 0)
    {
        scene->setSelectedCrazyFlyZone(index);
    }
}

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
}
281
282
283
284
285
286

void MainGUIWindow::on_checkBox_vicon_markers_toggled(bool checked)
{
    // This is temporal, just to see effect. In the end the marker will be created with data from vicon
    if(checked)
    {
287
288
289
290
        for(int i = 0; i < markers_vector.size(); i++)
        {
            scene->addItem(markers_vector[i]);
        }
291
        ui->checkBox_vicon_highlight_markers->setCheckable(true);
roangel's avatar
roangel committed
292
        ui->checkBox_vicon_highlight_markers->setEnabled(true);
293
294
295
    }
    else
    {
296
297
298
299
        for(int i = 0; i < markers_vector.size(); i++)
        {
            scene->removeItem(markers_vector[i]);
        }
300
301
        ui->checkBox_vicon_highlight_markers->setChecked(false);
        ui->checkBox_vicon_highlight_markers->setCheckable(false);
roangel's avatar
roangel committed
302
        ui->checkBox_vicon_highlight_markers->setEnabled(false);
303
304
305
306
307
308
309
    }
}

void MainGUIWindow::on_checkBox_vicon_highlight_markers_toggled(bool checked)
{
    if(checked)
    {
310
311
312
313
        for(int i = 0; i < markers_vector.size(); i++)
        {
            markers_vector[i]->setHighlighted();
        }
314
315
316
    }
    else
    {
317
318
319
320
        for(int i = 0; i < markers_vector.size(); i++)
        {
            markers_vector[i]->clearHighlighted();
        }
321
322
    }
}