dbhub: Fix weird progress bar behaviour

This commit is contained in:
Martin Kleusberg
2016-10-25 20:54:42 +02:00
parent c2a9608c80
commit 08e7f575ee
2 changed files with 22 additions and 17 deletions

View File

@@ -4,6 +4,7 @@
#include <QNetworkReply>
#include <QFile>
#include <QSslKey>
#include <QProgressDialog>
#include "RemoteDatabase.h"
#include "version.h"
@@ -11,6 +12,7 @@
RemoteDatabase::RemoteDatabase() :
m_manager(new QNetworkAccessManager),
m_progress(nullptr),
m_currentReply(nullptr)
{
// Load settings and set up some more stuff while doing so
@@ -27,6 +29,7 @@ RemoteDatabase::RemoteDatabase() :
RemoteDatabase::~RemoteDatabase()
{
delete m_manager;
delete m_progress;
}
void RemoteDatabase::reloadSettings()
@@ -86,10 +89,12 @@ void RemoteDatabase::fetchDatabase(const QString& url)
m_currentReply = m_manager->get(request);
// Initialise the progress dialog for this request
m_progress.setWindowModality(Qt::ApplicationModal);
m_progress.setCancelButtonText(tr("Cancel"));
m_progress.setLabelText(tr("Downloading remote database from\n%1.").arg(url));
m_progress.show();
if(!m_progress)
m_progress = new QProgressDialog();
m_progress->setWindowModality(Qt::ApplicationModal);
m_progress->setCancelButtonText(tr("Cancel"));
m_progress->setLabelText(tr("Downloading remote database from\n%1.").arg(url));
m_progress->show();
qApp->processEvents();
connect(m_currentReply, &QNetworkReply::downloadProgress, this, &RemoteDatabase::updateProgress);
}
@@ -129,7 +134,7 @@ void RemoteDatabase::gotReply(QNetworkReply* reply)
// Delete reply later, i.e. after returning from this slot function
m_currentReply = nullptr;
m_progress.hide();
m_progress->hide();
reply->deleteLater();
}
@@ -159,7 +164,7 @@ void RemoteDatabase::gotError(QNetworkReply* reply, const QList<QSslError>& erro
QMessageBox::warning(0, qApp->applicationName(), message);
// Delete reply later, i.e. after returning from this slot function
m_progress.hide();
m_progress->hide();
reply->deleteLater();
}
@@ -169,24 +174,24 @@ void RemoteDatabase::updateProgress(qint64 bytesReceived, qint64 bytesTotal)
if(bytesTotal == -1)
{
// We don't know anything about the current progress, but it's still downloading
m_progress.setMinimum(0);
m_progress.setMaximum(0);
m_progress.setValue(0);
m_progress->setMinimum(0);
m_progress->setMaximum(0);
m_progress->setValue(0);
} else if(bytesReceived == bytesTotal) {
// The download has finished
m_progress.hide();
m_progress->hide();
} else {
// It's still downloading and we know the current progress
m_progress.setMinimum(0);
m_progress.setMaximum(bytesTotal);
m_progress.setValue(bytesReceived);
m_progress->setMinimum(0);
m_progress->setMaximum(bytesTotal);
m_progress->setValue(bytesReceived);
}
// Check if the Cancel button has been pressed
qApp->processEvents();
if(m_currentReply && m_progress.wasCanceled())
if(m_currentReply && m_progress->wasCanceled())
{
m_currentReply->abort();
m_progress.hide();
m_progress->hide();
}
}