#include "RateCalculator.h" void RateCalculator::addBytes(qint64 bytes) { if (!m_timer.isValid()) start(); m_segments.prepend(qMakePair(m_timer.restart(), bytes)); m_byteSum += bytes; } void RateCalculator::updateTotalBytes(qint64 bytes) { addBytes(bytes - m_byteSum); } void RateCalculator::start() { m_byteSum = 0; m_segments.clear(); m_timer.restart(); } void RateCalculator::stop() { m_timer.invalidate(); } void RateCalculator::reset() { stop(); start(); } double RateCalculator::rate(qint64 interval) const { qint64 time = 0; qint64 bytes = 0; QPair i; foreach (i, m_segments) { time += i.first; bytes += i.second; if (interval > 0 && time >= interval) break; } return static_cast(bytes) / static_cast(time); } double RateCalculator::elapsed() const { qint64 time = 0; QPair i; foreach (i, m_segments) time += i.first; return time; }