Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / speedlimitdialog.cpp
blobdfd524064f47a1102eeadfeabd8f8c89209b7bd8
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 <algorithm>
33 #include <QStyle>
35 #include "base/bittorrent/session.h"
36 #include "ui_speedlimitdialog.h"
37 #include "uithememanager.h"
38 #include "utils.h"
40 #define SETTINGS_KEY(name) u"SpeedLimitDialog/" name
42 namespace
44 void updateSliderValue(QSlider *slider, const int value)
46 if (value > slider->maximum())
47 slider->setMaximum(value);
48 slider->setValue(value);
52 SpeedLimitDialog::SpeedLimitDialog(QWidget *parent)
53 : QDialog {parent}
54 , m_ui {new Ui::SpeedLimitDialog}
55 , m_storeDialogSize {SETTINGS_KEY(u"Size"_s)}
57 m_ui->setupUi(this);
59 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
60 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
62 m_ui->labelGlobalSpeedIcon->setPixmap(
63 UIThemeManager::instance()->getScaledPixmap(u"slow_off"_s, Utils::Gui::mediumIconSize(this).height()));
64 m_ui->labelAltGlobalSpeedIcon->setPixmap(
65 UIThemeManager::instance()->getScaledPixmap(u"slow"_s, Utils::Gui::mediumIconSize(this).height()));
67 const auto initSlider = [](QSlider *slider, const int value, const int maximum)
69 slider->setMaximum(maximum);
70 slider->setValue(value);
72 const auto *session = BitTorrent::Session::instance();
73 const int uploadVal = std::max(0, (session->globalUploadSpeedLimit() / 1024));
74 const int downloadVal = std::max(0, (session->globalDownloadSpeedLimit() / 1024));
75 const int maxUpload = std::max(10000, (session->globalUploadSpeedLimit() / 1024));
76 const int maxDownload = std::max(10000, (session->globalDownloadSpeedLimit() / 1024));
77 initSlider(m_ui->sliderUploadLimit, uploadVal, maxUpload);
78 initSlider(m_ui->sliderDownloadLimit, downloadVal, maxDownload);
80 const int altUploadVal = std::max(0, (session->altGlobalUploadSpeedLimit() / 1024));
81 const int altDownloadVal = std::max(0, (session->altGlobalDownloadSpeedLimit() / 1024));
82 const int altMaxUpload = std::max(10000, (session->altGlobalUploadSpeedLimit() / 1024));
83 const int altMaxDownload = std::max(10000, (session->altGlobalDownloadSpeedLimit() / 1024));
84 initSlider(m_ui->sliderAltUploadLimit, altUploadVal, altMaxUpload);
85 initSlider(m_ui->sliderAltDownloadLimit, altDownloadVal, altMaxDownload);
87 m_ui->spinUploadLimit->setValue(uploadVal);
88 m_ui->spinDownloadLimit->setValue(downloadVal);
89 m_ui->spinAltUploadLimit->setValue(altUploadVal);
90 m_ui->spinAltDownloadLimit->setValue(altDownloadVal);
92 m_initialValues =
94 m_ui->spinUploadLimit->value(),
95 m_ui->spinDownloadLimit->value(),
96 m_ui->spinAltUploadLimit->value(),
97 m_ui->spinAltDownloadLimit->value()
100 // Sync up/down speed limit sliders with their corresponding spinboxes
101 connect(m_ui->sliderUploadLimit, &QSlider::valueChanged, m_ui->spinUploadLimit, &QSpinBox::setValue);
102 connect(m_ui->sliderDownloadLimit, &QSlider::valueChanged, m_ui->spinDownloadLimit, &QSpinBox::setValue);
103 connect(m_ui->spinUploadLimit, qOverload<int>(&QSpinBox::valueChanged)
104 , this, [this](const int value) { updateSliderValue(m_ui->sliderUploadLimit, value); });
105 connect(m_ui->spinDownloadLimit, qOverload<int>(&QSpinBox::valueChanged)
106 , this, [this](const int value) { updateSliderValue(m_ui->sliderDownloadLimit, value); });
107 connect(m_ui->sliderAltUploadLimit, &QSlider::valueChanged, m_ui->spinAltUploadLimit, &QSpinBox::setValue);
108 connect(m_ui->sliderAltDownloadLimit, &QSlider::valueChanged, m_ui->spinAltDownloadLimit, &QSpinBox::setValue);
109 connect(m_ui->spinAltUploadLimit, qOverload<int>(&QSpinBox::valueChanged)
110 , this, [this](const int value) { updateSliderValue(m_ui->sliderAltUploadLimit, value); });
111 connect(m_ui->spinAltDownloadLimit, qOverload<int>(&QSpinBox::valueChanged)
112 , this, [this](const int value) { updateSliderValue(m_ui->sliderAltDownloadLimit, value); });
114 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
115 resize(dialogSize);
118 SpeedLimitDialog::~SpeedLimitDialog()
120 m_storeDialogSize = size();
121 delete m_ui;
124 void SpeedLimitDialog::accept()
126 auto *session = BitTorrent::Session::instance();
127 const int uploadLimit = (m_ui->spinUploadLimit->value() * 1024);
128 if (m_initialValues.uploadSpeedLimit != m_ui->spinUploadLimit->value())
129 session->setGlobalUploadSpeedLimit(uploadLimit);
131 const int downloadLimit = (m_ui->spinDownloadLimit->value() * 1024);
132 if (m_initialValues.downloadSpeedLimit != m_ui->spinDownloadLimit->value())
133 session->setGlobalDownloadSpeedLimit(downloadLimit);
135 const int altUploadLimit = (m_ui->spinAltUploadLimit->value() * 1024);
136 if (m_initialValues.altUploadSpeedLimit != m_ui->spinAltUploadLimit->value())
137 session->setAltGlobalUploadSpeedLimit(altUploadLimit);
139 const int altDownloadLimit = (m_ui->spinAltDownloadLimit->value() * 1024);
140 if (m_initialValues.altDownloadSpeedLimit != m_ui->spinAltDownloadLimit->value())
141 session->setAltGlobalDownloadSpeedLimit(altDownloadLimit);
143 QDialog::accept();