-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawableGraphicsScene.cpp
86 lines (75 loc) · 2.26 KB
/
DrawableGraphicsScene.cpp
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
#include "DrawableGraphicsScene.h"
DrawableGraphicsScene::DrawableGraphicsScene(QObject* parent) : QGraphicsScene(parent){
sceneMode = NoMode;
itemToDraw = 0;
}
DrawableGraphicsScene::~DrawableGraphicsScene(){
}
void DrawableGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if ((sceneMode == DrawLine) || (sceneMode == DrawInLine))
origPoint = event->scenePos();
QGraphicsScene::mousePressEvent(event);
}
void DrawableGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
if (sceneMode == DrawLine) {
if (!itemToDraw) {
itemToDraw = new QGraphicsLineItem;
this->addItem(itemToDraw);
itemToDraw->setPen(QPen(Qt::black, 3, Qt::SolidLine));
itemToDraw->setPos(origPoint);
}
itemToDraw->setLine(0, 0,
event->scenePos().x() - origPoint.x(),
event->scenePos().y() - origPoint.y());
}else
if (sceneMode == DrawInLine) {
if (!itemToDraw) {
itemToDraw = new QGraphicsLineItem;
this->addItem(itemToDraw);
itemToDraw->setPen(QPen(Qt::blue, 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 DrawableGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
itemToDraw = 0;
QGraphicsScene::mouseReleaseEvent(event);
}
void DrawableGraphicsScene::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Delete)
foreach(QGraphicsItem* item, selectedItems()) {
removeItem(item);
delete item;
}
else
QGraphicsScene::keyPressEvent(event);
}
void DrawableGraphicsScene::setMode(Mode mode) {
sceneMode = mode;
QGraphicsView::DragMode vMode =
QGraphicsView::NoDrag;
if ((mode == DrawLine) || (mode == DrawInLine)){
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 DrawableGraphicsScene::makeItemsControllable(bool areControllable) {
foreach(QGraphicsItem* item, items()) {
item->setFlag(QGraphicsItem::ItemIsSelectable,
areControllable);
item->setFlag(QGraphicsItem::ItemIsMovable,
areControllable);
}
}