Skip to content

Commit

Permalink
add downloader speed display chart (from qv2ray)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaniol-lck committed Mar 6, 2022
1 parent 22a10c9 commit c7836e6
Show file tree
Hide file tree
Showing 11 changed files with 462 additions and 12 deletions.
5 changes: 5 additions & 0 deletions modmanager.pro
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ SOURCES += \
src/ui/datetimetext.cpp \
src/ui/dockwidgetcontent.cpp \
src/ui/download/adddownloaddialog.cpp \
src/ui/download/downloaderinfowidget.cpp \
src/ui/download/downloaderspeedwidget.cpp \
src/ui/download/downloadstatusbarwidget.cpp \
src/ui/downloadpathselectmenu.cpp \
src/ui/explorestatusbarwidget.cpp \
Expand Down Expand Up @@ -206,6 +208,8 @@ HEADERS += \
src/ui/datetimetext.h \
src/ui/dockwidgetcontent.h \
src/ui/download/adddownloaddialog.h \
src/ui/download/downloaderinfowidget.h \
src/ui/download/downloaderspeedwidget.h \
src/ui/download/downloadstatusbarwidget.h \
src/ui/downloadpathselectmenu.h \
src/ui/explorestatusbarwidget.h \
Expand Down Expand Up @@ -286,6 +290,7 @@ FORMS += \
src/ui/datetimetext.ui \
src/ui/download/adddownloaddialog.ui \
src/ui/download/downloadbrowser.ui \
src/ui/download/downloaderinfowidget.ui \
src/ui/download/downloadstatusbarwidget.ui \
src/ui/explorestatusbarwidget.ui \
src/ui/github/githubfileitemwidget.ui \
Expand Down
28 changes: 21 additions & 7 deletions src/download/abstractdownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#include <QtConcurrent>
#include <QDebug>

AbstractDownloader::AbstractDownloader(QObject *parent) :
QObject(parent)
{}

AbstractDownloader::AbstractDownloader(QObject *parent, const DownloadFileInfo &info) :
QObject(parent),
info_(info)
{}
{
connect(this, &AbstractDownloader::downloadSpeed, [=](qint64 download, qint64 upload){
downSpeed_ = download;
upSpeed_ = upload;
});
}

AbstractDownloader::~AbstractDownloader()
{}
Expand Down Expand Up @@ -47,6 +48,7 @@ QUrl AbstractDownloader::handleRedirect(const QUrl &url)
#endif
QNetworkAccessManager accessManager;
auto reply = accessManager.head(request);
qDebug() << "redirect:" << url;
if(!reply) continue;
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
Expand All @@ -56,13 +58,15 @@ QUrl AbstractDownloader::handleRedirect(const QUrl &url)

//size_ = reply->header(QNetworkRequest::ContentLengthHeader).toLongLong();
QUrl redirected;
if(auto redirection = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); !redirection.isNull())
redirected = handleRedirect(redirection.toString());
if(auto redirection = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); !redirection.isNull()/* && redirection.toUrl() == url*/)
redirected = handleRedirect(redirection.toUrl());
else
redirected = url;
reply->deleteLater();
qDebug() << "rediect ended";
return redirected;
}
qDebug() << "rediect ended.";
return url;
}

Expand All @@ -82,3 +86,13 @@ void AbstractDownloader::setIcon(const QPixmap &newIcon)
info_.setIcon(newIcon);
emit infoChanged();
}

const QList<AbstractDownloader::PointData> &AbstractDownloader::dataCollection() const
{
return dataCollection_;
}

void AbstractDownloader::setDataCollection(const QList<PointData> &newDataCollection)
{
dataCollection_ = newDataCollection;
}
19 changes: 17 additions & 2 deletions src/download/abstractdownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
class AbstractDownloader : public QObject
{
Q_OBJECT
friend class DownloaderSpeedWidget;
public:
explicit AbstractDownloader(QObject *parent = nullptr);
explicit AbstractDownloader(QObject *parent, const DownloadFileInfo &info);
explicit AbstractDownloader(QObject *parent = nullptr, const DownloadFileInfo &info = {});
virtual ~AbstractDownloader() = 0;

virtual bool isStarted() const = 0;
Expand All @@ -20,6 +20,16 @@ class AbstractDownloader : public QObject

const DownloadFileInfo &info() const;

enum LineType { Download, Upload };
struct PointData
{
qint64 x;
qint64 y[2];
};

const QList<PointData> &dataCollection() const;
void setDataCollection(const QList<PointData> &newDataCollection);

public slots:
virtual int pause(bool force = false) = 0;
virtual int start() = 0;
Expand All @@ -42,6 +52,11 @@ public slots:

protected:
DownloadFileInfo info_;

private:
QList<PointData> dataCollection_;
qint64 downSpeed_;
qint64 upSpeed_;
};

