Changeset View
Changeset View
Standalone View
Standalone View
libs/ui/tool/KisStabilizerDelayedPaintHelper.cpp
| Show All 12 Lines | |||||
| 13 | * | 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program; if not, write to the Free Software | 15 | * along with this program; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 17 | */ | 17 | */ | ||
| 18 | 18 | | |||
| 19 | #include "KisStabilizerDelayedPaintHelper.h" | 19 | #include "KisStabilizerDelayedPaintHelper.h" | ||
| 20 | 20 | | |||
| 21 | constexpr int fixedPaintTimerInterval = 20; | ||||
| 22 | | ||||
| 21 | KisStabilizerDelayedPaintHelper::TimedPaintInfo::TimedPaintInfo(int elapsedTime, KisPaintInformation paintInfo) | 23 | KisStabilizerDelayedPaintHelper::TimedPaintInfo::TimedPaintInfo(int elapsedTime, KisPaintInformation paintInfo) | ||
| 22 | : elapsedTime(elapsedTime) | 24 | : elapsedTime(elapsedTime) | ||
| 23 | , paintInfo(paintInfo) | 25 | , paintInfo(paintInfo) | ||
| 24 | { | 26 | { | ||
| 25 | } | 27 | } | ||
| 26 | 28 | | |||
| 27 | KisStabilizerDelayedPaintHelper::KisStabilizerDelayedPaintHelper() | 29 | KisStabilizerDelayedPaintHelper::KisStabilizerDelayedPaintHelper() | ||
| 28 | { | 30 | { | ||
| 29 | connect(&m_paintTimer, SIGNAL(timeout()), SLOT(stabilizerDelayedPaint())); | 31 | connect(&m_paintTimer, SIGNAL(timeout()), SLOT(stabilizerDelayedPaintTimer())); | ||
| 30 | } | 32 | } | ||
| 31 | 33 | | |||
| 32 | void KisStabilizerDelayedPaintHelper::start(int paintInterval, const KisPaintInformation &firstPaintInfo) { | 34 | void KisStabilizerDelayedPaintHelper::start(const KisPaintInformation &firstPaintInfo) { | ||
| 33 | if (running()) { | 35 | if (running()) { | ||
| 34 | cancel(); | 36 | cancel(); | ||
| 35 | } | 37 | } | ||
| 36 | m_paintTimer.setInterval(paintInterval); | 38 | m_paintTimer.setInterval(fixedPaintTimerInterval); | ||
| 37 | m_paintTimer.start(); | 39 | m_paintTimer.start(); | ||
| 38 | m_elapsedTimer.start(); | 40 | m_elapsedTimer.start(); | ||
| 39 | m_lastPendingTime = m_elapsedTimer.elapsed(); | 41 | m_lastPendingTime = m_elapsedTimer.elapsed(); | ||
| 42 | m_lastPaintTime = m_lastPendingTime; | ||||
| 40 | m_paintQueue.enqueue(TimedPaintInfo(m_lastPendingTime, firstPaintInfo)); | 43 | m_paintQueue.enqueue(TimedPaintInfo(m_lastPendingTime, firstPaintInfo)); | ||
| 41 | } | 44 | } | ||
| 42 | 45 | | |||
| 43 | void KisStabilizerDelayedPaintHelper::update(const QVector<KisPaintInformation> &newPaintInfos) { | 46 | void KisStabilizerDelayedPaintHelper::update(const QVector<KisPaintInformation> &newPaintInfos) { | ||
| 44 | int now = m_elapsedTimer.elapsed(); | 47 | int now = m_elapsedTimer.elapsed(); | ||
| 45 | int delayedPaintInterval = m_elapsedTimer.elapsed() - m_lastPendingTime; | 48 | int delayedPaintInterval = m_elapsedTimer.elapsed() - m_lastPendingTime; | ||
| 46 | for (int i = 0; i < newPaintInfos.size(); i++) { | 49 | for (int i = 0; i < newPaintInfos.size(); i++) { | ||
| 47 | // TODO: Risk of overflowing? | 50 | // TODO: Risk of overflowing? | ||
| 48 | int offsetTime = (delayedPaintInterval * i) / newPaintInfos.size(); | 51 | int offsetTime = (delayedPaintInterval * i) / newPaintInfos.size(); | ||
| 49 | m_paintQueue.enqueue(TimedPaintInfo(now + offsetTime, newPaintInfos[i])); | 52 | m_paintQueue.enqueue(TimedPaintInfo(now + offsetTime, newPaintInfos[i])); | ||
| 50 | } | 53 | } | ||
| 51 | m_lastPendingTime = now; | 54 | m_lastPendingTime = now; | ||
| 52 | } | 55 | } | ||
| 53 | 56 | | |||
| 54 | void KisStabilizerDelayedPaintHelper::end() { | 57 | void KisStabilizerDelayedPaintHelper::paintSome() { | ||
| 55 | stabilizerDelayedPaint(true); | 58 | // This function is also called from KisToolFreehandHelper::paint | ||
| 56 | m_paintTimer.stop(); | 59 | m_lastPaintTime = m_elapsedTimer.elapsed(); | ||
| 60 | if (m_paintQueue.isEmpty()) { | ||||
| 61 | return; | ||||
| 62 | } | ||||
| 63 | int now = m_lastPaintTime; | ||||
| 64 | // Always keep one in the queue since painting requires two points | ||||
| 65 | while (m_paintQueue.size() > 1 && m_paintQueue.head().elapsedTime <= now) { | ||||
| 66 | const TimedPaintInfo dequeued = m_paintQueue.dequeue(); | ||||
| 67 | m_paintLine(dequeued.paintInfo, m_paintQueue.head().paintInfo); | ||||
| 57 | } | 68 | } | ||
| 58 | | ||||
| 59 | void KisStabilizerDelayedPaintHelper::cancel() { | | |||
| 60 | m_paintTimer.stop(); | | |||
| 61 | m_paintQueue.clear(); | | |||
| 62 | } | 69 | } | ||
| 63 | 70 | | |||
| 64 | void KisStabilizerDelayedPaintHelper::stabilizerDelayedPaint(bool isEndStroke) { | 71 | void KisStabilizerDelayedPaintHelper::end() { | ||
| 72 | m_paintTimer.stop(); | ||||
| 73 | m_elapsedTimer.invalidate(); | ||||
| 65 | if (m_paintQueue.isEmpty()) { | 74 | if (m_paintQueue.isEmpty()) { | ||
| 66 | return; | 75 | return; | ||
| 67 | } | 76 | } | ||
| 68 | if (isEndStroke) { | | |||
| 69 | TimedPaintInfo dequeued = m_paintQueue.dequeue(); | 77 | TimedPaintInfo dequeued = m_paintQueue.dequeue(); | ||
| 70 | while (!m_paintQueue.isEmpty()) { | 78 | while (!m_paintQueue.isEmpty()) { | ||
| 71 | const TimedPaintInfo dequeued2 = m_paintQueue.dequeue(); | 79 | const TimedPaintInfo dequeued2 = m_paintQueue.dequeue(); | ||
| 72 | m_paintLine(dequeued.paintInfo, dequeued2.paintInfo); | 80 | m_paintLine(dequeued.paintInfo, dequeued2.paintInfo); | ||
| 73 | dequeued = dequeued2; | 81 | dequeued = dequeued2; | ||
| 74 | } | 82 | } | ||
| 75 | m_requestUpdateOutline(); | | |||
| 76 | return; | | |||
| 77 | } | 83 | } | ||
| 78 | int now = m_elapsedTimer.elapsed(); | 84 | | ||
| 79 | // Always keep one in the queue since painting requires two points | 85 | void KisStabilizerDelayedPaintHelper::cancel() { | ||
| 80 | while (m_paintQueue.size() > 1 && m_paintQueue.head().elapsedTime <= now) { | 86 | m_paintTimer.stop(); | ||
| 81 | const TimedPaintInfo dequeued = m_paintQueue.dequeue(); | 87 | m_paintQueue.clear(); | ||
| 82 | m_paintLine(dequeued.paintInfo, m_paintQueue.head().paintInfo); | 88 | } | ||
| 89 | | ||||
| 90 | void KisStabilizerDelayedPaintHelper::stabilizerDelayedPaintTimer() { | ||||
| 91 | if (m_elapsedTimer.elapsed() - m_lastPaintTime < fixedPaintTimerInterval) { | ||||
| 92 | return; | ||||
| 83 | } | 93 | } | ||
| 94 | paintSome(); | ||||
| 95 | // Explicitly update the outline because this is outside the pointer event | ||||
| 84 | m_requestUpdateOutline(); | 96 | m_requestUpdateOutline(); | ||
| 85 | } | 97 | } | ||