-
Notifications
You must be signed in to change notification settings - Fork 0
/
scribblearea.cpp
432 lines (375 loc) · 11.2 KB
/
scribblearea.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <QtWidgets>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printdialog)
#include <QPrinter>
#include <QPrintDialog>
#endif
#endif
#include "scribblearea.h"
#include <QPainterPath>
#include <QFont>
#include <QLinearGradient>
#include <iterator>
#include <QDebug>
#include <QMatrix>
#include <QBrush>
#include <QGradient>
#include <QThread>
#include <QCursor>
#include "scribbleshape.h"
ScribbleArea::ScribbleArea(QWidget *parent)
: QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);
modified = false;
scribbling = false;
filling = 0;
fillbrush.setStyle(Qt::SolidPattern);
myPenWidth = 6;
myPenColor = Qt::blue;
myPenType = Qt::SolidLine;
type = ScribbleArea::Pen;
adjustSize();
// setMaximumSize(400,400);
bgColor = qRgb(255, 255, 255);
image = QImage(700, 500, QImage::Format_RGB32);
image.fill(bgColor);
tempImage = image;
canvasWidth = 0;
canvasHeight = 0;
moveTime = 0;
textEdit = 0;
selected = false;
QCursor *myCursor =new QCursor(QPixmap(":/icon/icons8-ball-point-pen-64.png"),-1,-1);
this->setCursor(*myCursor);
}
bool ScribbleArea::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;
}
bool ScribbleArea::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;
}
}
void ScribbleArea::setPenColor(const QColor &newColor)
{
myPenColor = newColor;
}
void ScribbleArea::setPenWidth(int newWidth)
{
myPenWidth = newWidth;
}
void ScribbleArea::setText(QString t)
{
textEdit = 1;
text = t;
}
void ScribbleArea::set2bgColor()
{
myPenColor = bgColor;
}
void ScribbleArea::setSize(QSize s)
{
if(s.width())
canvasWidth = 150000/s.width();
else
canvasWidth = 3000;
if(s.height())
canvasHeight = 60000/s.height();
else
canvasWidth = 3000;
update();
}
void ScribbleArea::setbgColor(const QColor &bgc)
{
bgColor = bgc;
image.fill(bgColor);
}
void ScribbleArea::setPenType(int s)
{
myPenType = s;
}
void ScribbleArea::setShape(ScribbleArea::shape s)
{
type = s;
}
void ScribbleArea::setMovement()
{
type = Move;
selected = true;
}
void ScribbleArea::setFillStyle(int s, QBrush brush)
{
filling = s;
fillbrush = brush;
}
void ScribbleArea::clearImage()
{
image.fill(bgColor);
modified = true;
states.clear();
moveStates.clear();
update();
}
void ScribbleArea::mousePressEvent(QMouseEvent *event)
{
if(type != ScribbleArea::Fill)
filling = 0;
if(type != Move){
moveTime = 0;
moveStates.clear();
}
if(textEdit > 1)
type = ScribbleArea::Pen, textEdit = 0;
if (event->button() == Qt::LeftButton) {
lastPoint = event->pos();
}
if(event->button() == Qt::LeftButton && !shapeSet.empty() && filling)
{
for(QVector<ScribbleShape>::iterator i = shapeSet.begin(); i != shapeSet.end();)
{
if(i->shapetype == filling && i->isInside(event->pos()))
{
QPainter painter(&image);
painter.fillPath(i->path, fillbrush);
update();
QThread::msleep(500);
shapeSet.erase(i);
break;
}
else i ++;
}
}
}
void ScribbleArea::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton && !filling)
{
endPoint = event->pos();
scribbling = true;
tempImage = image;
if(type == Pen)
paint(image);
// drawLineTo(event->pos());
else
paint(tempImage);
}
}
void ScribbleArea::mouseReleaseEvent(QMouseEvent *event)
{
scribbling = false;
endPoint = event->pos();
if(type != Pen && !selected && !filling && !textEdit)
paint(image);
if(type == Move)
++moveTime;
}
void ScribbleArea::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
if(scribbling == true && type != Pen)
painter.drawImage(0, 0, tempImage, canvasWidth, canvasHeight);
else
painter.drawImage(0, 0, image, canvasWidth, canvasHeight);
}
void ScribbleArea::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 ScribbleArea::paint(QImage &theImage)
{
QPainter painter(&theImage);
if(type != ScribbleArea::Eraser && myPenColor == bgColor)
myPenColor = Qt::blue;
//决定画笔的线性,比如实线还是虚线还是点画线,双点画线
if(myPenType == Qt::SolidLine)
state.pen = QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin);
else if(myPenType == Qt::DotLine)
state.pen = QPen(myPenColor, myPenWidth, Qt::DotLine, Qt::RoundCap,
Qt::RoundJoin);
else if(myPenType == Qt::DashLine)
state.pen = QPen(myPenColor, myPenWidth, Qt::DashLine, Qt::RoundCap,
Qt::RoundJoin);
else if(myPenType == Qt::DashDotLine)
state.pen = QPen(myPenColor, myPenWidth, Qt::DashDotLine, Qt::RoundCap,
Qt::RoundJoin);
else
state.pen = QPen(myPenColor, myPenWidth, Qt::DashDotDotLine, Qt::RoundCap,
Qt::RoundJoin);
painter.setPen(state.pen);
//对画线,矩形,椭圆,画笔,移动,文本功能分别做处理,通过type来区分
switch(type)
{
case ScribbleArea::Eraser:
{
set2bgColor();
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,Qt::RoundJoin));
state.path.moveTo(lastPoint);
state.path.lineTo(endPoint);
painter.drawPath(state.path);
states.push_back(state);
state.path= state.path.subtracted(state.path);
lastPoint = endPoint;
break;
}
case ScribbleArea::Rect:
{
//绘制矩阵
QPainterPath path;
QRect rect = QRect(lastPoint.x(), lastPoint.y(), endPoint.x(), endPoint.y());
path.addRect(rect);
painter.drawPath(path);
//如果释放了鼠标,scribbling就是false了,表示画完了。此种情况下,在shapeSet中保存矩阵,为填充功能服务。
if(scribbling == false)
{
QPoint v[4] = {rect.topLeft(), rect.bottomRight(), rect.bottomLeft(), rect.topRight()};
ScribbleShape temp(ScribbleArea::Rect, v, path);
shapeSet.push_back(temp);
}
//在states中放入这个QPaintPath,为移动功能服务
if(scribbling == false)
{
state.path = path;
states.push_back(state);
state.path= state.path.subtracted(state.path);
}
break;
}
case ScribbleArea::Ellipse:
{
QPainterPath path;
path.addEllipse(lastPoint.x(), lastPoint.y(), endPoint.x() - lastPoint.x(), endPoint.y() - lastPoint.y());
painter.drawPath(path);
if(scribbling == false)
{
QPoint v[4] = {lastPoint, endPoint, endPoint, lastPoint};
ScribbleShape temp(ScribbleArea::Ellipse, v, path);
shapeSet.push_back(temp);
}
if(scribbling == false)
{
state.path = path;
states.push_back(state);
state.path= state.path.subtracted(state.path);
}
break;
}
case ScribbleArea::Pen:
{
//小段小段的线段
state.path.moveTo(lastPoint);
state.path.lineTo(endPoint);
//画出来
painter.drawPath(state.path);
//保存路径
states.push_back(state);
state.path= state.path.subtracted(state.path);
//更新点
lastPoint = endPoint;
break;
}
case ScribbleArea::Move:
{
//一开始选中移动功能,先画矩阵框
if(moveTime == 0)
{
painter.setPen(Qt::DashLine);
painter.drawRect(lastPoint.x(), lastPoint.y(), endPoint.x(), endPoint.y());
pre = lastPoint;
cur = endPoint;
}
//画完矩阵框以后,把在矩阵框内的对象存入准备要移动的QVector,就是moveStates。
if(moveTime >= 1)
{
selected = false;
for(QVector <State> :: iterator iter = states.begin();iter != states.end();){
if((*iter).path.currentPosition().x()< cur.x() && (*iter).path.currentPosition().x() > pre.x() && (*iter).path.currentPosition().y() < cur.y() && (*iter).path.currentPosition().y() > pre.y())
{
moveStates.push_back(*iter);
states.erase(iter);
}
else
iter++;
}
for(int i = 0;i<moveStates.size();++i){
moveStates[i].path.translate(endPoint.x()-lastPoint.x(),endPoint.y()-lastPoint.y());
}
lastPoint = endPoint;
state.path= state.path.subtracted(state.path);
}
break;
}
case ScribbleArea::Text:
{
//打文本
QPainter painter(&image);
const QRect rectangle = QRect(lastPoint, endPoint);
painter.setFont(QFont("Times", 15));
QPen *pen = new QPen;
painter.drawText(rectangle, 0, text);
update();
textEdit ++;
break;
}
}
foreach (State state, states) {
painter.setPen(state.pen);
painter.drawPath(state.path);
}
foreach (State moveState, moveStates) {
painter.setPen(moveState.pen);
painter.drawPath(moveState.path);
}
state.path.moveTo(0,0);
modified = true;
update();
}
void ScribbleArea::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 ScribbleArea::print()
{
#if QT_CONFIG(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);
}
#endif // QT_CONFIG(printdialog)
}