-
Notifications
You must be signed in to change notification settings - Fork 0
/
cropcanvas.cpp
217 lines (179 loc) · 5.33 KB
/
cropcanvas.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
#include "cropcanvas.h"
#include <QtWidgets>
//TODO: use const values instead of macro defs for defaults
#define CROP_CANVAS_DEFAULT_ZOOM_AMOUNT 1.0
CropCanvas::CropCanvas(QWidget *parent) :
QWidget(parent),
cropArea(RectSelection()),
brushSrcImgFrame(Qt::BrushStyle::Dense4Pattern),
padding(0),
canvasSize(0,0),
zoomAmount(CROP_CANVAS_DEFAULT_ZOOM_AMOUNT)
{
//TODO: use transformations instead of keeping points and ratios
}
CropCanvas::~CropCanvas()
{
}
void CropCanvas::loadImage(QImage srcImg)
{
int handleSize = cropArea.getHandleSize();
this->srcImg = srcImg;
this->padding = handleSize + 1;
this->canvasSize = QSize(
srcImg.width() + padding * 2,
srcImg.height() + padding * 2);
this->setMinimumSize(canvasSize);
this->paddingRects[0] = QRect(0, 0, canvasSize.width(), padding);
this->paddingRects[1] = QRect(0, padding, padding, srcImg.height());
this->paddingRects[2] = QRect(canvasSize.width() - padding, padding, padding, srcImg.height());
this->paddingRects[3] = QRect(0, canvasSize.height() - padding, canvasSize.width(), padding);
update();
cropArea.setCanvasSize(canvasSize);
cropArea.reinit();
if(!croppedImg.isNull())
croppedImg = QImage();
if(!croppedImgPixmap.isNull())
croppedImgPixmap = QPixmap();
emit cropCleared();
}
//events
void CropCanvas::paintEvent(QPaintEvent * /* event */)
{
if(!this->srcImg.isNull()) {
//paint whole widget, not just dirty portion
//TODO: only render necessary region later
QPainter painter(this);
QBrush tmpBrush = painter.brush();
painter.fillRect(paddingRects[0], brushSrcImgFrame);
painter.fillRect(paddingRects[1], brushSrcImgFrame);
painter.fillRect(paddingRects[2], brushSrcImgFrame);
painter.fillRect(paddingRects[3], brushSrcImgFrame);
//painter.scale(zoomAmount, zoomAmount);
painter.drawImage(padding, padding, srcImg);
//painter.scale(1/zoomAmount, 1/zoomAmount);
cropArea.draw(painter);
painter.setBrush(tmpBrush);
}
}
void CropCanvas::mousePressEvent(QMouseEvent *event) {
if(event->button() == Qt::LeftButton) {
cropArea.pressed(event->pos());
}
}
void CropCanvas::mouseMoveEvent(QMouseEvent *event) {
if(event->buttons() & Qt::LeftButton) {
cropArea.moved(event->pos());
}
update();
}
void CropCanvas::mouseReleaseEvent(QMouseEvent *event) {
if(event->button() == Qt::LeftButton) {
cropArea.released(event->pos());
}
}
QBrush CropCanvas::getBrushSrcImgFrame() const
{
return brushSrcImgFrame;
}
void CropCanvas::setBrushSrcImgFrame(const QBrush &value)
{
brushSrcImgFrame = value;
}
CropCanvas::ImageCroppedOutput CropCanvas::getCropOutput() const
{
return cropOutput;
}
void CropCanvas::setCropOutput(const ImageCroppedOutput &value)
{
cropOutput = value;
}
QImage CropCanvas::getCroppedImg() const
{
return croppedImg;
}
QPixmap CropCanvas::getCroppedImgPixmap()
{
if(!croppedImg.isNull() && croppedImgPixmap.isNull())
croppedImgPixmap = QPixmap::fromImage(croppedImg);
return croppedImgPixmap;
}
//properties - widget should be redrawn after setting visual properties!
qreal CropCanvas::getZoomAmount() const
{
return zoomAmount;
}
void CropCanvas::setZoomAmount(qreal newZoomAmount) {
if(newZoomAmount <= 0)
zoomAmount = CROP_CANVAS_DEFAULT_ZOOM_AMOUNT;
else
zoomAmount = newZoomAmount;
}
int CropCanvas::getCropAreaHandleSize() const
{
return cropArea.getHandleSize();
}
void CropCanvas::setCropAreaHandleSize(int size)
{
cropArea.setHandleSize(size);
}
QSize CropCanvas::getCropAreaMinSize() const
{
return cropArea.getMinSize();
}
void CropCanvas::setCropAreaMinSize(QSize size)
{
cropArea.setMinSize(size);
}
QPen CropCanvas::getCropAreaPenFrame() const
{
return cropArea.getPenFrame();
}
void CropCanvas::setCropAreaPenFrame(const QPen &value)
{
cropArea.setPenFrame(value);
}
QPen CropCanvas::getCropAreaPenHandle() const
{
return cropArea.getPenHandle();
}
void CropCanvas::setCropAreaPenHandle(const QPen &value)
{
cropArea.setPenHandle(value);
}
QBrush CropCanvas::getCropAreaBrushHandle() const
{
return cropArea.getBrushHandle();
}
void CropCanvas::setCropAreaBrushHandle(const QBrush &value)
{
cropArea.setBrushHandle(value);
}
void CropCanvas::crop()
{
if(srcImg.isNull()) return;
QRect translatedRect = cropArea.getFrame();
//TODO: do these using transformations
//translatedRect.moveTopLeft((translatedRect.topLeft() - (srcImgPos*zoomAmount)) * 1/zoomAmount);
//translatedRect.setBottomRight(translatedRect.bottomRight() * 1/zoomAmount);
croppedImg = srcImg.copy(translatedRect);
if(!croppedImgPixmap.isNull())
croppedImgPixmap = QPixmap();
switch (cropOutput) {
case ImageCroppedOutput::qImage:
//do not produce a QPixmap until asked
emit imageCropped(croppedImg);
break;
case ImageCroppedOutput::qPixmap:
croppedImgPixmap = QPixmap::fromImage(croppedImg);
emit imageCropped(croppedImgPixmap);
break;
case ImageCroppedOutput::both:
croppedImgPixmap = QPixmap::fromImage(croppedImg);
emit imageCropped(croppedImgPixmap);
emit imageCropped(croppedImg);
break;
default:
break;
}
}