File indexing completed on 2024-12-22 04:12:59
0001 /* 0002 * SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "kis_stabilized_events_sampler.h" 0008 0009 #include <QList> 0010 #include <QElapsedTimer> 0011 #include <QtMath> 0012 0013 #include "kis_paint_information.h" 0014 0015 0016 struct KisStabilizedEventsSampler::Private 0017 { 0018 Private(int _sampleTime) : sampleTime(_sampleTime), elapsedTimeOverride(0) {} 0019 0020 QElapsedTimer lastPaintTime; 0021 QList<KisPaintInformation> realEvents; 0022 int sampleTime; 0023 int elapsedTimeOverride; 0024 0025 KisPaintInformation lastPaintInformation; 0026 }; 0027 0028 KisStabilizedEventsSampler::KisStabilizedEventsSampler(int sampleTime) 0029 : m_d(new Private(sampleTime)) 0030 { 0031 } 0032 0033 KisStabilizedEventsSampler::~KisStabilizedEventsSampler() 0034 { 0035 } 0036 0037 void KisStabilizedEventsSampler::clear() 0038 { 0039 if (!m_d->realEvents.isEmpty()) { 0040 m_d->lastPaintInformation = m_d->realEvents.last(); 0041 } 0042 0043 m_d->realEvents.clear(); 0044 m_d->lastPaintTime.start(); 0045 } 0046 0047 void KisStabilizedEventsSampler::addEvent(const KisPaintInformation &pi) 0048 { 0049 if (!m_d->lastPaintTime.isValid()) { 0050 m_d->lastPaintTime.start(); 0051 } 0052 0053 m_d->realEvents.append(pi); 0054 } 0055 0056 void KisStabilizedEventsSampler::addFinishingEvent(int numSamples) 0057 { 0058 if (m_d->realEvents.size() > 0) { 0059 dbgKrita << "DEBUG: KisStabilizedEventsSampler::addFinishingEvent called " 0060 "before `realEvents` is cleared"; 0061 clear(); 0062 } 0063 0064 m_d->elapsedTimeOverride = numSamples; 0065 m_d->realEvents.append(m_d->lastPaintInformation); 0066 } 0067 0068 const KisPaintInformation& KisStabilizedEventsSampler::iterator::dereference() const 0069 { 0070 const int k = qFloor(m_alpha * m_index); 0071 return k < m_sampler->m_d->realEvents.size() ? 0072 m_sampler->m_d->realEvents[k] : m_sampler->m_d->lastPaintInformation; 0073 } 0074 0075 std::pair<KisStabilizedEventsSampler::iterator, KisStabilizedEventsSampler::iterator> 0076 KisStabilizedEventsSampler::range() const 0077 { 0078 const int elapsed = (m_d->lastPaintTime.restart() + m_d->elapsedTimeOverride) / m_d->sampleTime; 0079 const qreal alpha = qreal(m_d->realEvents.size()) / elapsed; 0080 0081 m_d->elapsedTimeOverride = 0; 0082 0083 return std::make_pair(iterator(this, 0, alpha), 0084 iterator(this, elapsed, alpha)); 0085 } 0086 0087