Clean up SpeedLimitDialog class

This commit is contained in:
Chocobo1 2018-11-02 13:45:16 +08:00
parent 3fa5358c3f
commit 266ce1d226
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
4 changed files with 38 additions and 53 deletions

View file

@ -40,6 +40,7 @@
#include <QWheelEvent>
#include "base/bittorrent/peerinfo.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrenthandle.h"
#include "base/logger.h"
#include "base/net/geoipmanager.h"

View file

@ -28,16 +28,14 @@
#include "speedlimitdialog.h"
#include "base/unicodestrings.h"
#include "ui_speedlimitdialog.h"
#include "utils.h"
SpeedLimitDialog::SpeedLimitDialog(QWidget *parent)
: QDialog(parent)
, m_ui(new Ui::SpeedLimitDialog())
, m_ui(new Ui::SpeedLimitDialog)
{
m_ui->setupUi(this);
qDebug("Bandwidth allocation dialog creation");
// Connect to slots
connect(m_ui->bandwidthSlider, &QSlider::valueChanged, this, &SpeedLimitDialog::updateSpinValue);
@ -49,73 +47,59 @@ SpeedLimitDialog::SpeedLimitDialog(QWidget *parent)
SpeedLimitDialog::~SpeedLimitDialog()
{
qDebug("Deleting bandwidth allocation dialog");
delete m_ui;
}
// -2: if cancel
long SpeedLimitDialog::askSpeedLimit(QWidget *parent, bool *ok, QString title, long defaultVal, long maxVal)
long SpeedLimitDialog::askSpeedLimit(QWidget *parent, bool *ok, const QString &title, const long defaultVal, const long maxVal)
{
if (ok) *ok = false;
SpeedLimitDialog dlg(parent);
dlg.setWindowTitle(title);
dlg.setupDialog(maxVal / 1024., defaultVal / 1024.);
dlg.setupDialog((maxVal / 1024.), (defaultVal / 1024.));
if (dlg.exec() == QDialog::Accepted) {
*ok = true;
int val = dlg.getSpeedLimit();
if (val <= 0)
if (ok) *ok = true;
const int val = dlg.getSpeedLimit();
if (val < 0)
return 0;
return (val * 1024);
}
else {
*ok = false;
return -2;
}
return -2;
}
void SpeedLimitDialog::updateSpinValue(int val) const
void SpeedLimitDialog::updateSpinValue(const int value)
{
qDebug("Called updateSpinValue with %d", val);
if (val <= 0) {
m_ui->spinBandwidth->setValue(0);
m_ui->spinBandwidth->setSpecialValueText(QString::fromUtf8(C_INFINITY));
m_ui->spinBandwidth->setSuffix("");
}
else {
m_ui->spinBandwidth->setValue(val);
m_ui->spinBandwidth->setSuffix(' ' + tr("KiB/s"));
}
m_ui->spinBandwidth->setValue(value);
}
void SpeedLimitDialog::updateSliderValue(int val) const
void SpeedLimitDialog::updateSliderValue(const int value)
{
if (val <= 0) {
m_ui->spinBandwidth->setValue(0);
m_ui->spinBandwidth->setSpecialValueText(QString::fromUtf8(C_INFINITY));
m_ui->spinBandwidth->setSuffix("");
}
if (val > m_ui->bandwidthSlider->maximum())
m_ui->bandwidthSlider->setMaximum(val);
m_ui->bandwidthSlider->setValue(val);
if (value > m_ui->bandwidthSlider->maximum())
m_ui->bandwidthSlider->setMaximum(value);
m_ui->bandwidthSlider->setValue(value);
}
long SpeedLimitDialog::getSpeedLimit() const
int SpeedLimitDialog::getSpeedLimit() const
{
long val = m_ui->bandwidthSlider->value();
if (val > 0)
return val;
return -1;
return m_ui->spinBandwidth->value();
}
void SpeedLimitDialog::setupDialog(long maxSlider, long val) const
void SpeedLimitDialog::setupDialog(long maxSlider, long val)
{
if (val < 0)
val = 0;
val = qMax<long>(0, val);
if (maxSlider <= 0)
maxSlider = 10000;
// This can happen for example if global rate limit is lower
// than torrent rate limit.
if (val > maxSlider)
maxSlider = val;
m_ui->bandwidthSlider->setMaximum(maxSlider);
m_ui->bandwidthSlider->setValue(val);
updateSpinValue(val);

View file

@ -31,9 +31,6 @@
#include <QDialog>
#include "base/bittorrent/session.h"
#include "base/utils/misc.h"
namespace Ui
{
class SpeedLimitDialog;
@ -46,15 +43,15 @@ class SpeedLimitDialog : public QDialog
public:
explicit SpeedLimitDialog(QWidget *parent);
~SpeedLimitDialog();
static long askSpeedLimit(QWidget *parent, bool *ok, QString title, long defaultVal, long maxVal=10240000);
static long askSpeedLimit(QWidget *parent, bool *ok, const QString &title, long defaultVal, long maxVal = 10240000);
protected slots:
void updateSpinValue(int val) const;
void updateSliderValue(int val) const;
void setupDialog(long maxSlider, long val) const;
private slots:
void updateSpinValue(int val);
void updateSliderValue(int val);
private:
long getSpeedLimit() const;
void setupDialog(long maxSlider, long val);
int getSpeedLimit() const;
Ui::SpeedLimitDialog *m_ui;
};

View file

@ -22,12 +22,15 @@
</item>
<item>
<widget class="QSpinBox" name="spinBandwidth">
<property name="specialValueText">
<string>∞</string>
</property>
<property name="suffix">
<string> KiB/s</string>
</property>
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
</item>
</layout>