20 #include "layervector.h"
21 #include "colormanager.h"
22 #include "strokemanager.h"
23 #include "layermanager.h"
24 #include "pencilsettings.h"
26 #include "scribblearea.h"
32 PenTool::PenTool( QObject *parent ) :
StrokeTool( parent )
36 void PenTool::loadSettings()
38 m_enabledProperties[WIDTH] =
true;
39 m_enabledProperties[PRESSURE] =
true;
40 m_enabledProperties[VECTORMERGE] =
true;
41 m_enabledProperties[ANTI_ALIASING] =
true;
42 m_enabledProperties[INTERPOLATION] =
true;
44 QSettings settings( PENCIL2D, PENCIL2D );
46 properties.width = settings.value(
"penWidth" ).toDouble();
47 properties.pressure = settings.value(
"penPressure" ).toBool();
48 properties.invisibility = OFF;
49 properties.preserveAlpha = OFF;
50 properties.useAA = settings.value(
"brushAA").toBool();
51 properties.inpolLevel = 0;
54 if ( properties.width <= 0 )
60 mCurrentWidth = properties.width;
63 void PenTool::setWidth(
const qreal width)
66 properties.width = width;
69 QSettings settings( PENCIL2D, PENCIL2D );
70 settings.setValue(
"penWidth", width);
74 void PenTool::setPressure(
const bool pressure )
77 properties.pressure = pressure;
80 QSettings settings( PENCIL2D, PENCIL2D );
81 settings.setValue(
"penPressure", pressure);
85 void PenTool::setAA(
const int AA )
88 properties.useAA = AA;
91 QSettings settings( PENCIL2D, PENCIL2D );
92 settings.setValue(
"brushAA", AA);
96 void PenTool::setInpolLevel(
const int level)
98 properties.inpolLevel = level;
100 QSettings settings( PENCIL2D, PENCIL2D);
101 settings.setValue(
"lineInpol", level);
105 QCursor PenTool::cursor()
107 if ( mEditor->preference()->isOn( SETTING::TOOL_CURSOR ) )
109 return QCursor( QPixmap(
":icons/pen.png" ), -5, 0 );
111 return Qt::CrossCursor;
114 void PenTool::adjustPressureSensitiveProperties( qreal pressure,
bool mouseDevice )
116 mCurrentWidth = properties.width;
118 if ( properties.pressure && !mouseDevice )
120 mCurrentPressure = pressure;
124 mCurrentPressure = 1.0;
128 void PenTool::mousePressEvent( QMouseEvent *event )
130 if ( event->button() == Qt::LeftButton )
132 mEditor->backup( typeName() );
133 mScribbleArea->setAllDirty();
136 mMouseDownPoint = getCurrentPoint();
137 mLastBrushPoint = getCurrentPoint();
142 void PenTool::mouseReleaseEvent( QMouseEvent *event )
144 if ( event->button() == Qt::LeftButton )
146 Layer* layer = mEditor->layers()->currentLayer();
147 if ( mScribbleArea->isLayerPaintable() )
149 qreal distance = QLineF( getCurrentPoint(), mMouseDownPoint ).length();
152 paintAt(mMouseDownPoint);
160 if ( layer->type() == Layer::BITMAP ) {
163 else if (layer->type() == Layer::VECTOR )
165 paintVectorStroke( layer );
171 void PenTool::mouseMoveEvent( QMouseEvent *event )
173 Layer* layer = mEditor->layers()->currentLayer();
174 if ( layer->type() == Layer::BITMAP || layer->type() == Layer::VECTOR )
176 if ( event->buttons() & Qt::LeftButton )
179 if (properties.inpolLevel != m_pStrokeManager->getInpolLevel()) {
180 m_pStrokeManager->setInpolLevel(properties.inpolLevel);
188 void PenTool::paintAt( QPointF point )
190 qDebug() <<
"Made a single dab at " << point;
191 Layer* layer = mEditor->layers()->currentLayer();
192 if ( layer->type() == Layer::BITMAP )
194 mCurrentWidth = properties.width;
195 if (properties.pressure ==
true)
197 mCurrentWidth = properties.width;
199 qreal brushWidth = mCurrentWidth;
203 rect.extend( point.toPoint() );
204 mScribbleArea->drawPen( QPoint( qRound(point.x() ), qRound(point.y() )),
206 mEditor->color()->frontColor(),
209 int rad = qRound( brushWidth ) / 2 + 2;
211 mScribbleArea->refreshBitmap( rect, rad );
215 void PenTool::drawStroke()
217 StrokeTool::drawStroke();
220 Layer* layer = mEditor->layers()->currentLayer();
222 if ( layer->type() == Layer::BITMAP )
224 for (
int i = 0; i < p.size(); i++ )
226 p[ i ] = mEditor->view()->mapScreenToCanvas( p[ i ] );
229 mCurrentWidth = properties.width;
230 if (properties.pressure ==
true)
232 mCurrentWidth = properties.width * mCurrentPressure;
234 qreal brushWidth = mCurrentWidth;
238 qreal brushStep = ( 0.5 * brushWidth );
239 brushStep = qMax( 1.0, brushStep );
243 QPointF a = mLastBrushPoint;
244 QPointF b = getCurrentPoint();
246 qreal distance = 4 * QLineF( b, a ).length();
247 int steps = qRound( distance / brushStep );
249 for (
int i = 0; i < steps; i++ )
251 QPointF point = mLastBrushPoint + ( i + 1 ) * brushStep * ( getCurrentPoint() - mLastBrushPoint ) / distance;
252 rect.extend( point.toPoint() );
253 mScribbleArea->drawPen( QPoint( qRound(point.x() ), qRound(point.y() )),
255 mEditor->color()->frontColor(),
258 if ( i == ( steps - 1 ) )
260 mLastBrushPoint = getCurrentPoint();
264 int rad = qRound( brushWidth ) / 2 + 2;
266 mScribbleArea->paintBitmapBufferRect( rect );
267 mScribbleArea->refreshBitmap( rect, rad );
269 else if ( layer->type() == Layer::VECTOR )
271 qreal brushWidth = 0;
272 brushWidth = properties.width;
273 if (properties.pressure ==
true) {
274 brushWidth = properties.width * mCurrentPressure;
277 int rad = qRound( ( brushWidth / 2 + 2 ) * mEditor->view()->scaling() );
279 QPen pen( mEditor->color()->frontColor(),
280 brushWidth * mEditor->view()->scaling(),
287 QPainterPath path( p[ 0 ] );
288 path.cubicTo( p[ 1 ],
291 mScribbleArea->drawPath( path, pen, Qt::NoBrush, QPainter::CompositionMode_Source );
292 mScribbleArea->refreshVector( path.boundingRect().toRect(), rad );
297 void PenTool::paintBitmapStroke()
299 mScribbleArea->paintBitmapBuffer();
300 mScribbleArea->setAllDirty();
301 mScribbleArea->clearBitmapBuffer();
304 void PenTool::paintVectorStroke(
Layer* layer)
307 mScribbleArea->clearBitmapBuffer();
308 qreal tol = mScribbleArea->getCurveSmoothing() / mEditor->view()->scaling();
310 BezierCurve curve( mStrokePoints, mStrokePressures, tol );
311 curve.setWidth( properties.width );
312 curve.setFeather( properties.feather );
313 curve.setInvisibility( properties.invisibility );
314 curve.setVariableWidth( properties.pressure );
315 curve.setColourNumber( mEditor->color()->frontColorNumber() );
317 auto pLayerVector =
static_cast< LayerVector*
>( layer );
318 VectorImage* vectorImage = pLayerVector->getLastVectorImageAtFrame( mEditor->currentFrame(), 0 );
319 vectorImage->insertCurve( 0, curve, mEditor->view()->scaling(), false );
321 mScribbleArea->setModified( mEditor->layers()->currentLayerIndex(), mEditor->currentFrame() );
322 mScribbleArea->setAllDirty();