myGraphicsScene.cpp 12.4 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#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);
                        removeCrazyFlyZone(i);
                    }
                }
            }
            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
98
99
100
101
        }
    }
    return -1;
}

void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (mouseEvent->button() != Qt::LeftButton)
        return;
102
    qDebug("Mouse Position in world coord: %d, %d", (int)mapFromSceneToWorld((mouseEvent->scenePos()).toPoint()).x(), (int)mapFromSceneToWorld((mouseEvent->scenePos()).toPoint()).y());
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    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);
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
                // 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);
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
                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()));
                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);
199
    emit numCrazyFlyZonesChanged(rect->getIndex());
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
}

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;
}

void myGraphicsScene::removeCrazyFlyZone(int index)
{
326
    int n = crazyfly_zones[index]->getIndex();
327
328
329
    this->removeItem(crazyfly_zones[index]);
    crazyfly_zones.erase(crazyfly_zones.begin() + index);
    qDebug("removed CFzone %d", index);
330
    emit numCrazyFlyZonesChanged(n); // for tab managing
331
332
333
334
335
336
337
338
339
340
341
342
343
344
}

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
345
346
347
348
349
350
351
352
353
    // 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()));
354
355
356

    if(grid_enable)
    {
357
        const int gridSize = 1 * FROM_METERS_TO_UNITS;
358
359
360
361
362
363
364

        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
365
            if(x != 0)
366
367
            lines.append(QLineF(x, rect.top(), x, rect.bottom()));
        for (qreal y = top; y < rect.bottom(); y += gridSize)
roangel's avatar
roangel committed
368
            if(y != 0)
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
            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);
410
411
                    // TODO: set first available number, not size of vector. Index + 1
                    std::string str = std::to_string(tmp_crazyfly_zone_item->getIndex() + 1);
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
                    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);
}
436
437
438
439
440
441
442
443
444
445
446
447
448


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;
}