myGraphicsScene.cpp 13.3 KB
Newer Older
1
#include "myGraphicsScene.h"
2
#include "globalDefinitions.h"
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

#include <QGraphicsSceneMouseEvent>
#include <QRect>
#include <QApplication>
#include <QKeyEvent>

myGraphicsScene::myGraphicsScene(QObject *parent)
    : QGraphicsScene(parent)
{
    pen = new QPen(Qt::black);
    brush = new QBrush(Qt::blue);
    setMode(mode_table);
    tmp_rect = 0;
    startedRect = false;
    setGrid(true);
}


void myGraphicsScene::keyPressEvent(QKeyEvent * keyEvent)
{

    switch(mode)
    {
        case mode_table:
        {
            if(keyEvent->key() == Qt::Key_Delete)
            {
                qDebug("del key pressed");
                for(unsigned int i = 0; i < table_pieces.size(); i++)
                {
                    if(table_pieces[i]->isSelected())
                    {
                        qDebug("selectedRectangle: %d", i);
                        removeTablePiece(i);
                    }
                }
            }
            break;
        }
        case mode_crazyfly_zones:
        {
            if(keyEvent->key() == Qt::Key_Delete)
            {
                qDebug("del key pressed");
                for(unsigned int i = 0; i < crazyfly_zones.size(); i++)
                {
                    if(crazyfly_zones[i]->isSelected())
                    {
                        qDebug("selectedRectangle: %d", i);
roangel's avatar
roangel committed
52
                        removeCrazyFlyZone(crazyfly_zones[i]->getIndex());
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
                    }
                }
            }
            break;
        }
        case mode_locked:
        {
            // nothing so far
            break;
        }
        default:
            break;
    }

    QGraphicsScene::keyPressEvent(keyEvent);
}

void myGraphicsScene::setSelectedCrazyFlyZone(int index)
{
    for(unsigned int i = 0; i < crazyfly_zones.size(); i++)
    {
74
75
76
        qDebug("index: %d", index);
        qDebug("i: %d", i);
        qDebug("getIndex: %d", crazyfly_zones[i]->getIndex());
77
        crazyfly_zones[i]->setSelected(false);
78
        if(index == crazyfly_zones[i]->getIndex())
79
        {
80
            crazyfly_zones[i]->setSelected(true);
81
82
83
84
85
86
87
88
89
90
        }
    }
}

int myGraphicsScene::checkSelectedCrazyFlyZone()
{
    for(unsigned int i = 0; i < crazyfly_zones.size(); i++)
    {
        if(crazyfly_zones[i]->isSelected())
        {
91
92
            qDebug("rectangle selected index = %d", crazyfly_zones[i]->getIndex());
            return crazyfly_zones[i]->getIndex();
93
94
95
96
97
        }
    }
    return -1;
}

98
99
100
101
102
103
104
105
void myGraphicsScene::addCFZone(QRectF rect, int index)
{
    tmp_crazyfly_zone_item = new crazyFlyZone(rect, index);
    addItem(tmp_crazyfly_zone_item);
    tmp_crazyfly_zone_item->setRect(tmp_crazyfly_zone_item->rect().normalized());
    addCrazyFlyZoneToVector(tmp_crazyfly_zone_item);
    std::string str = std::to_string(tmp_crazyfly_zone_item->getIndex() + 1);
    tmp_crazyfly_zone_item->setLabel(str.c_str());
roangel's avatar
roangel committed
106
    // setSelectedCrazyFlyZone(crazyfly_zones.size() - 1); //select just created rectangle
107
108
}

109
110
111
112
void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (mouseEvent->button() != Qt::LeftButton)
        return;
113
    qDebug("Mouse Position in world coord: %d, %d", (int)mapFromSceneToWorld((mouseEvent->scenePos()).toPoint()).x(), (int)mapFromSceneToWorld((mouseEvent->scenePos()).toPoint()).y());
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    if(Qt::ControlModifier == QApplication::keyboardModifiers())
    {
        switch(mode)
        {
            case mode_table:
            {
                startedRect = true;
                p1 = new QPointF(mouseEvent->scenePos());
                tmp_rect = new QRectF(*p1, *p1);
                tmp_table_piece_item = new tablePiece(*tmp_rect);
                addItem(tmp_table_piece_item);
                break;
            }
            case mode_crazyfly_zones:
            {
                startedRect = true;
                p1 = new QPointF(mouseEvent->scenePos());
                tmp_rect = new QRectF(*p1, *p1);
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
                // look for first available index here
                bool first_available_value_found = false;
                int first_available_value = 0;
                while(!first_available_value_found)
                {
                    if(crazyfly_zones.size() == 0)
                    {
                        first_available_value_found = true;
                        break;
                    }
                    else        // we enter the for loop for sure
                    {
                        bool index_is_present = false;
                        for(int i = 0; i < crazyfly_zones.size(); i++)
                        {
                            if(crazyfly_zones[i]->getIndex() == first_available_value)
                            {
                                index_is_present = true;
                                break; // break because we finish searching here
                            }
                        }
                        if(!index_is_present)
                        {
                            first_available_value_found = true; // finish loop here, found value
                            break;
                        }
                    }

                    first_available_value++; //first iteration 0
                }
                qDebug("---------------------------first available value: %d", first_available_value);
                tmp_crazyfly_zone_item = new crazyFlyZone(*tmp_rect, first_available_value);
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
                addItem(tmp_crazyfly_zone_item);
                break;
            }
            case mode_locked:
            {
                // do nothing so far
                startedRect = false;
                break;
            }
            default:
                break;
        }
    }
    else
    {
        QGraphicsScene::mousePressEvent(mouseEvent);
    }
}

void myGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if(startedRect)
    {
        switch(mode)
        {
            case mode_table:
            {
                tmp_table_piece_item->setRect(QRectF(*p1, mouseEvent->scenePos()));
                break;
            }
            case mode_crazyfly_zones:
            {
                tmp_crazyfly_zone_item->setRect(QRectF(*p1, mouseEvent->scenePos()));
197
                tmp_crazyfly_zone_item->updateCenterMarker();
198
199
200
201
202
203
204
205
206
207
208
209
210
                qDebug("Mouse Position: %d, %d", (mouseEvent->scenePos()).toPoint().x(), (mouseEvent->scenePos()).toPoint().y());
                qDebug("Rectangle BottomRight Position: %d, %d", tmp_crazyfly_zone_item->rect().bottomRight().x(), tmp_crazyfly_zone_item->rect().bottomRight().y());
                break;
            }
        }

    }
    QGraphicsScene::mouseMoveEvent(mouseEvent);
}

void myGraphicsScene::addCrazyFlyZoneToVector(crazyFlyZone* rect)
{
    crazyfly_zones.push_back(rect);
roangel's avatar
roangel committed
211
212
    int size = crazyfly_zones.size();
    qDebug("size crazyflyzones %d", size);
213
    emit numCrazyFlyZonesChanged(rect->getIndex());
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
}

void myGraphicsScene::addTablePieceToVector(tablePiece* rect)
{
    table_pieces.push_back(rect);
    emit numTablePiecesChanged(table_pieces.size());
}

void myGraphicsScene::changeModeTo(int next_mode)
{
    mode = next_mode;
}

int myGraphicsScene::getMode(void)
{
    return mode;
}

void myGraphicsScene::lockTablePieces(void)
{
    for(int i = 0; i < table_pieces.size(); i++)
    {
        table_pieces[i]->lock();
        table_pieces[i]->setDarkColor();
    }
}

void myGraphicsScene::unlockTablePieces(void)
{
    for(int i = 0; i < table_pieces.size(); i++)
    {
        table_pieces[i]->unlock();
        table_pieces[i]->setLightColor();
    }
}

void myGraphicsScene::lockCrazyFlyZones(void)
{
    for(int i = 0; i < crazyfly_zones.size(); i++)
    {
        crazyfly_zones[i]->lock();
    }
}

void myGraphicsScene::unlockCrazyFlyZones(void)
{
    for(int i = 0; i < crazyfly_zones.size(); i++)
    {
        crazyfly_zones[i]->unlock();
    }
}

void myGraphicsScene::setMode(int new_mode)
{
    switch(new_mode)
    {
        case mode_table:
        {
            lockCrazyFlyZones();
            unlockTablePieces();
            break;
        }
        case mode_crazyfly_zones:
        {
            lockTablePieces();
            unlockCrazyFlyZones();
            break;
        }
        case mode_locked:
        {
            // TODO: define locked mode. Do not allow to create anything, change some color of something to state that we are in that mode
            lockTablePieces();
            lockCrazyFlyZones();
            break;
        }
    }
    mode = new_mode;
    emit modeChanged(new_mode);
}

void myGraphicsScene::setGrid(bool enable)
{
    grid_enable = enable;
    update();
}

void myGraphicsScene::hideTable()
{
    for(int i = 0; i < table_pieces.size(); i++)
    {
        this->removeItem(table_pieces[i]);
    }
}

void myGraphicsScene::showTable()
{
    for(int i = 0; i < table_pieces.size(); i++)
    {
        this->addItem(table_pieces[i]);
    }
}

void myGraphicsScene::hideCrazyFlyZones()
{
    for(int i = 0; i < crazyfly_zones.size(); i++)
    {
        this->removeItem(crazyfly_zones[i]);
    }
}

void myGraphicsScene::showCrazyFlyZones()
{
    for(int i = 0; i < crazyfly_zones.size(); i++)
    {
        this->addItem(crazyfly_zones[i]);
    }
}

QRectF myGraphicsScene::getRectFCrazyFlyZone(int index)
{
    QRectF rect(crazyfly_zones[index]->sceneBoundingRect());
    return rect;
}

