26 #include <QPainterPath>
27 #include "strokemanager.h"
31 StrokeManager::StrokeManager()
40 connect(&timer, &QTimer::timeout,
this, &StrokeManager::interpolatePollAndPaint);
43 void StrokeManager::reset()
45 mStrokeStarted =
false;
46 pressureQueue.clear();
54 void StrokeManager::setPressure(
float pressure)
56 mTabletPressure = pressure;
59 QPointF StrokeManager::getEventPosition(QMouseEvent* event)
70 pos =
event->pos() + mTabletPosition - mTabletPosition.toPoint();
76 pos =
event->localPos();
82 void StrokeManager::mousePressEvent(QMouseEvent* event)
85 if ( !(event->button() == Qt::NoButton) )
87 mLastPressPixel = getEventPosition(event);
89 mLastPixel = getEventPosition( event );
90 mCurrentPixel = getEventPosition( event );
92 mStrokeStarted =
true;
96 void StrokeManager::mouseReleaseEvent(QMouseEvent* event)
101 mouseMoveEvent(event);
104 mStrokeStarted =
false;
107 void StrokeManager::tabletEvent(QTabletEvent* event)
109 if (event->type() == QEvent::TabletPress) { mTabletInUse =
true; }
110 if (event->type() == QEvent::TabletRelease) { mTabletInUse =
false; }
112 mTabletPosition =
event->posF();
113 setPressure(event->pressure());
116 void StrokeManager::setInpolLevel(
int level)
121 void StrokeManager::mouseMoveEvent(QMouseEvent* event)
123 QPointF pos = getEventPosition(event);
126 if (mInpolLevel != -1){
130 mLastPixel = mCurrentPixel;
132 mLastInterpolated = mCurrentPixel;
137 void StrokeManager::smoothMousePos(QPointF pos)
143 if (mInpolLevel == 0) {
145 mLastPixel = mCurrentPixel;
147 mLastInterpolated = mCurrentPixel;
149 else if (mInpolLevel == 1) {
152 smoothPos = QPointF( ( pos.x() + mCurrentPixel.x() ) / 2.0, ( pos.y() + mCurrentPixel.y() ) / 2.0 );
153 mLastPixel = mCurrentPixel;
154 mCurrentPixel = smoothPos;
155 mLastInterpolated = mCurrentPixel;
158 while ( strokeQueue.size() >= STROKE_QUEUE_LENGTH )
160 strokeQueue.pop_front();
163 strokeQueue.push_back( smoothPos );
164 }
else if (mInpolLevel == 2 ) {
166 smoothPos = QPointF( ( pos.x() + mLastInterpolated.x() ) / 2.0, ( pos.y() + mLastInterpolated.y() ) / 2.0 );
168 mLastInterpolated = mCurrentPixel;
169 mCurrentPixel = smoothPos;
170 mLastPixel = mLastInterpolated;
175 if ( !mStrokeStarted )
187 QPointF StrokeManager::interpolateStart(QPointF firstPoint)
189 if (mInpolLevel == 1) {
192 pressureQueue.clear();
194 mSingleshotTime.start();
195 previousTime = mSingleshotTime.elapsed();
197 mLastPixel = firstPoint;
199 else if (mInpolLevel == 2){
201 mSingleshotTime.start();
202 previousTime = mSingleshotTime.elapsed();
208 pressureQueue.clear();
210 assert(sampleSize > 0);
213 for (
int i = sampleSize; i > 0; i--) {
214 strokeQueue.enqueue(firstPoint);
218 mLastInterpolated = firstPoint;
221 timer.setInterval(sampleSize);
223 }
else if (mInpolLevel == 0) {
226 pressureQueue.clear();
228 mLastPixel = firstPoint;
233 void StrokeManager::interpolatePoll()
236 strokeQueue.dequeue();
239 strokeQueue.enqueue(mLastInterpolated);
242 void StrokeManager::interpolatePollAndPaint()
245 if (!strokeQueue.isEmpty())
261 if (mInpolLevel == 1) {
263 result = tangentInpolOp(result);
266 else if (mInpolLevel == 2){
268 result = meanInpolOp(result, x, y, pressure);
270 }
else if (mInpolLevel == 0) {
272 result = noInpolOp(result);
280 setPressure(getPressure());
282 points << mLastPixel << mLastPixel << mCurrentPixel << mCurrentPixel;
286 mLastPixel = mCurrentPixel;
293 int time = mSingleshotTime.elapsed();
294 static const qreal smoothness = 1.f;
295 QLineF line( mLastPixel, mCurrentPixel);
297 qreal scaleFactor = line.length() * 3.f;
299 if ( !hasTangent && scaleFactor > 0.01f)
307 m_previousTangent = (mCurrentPixel - mLastPixel) * smoothness / (3.0 * scaleFactor);
309 QLineF _line(QPointF(0,0), m_previousTangent);
311 if (_line.length() < 2)
313 m_previousTangent = QPointF(0,0);
318 QPointF c1 = mLastPixel + m_previousTangent * scaleFactor;
319 QPointF newTangent = (mCurrentPixel - c1) * smoothness / (3.0 * scaleFactor);
321 if (scaleFactor == 0)
323 newTangent = QPointF(0,0);
333 QPointF c2 = mCurrentPixel - newTangent * scaleFactor;
336 points << mLastPixel << c1 << c2 << mCurrentPixel;
343 m_previousTangent = newTangent;
354 for (
int i = 0; i < strokeQueue.size(); i++) {
355 x += strokeQueue[i].x();
356 y += strokeQueue[i].y();
357 pressure += getPressure();
361 x /= strokeQueue.size();
362 y /= strokeQueue.size();
363 pressure /= strokeQueue.size();
366 QPointF mNewInterpolated = mLastInterpolated;
367 mNewInterpolated = QPointF(x,y);
369 points << mLastPixel << mLastInterpolated << mNewInterpolated << mCurrentPixel;
373 mLastPixel = mNewInterpolated;
378 void StrokeManager::interpolateEnd()
382 if (mInpolLevel == 2) {
383 if (!strokeQueue.isEmpty())
390 assert(sampleSize > 0);
391 for (
int i = sampleSize; i > 0; i--)