Browse Source

Clean up SpeedLimitDialog class

adaptive-webui-19844
Chocobo1 6 years ago
parent
commit
266ce1d226
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 1
      src/gui/properties/peerlistwidget.cpp
  2. 66
      src/gui/speedlimitdialog.cpp
  3. 15
      src/gui/speedlimitdialog.h
  4. 9
      src/gui/speedlimitdialog.ui

1
src/gui/properties/peerlistwidget.cpp

@ -40,6 +40,7 @@ @@ -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"

66
src/gui/speedlimitdialog.cpp

@ -28,16 +28,14 @@ @@ -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) @@ -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);

15
src/gui/speedlimitdialog.h

@ -31,9 +31,6 @@ @@ -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 @@ -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;
};

9
src/gui/speedlimitdialog.ui

@ -22,12 +22,15 @@ @@ -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>

Loading…
Cancel
Save