-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyScene.cpp
177 lines (161 loc) · 4.13 KB
/
MyScene.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
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
#include "MyScene.h"
MyScene::MyScene(QObject *parent) : QGraphicsScene(parent)
{
//初始化边界
QPointF topLeft(0, 0);
bounding.setTopLeft(topLeft);
QPointF bottomRight(0, 0);
bounding.setBottomRight(bottomRight);
}
MyScene::~MyScene()
{
}
void MyScene::setBrush(bool checkBoxBrush, bool checkBoxClean)
{
this->checkBoxBrush = checkBoxBrush;
if (checkBoxBrush) {
if (checkBoxClean) {
cleanMode = true;
brushMode = false;
}
else {
cleanMode = false;
brushMode = true;
}
}
else {
cleanMode = false;
brushMode = false;
}
}
void MyScene::setBrushSize(int brushSize)
{
this->brushSize = brushSize;
}
void MyScene::setBounding(QPointF topLeft, QPointF bottomRight)
{
//要留出大约一个笔刷的距离,不然画出的图形也会出界
QPointF tmp(1*brushSize, 1*brushSize);
bounding.setTopLeft(topLeft+tmp);
bounding.setBottomRight(bottomRight-tmp);
}
void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (brushMode && bounding.contains(event->scenePos())) {
if (event->button() == Qt::LeftButton) {
brushing = true;
// Save the coordinates of the point of pressing
previousPoint = event->scenePos();
}
else if(event->button() == Qt::RightButton){
brushing = true;
brushBackground = true;
}
}
else if (cleanMode && event->button() != Qt::MiddleButton) {
cleaning = true;
/*
QGraphicsItem *itemToRemove = 0;
foreach (QGraphicsItem *item, items(event->scenePos())){
if (item->type() == QGraphicsLineItem::Type) {
itemToRemove = item;
break;
}
}
if (itemToRemove != 0)
removeItem(itemToRemove);
*/
//只要图形的位置在鼠标位置的一个邻域内,就会被删除
QGraphicsItem *itemToRemove = 0;
foreach(QGraphicsItem *item, this->items()) {
QPointF itemPos;
itemPos.setX(item->boundingRect().x());
itemPos.setY(item->boundingRect().y());
if ((abs(event->scenePos().x() - itemPos.x()) < brushSize*1.5)
&& (abs(event->scenePos().y() - itemPos.y()) < brushSize*1.5)
&& (item->type() == QGraphicsLineItem::Type
|| item->type() == QGraphicsEllipseItem::Type)) {
itemToRemove = item;
break;
}
}
if (itemToRemove != 0)
removeItem(itemToRemove);
}
else {
event->ignore();
//将事件继续传递
QGraphicsScene::mousePressEvent(event);
}
}
void MyScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
//if (brushMode && brushing && bounding.contains(event->scenePos())) {
if (brushMode && brushing && this->sceneRect().contains(event->scenePos())) {
if (!brushBackground) {
// We draw the line with the previous coordinates
addLine(previousPoint.x(),
previousPoint.y(),
event->scenePos().x(),
event->scenePos().y(),
QPen(Qt::red, brushSize, Qt::SolidLine, Qt::RoundCap));
// Update on the previous coordinate data
previousPoint = event->scenePos();
}
else {
addEllipse(event->scenePos().x()- brushSize/2.0,
event->scenePos().y()- brushSize/2.0,
brushSize,
brushSize,
QPen(Qt::NoPen),
QBrush(Qt::blue));
}
}
else if (cleanMode && cleaning) {
/*
QGraphicsItem *itemToRemove = 0;
foreach(QGraphicsItem *item, items(event->scenePos())) {
if (item->type() == QGraphicsLineItem::Type
|| item->type() == QGraphicsEllipseItem::Type) {
itemToRemove = item;
break;
}
}
if (itemToRemove != 0)
removeItem(itemToRemove);
*/
QGraphicsItem *itemToRemove = 0;
foreach(QGraphicsItem *item, this->items()) {
QPointF itemPos;
itemPos.setX(item->boundingRect().x());
itemPos.setY(item->boundingRect().y());
if ( (abs(event->scenePos().x()- itemPos.x()) < brushSize*1.5)
&& (abs(event->scenePos().y() - itemPos.y()) < brushSize*1.5)
&& (item->type() == QGraphicsLineItem::Type
|| item->type() == QGraphicsEllipseItem::Type)) {
itemToRemove = item;
break;
}
}
if (itemToRemove != 0)
removeItem(itemToRemove);
}
else {
event->ignore();
//将事件继续传递
QGraphicsScene::mouseMoveEvent(event);
}
}
void MyScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (brushing || cleaning) {
cleaning = false;
brushing = false;
brushBackground = false;
}
else {
event->ignore();
//将事件继续传递
QGraphicsScene::mouseReleaseEvent(event);
}
}