-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawboard.cpp
255 lines (200 loc) · 6.54 KB
/
drawboard.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <QtWidgets>
#ifndef QT_NO_PRINTER
#include <QPrinter>
#include <QPrintDialog>
#endif
#include "drawboard.h"
#include "paintdata.h"
DrawBoardArea::DrawBoardArea(QWidget *parent) : QWidget(parent) {
setAttribute(Qt::WA_StaticContents);
modified = false;
scribbling = false;
myPenWidth = 2;
myPenColor = Qt::black;
}
bool DrawBoardArea::openImage(const QString &fileName)
{
QImage loadedImage;
if (!loadedImage.load(fileName))
return false;
QSize newSize = loadedImage.size().expandedTo(size());
resizeImage(&loadedImage, newSize);
image = loadedImage;
modified = false;
update();
return true;
}
/*
* In the openImage() function, we load the given image then we resize it to be
* at least as large as the widget.
* At the end, we call QWidget::update() to schedule a repaint.
*/
bool DrawBoardArea::saveImage(const QString &fileName, const char *fileFormat)
{
QImage visibleImage = image;
resizeImage(&visibleImage, size());
if (visibleImage.save(fileName, fileFormat))
{
modified = false;
return true;
} else
{
return false;
}
}
/*
* saveImage() creates a QImage object that covers only the visible section of the
* actual image and saves it using QImage::save().
*/
void DrawBoardArea::setPenColor(const QColor &newColor)
{
this->myPenColor = newColor;
}
void DrawBoardArea::setPenWidth(int newWidth)
{
myPenWidth = newWidth;
}
void DrawBoardArea::clearImage()
{
image.fill(qRgb(255, 255, 255));
modified = true;
update();
}
/*
* clearImage() clears the image displayed.
* We simply fill the entire image with white - RGB value (255, 255, 255).
*/
void DrawBoardArea::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
lastPoint = event->pos(); //lastPoint is the point from where we begin scribbling
scribbling = true;
myPaintData.addPoint(lastPoint);
myPaintData.mouseReleasePoint = false;
// client_ptr->sendData("1",2);
}
}
void DrawBoardArea::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && scribbling)
{
drawLineTo(event->pos()); //event->pos() returns a Qpoint of the current mouse position
temporaryPoint = event->pos();
myPaintData.addPoint(temporaryPoint);
}
}
void DrawBoardArea::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && scribbling)
{
drawLineTo(event->pos());
temporaryPoint = event->pos();
myPaintData.mouseReleasePoint = true;
myPaintData.addPoint(temporaryPoint);
scribbling = false;
}
}
/*
* If the users press the left mouse button, we store the position of the mouse cursor in lastPoint.
* We also make a note that the user is currently scribbling.
*/
void DrawBoardArea::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QRect dirtyRect = event->rect();
painter.drawImage(dirtyRect, image, dirtyRect);
}
void DrawBoardArea::resizeEvent(QResizeEvent *event)
{
if (width() > image.width() || height() > image.height())
{
int newWidth = qMax(width() + 128, image.width());
int newHeight = qMax(height() + 128, image.height());
resizeImage(&image, QSize(newWidth, newHeight));
update();
}
QWidget::resizeEvent(event);
}
void DrawBoardArea::drawLineTo(const QPoint &endPoint)
{
QPainter painter(&image);
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawLine(lastPoint, endPoint);
modified = true;
int rad = (myPenWidth / 2) + 2;
update(QRect(lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
lastPoint = endPoint;
}
/*
* drawLineTo() draws a line from the point where the mouse was located when the last mouse press
* or mouse move occurred
*
* Optimization:
* Instead of calling update() with no parameter, we pass a QRect that specifies the rectangle
* inside the drawboard are needs updating, to avoid a complete repaint of the widget.
*/
void DrawBoardArea::recieveData(char *bufferFromServer, int bufferFromServerSize)
{
QByteArray byte_array;
byte_array.fromRawData(bufferFromServer, bufferFromServerSize);
QDataStream in(byte_array);
int width;
in >> width;
QColor color;
in >> color;
QPainter painter(&image);
painter.setPen(QPen(color, width, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
int number_of_points;
number_of_points = (bufferFromServerSize - 4 - 16) / 8;
QVector <QPoint> newPointArray;
QPoint tempPoint;
for(int i = 0; i < number_of_points; i++)
{
in >> tempPoint;
newPointArray.push_back(tempPoint);
}
for(int i = 0; i < number_of_points; i++)
{
myPaintData.startPoint = newPointArray[i];
myPaintData.finishPoint = myPaintData.Shape.shapeBuffer[i+1];
painter.drawLine(myPaintData.startPoint, myPaintData.finishPoint);
int rad = (myPaintData.Shape.penWidth / 2) + 2;
update(QRect(myPaintData.startPoint, myPaintData.finishPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
}
}
/*
* recieveData() draws the shape recieved from the server
* Same optimization as before.
*/
void DrawBoardArea::resizeImage(QImage *image, const QSize &newSize)
{
if (image->size() == newSize)
return;
QImage newImage(newSize, QImage::Format_RGB32);
newImage.fill(qRgb(255, 255, 255));
QPainter painter(&newImage);
painter.drawImage(QPoint(0, 0), *image);
*image = newImage;
}
void DrawBoardArea::print()
{
#if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG)
QPrinter printer(QPrinter::HighResolution);
QPrintDialog printDialog(&printer, this);
if (printDialog.exec() == QDialog::Accepted)
{
QPainter painter(&printer);
QRect rect = painter.viewport();
QSize size = image.size();
size.scale(rect.size(), Qt::KeepAspectRatio);
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
painter.setWindow(image.rect());
painter.drawImage(0, 0, image);
/*
* We scale the image to fit within the available space
* on the page before painting it onto the paint device.
*/
}
#endif // QT_NO_PRINTER
}