Repository navigation
Expand file tree
/
Copy pathWorkspaceArea.cpp
More file actions
438 lines (400 loc) · 11.7 KB
/
Copy pathWorkspaceArea.cpp
File metadata and controls
438 lines (400 loc) · 11.7 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
/**
* @class WorkspaceArea
* @brief Stores and handles image related functions, viewing.
* This class inherits the QGraphicsScene parent class.
* Thus, the instance of this class will be handled by a QGraphicsView.
*/
#include "WorkspaceArea.h"
#include "Utilities/PixelHelper.h"
#include "FilterTransform/NonKernelBased/MagicWand.h"
#include <QtWidgets>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printdialog)
#include <QPrinter>
#include <QPrintDialog>
#endif
#endif
/**
* @brief Construct a new Workspace Area:: Workspace Area object.
* @param parent To be passed to QGraphicScene constructor.
*/
WorkspaceArea::WorkspaceArea(QObject *parent)
: QGraphicsScene(0, 0, SCENE_WIDTH, SCENE_HEIGHT, parent)
{
// Set default workspace resolution
imageWidth = SCENE_WIDTH;
imageHeight = SCENE_HEIGHT;
// Set default values for the monitored variables
modified = false;
myPenWidth = 5;
myPenColor = Qt::blue;
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
}
/**
* @brief Construct a new Workspace Area:: Workspace Area object.
* @details Workspace area will have dimensions width and height.
*
* @param width Workspace area width.
* @param height Workspace area height.
* @param parent To be passed to QGraphicScene constructor.
*/
WorkspaceArea::WorkspaceArea(int width, int height, QObject *parent)
: QGraphicsScene(0, 0, width, height, parent)
{
// Set workspace resolution
imageWidth = width;
imageHeight = height;
// Set default values for the monitored variables
modified = false;
myPenWidth = 5;
myPenColor = Qt::blue;
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
}
/**
* @brief Place image in the workspace area.
*
* @param loadedImage Image to load.
* @param imageWidth Width of thhe image
* @param imageHeight height of the image
*/
void WorkspaceArea::openImage(const QImage &loadedImage, int imageWidth, int imageHeight)
{
image = loadedImage.copy();
isImageLoaded = true;
this->imageWidth = imageWidth;
this->imageHeight = imageHeight;
QImage &&scaledImage = loadedImage.scaled(imageWidth, imageHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
// Loads the image to the workspace area.
if (pixmapGraphics)
{
removeItem(pixmapGraphics);
delete pixmapGraphics;
}
pixmapGraphics = addPixmap(QPixmap::fromImage(scaledImage));
pixmapGraphics->setTransformationMode(Qt::SmoothTransformation);
pixmapGraphics->setPos({imageWidth / 2.0 - scaledImage.width() / 2.0, imageHeight / 2.0 - scaledImage.height() / 2.0});
pixmapGraphics->setZValue(-2000);
emit imageLoaded(image);
modified = false;
update();
}
/**
* @brief Makes the brush strokes/drawing permanent, i.e. fused into the image.
* @return QImage Fused image (fused with brush strokes).
*/
QImage WorkspaceArea::commitImage()
{
if (isImageLoaded)
{
QImage commitImage(imageWidth, imageHeight, QImage::Format_ARGB32_Premultiplied);
QPainter painter;
painter.begin(&commitImage);
render(&painter); // Renders the Workspace area to the image
painter.end();
return commitImage;
}
else
{
// We generate a white canvas
clearSelection(); // Selections would also render to the file
setSceneRect(this->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
QImage commitImage(this->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene
if (commitImage.isNull())
{
return commitImage;
}
commitImage.fill(Qt::white); // Start all pixels white color
QPainter painter(&commitImage);
render(&painter);
return commitImage;
}
}
/**
* @brief Sets the workspaceImage to a committed version of it.
*/
void WorkspaceArea::commitImageAndSet()
{
image = commitImage();
imageWidth = image.width();
imageHeight = image.height();
}
/**
* @brief Saves the current image.
*
* @param fileName
* @param fileFormat
* @return true image saved
* @return false image not saved
*/
bool WorkspaceArea::saveImage(const QString &fileName, const char *fileFormat)
{
QImage &&snap = commitImage();
image = snap;
if (snap.isNull())
{
return false;
}
if (snap.save(fileName, fileFormat))
{
modified = false;
return true;
}
else
{
return false;
}
}
/**
* @brief Mouse press event override.
* @details Press event will be based on which cursor is selected.
* @param event Mouse event.
*/
void WorkspaceArea::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (cursorMode == CursorMode::RECTANGLECROP)
{
cropOriginScreen = event->screenPos();
cropOrigin = event->scenePos().toPoint();
if (!rubberBand)
{
rubberBand = new QRubberBand(QRubberBand::Rectangle, nullptr);
rubberBand->setGeometry(QRect(cropOrigin, QSize()));
rubberBand->show();
}
}
else if (cursorMode == CursorMode::MAGICWAND)
{
cropOriginScreen = event->screenPos();
cropOrigin = event->scenePos().toPoint();
}
QGraphicsScene::mousePressEvent(event);
}
/**
* @brief Mouse move event override.
* @details Move event will be based on which cursor is selected.
* @param event Mouse event.
*/
void WorkspaceArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) == 0)
{
QGraphicsScene::mouseMoveEvent(event);
return;
}
switch (cursorMode)
{
case CursorMode::SCRIBBLE:
{
onMoveScribble(event->scenePos(), myPenColor, myPenWidth);
sendMoveScribble(event->scenePos().x(), event->scenePos().y(), myPenColor.name(), myPenWidth);
break;
}
case CursorMode::RECTANGLECROP:
{
if (cropOrigin.x() + (event->scenePos().toPoint().x() - cropOrigin.x()) <= width() &&
cropOrigin.x() + (event->scenePos().toPoint().x() - cropOrigin.x()) > 0)
{
cropX = event->scenePos().toPoint().x() - cropOrigin.x();
dx = event->screenPos().x() - cropOriginScreen.x();
}
if (cropOrigin.y() + (event->scenePos().toPoint().y() - cropOrigin.y()) <= height() &&
cropOrigin.y() + (event->scenePos().toPoint().y() - cropOrigin.y()) > 0)
{
cropY = event->scenePos().toPoint().y() - cropOrigin.y();
dy = event->screenPos().y() - cropOriginScreen.y();
}
QRect rect = QRect(cropOriginScreen.x(), cropOriginScreen.y(), dx, dy).normalized();
rubberBand->setGeometry(rect);
break;
}
}
QGraphicsScene::mouseMoveEvent(event);
modified = true;
}
/**
* @brief Scribble action, draws painter path to path item.
*
* @param pos Position to move
* @param penColor
* @param penWidth
*/
void WorkspaceArea::onMoveScribble(QPointF pos, QColor penColor, int penWidth) {
QPainterPath path;
if (pathItem == nullptr)
{
path.moveTo(pos); // move path to event scene position
pathItem = new QGraphicsPathItem();
pen.setColor(penColor);
pen.setBrush(penColor);
pen.setWidth(penWidth);
pathItem->setPen(pen); // set sticker pen to default pen
pathItem->setPath(path); // set sticker path to the new path
this->addItem(pathItem);
}
path = pathItem->path(); // get path from sticker
path.lineTo(pos); // draw line from last event pos to current pos
pathItem->setPath(path);
pathItem->update();
}
/**
* @brief Mouse release event override.
* @details Release event will be based on which cursor is selected.
* @param event Mouse event.
*/
void WorkspaceArea::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
switch (cursorMode)
{
case CursorMode::SCRIBBLE:
onReleaseScribble();
emit sendReleaseScribble();
break;
case CursorMode::RECTANGLECROP:
{
if (cropX < 0)
{
cropOrigin.setX(cropOrigin.x() + cropX);
cropX = -cropX;
}
if (cropY < 0)
{
cropOrigin.setY(cropOrigin.y() + cropY);
cropY = -cropY;
}
cropImage(cropOrigin.x(), cropOrigin.y(), cropX, cropY);
break;
}
case CursorMode::MAGICWAND:
cropImageWithMagicWand(cropOrigin.x(), cropOrigin.y());
break;
}
QGraphicsScene::mouseReleaseEvent(event);
}
/**
* @brief When user release mouse press (cursor == SCRIBBLE), emits signal to update image preview and image drawn.
*/
void WorkspaceArea::onReleaseScribble() {
emit updateImagePreview();
emit imageDrawn();
pathItem = nullptr;
}
/**
* @brief Crops image on position x, y, for size width, height.
*
* @param x
* @param y
* @param width
* @param height
* @param fromServer Command from the server.
*/
void WorkspaceArea::cropImage(int x, int y, int width, int height, bool fromServer) {
QRect cropRect = QRect(x, y, width, height);
QImage &&uncroppedImage = commitImage();
QImage &&croppedImage = uncroppedImage.copy(cropRect);
if (rubberBand != nullptr) {
delete rubberBand;
rubberBand = nullptr;
}
emit commitChanges("Rectangle Crop");
if (!fromServer) {
emit sendCrop(x, y, width, height);
}
emit imageCropped(croppedImage, width, height);
}
/**
* @brief Resizes image to width and height.
*
* @param width resized image width.
* @param height resized image height.
*/
void WorkspaceArea::resizeImage(int width, int height, bool fromServer)
{
QImage &&unresizedImage = commitImage();
QImage &&resizedImage = unresizedImage.scaled(width, height, Qt::AspectRatioMode::KeepAspectRatio);
if (!fromServer) {
emit sendResize(width, height);
}
emit commitChanges("Image Resized");
emit imageResized(resizedImage, width, height);
}
/**
* @brief Magic Wand cropping uses a filter from class MagicWand, with specified threshold (from data member)
*
* @param x x starting position.
* @param y y starting position.
* @param fromServer
*/
void WorkspaceArea::cropImageWithMagicWand(int x, int y, bool fromServer) {
thisColor = PixelHelper::getPixel(image, x, y);
MagicWand m;
commitImageAndSet();
QImage &&newImage = m.crop(image, x, y, magicWandThreshold);
emit imageCropped(newImage, newImage.width(), newImage.height());
emit commitChanges("Magic Removal");
if (!fromServer) {
emit sendCropWithMagicWand(x, y);
}
}
/**
* @brief Commits the image. Passed to color controls' image previewer.
* @details check commitImage() for how this works.
*
* @return QImage Comitted image.
*/
QImage WorkspaceArea::commitImageForPreview()
{
if (isImageLoaded)
{
// The image previewer will have dimensions 200x200
// We sample a small portion of current image for previewing.
int width = 200;
int height = 200;
int x = imageWidth / 2;
int y = imageHeight / 2;
if (imageWidth < 400 || imageHeight < 400)
{
width = height = qMin(imageWidth, imageHeight);
x = y = 0;
}
QRect previewRect = QRect(x, y, width, height);
// We get a committed image ready.
QImage commitImage(imageWidth, imageHeight, QImage::Format_ARGB32_Premultiplied);
QPainter painter;
painter.begin(&commitImage);
render(&painter); // Renders the Workspace area to the image
painter.end();
return commitImage.copy(previewRect).scaled(200, 200, Qt::KeepAspectRatio);
}
else
{
// Return a 200x200 white image if there is no image loaded in the workspace area.
QImage commitImage(200, 200, QImage::Format_ARGB32_Premultiplied);
commitImage.fill(Qt::white);
return commitImage;
}
}
/**
* @brief Prints the image. Opens a printer dialog.
*/
void WorkspaceArea::print()
{
// Check for print dialog availability
#if QT_CONFIG(printdialog)
QPrinter printer(QPrinter::HighResolution);
// Open printer dialog and print if asked
QPrintDialog printDialog(&printer);
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
}