qBittorrent
speedlimitdialog.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
28 
29 #include "speedlimitdialog.h"
30 
31 #include <algorithm>
32 
33 #include <QStyle>
34 
36 #include "ui_speedlimitdialog.h"
37 #include "uithememanager.h"
38 #include "utils.h"
39 
40 #define SETTINGS_KEY(name) "SpeedLimitDialog/" name
41 
42 namespace
43 {
44  void updateSliderValue(QSlider *slider, const int value)
45  {
46  if (value > slider->maximum())
47  slider->setMaximum(value);
48  slider->setValue(value);
49  }
50 }
51 
53  : QDialog {parent}
54  , m_ui {new Ui::SpeedLimitDialog}
55  , m_storeDialogSize {SETTINGS_KEY("Size")}
56 {
57  m_ui->setupUi(this);
58 
59  m_ui->labelGlobalSpeedIcon->setPixmap(Utils::Gui::scaledPixmap(UIThemeManager::instance()->getIcon(QLatin1String("slow_off"))
60  , this, style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this)));
61  m_ui->labelAltGlobalSpeedIcon->setPixmap(Utils::Gui::scaledPixmap(UIThemeManager::instance()->getIcon(QLatin1String("slow"))
62  , this, style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this)));
63 
64  const auto initSlider = [](QSlider *slider, const int value, const int maximum)
65  {
66  slider->setMaximum(maximum);
67  slider->setValue(value);
68  };
69  const auto *session = BitTorrent::Session::instance();
70  const int uploadVal = std::max(0, (session->globalUploadSpeedLimit() / 1024));
71  const int downloadVal = std::max(0, (session->globalDownloadSpeedLimit() / 1024));
72  const int maxUpload = std::max(10000, (session->globalUploadSpeedLimit() / 1024));
73  const int maxDownload = std::max(10000, (session->globalDownloadSpeedLimit() / 1024));
74  initSlider(m_ui->sliderUploadLimit, uploadVal, maxUpload);
75  initSlider(m_ui->sliderDownloadLimit, downloadVal, maxDownload);
76 
77  const int altUploadVal = std::max(0, (session->altGlobalUploadSpeedLimit() / 1024));
78  const int altDownloadVal = std::max(0, (session->altGlobalDownloadSpeedLimit() / 1024));
79  const int altMaxUpload = std::max(10000, (session->altGlobalUploadSpeedLimit() / 1024));
80  const int altMaxDownload = std::max(10000, (session->altGlobalDownloadSpeedLimit() / 1024));
81  initSlider(m_ui->sliderAltUploadLimit, altUploadVal, altMaxUpload);
82  initSlider(m_ui->sliderAltDownloadLimit, altDownloadVal, altMaxDownload);
83 
84  m_ui->spinUploadLimit->setValue(uploadVal);
85  m_ui->spinDownloadLimit->setValue(downloadVal);
86  m_ui->spinAltUploadLimit->setValue(altUploadVal);
87  m_ui->spinAltDownloadLimit->setValue(altDownloadVal);
88 
90  {
91  m_ui->spinUploadLimit->value(),
92  m_ui->spinDownloadLimit->value(),
93  m_ui->spinAltUploadLimit->value(),
94  m_ui->spinAltDownloadLimit->value()
95  };
96 
97  // Sync up/down speed limit sliders with their corresponding spinboxes
98  connect(m_ui->sliderUploadLimit, &QSlider::valueChanged, m_ui->spinUploadLimit, &QSpinBox::setValue);
99  connect(m_ui->sliderDownloadLimit, &QSlider::valueChanged, m_ui->spinDownloadLimit, &QSpinBox::setValue);
100  connect(m_ui->spinUploadLimit, qOverload<int>(&QSpinBox::valueChanged)
101  , this, [this](const int value) { updateSliderValue(m_ui->sliderUploadLimit, value); });
102  connect(m_ui->spinDownloadLimit, qOverload<int>(&QSpinBox::valueChanged)
103  , this, [this](const int value) { updateSliderValue(m_ui->sliderDownloadLimit, value); });
104  connect(m_ui->sliderAltUploadLimit, &QSlider::valueChanged, m_ui->spinAltUploadLimit, &QSpinBox::setValue);
105  connect(m_ui->sliderAltDownloadLimit, &QSlider::valueChanged, m_ui->spinAltDownloadLimit, &QSpinBox::setValue);
106  connect(m_ui->spinAltUploadLimit, qOverload<int>(&QSpinBox::valueChanged)
107  , this, [this](const int value) { updateSliderValue(m_ui->sliderAltUploadLimit, value); });
108  connect(m_ui->spinAltDownloadLimit, qOverload<int>(&QSpinBox::valueChanged)
109  , this, [this](const int value) { updateSliderValue(m_ui->sliderAltDownloadLimit, value); });
110 
112 }
113 
115 {
116  m_storeDialogSize = size();
117  delete m_ui;
118 }
119 
121 {
122  auto *session = BitTorrent::Session::instance();
123  const int uploadLimit = (m_ui->spinUploadLimit->value() * 1024);
124  if (m_initialValues.uploadSpeedLimit != m_ui->spinUploadLimit->value())
125  session->setGlobalUploadSpeedLimit(uploadLimit);
126 
127  const int downloadLimit = (m_ui->spinDownloadLimit->value() * 1024);
128  if (m_initialValues.downloadSpeedLimit != m_ui->spinDownloadLimit->value())
129  session->setGlobalDownloadSpeedLimit(downloadLimit);
130 
131  const int altUploadLimit = (m_ui->spinAltUploadLimit->value() * 1024);
132  if (m_initialValues.altUploadSpeedLimit != m_ui->spinAltUploadLimit->value())
133  session->setAltGlobalUploadSpeedLimit(altUploadLimit);
134 
135  const int altDownloadLimit = (m_ui->spinAltDownloadLimit->value() * 1024);
136  if (m_initialValues.altDownloadSpeedLimit != m_ui->spinAltDownloadLimit->value())
137  session->setAltGlobalDownloadSpeedLimit(altDownloadLimit);
138 
139  QDialog::accept();
140 }
static Session * instance()
Definition: session.cpp:997
SpeedLimitDialog(QWidget *parent)
Ui::SpeedLimitDialog * m_ui
~SpeedLimitDialog() override
SettingValue< QSize > m_storeDialogSize
struct SpeedLimitDialog::@9 m_initialValues
void accept() override
static UIThemeManager * instance()
QPixmap scaledPixmap(const QIcon &icon, const QWidget *widget, int height)
Definition: utils.cpp:68
void resize(QWidget *widget, const QSize &newSize={})
Definition: utils.cpp:54
void setValue(const QString &key, const T &value)
Definition: preferences.cpp:76
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64
void updateSliderValue(QSlider *slider, const int value)
#define SETTINGS_KEY(name)