|
|
@ -28,80 +28,107 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "speedlimitdialog.h" |
|
|
|
#include "speedlimitdialog.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <QStyle> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "base/bittorrent/session.h" |
|
|
|
#include "ui_speedlimitdialog.h" |
|
|
|
#include "ui_speedlimitdialog.h" |
|
|
|
|
|
|
|
#include "uithememanager.h" |
|
|
|
#include "utils.h" |
|
|
|
#include "utils.h" |
|
|
|
|
|
|
|
|
|
|
|
SpeedLimitDialog::SpeedLimitDialog(QWidget *parent) |
|
|
|
namespace |
|
|
|
: QDialog(parent) |
|
|
|
|
|
|
|
, m_ui(new Ui::SpeedLimitDialog) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
m_ui->setupUi(this); |
|
|
|
void updateSliderValue(QSlider *slider, const int value) |
|
|
|
|
|
|
|
{ |
|
|
|
// Connect to slots
|
|
|
|
if (value > slider->maximum()) |
|
|
|
connect(m_ui->bandwidthSlider, &QSlider::valueChanged, this, &SpeedLimitDialog::updateSpinValue); |
|
|
|
slider->setMaximum(value); |
|
|
|
connect(m_ui->spinBandwidth, qOverload<int>(&QSpinBox::valueChanged) |
|
|
|
slider->setValue(value); |
|
|
|
, this, &SpeedLimitDialog::updateSliderValue); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Utils::Gui::resize(this); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SpeedLimitDialog::~SpeedLimitDialog() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
delete m_ui; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// -2: if cancel
|
|
|
|
SpeedLimitDialog::SpeedLimitDialog(QWidget *parent) |
|
|
|
long SpeedLimitDialog::askSpeedLimit(QWidget *parent, bool *ok, const QString &title, const long defaultVal, const long maxVal) |
|
|
|
: QDialog {parent} |
|
|
|
|
|
|
|
, m_ui {new Ui::SpeedLimitDialog} |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (ok) *ok = false; |
|
|
|
m_ui->setupUi(this); |
|
|
|
|
|
|
|
|
|
|
|
SpeedLimitDialog dlg(parent); |
|
|
|
m_ui->labelGlobalSpeedIcon->setPixmap(Utils::Gui::scaledPixmap(UIThemeManager::instance()->getIcon(QLatin1String("slow_off")) |
|
|
|
dlg.setWindowTitle(title); |
|
|
|
, this, style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this))); |
|
|
|
dlg.setupDialog((maxVal / 1024.), (defaultVal / 1024.)); |
|
|
|
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; |
|
|
|
slider->setMaximum(maximum); |
|
|
|
|
|
|
|
slider->setValue(value); |
|
|
|
const int val = dlg.getSpeedLimit(); |
|
|
|
}; |
|
|
|
if (val < 0) |
|
|
|
const auto *session = BitTorrent::Session::instance(); |
|
|
|
return 0; |
|
|
|
const int uploadVal = qMax(0, (session->globalUploadSpeedLimit() / 1024)); |
|
|
|
return (val * 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)); |
|
|
|
return -2; |
|
|
|
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) |
|
|
|
Utils::Gui::resize(this); |
|
|
|
{ |
|
|
|
|
|
|
|
m_ui->spinBandwidth->setValue(value); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SpeedLimitDialog::updateSliderValue(const int value) |
|
|
|
SpeedLimitDialog::~SpeedLimitDialog() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (value > m_ui->bandwidthSlider->maximum()) |
|
|
|
delete m_ui; |
|
|
|
m_ui->bandwidthSlider->setMaximum(value); |
|
|
|
|
|
|
|
m_ui->bandwidthSlider->setValue(value); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
const int downloadLimit = (m_ui->spinDownloadLimit->value() * 1024); |
|
|
|
{ |
|
|
|
if (m_initialValues.downloadSpeedLimit != m_ui->spinDownloadLimit->value()) |
|
|
|
val = qMax<long>(0, val); |
|
|
|
session->setGlobalDownloadSpeedLimit(downloadLimit); |
|
|
|
|
|
|
|
|
|
|
|
if (maxSlider <= 0) |
|
|
|
const int altUploadLimit = (m_ui->spinAltUploadLimit->value() * 1024); |
|
|
|
maxSlider = 10000; |
|
|
|
if (m_initialValues.altUploadSpeedLimit != m_ui->spinAltUploadLimit->value()) |
|
|
|
|
|
|
|
session->setAltGlobalUploadSpeedLimit(altUploadLimit); |
|
|
|
|
|
|
|
|
|
|
|
// This can happen for example if global rate limit is lower
|
|
|
|
const int altDownloadLimit = (m_ui->spinAltDownloadLimit->value() * 1024); |
|
|
|
// than torrent rate limit.
|
|
|
|
if (m_initialValues.altDownloadSpeedLimit != m_ui->spinAltDownloadLimit->value()) |
|
|
|
if (val > maxSlider) |
|
|
|
session->setAltGlobalDownloadSpeedLimit(altDownloadLimit); |
|
|
|
maxSlider = val; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_ui->bandwidthSlider->setMaximum(maxSlider); |
|
|
|
QDialog::accept(); |
|
|
|
m_ui->bandwidthSlider->setValue(val); |
|
|
|
|
|
|
|
updateSpinValue(val); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|