Update infowidgets in Launcher based on FileFutures and torrent_handles

This commit is contained in:
Alexander Bock
2015-06-18 23:45:08 +02:00
parent 4d6663f824
commit cc76be6602
4 changed files with 63 additions and 39 deletions

View File

@@ -31,7 +31,7 @@
#include <QProgressBar>
InfoWidget::InfoWidget(QString name, int totalBytes)
: QWidget(nullptr)
: QGroupBox(nullptr)
, _name(nullptr)
, _bytes(nullptr)
, _progress(nullptr)
@@ -41,39 +41,40 @@ InfoWidget::InfoWidget(QString name, int totalBytes)
setFixedHeight(100);
QGridLayout* layout = new QGridLayout;
layout->setVerticalSpacing(0);
layout->setHorizontalSpacing(10);
layout->setContentsMargins(0, 0, 0, 0);
_name = new QLabel(name);
layout->addWidget(_name, 0, 0);
_bytes = new QLabel("");
layout->addWidget(_bytes, 0, 1);
layout->addWidget(_bytes, 1, 0);
_progress = new QProgressBar;
layout->addWidget(_progress, 0, 2);
layout->addWidget(_progress, 1, 1);
_messages = new QLabel("");
layout->addWidget(_messages, 1, 0, 1, 3);
layout->addWidget(_messages, 2, 0, 1, 2);
setLayout(layout);
update(0);
}
void InfoWidget::update(int currentBytes) {
_bytes->setText(
QString("%1 / %2")
.arg(currentBytes)
.arg(_totalBytes)
);
float progress = static_cast<float>(currentBytes) / static_cast<float>(_totalBytes);
_progress->setValue(static_cast<int>(progress * 100));
}
void InfoWidget::update(float progress) {
_bytes->setText("");
_progress->setValue(static_cast<int>(progress * 100));
}
//void InfoWidget::update(int currentBytes) {
// _bytes->setText(
// QString("%1 / %2")
// .arg(currentBytes)
// .arg(_totalBytes)
// );
//
// float progress = static_cast<float>(currentBytes) / static_cast<float>(_totalBytes);
// _progress->setValue(static_cast<int>(progress * 100));
//}
//
//void InfoWidget::update(float progress) {
// _bytes->setText("");
// _progress->setValue(static_cast<int>(progress * 100));
//}
void InfoWidget::update(openspace::DownloadManager::FileFuture* f) {
_bytes->setText(
@@ -85,13 +86,44 @@ void InfoWidget::update(openspace::DownloadManager::FileFuture* f) {
if (f->errorMessage.empty()) {
QString t = "Time remaining %1 s";
_messages->setText(t.arg(f->secondsRemaining));
_messages->setText(t.arg(static_cast<int>(f->secondsRemaining)));
}
else {
_messages->setText(QString::fromStdString(f->errorMessage));
}
}
void InfoWidget::update(libtorrent::torrent_status s) {
_bytes->setText(
QString("%1 / %2")
.arg(s.total_wanted_done)
.arg(s.total_wanted)
);
float progress = static_cast<float>(s.total_wanted_done) / s.total_wanted;
_progress->setValue(static_cast<int>(progress * 100));
if (s.error.empty()) {
int bytesPerSecond = s.download_rate;
long long remainingBytes = s.total_wanted - s.total_wanted_done;
if (remainingBytes > 0) {
float seconds = static_cast<float>(remainingBytes) / remainingBytes;
//auto now = time(NULL);
//auto transferTime = now - s.added_time;
//auto estimatedTime = transferTime / progress;
//auto timeRemaining = estimatedTime - transferTime;
QString t = "Time remaining %1 s";
_messages->setText(t.arg(static_cast<int>(seconds)));
}
else
_messages->setText("");
}
else {
_messages->setText(QString::fromStdString(s.error));
}
}
void InfoWidget::error(QString message) {
_messages->setText(message);
}

View File

@@ -25,22 +25,23 @@
#ifndef __INFOWIDGET_H__
#define __INFOWIDGET_H__
#include <QWidget>
//#include <QWidget>
#include <QGroupBox>
#include <openspace/engine/downloadmanager.h>
#include <libtorrent/torrent_handle.hpp>
class QLabel;
class QProgressBar;
class InfoWidget : public QWidget {
class InfoWidget : public QGroupBox {
Q_OBJECT
public:
InfoWidget(QString name, int totalBytes = -1);
void update(int currentBytes);
void update(float progress);
void update(openspace::DownloadManager::FileFuture* f);
void update(libtorrent::torrent_status s);
void error(QString message);

View File

@@ -104,6 +104,8 @@ SyncWidget::SyncWidget(QWidget* parent, Qt::WindowFlags f)
area->setWidget(w);
_downloadLayout = new QVBoxLayout(w);
_downloadLayout->setMargin(0);
_downloadLayout->setSpacing(0);
_downloadLayout->addStretch(100);
layout->addWidget(area);
@@ -498,7 +500,7 @@ void SyncWidget::handleTimer() {
InfoWidget* w = _torrentInfoWidgetMap[h];
if (w)
w->update(static_cast<int>(s.total_wanted_done));
w->update(s);
if (CleanInfoWidgets && (s.state == torrent_status::finished || s.state == torrent_status::seeding)) {
_torrentInfoWidgetMap.remove(h);
@@ -574,12 +576,4 @@ void SyncWidget::handleFileFutureAddition(
while (_mutex.test_and_set()) {}
_futuresToAdd.insert(_futuresToAdd.end(), futures.begin(), futures.end());
_mutex.clear();
//_futures.insert(_futures.end(), futures.begin(), futures.end());
//for (openspace::DownloadManager::FileFuture* f : futures) {
// InfoWidget* w = new InfoWidget(QString::fromStdString(f->filePath), -1);
// _downloadLayout->addWidget(w);
// _futureInfoWidgetMap[f] = w;
//}
}

View File

@@ -44,7 +44,6 @@ namespace {
const std::string RequestApplicationVersion = "application_version";
struct ProgressInformation {
CURL* curl;
openspace::DownloadManager::FileFuture* future;
std::chrono::system_clock::time_point startTime;
const openspace::DownloadManager::DownloadProgressCallback* callback;
@@ -67,7 +66,6 @@ namespace {
ghoul_assert(p, "Passed progress information is nullptr");
ProgressInformation* i = static_cast<ProgressInformation*>(p);
ghoul_assert(i, "Passed pointer is not a ProgressInformation");
ghoul_assert(i->curl, "CURL pointer is nullptr");
ghoul_assert(i->future, "FileFuture is not initialized");
ghoul_assert(i->callback, "Callback pointer is nullptr");
@@ -152,7 +150,6 @@ DownloadManager::FileFuture* DownloadManager::downloadFile(
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
ProgressInformation p = {
curl,
future,
std::chrono::system_clock::now(),
&progressCallback