|
|
|
@ -28,80 +28,107 @@
@@ -28,80 +28,107 @@
|
|
|
|
|
|
|
|
|
|
#include "speedlimitdialog.h" |
|
|
|
|
|
|
|
|
|
#include <QStyle> |
|
|
|
|
|
|
|
|
|
#include "base/bittorrent/session.h" |
|
|
|
|
#include "ui_speedlimitdialog.h" |
|
|
|
|
#include "uithememanager.h" |
|
|
|
|
#include "utils.h" |
|
|
|
|
|
|
|
|
|
SpeedLimitDialog::SpeedLimitDialog(QWidget *parent) |
|
|
|
|
: QDialog(parent) |
|
|
|
|
, m_ui(new Ui::SpeedLimitDialog) |
|
|
|
|
namespace |
|
|
|
|
{ |
|
|
|
|
m_ui->setupUi(this); |
|
|
|
|
|
|
|
|
|
// Connect to slots
|
|
|
|
|
connect(m_ui->bandwidthSlider, &QSlider::valueChanged, this, &SpeedLimitDialog::updateSpinValue); |
|
|
|
|
connect(m_ui->spinBandwidth, qOverload<int>(&QSpinBox::valueChanged) |
|
|
|
|
, this, &SpeedLimitDialog::updateSliderValue); |
|
|
|
|
|
|
|
|
|
Utils::Gui::resize(this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
SpeedLimitDialog::~SpeedLimitDialog() |
|
|
|
|
{ |
|
|
|
|
delete m_ui; |
|
|
|
|
void updateSliderValue(QSlider *slider, const int value) |
|
|
|
|
{ |
|
|
|
|
if (value > slider->maximum()) |
|
|
|
|
slider->setMaximum(value); |
|
|
|
|
slider->setValue(value); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// -2: if cancel
|
|
|
|
|
long SpeedLimitDialog::askSpeedLimit(QWidget *parent, bool *ok, const QString &title, const long defaultVal, const long maxVal) |
|
|
|
|
SpeedLimitDialog::SpeedLimitDialog(QWidget *parent) |
|
|
|
|
: QDialog {parent} |
|
|
|
|
, m_ui {new Ui::SpeedLimitDialog} |
|
|
|
|
{ |
|
|
|
|
if (ok) *ok = false; |
|
|
|
|
m_ui->setupUi(this); |
|
|
|
|
|
|
|
|
|
SpeedLimitDialog dlg(parent); |
|
|
|
|
dlg.setWindowTitle(title); |
|
|
|
|
dlg.setupDialog((maxVal / 1024.), (defaultVal / 1024.)); |
|
|
|
|
m_ui->labelGlobalSpeedIcon->setPixmap(Utils::Gui::scaledPixmap(UIThemeManager::instance()->getIcon(QLatin1String("slow_off")) |
|
|
|
|
, this, style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this))); |
|
|
|
|
m_ui->labelAltGlobalSpeedIcon->setPixmap(Utils::Gui::scaledPixmap(UIThemeManager::instance()->getIcon(QLatin1String("slow")) |
|
|
|
|
, this, style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this))); |
|
|
|
|
|
|
|
|
|
if (dlg.exec() == QDialog::Accepted) |
|
|
|
|
const auto initSlider = [](QSlider *slider, const int value, const int maximum) |
|
|
|
|
{ |
|
|
|
|
if (ok) *ok = true; |
|
|
|
|
|
|
|
|
|
const int val = dlg.getSpeedLimit(); |
|
|
|
|
if (val < 0) |
|
|
|
|
return 0; |
|
|
|
|
return (val * 1024); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return -2; |
|
|
|
|
} |
|
|
|
|
slider->setMaximum(maximum); |
|
|
|
|
slider->setValue(value); |
|
|
|
|
}; |
|
|
|
|
const auto *session = BitTorrent::Session::instance(); |
|
|
|
|
const int uploadVal = qMax(0, (session->globalUploadSpeedLimit() / 1024)); |
|
|
|
|
const int downloadVal = qMax(0, (session->globalDownloadSpeedLimit() / 1024)); |
|
|
|
|
const int maxUpload = qMax(10000, (session->globalUploadSpeedLimit() / 1024)); |
|
|
|
|
const int maxDownload = qMax(10000, (session->globalDownloadSpeedLimit() / 1024)); |
|
|
|
|
initSlider(m_ui->sliderUploadLimit, uploadVal, maxUpload); |
|
|
|
|
initSlider(m_ui->sliderDownloadLimit, downloadVal, maxDownload); |
|
|
|
|
|
|
|
|
|
const int altUploadVal = qMax(0, (session->altGlobalUploadSpeedLimit() / 1024)); |
|
|
|
|
const int altDownloadVal = qMax(0, (session->altGlobalDownloadSpeedLimit() / 1024)); |
|
|
|
|
const int altMaxUpload = qMax(10000, (session->altGlobalUploadSpeedLimit() / 1024)); |
|
|
|
|
const int altMaxDownload = qMax(10000, (session->altGlobalDownloadSpeedLimit() / 1024)); |
|
|
|
|
initSlider(m_ui->sliderAltUploadLimit, altUploadVal, altMaxUpload); |
|
|
|
|
initSlider(m_ui->sliderAltDownloadLimit, altDownloadVal, altMaxDownload); |
|
|
|
|
|
|
|
|
|
m_ui->spinUploadLimit->setValue(uploadVal); |
|
|
|
|
m_ui->spinDownloadLimit->setValue(downloadVal); |
|
|
|
|
m_ui->spinAltUploadLimit->setValue(altUploadVal); |
|
|
|
|
m_ui->spinAltDownloadLimit->setValue(altDownloadVal); |
|
|
|
|
|
|
|
|
|
m_initialValues = |
|
|
|
|
{ |
|
|
|
|
m_ui->spinUploadLimit->value(), |
|
|
|
|
m_ui->spinDownloadLimit->value(), |
|
|
|
|
m_ui->spinAltUploadLimit->value(), |
|
|
|
|
m_ui->spinAltDownloadLimit->value() |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Sync up/down speed limit sliders with their corresponding spinboxes
|
|
|
|
|
connect(m_ui->sliderUploadLimit, &QSlider::valueChanged, m_ui->spinUploadLimit, &QSpinBox::setValue); |
|
|
|
|
connect(m_ui->sliderDownloadLimit, &QSlider::valueChanged, m_ui->spinDownloadLimit, &QSpinBox::setValue); |
|
|
|
|
connect(m_ui->spinUploadLimit, qOverload<int>(&QSpinBox::valueChanged) |
|
|
|
|
, this, [this](const int value) { updateSliderValue(m_ui->sliderUploadLimit, value); }); |
|
|
|
|
connect(m_ui->spinDownloadLimit, qOverload<int>(&QSpinBox::valueChanged) |
|
|
|
|
, this, [this](const int value) { updateSliderValue(m_ui->sliderDownloadLimit, value); }); |
|
|
|
|
connect(m_ui->sliderAltUploadLimit, &QSlider::valueChanged, m_ui->spinAltUploadLimit, &QSpinBox::setValue); |
|
|
|
|
connect(m_ui->sliderAltDownloadLimit, &QSlider::valueChanged, m_ui->spinAltDownloadLimit, &QSpinBox::setValue); |
|
|
|
|
connect(m_ui->spinAltUploadLimit, qOverload<int>(&QSpinBox::valueChanged) |
|
|
|
|
, this, [this](const int value) { updateSliderValue(m_ui->sliderAltUploadLimit, value); }); |
|
|
|
|
connect(m_ui->spinAltDownloadLimit, qOverload<int>(&QSpinBox::valueChanged) |
|
|
|
|
, this, [this](const int value) { updateSliderValue(m_ui->sliderAltDownloadLimit, value); }); |
|
|
|
|
|
|
|
|
|
void SpeedLimitDialog::updateSpinValue(const int value) |
|
|
|
|
{ |
|
|
|
|
m_ui->spinBandwidth->setValue(value); |
|
|
|
|
Utils::Gui::resize(this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void SpeedLimitDialog::updateSliderValue(const int value) |
|
|
|
|
SpeedLimitDialog::~SpeedLimitDialog() |
|
|
|
|
{ |
|
|
|
|
if (value > m_ui->bandwidthSlider->maximum()) |
|
|
|
|
m_ui->bandwidthSlider->setMaximum(value); |
|
|
|
|
m_ui->bandwidthSlider->setValue(value); |
|
|
|
|
delete m_ui; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int SpeedLimitDialog::getSpeedLimit() const |
|
|
|
|
void SpeedLimitDialog::accept() |
|
|
|
|
{ |
|
|
|
|
return m_ui->spinBandwidth->value(); |
|
|
|
|
} |
|
|
|
|
auto *session = BitTorrent::Session::instance(); |
|
|
|
|
const int uploadLimit = (m_ui->spinUploadLimit->value() * 1024); |
|
|
|
|
if (m_initialValues.uploadSpeedLimit != m_ui->spinUploadLimit->value()) |
|
|
|
|
session->setGlobalUploadSpeedLimit(uploadLimit); |
|
|
|
|
|
|
|
|
|
void SpeedLimitDialog::setupDialog(long maxSlider, long val) |
|
|
|
|
{ |
|
|
|
|
val = qMax<long>(0, val); |
|
|
|
|
const int downloadLimit = (m_ui->spinDownloadLimit->value() * 1024); |
|
|
|
|
if (m_initialValues.downloadSpeedLimit != m_ui->spinDownloadLimit->value()) |
|
|
|
|
session->setGlobalDownloadSpeedLimit(downloadLimit); |
|
|
|
|
|
|
|
|
|
if (maxSlider <= 0) |
|
|
|
|
maxSlider = 10000; |
|
|
|
|
const int altUploadLimit = (m_ui->spinAltUploadLimit->value() * 1024); |
|
|
|
|
if (m_initialValues.altUploadSpeedLimit != m_ui->spinAltUploadLimit->value()) |
|
|
|
|
session->setAltGlobalUploadSpeedLimit(altUploadLimit); |
|
|
|
|
|
|
|
|
|
// This can happen for example if global rate limit is lower
|
|
|
|
|
// than torrent rate limit.
|
|
|
|
|
if (val > maxSlider) |
|
|
|
|
maxSlider = val; |
|
|
|
|
const int altDownloadLimit = (m_ui->spinAltDownloadLimit->value() * 1024); |
|
|
|
|
if (m_initialValues.altDownloadSpeedLimit != m_ui->spinAltDownloadLimit->value()) |
|
|
|
|
session->setAltGlobalDownloadSpeedLimit(altDownloadLimit); |
|
|
|
|
|
|
|
|
|
m_ui->bandwidthSlider->setMaximum(maxSlider); |
|
|
|
|
m_ui->bandwidthSlider->setValue(val); |
|
|
|
|
updateSpinValue(val); |
|
|
|
|
QDialog::accept(); |
|
|
|
|
} |
|
|
|
|