Ver código fonte

Added download status display to StatusWidget

Ivan Arkhipov 4 anos atrás
pai
commit
ea0b506220

+ 9 - 0
src/Legacy/models/patchdownloader.cpp

@@ -53,6 +53,7 @@ void PatchDownloader::onDownloaderFinished(Downloader* context) {
     context->targetFile = nullptr;
     Settings::setValue("DatabaseNeedInstall/" + patch_downloaders_.key(context), true);
 
+    patch_downloaders_status_[context] = context->getDownloadStatus();
     Downloader::Status cumulative_status;
     for (const Downloader::Status& status: patch_downloaders_status_.values()) {
         cumulative_status = cumulative_status + status;
@@ -113,6 +114,8 @@ void PatchDownloader::checkForUpdates() {
 }
 
 void PatchDownloader::beginDownload() {
+    bool downloads_started = false;
+
     for (const QString& patch: patches_) {
         QString target_filename = Settings::getValue("PatchDatabases/" + patch + "/path").toString();
 
@@ -134,6 +137,12 @@ void PatchDownloader::beginDownload() {
         patch_downloaders_[patch]->setUrl(Settings::getValue("PatchDatabases/" + patch + "/url").toUrl());
         patch_downloaders_[patch]->targetFile = target_file;
         patch_downloaders_[patch]->start();
+        downloads_started = true;
+    }
+
+    if (!downloads_started) {
+        started_ = false;
+        emit finished();
     }
 }
 

+ 1 - 1
src/Legacy/widgets/mainwindow.cpp

@@ -48,7 +48,7 @@ MainWindow::MainWindow(PatchList *legacy_patches, QWidget *parent)
     current_hovered_entry_ = nullptr;
 
     qDebug() << __FUNCTION__ << "Initialising main frame...";
-    status_widget_ = new StatusWidget(legacy_patches_, this);
+    status_widget_ = new StatusWidget(this);
     settings_widget_ = new SettingsWidget(legacy_patches_, this);
     help_widget_ = new HelpWidget(legacy_patches_, this);
     about_widget_ = new AboutWidget(legacy_patches_, this);

+ 23 - 13
src/Legacy/widgets/statuswidget.cpp

@@ -1,7 +1,7 @@
 #include "ui_statuswidget.h"
 #include "statuswidget.h"
 
-#include "models/patchlist.h"
+#include "models/patchdownloader.h"
 
 #include "widgets/mainwindow.h"
 
@@ -13,10 +13,9 @@
 #include <QMessageBox>
 #include <QRandomGenerator>
 
-StatusWidget::StatusWidget(PatchList *legacy_patches, QWidget *parent)
+StatusWidget::StatusWidget(QWidget *parent)
     : QWidget(parent)
     , ui(new Ui::StatusWidget)
-    , legacy_patches_(legacy_patches)
 {
     ui->setupUi(this);
     last_statusbar_update_time_.start();
@@ -32,9 +31,9 @@ StatusWidget::StatusWidget(PatchList *legacy_patches, QWidget *parent)
     connect(ui->server_status_widget, &ServerStatusWidget::showServersTooltip, this, [this](QString message){setToolTipMessage(message, E_INFO);});
     connect(ui->server_status_widget, &ServerStatusWidget::showNoTooltip, this, [this](){unsetToolTipMessage(E_INFO);});
 
-//    connect(legacy_patches_, &PatchList::patchOperationsStarted, this, &StatusWidget::onPatchTotalOperationsStarted);
-//    connect(legacy_patches_, &PatchList::patchOperationsFinished, this, &StatusWidget::onPatchTotalOperationsFinished);
-//    connect(legacy_patches_, &PatchList::progressChanged, this, &StatusWidget::updatePatchProgressStatus);
+    connect(&PatchDownloader::instance(), &PatchDownloader::started, this, &StatusWidget::onPatchDownloaderStarted);
+    connect(&PatchDownloader::instance(), &PatchDownloader::progressChanged, this, &StatusWidget::onPatchDownloaderProgressChanged);
+    connect(&PatchDownloader::instance(), &PatchDownloader::finished, this, &StatusWidget::onPatchDownloaderFinished);
 
     generateRandomTooltipMessage();
     random_tooltip_generator_timer_.setInterval(5 * 1000);
@@ -233,23 +232,34 @@ void StatusWidget::fadeBetweenToolTips(QString tooltip_id)
     current_tooltip_message_ = tooltip_id;
 }
 
-void StatusWidget::onPatchTotalOperationsStarted()
-{
+void StatusWidget::onPatchDownloaderStarted() {
     all_patch_operations_finished_ = false;
     ui->game_button->setEnabled(false);
     ui->check_for_updates_button->setEnabled(false);
-    setToolTipMessage("Запуск процесса проверки и установки русификации...", E_PROCESS);
+    setToolTipMessage("Проверка обновлений русификации...", E_PROCESS);
 }
 
-void StatusWidget::onPatchTotalOperationsFinished()
-{
+void StatusWidget::onPatchDownloaderFinished() {
     all_patch_operations_finished_ = true;
     ui->game_button->setEnabled(true);
     ui->check_for_updates_button->setEnabled(true);
-    setToolTipMessage("Все процессы установки русификации завершены. Можно играть. Mae govannen. Еще какой-то текст...", E_PROCESS);
+    setToolTipMessage("Скачивание данных русификации завершено!", E_PROCESS);
     process_completed_tooltip_hide_timer_.start();
 }
 
+void StatusWidget::onPatchDownloaderProgressChanged(Downloader::Status status) {
+    if (last_statusbar_update_time_.elapsed() > 1000) {
+        double download_percent = double(status.downloaded_bytes) * 100.0 / double(status.total_bytes);
+        QString text = "Загрузка данных: " + QString::number(download_percent, 'f', 1) + "% ("
+                     + Downloader::getSizeFormatted(status.downloaded_bytes) + "/"
+                     + Downloader::getSizeFormatted(status.total_bytes) + ")\n"
+                     + "До конца загрузки: " + Downloader::getElapsedTimeFormatted(status.elapsed_time);
+
+        setToolTipMessage(text, E_PROCESS);
+        last_statusbar_update_time_.restart();
+    }
+}
+
 void StatusWidget::on_game_button_clicked()
 {
     MainWindow* window = qobject_cast<MainWindow*>(parentWidget()->parentWidget()->parentWidget());
@@ -302,5 +312,5 @@ void StatusWidget::createTooltipMessageWidget(QString tooltip_id)
 
 void StatusWidget::on_check_for_updates_button_clicked()
 {
-    QMetaObject::invokeMethod(legacy_patches_, &PatchList::update, Qt::QueuedConnection);
+    QMetaObject::invokeMethod(&PatchDownloader::instance(), &PatchDownloader::startPatchDownloaderChain, Qt::QueuedConnection);
 }

+ 5 - 5
src/Legacy/widgets/statuswidget.h

@@ -29,7 +29,7 @@ public:
         E_RANDOM = 1
     };
 
-    explicit StatusWidget(PatchList *legacy_patches, QWidget *parent = 0);
+    explicit StatusWidget(QWidget *parent = 0);
     ~StatusWidget();
 
 public slots:
@@ -52,9 +52,11 @@ private slots:
 
     void fadeBetweenToolTips(QString tooltip_id);
 
-    void onPatchTotalOperationsStarted();
+    void onPatchDownloaderStarted();
 
-    void onPatchTotalOperationsFinished();
+    void onPatchDownloaderFinished();
+
+    void onPatchDownloaderProgressChanged(Downloader::Status status);
 
     void updatePatchProgressStatus(Patch::OperationProgress progress);
 
@@ -68,8 +70,6 @@ private:
 private:
     Ui::StatusWidget *ui;
 
-    PatchList *legacy_patches_;
-
     bool all_patch_operations_finished_ = false;
 
     QMap<QString, QLabel*> tooltip_widgets_;