-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaintwidget.cpp
More file actions
357 lines (273 loc) · 9.1 KB
/
paintwidget.cpp
File metadata and controls
357 lines (273 loc) · 9.1 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
#include "paintwidget.h"
PaintWidget::PaintWidget(QWidget *parent) : QWidget(parent){
setFixedSize(400, 400);
toggleMulticolor(m_multicolor);
QPixmap newPixmap{rect().size()};
m_pixmap = newPixmap;
clear();
}
void PaintWidget::paintEvent(QPaintEvent *) {
QPainter painter{this};
painter.drawPixmap(0, 0, m_grid);
painter.drawPixmap(0, 0, m_pixmap);
m_isSaved = false;
if(m_saveFilePath.isEmpty()){
emit modified(tr("Untitled"));
} else {
emit modified(m_saveFilePath);
}
}
void PaintWidget::resizeEvent(QResizeEvent *) {
QPixmap newPixmap{rect().size()};
newPixmap.fill(Qt::transparent);
QPainter painter{&newPixmap};
painter.fillRect(newPixmap.rect(), Qt::transparent);
painter.drawPixmap((newPixmap.width() - m_pixmap.width()) / 2, (newPixmap.height() - m_pixmap.height()) / 2, m_pixmap);
m_pixmap = newPixmap;
maps.push_back(m_pixmap);
drawGrid();
emit resized(m_pixmap.width());
}
void PaintWidget::mousePressEvent(QMouseEvent * ev) {
m_lastPos = ev->pos();
draw(ev->pos());
}
void PaintWidget::mouseMoveEvent(QMouseEvent * ev) {
draw(ev->pos());
}
void PaintWidget::mouseReleaseEvent(QMouseEvent *) {
maps.push_back(m_pixmap);
}
QPixmap PaintWidget::clearPixmap(QPixmap & pixmap){
QPixmap newPixmap{rect().size()};
QPainter painter{&newPixmap};
painter.fillRect(newPixmap.rect(), Qt::white);
painter.drawPixmap(0, 0, newPixmap);
pixmap = newPixmap;
return pixmap;
}
void PaintWidget::drawGrid(){
clearPixmap(m_grid);
QPainter painter{&m_grid};
painter.setRenderHint(QPainter::Antialiasing);
QPoint start(m_grid.width() / 2, -m_grid.height());
QPoint end(m_grid.width() / 2, m_grid.height() / 2);
drawWithPossibleRotations(start, end, &painter, false, m_nbParts);
}
void PaintWidget::draw(const QPoint & position) {
QPoint pos(position);
QPoint initPos(pos);
QPainter painter{&m_pixmap};
painter.setRenderHint(QPainter::Antialiasing);
if(m_showGrid){
drawGrid();
}
drawWithPossibleRotations(pos, m_lastPos, &painter, true, m_nbParts);
m_lastPos = initPos;
update();
}
void PaintWidget::drawWithPossibleRotations(QPoint & end, QPoint & start, QPainter *_painter, bool fromUser, int nbParts)
{
// save starting positions
QPoint initPos(end);
QPoint initLastPos(start);
if(m_mirror && !fromUser){
nbParts *= 2;
}
for(int k = 0; k < nbParts; k++)
{
if(fromUser){
_painter->setPen({brushScale[static_cast<unsigned int>(k)], m_penSize, Qt::SolidLine, Qt::RoundCap});
} else {
if(!m_mirror && nbParts == 1){
continue;
}
if(m_mirror && (k % 2 == 1 || (m_mirror && nbParts == 2))){
_painter->setPen({QColor(Qt::GlobalColor::gray), 3.0, Qt::SolidLine});
_painter->setOpacity(m_gridOpacity);
} else {
_painter->setPen({QColor(Qt::GlobalColor::gray), 3.0, Qt::DotLine});
_painter->setOpacity(m_gridOpacity);
}
}
// save starting positions
QPoint pos1(initPos);
QPoint last1(initLastPos);
// change reference
start.setX(last1.x() - m_pixmap.width() / 2);
start.setY(last1.y() - m_pixmap.height() / 2);
end.setX(pos1.x() - m_pixmap.width() / 2);
end.setY(pos1.y() - m_pixmap.height() / 2);
// save starting positions
QPoint pos2(end);
QPoint last2(start);
// rotate points
QPoint mirrorStart(start);
QPoint mirrorEnd(end);
if(m_mirror && fromUser)
{
k++;
mirrorStart.setX(static_cast<int>(-last2.x() * cos(2*k*M_PI/nbParts) + last2.y() * sin(2*k*M_PI/nbParts)));
mirrorStart.setY(static_cast<int>(-last2.y() * cos(2*k*M_PI/nbParts) - last2.x() * sin(2*k*M_PI/nbParts)));
mirrorEnd.setX(static_cast<int>(-pos2.x() * cos(2*k*M_PI/nbParts) + pos2.y() * sin(2*k*M_PI/nbParts)));
mirrorEnd.setY(static_cast<int>(-pos2.y() * cos(2*k*M_PI/nbParts) - pos2.x() * sin(2*k*M_PI/nbParts)));
k--;
}
start.setX(static_cast<int>(last2.x() * cos(2*k*M_PI/nbParts) - last2.y() * sin(2*k*M_PI/nbParts)));
start.setY(static_cast<int>(last2.y() * cos(2*k*M_PI/nbParts) + last2.x() * sin(2*k*M_PI/nbParts)));
end.setX(static_cast<int>(pos2.x() * cos(2*k*M_PI/nbParts) - pos2.y() * sin(2*k*M_PI/nbParts)));
end.setY(static_cast<int>(pos2.y() * cos(2*k*M_PI/nbParts) + pos2.x() * sin(2*k*M_PI/nbParts)));
if(m_mirror && fromUser)
{
mirrorStart.setX(mirrorStart.x() + m_pixmap.width() / 2);
mirrorStart.setY(m_pixmap.height() / 2 - mirrorStart.y());
mirrorEnd.setX(mirrorEnd.x() + m_pixmap.width() / 2);
mirrorEnd.setY(m_pixmap.height() / 2 - mirrorEnd.y());
_painter->drawLine(mirrorStart, mirrorEnd);
}
// reset reference to draw
start.setX(start.x() + m_pixmap.width() / 2);
start.setY(start.y() + m_pixmap.height() / 2);
end.setX(end.x() + m_pixmap.width() / 2);
end.setY(end.y() + m_pixmap.height() / 2);
// draw
_painter->drawLine(start, end);
}
}
void PaintWidget::clear(){
QPixmap newPixmap = clearPixmap(m_pixmap);
m_pixmap.fill(Qt::transparent);
//maps.clear(); // should it do this
maps.push_back(m_pixmap);
update();
}
void PaintWidget::undo(){
if(maps.size() > 1){
m_pixmap = maps[static_cast<unsigned int>(std::max(0, static_cast<int>(maps.size()-2)))];
undoed.push_back(maps[maps.size()-1]);
maps.erase(maps.begin() + static_cast<int>(maps.size()-1));
resizeMap(m_pixmap.width());
}
update();
}
void PaintWidget::redo(){
if(undoed.size() > 0){
m_pixmap = undoed[undoed.size()-1];
maps.push_back(m_pixmap);
undoed.erase(undoed.begin() + static_cast<int>(undoed.size()-1));
resizeMap(m_pixmap.width());
}
update();
}
void PaintWidget::changeColor(){
QColorDialog * colorDialog = new QColorDialog;
QColor c = colorDialog->getColor();
if(c.isValid()){
m_color = c;
toggleMulticolor(m_multicolor);
}
}
void PaintWidget::setPenSize(int size){
m_penSize = size;
}
void PaintWidget::changeSlices(int nb){
m_nbParts = nb;
toggleMulticolor(m_multicolor);
if(m_showGrid){
drawGrid();
}
update();
}
void PaintWidget::toggleMulticolor(bool toggled){
m_multicolor = toggled;
brushScale.clear();
double h;
int h1, s, v, a;
double golden_ratio = 0.618033988749895;
for (int i = 0; i < m_nbParts; i++)
{
h = golden_ratio * i * 360/m_nbParts;
m_color.getHsv(&h1, &s, &v, &a);
if(toggled){
h1 = (h1 + static_cast<int>(h)) % 360;
}
brushScale.push_back(QColor::fromHsv(h1, s, v, a));
}
}
void PaintWidget::setGridVisibility(int checkedEnum){
m_showGrid = checkedEnum;
if(m_showGrid){
drawGrid();
}else {
clearPixmap(m_grid);
}
update();
}
void PaintWidget::setGridOpacity(int opacity){
m_gridOpacity = opacity / 10.0;
if(m_showGrid){
drawGrid();
}
update();
}
void PaintWidget::resizeMap(int w){
resize (w, w);
m_pixmap = m_pixmap.scaled(w, w, Qt::KeepAspectRatio);
setFixedSize(w, w);
}
void PaintWidget::toggleMirror(int checkedEnum){
m_mirror = checkedEnum;
drawGrid();
update();
}
void PaintWidget::saveAs(){
if(m_keepAlphaChannel){
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::home().absolutePath(), tr("Images (*.png)"));
if(!fileName.isEmpty()){
m_saveFilePath = fileName;
}
m_pixmap.save(m_saveFilePath);
} else {
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::home().absolutePath(), tr("Images (*.png *.gif *.jpg)"));
if(!fileName.isEmpty()){
m_saveFilePath = fileName;
}
QPixmap newPixmap{rect().size()};
QPainter painter{&newPixmap};
painter.fillRect(newPixmap.rect(), Qt::white);
painter.drawPixmap(0, 0, m_pixmap);
painter.drawPixmap(0, 0, newPixmap);
newPixmap.save(m_saveFilePath);
}
if(!m_saveFilePath.isEmpty()){
m_isSaved = true;
emit saved(m_saveFilePath);
}
}
void PaintWidget::save(){
if(m_saveFilePath.isEmpty()){
saveAs();
}
else
{
m_pixmap.save(m_saveFilePath);
m_isSaved = true;
emit saved(m_saveFilePath);
}
}
void PaintWidget::setKeepAlphaChannel(bool checked){
m_keepAlphaChannel = checked;
}
bool PaintWidget::getSaveState(){
return m_isSaved;
}
void PaintWidget::openFile(){
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::home().absolutePath(), tr("Images (*.png *.gif *.jpg)"));
if(!fileName.isEmpty()){
QPixmap img(fileName);
m_pixmap = img;
m_saveFilePath = fileName;
resizeMap(m_pixmap.width());
emit saved(m_saveFilePath);
}
}