Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / speedlimitdialog.cpp
blobadae8f56c98af41f2a84c31711b9fafa606781d9
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "speedlimitdialog.h"
31 #include <QStyle>
33 #include "base/bittorrent/session.h"
34 #include "ui_speedlimitdialog.h"
35 #include "uithememanager.h"
36 #include "utils.h"
38 #define SETTINGS_KEY(name) "SpeedLimitDialog/" name
40 namespace
42 void updateSliderValue(QSlider *slider, const int value)
44 if (value > slider->maximum())
45 slider->setMaximum(value);
46 slider->setValue(value);
50 SpeedLimitDialog::SpeedLimitDialog(QWidget *parent)
51 : QDialog {parent}
52 , m_ui {new Ui::SpeedLimitDialog}
53 , m_storeDialogSize {SETTINGS_KEY("Size")}
55 m_ui->setupUi(this);
57 m_ui->labelGlobalSpeedIcon->setPixmap(Utils::Gui::scaledPixmap(UIThemeManager::instance()->getIcon(QLatin1String("slow_off"))
58 , this, style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this)));
59 m_ui->labelAltGlobalSpeedIcon->setPixmap(Utils::Gui::scaledPixmap(UIThemeManager::instance()->getIcon(QLatin1String("slow"))
60 , this, style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this)));
62 const auto initSlider = [](QSlider *slider, const int value, const int maximum)
64 slider->setMaximum(maximum);
65 slider->setValue(value);
67 const auto *session = BitTorrent::Session::instance();
68 const int uploadVal = qMax(0, (session->globalUploadSpeedLimit() / 1024));
69 const int downloadVal = qMax(0, (session->globalDownloadSpeedLimit() / 1024));
70 const int maxUpload = qMax(10000, (session->globalUploadSpeedLimit() / 1024));
71 const int maxDownload = qMax(10000, (session->globalDownloadSpeedLimit() / 1024));
72 initSlider(m_ui->sliderUploadLimit, uploadVal, maxUpload);
73 initSlider(m_ui->sliderDownloadLimit, downloadVal, maxDownload);
75 const int altUploadVal = qMax(0, (session->altGlobalUploadSpeedLimit() / 1024));
76 const int altDownloadVal = qMax(0, (session->altGlobalDownloadSpeedLimit() / 1024));
77 const int altMaxUpload = qMax(10000, (session->altGlobalUploadSpeedLimit() / 1024));
78 const int altMaxDownload = qMax(10000, (session->altGlobalDownloadSpeedLimit() / 1024));
79 initSlider(m_ui->sliderAltUploadLimit, altUploadVal, altMaxUpload);
80 initSlider(m_ui->sliderAltDownloadLimit, altDownloadVal, altMaxDownload);
82 m_ui->spinUploadLimit->setValue(uploadVal);
83 m_ui->spinDownloadLimit->setValue(downloadVal);
84 m_ui->spinAltUploadLimit->setValue(altUploadVal);
85 m_ui->spinAltDownloadLimit->setValue(altDownloadVal);
87 m_initialValues =
89 m_ui->spinUploadLimit->value(),
90 m_ui->spinDownloadLimit->value(),
91 m_ui->spinAltUploadLimit->value(),
92 m_ui->spinAltDownloadLimit->value()
95 // Sync up/down speed limit sliders with their corresponding spinboxes
96 connect(m_ui->sliderUploadLimit, &QSlider::valueChanged, m_ui->spinUploadLimit, &QSpinBox::setValue);
97 connect(m_ui->sliderDownloadLimit, &QSlider::valueChanged, m_ui->spinDownloadLimit, &QSpinBox::setValue);
98 connect(m_ui->spinUploadLimit, qOverload<int>(&QSpinBox::valueChanged)
99 , this, [this](const int value) { updateSliderValue(m_ui->sliderUploadLimit, value); });
100 connect(m_ui->spinDownloadLimit, qOverload<int>(&QSpinBox::valueChanged)
101 , this, [this](const int value) { updateSliderValue(m_ui->sliderDownloadLimit, value); });
102 connect(m_ui->sliderAltUploadLimit, &QSlider::valueChanged, m_ui->spinAltUploadLimit, &QSpinBox::setValue);
103 connect(m_ui->sliderAltDownloadLimit, &QSlider::valueChanged, m_ui->spinAltDownloadLimit, &QSpinBox::setValue);
104 connect(m_ui->spinAltUploadLimit, qOverload<int>(&QSpinBox::valueChanged)
105 , this, [this](const int value) { updateSliderValue(m_ui->sliderAltUploadLimit, value); });
106 connect(m_ui->spinAltDownloadLimit, qOverload<int>(&QSpinBox::valueChanged)
107 , this, [this](const int value) { updateSliderValue(m_ui->sliderAltDownloadLimit, value); });
109 Utils::Gui::resize(this, m_storeDialogSize);
112 SpeedLimitDialog::~SpeedLimitDialog()
114 m_storeDialogSize = size();
115 delete m_ui;
118 void SpeedLimitDialog::accept()
120 auto *session = BitTorrent::Session::instance();
121 const int uploadLimit = (m_ui->spinUploadLimit->value() * 1024);
122 if (m_initialValues.uploadSpeedLimit != m_ui->spinUploadLimit->value())
123 session->setGlobalUploadSpeedLimit(uploadLimit);
125 const int downloadLimit = (m_ui->spinDownloadLimit->value() * 1024);
126 if (m_initialValues.downloadSpeedLimit != m_ui->spinDownloadLimit->value())
127 session->setGlobalDownloadSpeedLimit(downloadLimit);
129 const int altUploadLimit = (m_ui->spinAltUploadLimit->value() * 1024);
130 if (m_initialValues.altUploadSpeedLimit != m_ui->spinAltUploadLimit->value())
131 session->setAltGlobalUploadSpeedLimit(altUploadLimit);
133 const int altDownloadLimit = (m_ui->spinAltDownloadLimit->value() * 1024);
134 if (m_initialValues.altDownloadSpeedLimit != m_ui->spinAltDownloadLimit->value())
135 session->setAltGlobalDownloadSpeedLimit(altDownloadLimit);
137 QDialog::accept();