-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaint.cpp
More file actions
139 lines (118 loc) · 4.04 KB
/
paint.cpp
File metadata and controls
139 lines (118 loc) · 4.04 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
#include "Paint.h"
#include "ui_paint.h"
#include <QColor>
#include <qdebug.h>
#include <QAbstractSlider>
Paint::Paint(QWidget *parent) :
QWidget(parent),
ui(new Ui::Paint)
{
ui->setupUi(this);
scene = new PaintScene();
ui->graphicsView->setScene(scene);
ui->graphicsView->mapToGlobal(QPoint(0, 0));
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::AnyIPv4, 27888);
colorDialog = new QColorDialog(this);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readingData()));
connect(this, &Paint::signalColor, scene, &PaintScene::slotColor);
connect(this, &Paint::signalBrush, scene, &PaintScene::slotBrush);
connect(this, &Paint::signalInfo, scene, &PaintScene::drawFromOnline);
connect(this, &Paint::signalTrans, scene, &PaintScene::sceneTrans);
setMouseTracking(true);
emit signalTrans(scene);
}
Paint::~Paint()
{
delete ui;
}
void Paint::on_pushButton_clicked()
{
if(color.isValid())
{
colorDialog->setCurrentColor(color);
colorDialog->exec();
if(colorDialog->selectedColor().isValid())
color = colorDialog->selectedColor();
}
else
{
colorDialog->setCurrentColor(backgroundColor);
colorDialog->exec();
color = colorDialog->selectedColor();
}
emit signalColor(color);
}
void Paint::on_backGroundButton_clicked()
{
if(backgroundColor.isValid())
{
colorDialog->setCurrentColor(backgroundColor);
colorDialog->exec();
if(colorDialog->selectedColor().isValid())
backgroundColor = colorDialog->selectedColor();
}
else
{
backgroundColor = QColorDialog::getColor();
}
scene->setBackgroundBrush(QBrush(backgroundColor, Qt::SolidPattern));
QByteArray sendBackground;
sendBackground.append("background" + backgroundColor.name());
udpSocket->writeDatagram(sendBackground, QHostAddress::Broadcast, 27888);
sendBackground.clear();
// Тестовый метод очистки (слишком большая ресурсозатрачиваемость)
// QSize clearLayer = ui->graphicsView->maximumSize();
// scene->addLine(clearLayer.width(),
// clearLayer.height(),
// -clearLayer.width(),
// -clearLayer.width(),
// QPen(Qt::white,6000,Qt::SolidLine,Qt::RoundCap));
}
void Paint::on_horizontalSlider_valueChanged(qint32 brushSize)
{
emit signalBrush(brushSize);
QString s = QString::number(brushSize);
ui->label->setText(s);
}
void Paint::readingData()
{
while(udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress senderIP;
quint16 senderPort;
udpSocket->readDatagram(datagram.data(), datagram.size(), &senderIP, &senderPort);
// Тестовый метод очистки
// if(datagram == "clearLayer")
// {
// qDebug() << datagram;
// on_clearButton_clicked();
// }
if(datagram.contains("background"))
{
datagram.remove(0, datagram.indexOf("#"));
scene->setBackgroundBrush(QBrush(QString(datagram), Qt::SolidPattern));
backgroundColor = QString(datagram);
}
else
{
QColor setColor = QString(datagram.left(datagram.indexOf("|")));
datagram.remove(0, datagram.indexOf("|") + 1);
qint32 setBrushSize = datagram.left(datagram.indexOf("|")).toInt();
datagram.remove(0, datagram.indexOf("|") + 1);
QString transferCoord = datagram;
datagram.clear();
emit signalInfo(transferCoord, setColor, setBrushSize);
}
}
}
void Paint::sendingDate(QString coordinate, QColor brushColor, qint32 brushSize)
{
QByteArray sendData;
QString s = brushColor.name() + "|" + QString::number(brushSize) + "|" + coordinate;
sendData.append(s);
udpSocket->writeDatagram(sendData, QHostAddress::Broadcast, 27888);
sendData.clear();
}