1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

FileTransferWidget: calculate mean

This commit is contained in:
krepa098 2015-01-17 18:17:40 +01:00
parent 4d5470710a
commit b4f53944cc
2 changed files with 46 additions and 30 deletions

View File

@ -43,7 +43,7 @@ FileTransferWidget::FileTransferWidget(QWidget *parent, ToxFile file)
ui->progressBar->setValue(0); ui->progressBar->setValue(0);
ui->fileSizeLabel->setText(getHumanReadableSize(file.filesize)); ui->fileSizeLabel->setText(getHumanReadableSize(file.filesize));
ui->progressLabel->setText("0kiB/s"); ui->progressLabel->setText("0kiB/s");
ui->etaLabel->setText("-:-"); ui->etaLabel->setText("");
setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/grey.css")); setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/grey.css"));
Style::repolish(this); Style::repolish(this);
@ -116,7 +116,10 @@ bool FileTransferWidget::isFilePathWritable(const QString &filepath)
void FileTransferWidget::onFileTransferInfo(ToxFile file) void FileTransferWidget::onFileTransferInfo(ToxFile file)
{ {
if(fileInfo != file) QTime now = QTime::currentTime();
qint64 dt = lastTick.msecsTo(now); //ms
if(fileInfo != file || dt < 1000)
return; return;
fileInfo = file; fileInfo = file;
@ -127,37 +130,41 @@ void FileTransferWidget::onFileTransferInfo(ToxFile file)
qreal progress = static_cast<qreal>(file.bytesSent) / static_cast<qreal>(file.filesize); qreal progress = static_cast<qreal>(file.bytesSent) / static_cast<qreal>(file.filesize);
ui->progressBar->setValue(static_cast<int>(progress * 100.0)); ui->progressBar->setValue(static_cast<int>(progress * 100.0));
// eta, speed // ETA, speed
QTime now = QTime::currentTime(); qreal deltaSecs = dt / 1000.0;
qreal deltaSecs = lastTick.msecsTo(now) / 1000.0;
if(deltaSecs >= 1.0) qint64 deltaBytes = qMax(file.bytesSent - lastBytesSent, qint64(0));
{ qreal bytesPerSec = static_cast<int>(static_cast<qreal>(deltaBytes) / deltaSecs);
qint64 deltaBytes = file.bytesSent - lastBytesSent;
qint64 bytesPerSec = static_cast<int>(static_cast<qreal>(deltaBytes) / deltaSecs);
if(bytesPerSec > 0) // 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<size_t>(FTW_MEAN_PERIODES));
// update UI
if(meanBytesPerSec > 0)
{ {
QTime toGo = QTime(0,0).addSecs(file.filesize / bytesPerSec); // ETA
ui->etaLabel->setText(toGo.toString("mm:ss")); QTime toGo = QTime(0,0).addSecs((file.filesize - file.bytesSent) / meanBytesPerSec);
ui->etaLabel->setText(toGo.toString("hh:mm:ss"));
} }
else else
{ {
ui->etaLabel->setText("--:--"); ui->etaLabel->setText("");
} }
ui->progressLabel->setText(getHumanReadableSize(bytesPerSec) + "/s"); ui->progressLabel->setText(getHumanReadableSize(meanBytesPerSec) + "/s");
lastTick = now;
lastBytesSent = file.bytesSent; lastBytesSent = file.bytesSent;
} }
}
else if(fileInfo.status == ToxFile::PAUSED)
{
ui->etaLabel->setText("--:--");
ui->progressLabel->setText(getHumanReadableSize(0) + "/s");
}
lastTick = now;
// trigger repaint
update(); update();
} }
@ -197,9 +204,14 @@ void FileTransferWidget::onFileTransferPaused(ToxFile file)
fileInfo = file; fileInfo = file;
ui->etaLabel->setText("--:--"); ui->etaLabel->setText("");
ui->progressLabel->setText(getHumanReadableSize(0) + "/s"); ui->progressLabel->setText(getHumanReadableSize(0) + "/s");
// reset mean
meanIndex = 0;
for(size_t i=0; i<FTW_MEAN_PERIODES; ++i)
meanData[i] = 0.0;
setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/grey.css")); setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/grey.css"));
Style::repolish(this); Style::repolish(this);
@ -243,7 +255,7 @@ QString FileTransferWidget::getHumanReadableSize(qint64 size)
if (size > 0) if (size > 0)
exp = std::min( (int) (log(size) / log(1024)), (int) (sizeof(suffix) / sizeof(suffix[0]) - 1)); 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() void FileTransferWidget::hideWidgets()

View File

@ -22,6 +22,8 @@
#include "../chatlinecontent.h" #include "../chatlinecontent.h"
#include "../../corestructs.h" #include "../../corestructs.h"
#define FTW_MEAN_PERIODES 4
namespace Ui { namespace Ui {
class FileTransferWidget; class FileTransferWidget;
} }
@ -64,6 +66,8 @@ private:
QTime lastTick; QTime lastTick;
qint64 lastBytesSent = 0; qint64 lastBytesSent = 0;
qreal meanData[FTW_MEAN_PERIODES] = {0.0};
size_t meanIndex = 0;
}; };
#endif // FILETRANSFERWIDGET_H #endif // FILETRANSFERWIDGET_H