-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcustomscene.cpp
More file actions
103 lines (92 loc) · 2.93 KB
/
customscene.cpp
File metadata and controls
103 lines (92 loc) · 2.93 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
#include "customscene.h"
#include <QDebug>
#include <defaults.h>
CustomScene::CustomScene(QObject* parent): QGraphicsScene(parent)
{
sceneMode = NoMode;
itemToDraw = 0;
}
void CustomScene::setMode(Mode mode){
sceneMode = mode;
QGraphicsView::DragMode vMode =
QGraphicsView::NoDrag;
if(mode == DrawLine){
makeItemsControllable(false);
vMode = QGraphicsView::NoDrag;
}
else if(mode == SelectObject){
makeItemsControllable(true);
vMode = QGraphicsView::RubberBandDrag;
}
QGraphicsView* mView = views().at(0);
if(mView)
mView->setDragMode(vMode);
}
void CustomScene::makeItemsControllable(bool areControllable){
foreach(QGraphicsItem* item, items()){
if (item->type() != 7) // Check to disable movement of Image render
{
item->setFlag(QGraphicsItem::ItemIsSelectable,
areControllable);
item->setFlag(QGraphicsItem::ItemIsMovable,
areControllable);
}
}
}
void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent *event){
try {
if(sceneMode == DrawLine){
origPoint = event->scenePos();
if (lines.count() == 1){
this->removeItem(lines.firstKey());
lines.remove(lines.firstKey());
}
}
QGraphicsScene::mousePressEvent(event);
} catch (std::exception &e) {
qDebug() << e.what();
}
}
void CustomScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
if(sceneMode == DrawLine){
if(!itemToDraw){
itemToDraw = new QGraphicsLineItem;
this->addItem(itemToDraw);
itemToDraw->setPen(QPen(Qt::red, 3, Qt::SolidLine));
itemToDraw->setPos(origPoint);
}
itemToDraw->setLine(0,0,
event->scenePos().x() - origPoint.x(),
event->scenePos().y() - origPoint.y());
}
else
QGraphicsScene::mouseMoveEvent(event);
}
void CustomScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
if (sceneMode == DrawLine){
auto *currentLine = new QLine();
currentLine->setLine(origPoint.x(), IMAGE_CONFIGURATION::IMAGE_RESOLUTION_HEIGHT-origPoint.y(), event->scenePos().x(), IMAGE_CONFIGURATION::IMAGE_RESOLUTION_HEIGHT-event->scenePos().y());
lines.insert(itemToDraw, currentLine);
}
itemToDraw = 0;
QGraphicsScene::mouseReleaseEvent(event);
}
void CustomScene::keyPressEvent(QKeyEvent *event){
if(event->key() == Qt::Key_Delete){
foreach(QGraphicsItem* item, selectedItems()){
removeItem(item);
lines.remove(item);
delete item;
}
}
else
QGraphicsScene::keyPressEvent(event);
}
QMap<QGraphicsItem *, QLine *> CustomScene::getLines() const
{
return lines;
}
void CustomScene::setLines(const QMap<QGraphicsItem *, QLine *> &value)
{
lines = value;
}