Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • tool
pentool.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17#include "pentool.h"
18
19#include <QPixmap>
20#include <QSettings>
21
22#include "vectorimage.h"
23#include "layervector.h"
24#include "colormanager.h"
25#include "layermanager.h"
26#include "viewmanager.h"
27#include "undoredomanager.h"
28#include "selectionmanager.h"
29#include "editor.h"
30#include "scribblearea.h"
31#include "blitrect.h"
32#include "pointerevent.h"
33
34
35PenTool::PenTool(QObject* parent) : StrokeTool(parent)
36{
37}
38
39void PenTool::loadSettings()
40{
41 StrokeTool::loadSettings();
42
43 mPropertyEnabled[WIDTH] = true;
44 mPropertyEnabled[PRESSURE] = true;
45 mPropertyEnabled[VECTORMERGE] = true;
46 mPropertyEnabled[ANTI_ALIASING] = true;
47 mPropertyEnabled[STABILIZATION] = true;
48
49 QSettings settings(PENCIL2D, PENCIL2D);
50
51 properties.width = settings.value("penWidth", 12.0).toDouble();
52 properties.pressure = settings.value("penPressure", true).toBool();
53 properties.invisibility = OFF;
54 properties.preserveAlpha = OFF;
55 properties.useAA = settings.value("penAA", true).toBool();
56 properties.stabilizerLevel = settings.value("penLineStabilization", StabilizationLevel::STRONG).toInt();
57
58 mQuickSizingProperties.insert(Qt::ShiftModifier, WIDTH);
59}
60
61void PenTool::saveSettings()
62{
63 QSettings settings(PENCIL2D, PENCIL2D);
64
65 settings.setValue("penWidth", properties.width);
66 settings.setValue("penPressure", properties.pressure);
67 settings.setValue("penAA", properties.useAA);
68 settings.setValue("penLineStabilization", properties.stabilizerLevel);
69
70 settings.sync();
71}
72
73void PenTool::resetToDefault()
74{
75 setWidth(12.0);
76 setUseFeather(false);
77 setPressure(true);
78 setStabilizerLevel(StabilizationLevel::STRONG);
79 setAA(1);
80}
81
82void PenTool::setWidth(const qreal width)
83{
84 // Set current property
85 properties.width = width;
86}
87
88void PenTool::setPressure(const bool pressure)
89{
90 // Set current property
91 properties.pressure = pressure;
92}
93
94void PenTool::setAA(const int AA)
95{
96 // Set current property
97 properties.useAA = AA;
98
99}
100
101void PenTool::setStabilizerLevel(const int level)
102{
103 properties.stabilizerLevel = level;
104}
105
106QCursor PenTool::cursor()
107{
108 if (mEditor->preference()->isOn(SETTING::TOOL_CURSOR))
109 {
110 return QCursor(QPixmap(":icons/general/cursor-pen.svg"), 5, 14);
111 }
112 return QCursor(QPixmap(":icons/general/cross.png"), 10, 10);
113}
114
115void PenTool::pointerPressEvent(PointerEvent *event)
116{
117 mInterpolator.pointerPressEvent(event);
118 if (handleQuickSizing(event)) {
119 return;
120 }
121
122 mMouseDownPoint = getCurrentPoint();
123 mLastBrushPoint = getCurrentPoint();
124
125 startStroke(event->inputType());
126
127 StrokeTool::pointerPressEvent(event);
128}
129
130void PenTool::pointerMoveEvent(PointerEvent* event)
131{
132 mInterpolator.pointerMoveEvent(event);
133 if (handleQuickSizing(event)) {
134 return;
135 }
136
137 if (event->buttons() & Qt::LeftButton && event->inputType() == mCurrentInputType)
138 {
139 mCurrentPressure = mInterpolator.getPressure();
140 drawStroke();
141 if (properties.stabilizerLevel != mInterpolator.getStabilizerLevel())
142 {
143 mInterpolator.setStabilizerLevel(properties.stabilizerLevel);
144 }
145 }
146
147 StrokeTool::pointerMoveEvent(event);
148}
149
150void PenTool::pointerReleaseEvent(PointerEvent *event)
151{
152 mInterpolator.pointerReleaseEvent(event);
153 if (handleQuickSizing(event)) {
154 return;
155 }
156
157 if (event->inputType() != mCurrentInputType) return;
158
159 mEditor->backup(typeName());
160
161 Layer* layer = mEditor->layers()->currentLayer();
162
163 qreal distance = QLineF(getCurrentPoint(), mMouseDownPoint).length();
164 if (distance < 1)
165 {
166 paintAt(mMouseDownPoint);
167 }
168 else
169 {
170 drawStroke();
171 }
172
173 if (layer->type() == Layer::VECTOR) {
174 paintVectorStroke(layer);
175 }
176 endStroke();
177
178 StrokeTool::pointerReleaseEvent(event);
179}
180
181// draw a single paint dab at the given location
182void PenTool::paintAt(QPointF point)
183{
184 Layer* layer = mEditor->layers()->currentLayer();
185 if (layer->type() == Layer::BITMAP)
186 {
187 qreal pressure = (properties.pressure) ? mCurrentPressure : 1.0;
188 qreal brushWidth = properties.width * pressure;
189 mCurrentWidth = brushWidth;
190
191 mScribbleArea->drawPen(point,
192 brushWidth,
193 mEditor->color()->frontColor(),
194 properties.useAA);
195 }
196}
197
198void PenTool::drawStroke()
199{
200 StrokeTool::drawStroke();
201 QList<QPointF> p = mInterpolator.interpolateStroke();
202
203 Layer* layer = mEditor->layers()->currentLayer();
204
205 if (layer->type() == Layer::BITMAP)
206 {
207 qreal pressure = (properties.pressure) ? mCurrentPressure : 1.0;
208 qreal brushWidth = properties.width * pressure;
209 mCurrentWidth = brushWidth;
210
211 // TODO: Make popup widget for less important properties,
212 // Eg. stepsize should be a slider.. will have fixed (0.3) value for now.
213 qreal brushStep = (0.5 * brushWidth);
214 brushStep = qMax(1.0, brushStep);
215
216 QPointF a = mLastBrushPoint;
217 QPointF b = getCurrentPoint();
218
219 qreal distance = 4 * QLineF(b, a).length();
220 int steps = qRound(distance / brushStep);
221
222 for (int i = 0; i < steps; i++)
223 {
224 QPointF point = mLastBrushPoint + (i + 1) * brushStep * (getCurrentPoint() - mLastBrushPoint) / distance;
225 mScribbleArea->drawPen(point,
226 brushWidth,
227 mEditor->color()->frontColor(),
228 properties.useAA);
229
230 if (i == (steps - 1))
231 {
232 mLastBrushPoint = getCurrentPoint();
233 }
234 }
235 }
236 else if (layer->type() == Layer::VECTOR)
237 {
238 qreal pressure = (properties.pressure) ? mCurrentPressure : 1.0;
239 qreal brushWidth = properties.width * pressure;
240
241 QPen pen(mEditor->color()->frontColor(),
242 brushWidth,
243 Qt::SolidLine,
244 Qt::RoundCap,
245 Qt::RoundJoin);
246
247 if (p.size() == 4)
248 {
249 QPainterPath path(p[0]);
250 path.cubicTo(p[1], p[2], p[3]);
251 mScribbleArea->drawPath(path, pen, Qt::NoBrush, QPainter::CompositionMode_Source);
252 }
253 }
254}
255
256void PenTool::paintVectorStroke(Layer* layer)
257{
258 if (mStrokePoints.empty())
259 return;
260
261 // Clear the temporary pixel path
262 mScribbleArea->clearDrawingBuffer();
263 qreal tol = mScribbleArea->getCurveSmoothing() / mEditor->view()->scaling();
264
265 BezierCurve curve(mStrokePoints, mStrokePressures, tol);
266 curve.setWidth(properties.width);
267 curve.setFeather(properties.feather);
268 curve.setFilled(false);
269 curve.setInvisibility(properties.invisibility);
270 curve.setVariableWidth(properties.pressure);
271 curve.setColorNumber(mEditor->color()->frontColorNumber());
272
273 auto pLayerVector = static_cast<LayerVector*>(layer);
274 VectorImage* vectorImage = pLayerVector->getLastVectorImageAtFrame(mEditor->currentFrame(), 0);
275 if (vectorImage == nullptr) { return; } // Can happen if the first frame is deleted while drawing
276 vectorImage->addCurve(curve, mEditor->view()->scaling(), false);
277
278 if (vectorImage->isAnyCurveSelected() || mEditor->select()->somethingSelected())
279 {
280 mEditor->deselectAll();
281 }
282
283 vectorImage->setSelected(vectorImage->getLastCurveNumber(), true);
284
285 mEditor->setModified(mEditor->layers()->currentLayerIndex(), mEditor->currentFrame());
286}
BezierCurve
Definition: beziercurve.h:34
ColorManager::frontColor
QColor frontColor(bool useIndexedColor=true)
frontColor
Definition: colormanager.cpp:61
Layer
Definition: layer.h:33
LayerVector
Definition: layervector.h:26
PointerEvent
Definition: pointerevent.h:8
StrokeTool
Definition: stroketool.h:34
StrokeTool::loadSettings
void loadSettings() override
Definition: stroketool.cpp:58
VectorImage
Definition: vectorimage.h:32
VectorImage::setSelected
void setSelected(int curveNumber, bool YesOrNo)
VectorImage::setSelected.
Definition: vectorimage.cpp:616
VectorImage::getLastCurveNumber
int getLastCurveNumber()
VectorImage::getLastCurveNumber.
Definition: vectorimage.cpp:2264
VectorImage::addCurve
void addCurve(BezierCurve &newCurve, qreal factor, bool interacts=true)
VectorImage::addCurve.
Definition: vectorimage.cpp:341
VectorImage::isAnyCurveSelected
bool isAnyCurveSelected()
VectorImage::isAnyCurveSelected.
Definition: vectorimage.cpp:826
QCursor
QHash::insert
QHash::iterator insert(const Key &key, const T &value)
QLineF
QLineF::length
qreal length() const const
QList
QList::empty
bool empty() const const
QList::size
int size() const const
QObject
QObject::event
virtual bool event(QEvent *e)
QPainter::CompositionMode_Source
CompositionMode_Source
QPainterPath
QPen
QPixmap
QPointF
QSettings
Qt::NoBrush
NoBrush
Qt::ShiftModifier
ShiftModifier
Qt::LeftButton
LeftButton
Qt::RoundCap
RoundCap
Qt::RoundJoin
RoundJoin
Qt::SolidLine
SolidLine
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39