-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathbasetool.cpp
469 lines (394 loc) · 12.4 KB
/
basetool.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
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
/*
Pencil2D - Traditional Animation Software
Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
Copyright (C) 2012-2020 Matthew Chiawen Chang
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "basetool.h"
#include <array>
#include <QtMath>
#include <QPixmap>
#include "editor.h"
#include "viewmanager.h"
#include "toolmanager.h"
#include "scribblearea.h"
#include "strokemanager.h"
#include "pointerevent.h"
// ---- shared static variables ---- ( only one instance for all the tools )
qreal BaseTool::msOriginalPropertyValue; // start value (width, feather ..)
bool BaseTool::msIsAdjusting = false;
QString BaseTool::TypeName(ToolType type)
{
static std::array<QString, TOOL_TYPE_COUNT> map;
if (map[0].isEmpty())
{
map[PENCIL] = tr("Pencil");
map[ERASER] = tr("Eraser");
map[SELECT] = tr("Select");
map[MOVE] = tr("Move");
map[HAND] = tr("Hand");
map[SMUDGE] = tr("Smudge");
map[PEN] = tr("Pen");
map[POLYLINE] = tr("Polyline");
map[BUCKET] = tr("Bucket");
map[EYEDROPPER] = tr("Eyedropper");
map[BRUSH] = tr("Brush");
}
return map.at(type);
}
BaseTool::BaseTool(QObject* parent) : QObject(parent)
{
mPropertyEnabled.insert(WIDTH, false);
mPropertyEnabled.insert(FEATHER, false);
mPropertyEnabled.insert(USEFEATHER, false);
mPropertyEnabled.insert(PRESSURE, false);
mPropertyEnabled.insert(INVISIBILITY, false);
mPropertyEnabled.insert(PRESERVEALPHA, false);
mPropertyEnabled.insert(BEZIER, false);
mPropertyEnabled.insert(ANTI_ALIASING, false);
mPropertyEnabled.insert(FILL_MODE, false);
mPropertyEnabled.insert(STABILIZATION, false);
mPropertyEnabled.insert(CAMERAPATH, false);
}
QCursor BaseTool::cursor()
{
return Qt::ArrowCursor;
}
void BaseTool::initialize(Editor* editor)
{
Q_ASSERT(editor);
mEditor = editor;
mScribbleArea = editor->getScribbleArea();
Q_ASSERT(mScribbleArea);
mStrokeManager = mEditor->getScribbleArea()->getStrokeManager();
loadSettings();
}
void BaseTool::pointerPressEvent(PointerEvent* event)
{
event->accept();
}
void BaseTool::pointerMoveEvent(PointerEvent* event)
{
event->accept();
}
void BaseTool::pointerReleaseEvent(PointerEvent* event)
{
event->accept();
}
void BaseTool::pointerDoubleClickEvent(PointerEvent* event)
{
pointerPressEvent(event);
}
/**
* @brief BaseTool::isDrawingTool - A drawing tool is anything that applies something to the canvas.
* SELECT and MOVE does not count here because they modify already applied content.
* @return true if not a drawing tool and false otherwise
*/
bool BaseTool::isDrawingTool()
{
if (type() == ToolType::HAND || type() == ToolType::MOVE || type() == ToolType::CAMERA || type() == ToolType::SELECT )
{
return false;
}
return true;
}
/**
* @brief precision circular cursor: used for drawing a cursor within scribble area.
* @return QPixmap
*/
QPixmap BaseTool::canvasCursor(float width, float feather, bool useFeather, float scalingFac, int windowWidth)
{
float propWidth = width * scalingFac;
float propFeather = feather * scalingFac;
float cursorWidth = 0.0f;
float xyA = 0.0f;
float xyB = 0.0f;
float whA = 0.0f;
float whB = 0.0f;
if (useFeather)
{
cursorWidth = propWidth + 0.5 * propFeather;
xyA = 1 + propFeather / 2;
xyB = 1 + propFeather / 8;
whA = qMax<float>(0, propWidth - xyA - 1);
whB = qMax<float>(0, cursorWidth - propFeather / 4 - 2);
}
else
{
cursorWidth = (propWidth + 0.5);
whA = qMax<float>(0, propWidth - 1);
whB = qMax<float>(0, cursorWidth / 4 - 2);
}
float radius = cursorWidth / 2;
// deallocate when cursor width gets some value larger than the widget
if (cursorWidth > windowWidth * 2)
{
return QPixmap(0, 0);
}
if (cursorWidth < 1) { cursorWidth = 1; }
QPixmap cursorPixmap = QPixmap(cursorWidth, cursorWidth);
if (!cursorPixmap.isNull())
{
cursorPixmap.fill(QColor(255, 255, 255, 0));
QPainter cursorPainter(&cursorPixmap);
QPen cursorPen = cursorPainter.pen();
cursorPainter.setRenderHint(QPainter::Antialiasing);
// Draw cross in center
cursorPen.setStyle(Qt::SolidLine);
cursorPen.setColor(QColor(0, 0, 0, 127));
cursorPainter.setPen(cursorPen);
cursorPainter.drawLine(QPointF(radius - 2, radius), QPointF(radius + 2, radius));
cursorPainter.drawLine(QPointF(radius, radius - 2), QPointF(radius, radius + 2));
// Draw outer circle
if (useFeather)
{
cursorPen.setStyle(Qt::DotLine);
cursorPen.setColor(QColor(0, 0, 0, 255));
cursorPainter.setPen(cursorPen);
cursorPainter.drawEllipse(QRectF(xyB, xyB, whB, whB));
cursorPen.setDashOffset(4);
cursorPen.setColor(QColor(255, 255, 255, 255));
cursorPainter.setPen(cursorPen);
cursorPainter.drawEllipse(QRectF(xyB, xyB, whB, whB));
}
// Draw inner circle
cursorPen.setStyle(Qt::DotLine);
cursorPen.setColor(QColor(0, 0, 0, 255));
cursorPainter.setPen(cursorPen);
cursorPainter.drawEllipse(QRectF(xyA, xyA, whA, whA));
cursorPen.setDashOffset(4);
cursorPen.setColor(QColor(255, 255, 255, 255));
cursorPainter.setPen(cursorPen);
cursorPainter.drawEllipse(QRectF(xyA, xyA, whA, whA));
cursorPainter.end();
}
return cursorPixmap;
}
bool BaseTool::isActive()
{
return strokeManager()->isActive();
}
/**
* @brief precision circular cursor: used for drawing stroke size while adjusting
* @return QPixmap
*/
QPixmap BaseTool::quickSizeCursor(qreal scalingFac)
{
qreal propSize = qMax(0., properties.width) * scalingFac;
qreal propFeather = qMax(0., properties.feather) * scalingFac;
QRectF cursorRect(0, 0, propSize+2, propSize+2);
QRectF sizeRect = cursorRect.adjusted(1, 1, -1, -1);
qreal featherRadius = (1 - propFeather / 100) * propSize / 2.;
QPixmap cursorPixmap = QPixmap(cursorRect.size().toSize());
if (!cursorPixmap.isNull())
{
cursorPixmap.fill(QColor(255, 255, 255, 0));
QPainter cursorPainter(&cursorPixmap);
cursorPainter.setRenderHints(QPainter::Antialiasing, true);
// Draw width (outside circle)
cursorPainter.setPen(QColor(255, 127, 127, 127));
cursorPainter.setBrush(QColor(0, 255, 127, 127));
cursorPainter.drawEllipse(sizeRect);
// Draw feather (inside circle)
cursorPainter.setCompositionMode(QPainter::CompositionMode_Darken);
cursorPainter.setPen(QColor(0, 0, 0, 0));
cursorPainter.setBrush(QColor(0, 191, 95, 127));
cursorPainter.drawEllipse(cursorRect.center(), featherRadius, featherRadius);
// Draw cursor in center
cursorPainter.setRenderHints(QPainter::Antialiasing, false);
cursorPainter.setPen(QColor(0, 0, 0, 255));
cursorPainter.drawLine(cursorRect.center() - QPoint(2, 0), cursorRect.center() + QPoint(2, 0));
cursorPainter.drawLine(cursorRect.center() - QPoint(0, 2), cursorRect.center() + QPoint(0, 2));
cursorPainter.end();
}
return cursorPixmap;
}
bool BaseTool::startAdjusting(Qt::KeyboardModifiers modifiers, qreal step)
{
if (mQuickSizingProperties.contains(modifiers))
{
switch (mQuickSizingProperties.value(modifiers)) {
case WIDTH:
msOriginalPropertyValue = properties.width;
break;
case FEATHER:
msOriginalPropertyValue = properties.feather;
break;
case TOLERANCE:
msOriginalPropertyValue = properties.tolerance;
break;
default:
qDebug() << "Unhandled quick sizing property for tool" << typeName();
Q_ASSERT(false);
return false;
}
msIsAdjusting = true;
mAdjustmentStep = step;
mScribbleArea->updateCanvasCursor();
return true;
}
return false;
}
void BaseTool::stopAdjusting()
{
msIsAdjusting = false;
mAdjustmentStep = 0;
msOriginalPropertyValue = 0;
mEditor->getScribbleArea()->updateCanvasCursor();
}
void BaseTool::adjustCursor(Qt::KeyboardModifiers modifiers)
{
qreal inc = qPow(msOriginalPropertyValue * 100, 0.5);
qreal newValue = inc + getCurrentPoint().x();
if (newValue < 0)
{
newValue = 0;
}
newValue = qPow(newValue, 2) / 100;
if (mAdjustmentStep > 0)
{
int tempValue = static_cast<int>(newValue / mAdjustmentStep); // + 0.5 ?
newValue = tempValue * mAdjustmentStep;
}
switch (mQuickSizingProperties.value(modifiers))
{
case WIDTH:
mEditor->tools()->setWidth(qBound(1., newValue, 200.));
break;
case FEATHER:
mEditor->tools()->setFeather(qBound(2., newValue, 200.));
break;
case TOLERANCE:
mEditor->tools()->setTolerance(qBound(0., newValue, 100.));
break;
default:
qDebug() << "Unhandled quick sizing property for tool" << typeName();
Q_ASSERT(false);
break;
}
}
QPointF BaseTool::getCurrentPressPixel() const
{
return strokeManager()->getCurrentPressPixel();
}
QPointF BaseTool::getCurrentPressPoint() const
{
return mEditor->view()->mapScreenToCanvas(strokeManager()->getCurrentPressPixel());
}
QPointF BaseTool::getCurrentPixel() const
{
return strokeManager()->getCurrentPixel();
}
QPointF BaseTool::getCurrentPoint() const
{
return mEditor->view()->mapScreenToCanvas(getCurrentPixel());
}
QPointF BaseTool::getLastPixel() const
{
return strokeManager()->getLastPixel();
}
QPointF BaseTool::getLastPoint() const
{
return mEditor->view()->mapScreenToCanvas(getLastPixel());
}
QPointF BaseTool::getLastPressPixel() const
{
return strokeManager()->getLastPressPixel();
}
QPointF BaseTool::getLastPressPoint() const
{
return mEditor->view()->mapScreenToCanvas(getLastPressPixel());
}
void BaseTool::setWidth(const qreal width)
{
properties.width = width;
}
void BaseTool::setFeather(const qreal feather)
{
properties.feather = feather;
}
void BaseTool::setUseFeather(const bool usingFeather)
{
properties.useFeather = usingFeather;
}
void BaseTool::setInvisibility(const bool invisibility)
{
properties.invisibility = invisibility;
}
void BaseTool::setBezier(const bool _bezier_state)
{
properties.bezier_state = _bezier_state;
}
void BaseTool::setPressure(const bool pressure)
{
properties.pressure = pressure;
}
void BaseTool::setPreserveAlpha(const bool preserveAlpha)
{
properties.preserveAlpha = preserveAlpha;
}
void BaseTool::setVectorMergeEnabled(const bool vectorMergeEnabled)
{
properties.vectorMergeEnabled = vectorMergeEnabled;
}
void BaseTool::setAA(const int useAA)
{
properties.useAA = useAA;
}
void BaseTool::setFillMode(const int mode)
{
properties.fillMode = mode;
}
void BaseTool::setStabilizerLevel(const int level)
{
properties.stabilizerLevel = level;
}
void BaseTool::setTolerance(const int tolerance)
{
properties.tolerance = tolerance;
}
void BaseTool::setToleranceEnabled(const bool enabled)
{
properties.toleranceEnabled = enabled;
}
void BaseTool::setFillExpand(const int fillExpandValue)
{
properties.bucketFillExpand = fillExpandValue;
}
void BaseTool::setFillReferenceMode(int referenceMode)
{
properties.bucketFillReferenceMode = referenceMode;
}
void BaseTool::setFillExpandEnabled(const bool enabled)
{
properties.bucketFillExpandEnabled = enabled;
}
void BaseTool::setUseFillContour(const bool useFillContour)
{
properties.useFillContour = useFillContour;
}
void BaseTool::setAutoSwitchTool(const bool autoSwitch)
{
properties.autoSwitchTool = autoSwitch;
}
void BaseTool::setShowSelectionInfo(const bool b)
{
properties.showSelectionInfo = b;
}
void BaseTool::setShowCameraPath(const bool showCameraPath)
{
properties.cameraShowPath = showCameraPath;
}
void BaseTool::setPathDotColorType(const DotColorType dotColorType)
{
properties.cameraPathDotColorType = dotColorType;
}
void BaseTool::resetCameraPath()
{
}