Use tray icon from system theme only if option is set
[qBittorrent.git] / src / gui / speedlimitdialog.cpp
blobeebd306dbd071263fd3b9b5a73bca0c5999eb493
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"_qs)}
57 m_ui->setupUi(this);
59 m_ui->labelGlobalSpeedIcon->setPixmap(
60 UIThemeManager::instance()->getScaledPixmap(u"slow_off"_qs, Utils::Gui::mediumIconSize(this).height()));
61 m_ui->labelAltGlobalSpeedIcon->setPixmap(
62 UIThemeManager::instance()->getScaledPixmap(u"slow"_qs, Utils::Gui::mediumIconSize(this).height()));
64 const auto initSlider = [](QSlider *slider, const int value, const int maximum)
66 slider->setMaximum(maximum);
67 slider->setValue(value);
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);
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);
84 m_ui->spinUploadLimit->setValue(uploadVal);
85 m_ui->spinDownloadLimit->setValue(downloadVal);
86 m_ui->spinAltUploadLimit->setValue(altUploadVal);
87 m_ui->spinAltDownloadLimit->setValue(altDownloadVal);
89 m_initialValues =
91 m_ui->spinUploadLimit->value(),
92 m_ui->spinDownloadLimit->value(),
93 m_ui->spinAltUploadLimit->value(),
94 m_ui->spinAltDownloadLimit->value()
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); });
111 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
112 resize(dialogSize);
115 SpeedLimitDialog::~SpeedLimitDialog()
117 m_storeDialogSize = size();
118 delete m_ui;
121 void SpeedLimitDialog::accept()
123 auto *session = BitTorrent::Session::instance();
124 const int uploadLimit = (m_ui->spinUploadLimit->value() * 1024);
125 if (m_initialValues.uploadSpeedLimit != m_ui->spinUploadLimit->value())
126 session->setGlobalUploadSpeedLimit(uploadLimit);
128 const int downloadLimit = (m_ui->spinDownloadLimit->value() * 1024);
129 if (m_initialValues.downloadSpeedLimit != m_ui->spinDownloadLimit->value())
130 session->setGlobalDownloadSpeedLimit(downloadLimit);
132 const int altUploadLimit = (m_ui->spinAltUploadLimit->value() * 1024);
133 if (m_initialValues.altUploadSpeedLimit != m_ui->spinAltUploadLimit->value())
134 session->setAltGlobalUploadSpeedLimit(altUploadLimit);
136 const int altDownloadLimit = (m_ui->spinAltDownloadLimit->value() * 1024);
137 if (m_initialValues.altDownloadSpeedLimit != m_ui->spinAltDownloadLimit->value())
138 session->setAltGlobalDownloadSpeedLimit(altDownloadLimit);
140 QDialog::accept();