From b4f53944cc2b83ee98af4eae9f4fc023e856f750 Mon Sep 17 00:00:00 2001 From: krepa098 Date: Sat, 17 Jan 2015 18:17:40 +0100 Subject: [PATCH] FileTransferWidget: calculate mean --- src/chatlog/content/filetransferwidget.cpp | 72 +++++++++++++--------- src/chatlog/content/filetransferwidget.h | 4 ++ 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/chatlog/content/filetransferwidget.cpp b/src/chatlog/content/filetransferwidget.cpp index 32a6e706a..67444cedd 100644 --- a/src/chatlog/content/filetransferwidget.cpp +++ b/src/chatlog/content/filetransferwidget.cpp @@ -43,7 +43,7 @@ FileTransferWidget::FileTransferWidget(QWidget *parent, ToxFile file) ui->progressBar->setValue(0); ui->fileSizeLabel->setText(getHumanReadableSize(file.filesize)); ui->progressLabel->setText("0kiB/s"); - ui->etaLabel->setText("-:-"); + ui->etaLabel->setText(""); setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/grey.css")); Style::repolish(this); @@ -116,7 +116,10 @@ bool FileTransferWidget::isFilePathWritable(const QString &filepath) void FileTransferWidget::onFileTransferInfo(ToxFile file) { - if(fileInfo != file) + QTime now = QTime::currentTime(); + qint64 dt = lastTick.msecsTo(now); //ms + + if(fileInfo != file || dt < 1000) return; fileInfo = file; @@ -127,37 +130,41 @@ void FileTransferWidget::onFileTransferInfo(ToxFile file) qreal progress = static_cast(file.bytesSent) / static_cast(file.filesize); ui->progressBar->setValue(static_cast(progress * 100.0)); - // eta, speed - QTime now = QTime::currentTime(); - qreal deltaSecs = lastTick.msecsTo(now) / 1000.0; + // ETA, speed + qreal deltaSecs = dt / 1000.0; - if(deltaSecs >= 1.0) + qint64 deltaBytes = qMax(file.bytesSent - lastBytesSent, qint64(0)); + qreal bytesPerSec = static_cast(static_cast(deltaBytes) / deltaSecs); + + // calculate mean + meanData[(meanIndex++) % FTW_MEAN_PERIODES] = bytesPerSec; + + qreal meanBytesPerSec = 0.0; + for(size_t i = 0; i < FTW_MEAN_PERIODES; ++i) + meanBytesPerSec += meanData[i]; + + meanBytesPerSec /= qMin(meanIndex, static_cast(FTW_MEAN_PERIODES)); + + // update UI + if(meanBytesPerSec > 0) { - qint64 deltaBytes = file.bytesSent - lastBytesSent; - qint64 bytesPerSec = static_cast(static_cast(deltaBytes) / deltaSecs); - - if(bytesPerSec > 0) - { - QTime toGo = QTime(0,0).addSecs(file.filesize / bytesPerSec); - ui->etaLabel->setText(toGo.toString("mm:ss")); - } - else - { - ui->etaLabel->setText("--:--"); - } - - ui->progressLabel->setText(getHumanReadableSize(bytesPerSec) + "/s"); - - lastTick = now; - lastBytesSent = file.bytesSent; + // ETA + QTime toGo = QTime(0,0).addSecs((file.filesize - file.bytesSent) / meanBytesPerSec); + ui->etaLabel->setText(toGo.toString("hh:mm:ss")); } - } - else if(fileInfo.status == ToxFile::PAUSED) - { - ui->etaLabel->setText("--:--"); - ui->progressLabel->setText(getHumanReadableSize(0) + "/s"); + else + { + ui->etaLabel->setText(""); + } + + ui->progressLabel->setText(getHumanReadableSize(meanBytesPerSec) + "/s"); + + lastBytesSent = file.bytesSent; } + lastTick = now; + + // trigger repaint update(); } @@ -197,9 +204,14 @@ void FileTransferWidget::onFileTransferPaused(ToxFile file) fileInfo = file; - ui->etaLabel->setText("--:--"); + ui->etaLabel->setText(""); ui->progressLabel->setText(getHumanReadableSize(0) + "/s"); + // reset mean + meanIndex = 0; + for(size_t i=0; i 0) exp = std::min( (int) (log(size) / log(1024)), (int) (sizeof(suffix) / sizeof(suffix[0]) - 1)); - return QString().setNum(size / pow(1024, exp),'f',2).append(suffix[exp]); + return QString().setNum(size / pow(1024, exp),'f', exp > 2 ? 2 : 0).append(suffix[exp]); } void FileTransferWidget::hideWidgets() diff --git a/src/chatlog/content/filetransferwidget.h b/src/chatlog/content/filetransferwidget.h index 34c0d7581..5d3b1336e 100644 --- a/src/chatlog/content/filetransferwidget.h +++ b/src/chatlog/content/filetransferwidget.h @@ -22,6 +22,8 @@ #include "../chatlinecontent.h" #include "../../corestructs.h" +#define FTW_MEAN_PERIODES 4 + namespace Ui { class FileTransferWidget; } @@ -64,6 +66,8 @@ private: QTime lastTick; qint64 lastBytesSent = 0; + qreal meanData[FTW_MEAN_PERIODES] = {0.0}; + size_t meanIndex = 0; }; #endif // FILETRANSFERWIDGET_H