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. 64
      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 @@
#include <QWheelEvent> #include <QWheelEvent>
#include "base/bittorrent/peerinfo.h" #include "base/bittorrent/peerinfo.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrenthandle.h" #include "base/bittorrent/torrenthandle.h"
#include "base/logger.h" #include "base/logger.h"
#include "base/net/geoipmanager.h" #include "base/net/geoipmanager.h"

64
src/gui/speedlimitdialog.cpp

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

15
src/gui/speedlimitdialog.h

@ -31,9 +31,6 @@
#include <QDialog> #include <QDialog>
#include "base/bittorrent/session.h"
#include "base/utils/misc.h"
namespace Ui namespace Ui
{ {
class SpeedLimitDialog; class SpeedLimitDialog;
@ -46,15 +43,15 @@ class SpeedLimitDialog : public QDialog
public: public:
explicit SpeedLimitDialog(QWidget *parent); explicit SpeedLimitDialog(QWidget *parent);
~SpeedLimitDialog(); ~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: private slots:
void updateSpinValue(int val) const; void updateSpinValue(int val);
void updateSliderValue(int val) const; void updateSliderValue(int val);
void setupDialog(long maxSlider, long val) const;
private: private:
long getSpeedLimit() const; void setupDialog(long maxSlider, long val);
int getSpeedLimit() const;
Ui::SpeedLimitDialog *m_ui; Ui::SpeedLimitDialog *m_ui;
}; };

9
src/gui/speedlimitdialog.ui

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

Loading…
Cancel
Save