#endif // ABSTRACTDOWNLOADER_H
2 changes: 1 addition & 1 deletion src/download/qaria2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ QAria2::QAria2(QObject *parent) : QObject(parent)
});
MMLogger() << "aria2 start running";
//interval between refreshing info
timer_.start(100);
timer_.start(1000);
connect(&timer_, &QTimer::timeout, this, [=]{
aria2::GlobalStat gstat = aria2::getGlobalStat(session_);
emit downloadSpeed(gstat.downloadSpeed, gstat.uploadSpeed);
Expand Down
10 changes: 9 additions & 1 deletion src/ui/download/downloadbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
#include "util/funcutil.h"
#include "util/smoothscrollbar.h"
#include "adddownloaddialog.h"
#include "downloaderinfowidget.h"

DownloadBrowser::DownloadBrowser(QWidget *parent) :
Browser(parent),
ui(new Ui::DownloadBrowser),
manager_(DownloadManager::manager()),
statusBarWidget_(new DownloadStatusBarWidget(this))
statusBarWidget_(new DownloadStatusBarWidget(this)),
infoWidget_(new DownloaderInfoWidget(this))
{
ui->setupUi(this);
ui->statusbar->addPermanentWidget(statusBarWidget_);
Expand Down Expand Up @@ -48,6 +50,11 @@ DownloadBrowser::~DownloadBrowser()
delete ui;
}

QWidget *DownloadBrowser::infoWidget() const
{
return infoWidget_;
}

QIcon DownloadBrowser::icon() const
{
return QIcon::fromTheme("download");
Expand Down Expand Up @@ -75,6 +82,7 @@ void DownloadBrowser::onCurrentRowChanged()
ui->actionShow_in_Folder->setEnabled(true);
}
auto downloader = manager_->downloaders().at(row);
infoWidget_->setDownloader(downloader);
auto updateButtons = [=]{
ui->actionPause->setEnabled(downloader->isStarted());
ui->actionForce_Pause->setEnabled(downloader->isStarted());
Expand Down
5 changes: 4 additions & 1 deletion src/ui/download/downloadbrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ class DownloadManager;
class QAria2Downloader;
class QAria2;
class DownloadStatusBarWidget;
class DownloaderInfoWidget;
class DownloadBrowser : public Browser
{
Q_OBJECT

public:
explicit DownloadBrowser(QWidget *parent = nullptr);
~DownloadBrowser();

QWidget *infoWidget() const override;

QIcon icon() const override;
QString name() const override;

Expand All @@ -44,6 +46,7 @@ private slots:
DownloadManager *manager_;
QMetaObject::Connection conn_;
DownloadStatusBarWidget *statusBarWidget_;
DownloaderInfoWidget *infoWidget_;
};

#endif // DOWNLOADBROWSER_H
26 changes: 26 additions & 0 deletions src/ui/download/downloaderinfowidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "downloaderinfowidget.h"
#include "ui_downloaderinfowidget.h"

#include "download/abstractdownloader.h"
#include "util/funcutil.h"

DownloaderInfoWidget::DownloaderInfoWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::DownloaderInfoWidget)
{
ui->setupUi(this);
}

DownloaderInfoWidget::~DownloaderInfoWidget()
{
delete ui;
}

void DownloaderInfoWidget::setDownloader(AbstractDownloader *downloader)
{
downloader_ = downloader;
emit downloaderChanged();
if(!downloader_) return;
ui->name->setText(downloader->info().displayName());
ui->speedWidget->setDownloader(downloader);
}
29 changes: 29 additions & 0 deletions src/ui/download/downloaderinfowidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef DOWNLOADERINFOWIDGET_H
#define DOWNLOADERINFOWIDGET_H

#include <QWidget>

class AbstractDownloader;
namespace Ui {
class DownloaderInfoWidget;
}

class DownloaderInfoWidget : public QWidget
{
Q_OBJECT

public:
explicit DownloaderInfoWidget(QWidget *parent = nullptr);
~DownloaderInfoWidget();

void setDownloader(AbstractDownloader *downloader);

signals:
void downloaderChanged();

private:
Ui::DownloaderInfoWidget *ui;
AbstractDownloader *downloader_;
};

#endif // DOWNLOADERINFOWIDGET_H
34 changes: 34 additions & 0 deletions src/ui/download/downloaderinfowidget.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DownloaderInfoWidget</class>
<widget class="QWidget" name="DownloaderInfoWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="name"/>
</item>
<item>
<widget class="DownloaderSpeedWidget" name="speedWidget"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>DownloaderSpeedWidget</class>
<extends>QGraphicsView</extends>
<header>ui/download/downloaderspeedwidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
Loading

0 comments on commit c7836e6

Please sign in to comment.