Index: libs/ui/tool/KisStabilizerDelayedPaintHelper.h =================================================================== --- libs/ui/tool/KisStabilizerDelayedPaintHelper.h +++ libs/ui/tool/KisStabilizerDelayedPaintHelper.h @@ -19,9 +19,9 @@ #ifndef KIS_STABILIZER_DELAYED_PAINT_HELPER_H #define KIS_STABILIZER_DELAYED_PAINT_HELPER_H +#include #include #include -#include #include #include @@ -40,14 +40,14 @@ TimedPaintInfo(int elapsedTime, KisPaintInformation paintInfo); }; - QTimer m_paintTimer; + QBasicTimer m_paintTimer; QQueue m_paintQueue; int m_lastPendingTime; + int m_paintInterval; QElapsedTimer m_elapsedTimer; // Callbacks std::function m_paintLine; - std::function m_requestUpdateOutline; public: KisStabilizerDelayedPaintHelper(); @@ -70,17 +70,14 @@ m_paintLine = paintLine; } - void setUpdateOutlineCallback(std::function requestUpdateOutline) { - m_requestUpdateOutline = requestUpdateOutline; - } - void start(int paintInterval, const KisPaintInformation &firstPaintInfo); void update(const QVector &newPaintInfos); + void paintSome(); void end(); void cancel(); -private Q_SLOTS: - void stabilizerDelayedPaint(bool isEndStroke = false); +protected: + virtual void timerEvent(QTimerEvent *event) override; }; #endif // KIS_STABILIZER_DELAYED_PAINT_HELPER_H Index: libs/ui/tool/KisStabilizerDelayedPaintHelper.cpp =================================================================== --- libs/ui/tool/KisStabilizerDelayedPaintHelper.cpp +++ libs/ui/tool/KisStabilizerDelayedPaintHelper.cpp @@ -18,6 +18,8 @@ #include "KisStabilizerDelayedPaintHelper.h" +#include + KisStabilizerDelayedPaintHelper::TimedPaintInfo::TimedPaintInfo(int elapsedTime, KisPaintInformation paintInfo) : elapsedTime(elapsedTime) , paintInfo(paintInfo) @@ -26,15 +28,16 @@ KisStabilizerDelayedPaintHelper::KisStabilizerDelayedPaintHelper() { - connect(&m_paintTimer, SIGNAL(timeout()), SLOT(stabilizerDelayedPaint())); } void KisStabilizerDelayedPaintHelper::start(int paintInterval, const KisPaintInformation &firstPaintInfo) { if (running()) { cancel(); } - m_paintTimer.setInterval(paintInterval); - m_paintTimer.start(); + m_paintInterval = paintInterval; + // No need to specify Qt::PreciseTimer. + // Delayed strokes are also painted from KisToolFreehandHelper::paint + m_paintTimer.start(paintInterval, Qt::CoarseTimer, this); m_elapsedTimer.start(); m_lastPendingTime = m_elapsedTimer.elapsed(); m_paintQueue.enqueue(TimedPaintInfo(m_lastPendingTime, firstPaintInfo)); @@ -51,9 +54,31 @@ m_lastPendingTime = now; } +void KisStabilizerDelayedPaintHelper::paintSome() { + // Restart timer + m_paintTimer.start(m_paintInterval, Qt::CoarseTimer, this); + if (m_paintQueue.isEmpty()) { + return; + } + int now = m_elapsedTimer.elapsed(); + // Always keep one in the queue since painting requires two points + while (m_paintQueue.size() > 1 && m_paintQueue.head().elapsedTime <= now) { + const TimedPaintInfo dequeued = m_paintQueue.dequeue(); + m_paintLine(dequeued.paintInfo, m_paintQueue.head().paintInfo); + } +} + void KisStabilizerDelayedPaintHelper::end() { - stabilizerDelayedPaint(true); m_paintTimer.stop(); + if (m_paintQueue.isEmpty()) { + return; + } + TimedPaintInfo dequeued = m_paintQueue.dequeue(); + while (!m_paintQueue.isEmpty()) { + const TimedPaintInfo dequeued2 = m_paintQueue.dequeue(); + m_paintLine(dequeued.paintInfo, dequeued2.paintInfo); + dequeued = dequeued2; + } } void KisStabilizerDelayedPaintHelper::cancel() { @@ -61,25 +86,8 @@ m_paintQueue.clear(); } -void KisStabilizerDelayedPaintHelper::stabilizerDelayedPaint(bool isEndStroke) { - if (m_paintQueue.isEmpty()) { - return; - } - if (isEndStroke) { - TimedPaintInfo dequeued = m_paintQueue.dequeue(); - while (!m_paintQueue.isEmpty()) { - const TimedPaintInfo dequeued2 = m_paintQueue.dequeue(); - m_paintLine(dequeued.paintInfo, dequeued2.paintInfo); - dequeued = dequeued2; - } - m_requestUpdateOutline(); - return; - } - int now = m_elapsedTimer.elapsed(); - // Always keep one in the queue since painting requires two points - while (m_paintQueue.size() > 1 && m_paintQueue.head().elapsedTime <= now) { - const TimedPaintInfo dequeued = m_paintQueue.dequeue(); - m_paintLine(dequeued.paintInfo, m_paintQueue.head().paintInfo); +void KisStabilizerDelayedPaintHelper::timerEvent(QTimerEvent *event) { + if (event->timerId() == m_paintTimer.timerId()) { + paintSome(); } - m_requestUpdateOutline(); } Index: libs/ui/tool/kis_tool_freehand_helper.h =================================================================== --- libs/ui/tool/kis_tool_freehand_helper.h +++ libs/ui/tool/kis_tool_freehand_helper.h @@ -137,6 +137,8 @@ const QPointF &control2, const KisPaintInformation &pi2); + virtual void timerEvent(QTimerEvent *event) override; + private: void paint(KisPaintInformation &info); void paintBezierSegment(KisPaintInformation pi1, KisPaintInformation pi2, Index: libs/ui/tool/kis_tool_freehand_helper.cpp =================================================================== --- libs/ui/tool/kis_tool_freehand_helper.cpp +++ libs/ui/tool/kis_tool_freehand_helper.cpp @@ -18,6 +18,7 @@ #include "kis_tool_freehand_helper.h" +#include #include #include @@ -97,7 +98,7 @@ // Stabilizer data QQueue stabilizerDeque; - QTimer stabilizerPollTimer; + QBasicTimer stabilizerPollTimer; KisStabilizedEventsSampler stabilizedSampler; KisStabilizerDelayedPaintHelper stabilizerDelayedPaintHelper; @@ -124,16 +125,11 @@ m_d->strokeTimeoutTimer.setSingleShot(true); connect(&m_d->strokeTimeoutTimer, SIGNAL(timeout()), SLOT(finishStroke())); connect(&m_d->airbrushingTimer, SIGNAL(timeout()), SLOT(doAirbrushing())); - connect(&m_d->stabilizerPollTimer, SIGNAL(timeout()), SLOT(stabilizerPollAndPaint())); m_d->stabilizerDelayedPaintHelper.setPaintLineCallback( [this](const KisPaintInformation &pi1, const KisPaintInformation &pi2) { paintLine(pi1, pi2); }); - m_d->stabilizerDelayedPaintHelper.setUpdateOutlineCallback( - [this]() { - emit requestExplicitUpdateOutline(); - }); } KisToolFreehandHelper::~KisToolFreehandHelper() @@ -586,6 +582,10 @@ if (m_d->smoothingOptions->smoothingType() == KisSmoothingOptions::STABILIZER) { m_d->stabilizedSampler.addEvent(info); + if (m_d->stabilizerDelayedPaintHelper.running()) { + // Paint here so we don't have to rely on the timer + m_d->stabilizerDelayedPaintHelper.paintSome(); + } } else { m_d->previousPaintInformation = info; } @@ -678,8 +678,7 @@ // Poll and draw regularly KisConfig cfg; int stabilizerSampleSize = cfg.stabilizerSampleSize(); - m_d->stabilizerPollTimer.setInterval(stabilizerSampleSize); - m_d->stabilizerPollTimer.start(); + m_d->stabilizerPollTimer.start(stabilizerSampleSize, Qt::PreciseTimer, this); int delayedPaintInterval = cfg.stabilizerDelayedPaintInterval(); if (delayedPaintInterval < stabilizerSampleSize) { @@ -976,3 +975,9 @@ m_d->canvasMirroredH = mirrored; } +void KisToolFreehandHelper::timerEvent(QTimerEvent *event) +{ + if (event->timerId() == m_d->stabilizerPollTimer.timerId()) { + stabilizerPollAndPaint(); + } +}