-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaintscene.cpp
More file actions
157 lines (135 loc) · 5.06 KB
/
paintscene.cpp
File metadata and controls
157 lines (135 loc) · 5.06 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
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
#include "paintscene.h"
#include <qdebug.h>
#include "paint.h"
#include <QGraphicsItemGroup>
#include <QThread>
PaintScene::PaintScene(QObject *parent) : QGraphicsScene(parent)
{
QList<QPointF> coordinateArray;
group_1 = new QGraphicsItemGroup();
}
PaintScene::~PaintScene()
{
}
// Отрисовка полученных координат
void PaintScene::drawFromOnline(QString setCoordinate, QColor brushColor, qint32 brushSize)
{
// Проверка
if(setCoordinate.length() <= 10)
{
QPointF coordinate;
coordinate.setX(setCoordinate.left(setCoordinate.indexOf("|")).toInt());
setCoordinate.remove(0, setCoordinate.indexOf("|") + 1);
coordinate.setY(setCoordinate.left(setCoordinate.indexOf("|")).toInt());
setCoordinate.clear();
addEllipse(coordinate.x() - brushSize/2,
coordinate.y() - brushSize/2,
brushSize,
brushSize,
QPen(Qt::NoPen),
QBrush(brushColor));
previousPointOnline = coordinate;
}
if(setCoordinate.length() > 10)
{
while(setCoordinate.length() > 1)
{
QPointF coordinate;
coordinate.setX(setCoordinate.left(setCoordinate.indexOf("|")).toInt());
setCoordinate.remove(0, setCoordinate.indexOf("|") + 1);
coordinate.setY(setCoordinate.left(setCoordinate.indexOf("|")).toInt());
setCoordinate.remove(0, setCoordinate.indexOf("|") + 1);
coordinateArray << coordinate;
}
setCoordinate.clear();
// Отрисовываем линии с использованием предыдущей координаты
if(previousPointOnline == QPointF(0.0, 0.0))
{
previousPointOnline = coordinateArray.takeFirst();
}
while(coordinateArray.length() != 0)
{
QPointF nextPoint = coordinateArray.takeFirst();
addLine(previousPointOnline.x(),
previousPointOnline.y(),
nextPoint.x(),
nextPoint.y(),
QPen(brushColor, brushSize, Qt::SolidLine, Qt::RoundCap));
previousPointOnline = nextPoint;
}
coordinateArray.clear();
}
previousPointOnline = QPointF(0.0, 0.0);
}
// Отрисовка второго слоя
void PaintScene::sceneTrans(PaintScene *scene)
{
tempscene = scene;
tempscene->addItem(group_1);
}
void PaintScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// При нажатии кнопки мыши отрисовываем эллипс
addEllipse(event->scenePos().x() - changedBrush/2,
event->scenePos().y() - changedBrush/2,
changedBrush,
changedBrush,
QPen(Qt::NoPen),
QBrush(changedColor));
// Сохраняем координаты точки нажатия
previousPoint = event->scenePos();
// Передача данных
sendButtonPress = QString::number(previousPoint.x()) + "|" + QString::number(previousPoint.y()) + "|";
}
void PaintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// qDebug() << event->scenePos();
group_1->hide();
group_1 = new QGraphicsItemGroup();
tempscene->addItem(group_1);
group_1->addToGroup(tempscene->addEllipse(event->scenePos().x() - changedBrush/2,
event->scenePos().y() - changedBrush/2,
changedBrush,
changedBrush));
if( event->buttons() == Qt::LeftButton )
{
// Отрисовываем линии с использованием предыдущей координаты
addLine(previousPoint.x(),
previousPoint.y(),
event->scenePos().x(),
event->scenePos().y(),
QPen(changedColor,changedBrush,Qt::SolidLine,Qt::RoundCap));
// Обновляем данные о предыдущей координате
previousPoint = event->scenePos();
// Передача данных
transferData += QString::number(previousPoint.x()) + "|" + QString::number(previousPoint.y()) + "|";
}
}
// Отправка данных клиентам при отпускании ЛКМ
void PaintScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
Paint paint;
if(sendButtonPress != 0 && transferData.isEmpty())
{
paint.sendingDate(sendButtonPress, changedColor, changedBrush);
sendButtonPress.clear();
}
if(transferData > 0)
{
paint.sendingDate(transferData, changedColor, changedBrush);
transferData.clear();
}
}
}
// Смена цвета
void PaintScene::slotColor(QColor a)
{
changedColor = a;
}
// Смена размера кисти
void PaintScene::slotBrush(qint32 a)
{
changedBrush = a;
}