-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdraw_area.cpp
More file actions
558 lines (488 loc) · 13.2 KB
/
draw_area.cpp
File metadata and controls
558 lines (488 loc) · 13.2 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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#include <QPainter>
#include <QPaintEvent>
#include "commands.h"
#include "draw_area.h"
#include "main_window.h"
/**
* @brief DrawArea::DrawArea - constructor for our Draw Area.
* Pointers to the MainWindow's
* tools and image are mandatory
*
*/
DrawArea::DrawArea(QWidget *parent)
: QWidget(parent)
{
// initialize the undo stack
undoStack = new QUndoStack(this);
undoStack->setUndoLimit(UNDO_LIMIT);
// initialize image
image = new QPixmap();
//create the pen, line, eraser, & rect tools
createTools();
// initialize colors
foregroundColor = Qt::black;
backgroundColor = Qt::white;
// initialize state variables
drawing = false;
drawingPoly = false;
currentLineMode = single;
// small optimizations
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_StaticContents);
}
DrawArea::~DrawArea()
{
delete image;
delete penTool;
delete lineTool;
delete eraserTool;
delete rectTool;
}
/**
* @brief DrawArea::paintEvent - redraw part of the image based
* on what was modified
*
*/
void DrawArea::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
QRect modifiedArea = e->rect(); // only need to redraw a small area
painter.drawPixmap(modifiedArea, *image, modifiedArea);
}
/**
* @brief DrawArea::mousePressEvent - left-click initiates a draw
*
* - right-click opens a dialog
* menu for the current tool
*
*/
void DrawArea::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::RightButton)
{
// open the dialog menu
static_cast<MainWindow*>(parent())->mousePressEvent(e);
}
else if (e->button() == Qt::LeftButton)
{
if(image->isNull())
return;
drawing = true;
if(!drawingPoly)
currentTool->setStartPoint(e->pos());
// save a copy of the old image
oldImage = image->copy(QRect());
}
}
/**
* @brief DrawArea::mouseMoveEvent - draw
*
*/
void DrawArea::mouseMoveEvent(QMouseEvent *e)
{
if (e->buttons() & Qt::LeftButton && drawing)
{
if(image->isNull())
return;
ToolType type = currentTool->getType();
if(type == line || type == rect_tool)
{
*image = oldImage;
if(type == line && currentLineMode == poly)
{
drawingPoly = true;
}
}
currentTool->drawTo(e->pos(), this, image);
}
}
/**
* @brief DrawArea::mouseReleaseEvent - finish drawing
*
*/
void DrawArea::mouseReleaseEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton && drawing)
{
drawing = false;
if(image->isNull())
return;
if(drawingPoly)
{
currentTool->setStartPoint(e->pos());
//return;
}
if(currentTool->getType() == pen)
currentTool->drawTo(e->pos(), this, image);
// for undo/redo - make sure there was a change
// (in case drawing began off-image)
if(oldImage.toImage() != image->toImage())
saveDrawCommand(oldImage);
}
}
/**
* @brief DrawArea::mouseDoubleClickEvent - cancel poly mode
*
*/
void DrawArea::mouseDoubleClickEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
{
if(drawingPoly)
drawingPoly = false;
}
}
/**
* @brief DrawArea::OnSaveImage - Undo a previous action
*
*/
void DrawArea::OnUndo()
{
if(!undoStack->canUndo())
return;
undoStack->undo();
update();
}
/**
* @brief DrawArea::OnRedo - Redo a previously undone action
*
*/
void DrawArea::OnRedo()
{
if(!undoStack->canRedo())
return;
undoStack->redo();
update();
}
/**
* @brief DrawArea::OnClearAll - Clear the image
*
*/
void DrawArea::OnClearAll()
{
if(image->isNull())
return;
clearImage();
}
/**
* @brief DrawArea::OnPenCapConfig - Update cap style for pen tool
*
*/
void DrawArea::OnPenCapConfig(int capStyle)
{
switch (capStyle)
{
case flat: penTool->setCapStyle(Qt::FlatCap); break;
case square: penTool->setCapStyle(Qt::SquareCap); break;
case round_cap: penTool->setCapStyle(Qt::RoundCap); break;
default: break;
}
}
/**
* @brief DrawArea::OnPenSizeConfig - Update pen size
*
*/
void DrawArea::OnPenSizeConfig(int value)
{
penTool->setWidth(value);
}
/**
* @brief DrawArea::OnEraserConfig - Update eraser thickness
*
*/
void DrawArea::OnEraserConfig(int value)
{
eraserTool->setWidth(value);
}
/**
* @brief DrawArea::OnLineStyleConfig - Update line style for line tool
*
*/
void DrawArea::OnLineStyleConfig(int lineStyle)
{
switch (lineStyle)
{
case solid: lineTool->setStyle(Qt::SolidLine); break;
case dashed: lineTool->setStyle(Qt::DashLine); break;
case dotted: lineTool->setStyle(Qt::DotLine); break;
case dash_dotted: lineTool->setStyle(Qt::DashDotLine); break;
case dash_dot_dotted: lineTool->setStyle(Qt::DashDotDotLine); break;
default: break;
}
}
/**
* @brief DrawArea::OnLineCapConfig - Update cap style for line tool
*
*/
void DrawArea::OnLineCapConfig(int capStyle)
{
switch (capStyle)
{
case flat: lineTool->setCapStyle(Qt::FlatCap); break;
case square: lineTool->setCapStyle(Qt::SquareCap); break;
case round_cap: lineTool->setCapStyle(Qt::RoundCap); break;
default: break;
}
}
/**
* @brief DrawArea::OnDrawTypeConfig - Update draw type for line tool
*
*/
void DrawArea::OnDrawTypeConfig(int drawType)
{
switch (drawType)
{
case single: setLineMode(single); break;
case poly: setLineMode(poly); break;
default: break;
}
}
/**
* @brief DrawArea::OnLineThicknessConfig - Update line thickness for line tool
*
*/
void DrawArea::OnLineThicknessConfig(int value)
{
lineTool->setWidth(value);
}
/**
* @brief DrawArea::OnRectBStyleConfig - Update rectangle boundary line style
*
*/
void DrawArea::OnRectBStyleConfig(int boundaryStyle)
{
switch (boundaryStyle)
{
case solid: rectTool->setStyle(Qt::SolidLine); break;
case dashed: rectTool->setStyle(Qt::DashLine); break;
case dotted: rectTool->setStyle(Qt::DotLine); break;
case dash_dotted: rectTool->setStyle(Qt::DashDotLine); break;
case dash_dot_dotted: rectTool->setStyle(Qt::DashDotDotLine); break;
default: break;
}
}
/**
* @brief DrawArea::OnRectShapeTypeConfig - Update rectangle shape setting
*
*/
void DrawArea::OnRectShapeTypeConfig(int shape)
{
switch (shape)
{
case rectangle: rectTool->setShapeType(rectangle); break;
case rounded_rectangle: rectTool->setShapeType(rounded_rectangle); break;
case ellipse: rectTool->setShapeType(ellipse); break;
default: break;
}
}
/**
* @brief DrawArea::OnRectFillConfig - Update rectangle fill setting
*
*/
void DrawArea::OnRectFillConfig(int fillType)
{
switch (fillType)
{
case foreground: rectTool->setFillMode(foreground);
rectTool->setFillColor(foregroundColor); break;
case background: rectTool->setFillMode(background);
rectTool->setFillColor(backgroundColor); break;
case no_fill: rectTool->setFillMode(no_fill);
rectTool->setFillColor(QColor(Qt::transparent)); break;
default: break;
}
}
/**
* @brief DrawArea::OnRectBTypeConfig - Update rectangle join style
*
*/
void DrawArea::OnRectBTypeConfig(int boundaryType)
{
switch (boundaryType)
{
case miter_join: rectTool->setJoinStyle(Qt::MiterJoin); break;
case bevel_join: rectTool->setJoinStyle(Qt::BevelJoin); break;
case round_join: rectTool->setJoinStyle(Qt::RoundJoin); break;
default: break;
}
}
/**
* @brief DrawArea::OnRectLineConfig - Update rectangle line width
*
*/
void DrawArea::OnRectLineConfig(int value)
{
rectTool->setWidth(value);
}
/**
* @brief DrawArea::OnRectCurveConfig - Update rounded rectangle curve
*
*/
void DrawArea::OnRectCurveConfig(int value)
{
rectTool->setCurve(value);
}
/**
* @brief DrawArea::createNewImage - creates a new image of
* user-specified dimensions
*
*/
void DrawArea::createNewImage(const QSize &size)
{
// save a copy of the old image
oldImage = image->copy();
*image = QPixmap(size);
image->fill(backgroundColor);
update();
// for undo/redo
if(!imagesEqual(oldImage, *image))
saveDrawCommand(oldImage);
}
/**
* @brief DrawArea::loadImage - Load an image from a user-specified file
*
*/
void DrawArea::loadImage(const QString &fileName)
{
// save a copy of the old image
oldImage = image->copy();
image->load(fileName);
update();
// for undo/redo
if(!imagesEqual(oldImage, *image))
saveDrawCommand(oldImage);
}
/**
* @brief DrawArea::saveImage - Save an image to user-specified file
*
*/
void DrawArea::saveImage(const QString &fileName)
{
image->save(fileName, "BMP");
}
/**
* @brief DrawArea::resizeImage - Resize image to user-specified dimensions
*
*/
void DrawArea::resizeImage(const QSize &size)
{
// save a copy of the old image
oldImage = image->copy();
// if no change, do nothing
if(image->size() == size)
{
return;
}
// else re-scale the image
*image = image->scaled(size, Qt::IgnoreAspectRatio);
update();
// for undo/redo
saveDrawCommand(oldImage);
}
/**
* @brief DrawArea::clearImage - clears an image by filling it with
* the background color
*
*/
void DrawArea::clearImage()
{
// save a copy of the old image
oldImage = image->copy();
image->fill(backgroundColor);
update(image->rect());
// for undo/redo
if(!imagesEqual(oldImage, *image))
saveDrawCommand(oldImage);
}
/**
* @brief DrawArea::updateColorConfig - Updates the tools' colors
* as appropriate
*
*/
void DrawArea::updateColorConfig(const QColor &color, int which)
{
if(which == foreground)
{
foregroundColor = color;
penTool->setColor(foregroundColor);
lineTool->setColor(foregroundColor);
rectTool->setColor(foregroundColor);
if(rectTool->getFillMode() == foreground)
rectTool->setFillColor(foregroundColor);
}
else
{
backgroundColor = color;
eraserTool->setColor(backgroundColor);
if(rectTool->getFillMode() == background)
rectTool->setFillColor(backgroundColor);
}
}
/**
* @brief DrawArea::setCurrentTool - Sets the current tool, unsetting
* poly mode if necessary
*
*/
Tool* DrawArea::setCurrentTool(int newType)
{
// get the current tool's type
int currType = currentTool->getType();
// if no change, return --else cancel poly mode & set tool
if(newType == currType)
return currentTool;
if(currType == line)
drawingPoly = false;
switch(newType)
{
case pen: currentTool = penTool; break;
case line: currentTool = lineTool; break;
case eraser: currentTool = eraserTool; break;
case rect_tool: currentTool = rectTool; break;
default: break;
}
return currentTool;
}
/**
* @brief DrawArea::setLineMode - Sets the current line draw mode,
* unsetting poly mode if necessary
*
*/
void DrawArea::setLineMode(const DrawType mode)
{
if(mode == single)
drawingPoly = false;
currentLineMode = mode;
}
/**
* @brief DrawArea::SaveDrawCommand - Put together a DrawCommand
* and save it on the undo/redo stack.
*
*/
void DrawArea::saveDrawCommand(const QPixmap &old_image)
{
// put the old and new image on the stack for undo/redo
QUndoCommand *drawCommand = new DrawCommand(old_image, image);
undoStack->push(drawCommand);
}
/**
* @brief DrawArea::createTools - takes care of creating the tools
*
*/
void DrawArea::createTools()
{
// create the tools
penTool = new PenTool(QBrush(Qt::black), DEFAULT_PEN_THICKNESS);
lineTool = new LineTool(QBrush(Qt::black), DEFAULT_PEN_THICKNESS);
eraserTool = new EraserTool(QBrush(Qt::white), DEFAULT_ERASER_THICKNESS);
rectTool = new RectTool(QBrush(Qt::black), DEFAULT_PEN_THICKNESS);
// set default tool
currentTool = static_cast<Tool*>(penTool);
}
/**
* @brief imagesEqual - returns true if the two images are the same
*
*/
bool imagesEqual(const QPixmap &image1, const QPixmap &image2)
{
return image1.toImage() == image2.toImage();
}