roangel's avatar
roangel committed
338
void myGraphicsScene::removeCrazyFlyZone(int cf_zone_index)
339
{
roangel's avatar
roangel committed
340
341
342
343
344
345
346
347
348
349
350
    for(int i = 0; i < crazyfly_zones.size(); i++)
    {
        if(crazyfly_zones[i]->getIndex() == cf_zone_index)
        {
            this->removeItem(crazyfly_zones[i]);
            crazyfly_zones.erase(crazyfly_zones.begin() + i);
            qDebug("removed CFzone %d", cf_zone_index);
            emit numCrazyFlyZonesChanged(cf_zone_index); // for tab managing
            break;
        }
    }
351
352
}

353
354
355
356
357
358
359
360
361
362
void myGraphicsScene::removeAllCFZones()
{
    for(int i = 0; i < crazyfly_zones.size(); i++)
    {
        this->removeItem(crazyfly_zones[i]);
    }
    crazyfly_zones.clear();
    emit numCrazyFlyZonesChanged(0);
}

363
364
365
366
367
368
369
370
371
372
373
374
void myGraphicsScene::removeTable()
{
    for(int i = 0; i < table_pieces.size(); i++)
    {
        this->removeItem(table_pieces[i]);
    }
    table_pieces.clear();
    emit numTablePiecesChanged(table_pieces.size());
}

void myGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
{
roangel's avatar
roangel committed
375
376
377
378
379
380
381
382
383
    // add coordinate origin. X is red, Y is Green

    // X axis
    painter->setPen(QPen(QColor(0xFF, 0, 0, 0xFF), 0));
    painter->drawLine(QLineF(rect.left(), 0, rect.right(), 0));

    // Y axis
    painter->setPen(QPen(QColor(0, 0xFF, 0, 0xFF), 0));
    painter->drawLine(QLineF(0, rect.top(), 0, rect.bottom()));
384
385
386

    if(grid_enable)
    {
387
        const int gridSize = 1 * FROM_METERS_TO_UNITS;
388
389
390
391
392
393
394

        qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
        qreal top = int(rect.top()) - (int(rect.top()) % gridSize);

        QVarLengthArray<QLineF, 100> lines;

        for (qreal x = left; x < rect.right(); x += gridSize)
roangel's avatar
roangel committed
395
            if(x != 0)
396
397
            lines.append(QLineF(x, rect.top(), x, rect.bottom()));
        for (qreal y = top; y < rect.bottom(); y += gridSize)
roangel's avatar
roangel committed
398
            if(y != 0)
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
            lines.append(QLineF(rect.left(), y, rect.right(), y));

        // qDebug() << lines.size();

        painter->setPen(QPen(QColor(0, 0, 0, 0x40), 0));
        painter->drawLines(lines.data(), lines.size());
    }
}

void myGraphicsScene::removeTablePiece(int index)
{
    this->removeItem(table_pieces[index]);
    table_pieces.erase(table_pieces.begin() + index);
    qDebug("removed TabledPiece %d", index);
    emit numTablePiecesChanged(table_pieces.size());
}

void myGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
        if (mouseEvent->button() != Qt::LeftButton)
            return;

        switch(mode)
        {
            case mode_table:
            {
                if(startedRect)
                {
                    tmp_table_piece_item->setRect(tmp_table_piece_item->rect().normalized());
                    addTablePieceToVector(tmp_table_piece_item);
                    tmp_rect = 0;
                    startedRect = false;
                }
                break;
            }
            case mode_crazyfly_zones:
            {
                if(startedRect)
                {
                    tmp_crazyfly_zone_item->setRect(tmp_crazyfly_zone_item->rect().normalized());
                    addCrazyFlyZoneToVector(tmp_crazyfly_zone_item);
440
                    std::string str = std::to_string(tmp_crazyfly_zone_item->getIndex() + 1);
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
                    tmp_crazyfly_zone_item->setLabel(str.c_str());
                    setSelectedCrazyFlyZone(crazyfly_zones.size() - 1); //select just created rectangle
                    tmp_rect = 0;
                    startedRect = false;
                }
                int selected_crazyfly_zone = checkSelectedCrazyFlyZone();
                if(selected_crazyfly_zone != -1)
                {
                    emit crazyFlyZoneSelected(selected_crazyfly_zone);
                }
                break;
            }
            case mode_locked:
            {
                // Do nothing so far..
                break;
            }
            default:
                break;
        }


    QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
465
466
467
468
469
470
471
472
473
474
475
476
477


QPointF myGraphicsScene::mapFromWorldToScene(QPointF point)
{
    QPointF new_point(point.x(), -1 * point.y());
    return new_point;
}

QPointF myGraphicsScene::mapFromSceneToWorld(QPointF point)
{
    QPointF new_point(point.x(), -1 * point.y());
    return new_point;
}