Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / statusbar.cpp
blobc990cca89778421b85921d5002870888aaeeb9ff
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 "statusbar.h"
31 #include <QApplication>
32 #include <QDebug>
33 #include <QFrame>
34 #include <QHBoxLayout>
35 #include <QLabel>
36 #include <QPushButton>
37 #include <QStyle>
39 #include "base/bittorrent/session.h"
40 #include "base/bittorrent/sessionstatus.h"
41 #include "base/utils/misc.h"
42 #include "speedlimitdialog.h"
43 #include "uithememanager.h"
44 #include "utils.h"
46 StatusBar::StatusBar(QWidget *parent)
47 : QStatusBar(parent)
49 #ifndef Q_OS_MACOS
50 // Redefining global stylesheet breaks certain elements on mac like tabs.
51 // Qt checks whether the stylesheet class inherts("QMacStyle") and this becomes false.
52 setStyleSheet("QStatusBar::item { border-width: 0; }");
53 #endif
55 BitTorrent::Session *const session = BitTorrent::Session::instance();
56 connect(session, &BitTorrent::Session::speedLimitModeChanged, this, &StatusBar::updateAltSpeedsBtn);
57 QWidget *container = new QWidget(this);
58 auto *layout = new QHBoxLayout(container);
59 layout->setContentsMargins(0,0,0,0);
61 container->setLayout(layout);
62 m_connecStatusLblIcon = new QPushButton(this);
63 m_connecStatusLblIcon->setFlat(true);
64 m_connecStatusLblIcon->setFocusPolicy(Qt::NoFocus);
65 m_connecStatusLblIcon->setCursor(Qt::PointingHandCursor);
66 m_connecStatusLblIcon->setIcon(UIThemeManager::instance()->getIcon(QLatin1String("firewalled")));
67 m_connecStatusLblIcon->setToolTip(
68 QString::fromLatin1("<b>%1</b><br><i>%2</i>").arg(tr("Connection status:")
69 , tr("No direct connections. This may indicate network configuration problems.")));
70 connect(m_connecStatusLblIcon, &QAbstractButton::clicked, this, &StatusBar::connectionButtonClicked);
72 m_dlSpeedLbl = new QPushButton(this);
73 m_dlSpeedLbl->setIcon(UIThemeManager::instance()->getIcon(QLatin1String("downloading_small")));
74 connect(m_dlSpeedLbl, &QAbstractButton::clicked, this, &StatusBar::capSpeed);
75 m_dlSpeedLbl->setFlat(true);
76 m_dlSpeedLbl->setFocusPolicy(Qt::NoFocus);
77 m_dlSpeedLbl->setCursor(Qt::PointingHandCursor);
78 m_dlSpeedLbl->setStyleSheet("text-align:left;");
79 m_dlSpeedLbl->setMinimumWidth(200);
81 m_upSpeedLbl = new QPushButton(this);
82 m_upSpeedLbl->setIcon(UIThemeManager::instance()->getIcon(QLatin1String("seeding")));
83 connect(m_upSpeedLbl, &QAbstractButton::clicked, this, &StatusBar::capSpeed);
84 m_upSpeedLbl->setFlat(true);
85 m_upSpeedLbl->setFocusPolicy(Qt::NoFocus);
86 m_upSpeedLbl->setCursor(Qt::PointingHandCursor);
87 m_upSpeedLbl->setStyleSheet("text-align:left;");
88 m_upSpeedLbl->setMinimumWidth(200);
90 m_DHTLbl = new QLabel(tr("DHT: %1 nodes").arg(0), this);
91 m_DHTLbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
93 m_altSpeedsBtn = new QPushButton(this);
94 m_altSpeedsBtn->setFlat(true);
95 m_altSpeedsBtn->setFocusPolicy(Qt::NoFocus);
96 m_altSpeedsBtn->setCursor(Qt::PointingHandCursor);
97 updateAltSpeedsBtn(session->isAltGlobalSpeedLimitEnabled());
98 connect(m_altSpeedsBtn, &QAbstractButton::clicked, this, &StatusBar::alternativeSpeedsButtonClicked);
100 // Because on some platforms the default icon size is bigger
101 // and it will result in taller/fatter statusbar, even if the
102 // icons are actually 16x16
103 m_connecStatusLblIcon->setIconSize(Utils::Gui::smallIconSize());
104 m_dlSpeedLbl->setIconSize(Utils::Gui::smallIconSize());
105 m_upSpeedLbl->setIconSize(Utils::Gui::smallIconSize());
106 m_altSpeedsBtn->setIconSize(QSize(Utils::Gui::mediumIconSize().width(), Utils::Gui::smallIconSize().height()));
108 // Set to the known maximum width(plus some padding)
109 // so the speed widgets will take the rest of the space
110 m_connecStatusLblIcon->setMaximumWidth(Utils::Gui::largeIconSize().width());
111 m_altSpeedsBtn->setMaximumWidth(Utils::Gui::largeIconSize().width());
113 QFrame *statusSep1 = new QFrame(this);
114 statusSep1->setFrameStyle(QFrame::VLine);
115 #ifndef Q_OS_MACOS
116 statusSep1->setFrameShadow(QFrame::Raised);
117 #endif
118 QFrame *statusSep2 = new QFrame(this);
119 statusSep2->setFrameStyle(QFrame::VLine);
120 #ifndef Q_OS_MACOS
121 statusSep2->setFrameShadow(QFrame::Raised);
122 #endif
123 QFrame *statusSep3 = new QFrame(this);
124 statusSep3->setFrameStyle(QFrame::VLine);
125 #ifndef Q_OS_MACOS
126 statusSep3->setFrameShadow(QFrame::Raised);
127 #endif
128 QFrame *statusSep4 = new QFrame(this);
129 statusSep4->setFrameStyle(QFrame::VLine);
130 #ifndef Q_OS_MACOS
131 statusSep4->setFrameShadow(QFrame::Raised);
132 #endif
133 layout->addWidget(m_DHTLbl);
134 layout->addWidget(statusSep1);
135 layout->addWidget(m_connecStatusLblIcon);
136 layout->addWidget(statusSep2);
137 layout->addWidget(m_altSpeedsBtn);
138 layout->addWidget(statusSep4);
139 layout->addWidget(m_dlSpeedLbl);
140 layout->addWidget(statusSep3);
141 layout->addWidget(m_upSpeedLbl);
143 addPermanentWidget(container);
144 setStyleSheet("QWidget {margin: 0;}");
145 container->adjustSize();
146 adjustSize();
147 // Is DHT enabled
148 m_DHTLbl->setVisible(session->isDHTEnabled());
149 refresh();
150 connect(session, &BitTorrent::Session::statsUpdated, this, &StatusBar::refresh);
153 StatusBar::~StatusBar()
155 qDebug() << Q_FUNC_INFO;
158 void StatusBar::showRestartRequired()
160 // Restart required notification
161 const QString restartText = tr("qBittorrent needs to be restarted!");
162 QLabel *restartIconLbl = new QLabel(this);
163 restartIconLbl->setPixmap(style()->standardPixmap(QStyle::SP_MessageBoxWarning));
164 restartIconLbl->setToolTip(restartText);
165 insertWidget(0, restartIconLbl);
167 QLabel *restartLbl = new QLabel(this);
168 restartLbl->setText(restartText);
169 insertWidget(1, restartLbl);
172 void StatusBar::updateConnectionStatus()
174 const BitTorrent::SessionStatus &sessionStatus = BitTorrent::Session::instance()->status();
176 if (!BitTorrent::Session::instance()->isListening())
178 m_connecStatusLblIcon->setIcon(UIThemeManager::instance()->getIcon(QLatin1String("disconnected")));
179 m_connecStatusLblIcon->setToolTip(QLatin1String("<b>") + tr("Connection Status:") + QLatin1String("</b><br>") + tr("Offline. This usually means that qBittorrent failed to listen on the selected port for incoming connections."));
181 else
183 if (sessionStatus.hasIncomingConnections)
185 // Connection OK
186 m_connecStatusLblIcon->setIcon(UIThemeManager::instance()->getIcon(QLatin1String("connected")));
187 m_connecStatusLblIcon->setToolTip(QLatin1String("<b>") + tr("Connection Status:") + QLatin1String("</b><br>") + tr("Online"));
189 else
191 m_connecStatusLblIcon->setIcon(UIThemeManager::instance()->getIcon(QLatin1String("firewalled")));
192 m_connecStatusLblIcon->setToolTip(QLatin1String("<b>") + tr("Connection status:") + QLatin1String("</b><br>") + QLatin1String("<i>") + tr("No direct connections. This may indicate network configuration problems.") + QLatin1String("</i>"));
197 void StatusBar::updateDHTNodesNumber()
199 if (BitTorrent::Session::instance()->isDHTEnabled())
201 m_DHTLbl->setVisible(true);
202 m_DHTLbl->setText(tr("DHT: %1 nodes")
203 .arg(BitTorrent::Session::instance()->status().dhtNodes));
205 else
207 m_DHTLbl->setVisible(false);
211 void StatusBar::updateSpeedLabels()
213 const BitTorrent::SessionStatus &sessionStatus = BitTorrent::Session::instance()->status();
215 QString dlSpeedLbl = Utils::Misc::friendlyUnit(sessionStatus.payloadDownloadRate, true);
216 const int dlSpeedLimit = BitTorrent::Session::instance()->downloadSpeedLimit();
217 if (dlSpeedLimit > 0)
218 dlSpeedLbl += " [" + Utils::Misc::friendlyUnit(dlSpeedLimit, true) + ']';
219 dlSpeedLbl += " (" + Utils::Misc::friendlyUnit(sessionStatus.totalPayloadDownload) + ')';
220 m_dlSpeedLbl->setText(dlSpeedLbl);
222 QString upSpeedLbl = Utils::Misc::friendlyUnit(sessionStatus.payloadUploadRate, true);
223 const int upSpeedLimit = BitTorrent::Session::instance()->uploadSpeedLimit();
224 if (upSpeedLimit > 0)
225 upSpeedLbl += " [" + Utils::Misc::friendlyUnit(upSpeedLimit, true) + ']';
226 upSpeedLbl += " (" + Utils::Misc::friendlyUnit(sessionStatus.totalPayloadUpload) + ')';
227 m_upSpeedLbl->setText(upSpeedLbl);
230 void StatusBar::refresh()
232 updateConnectionStatus();
233 updateDHTNodesNumber();
234 updateSpeedLabels();
237 void StatusBar::updateAltSpeedsBtn(bool alternative)
239 if (alternative)
241 m_altSpeedsBtn->setIcon(UIThemeManager::instance()->getIcon(QLatin1String("slow")));
242 m_altSpeedsBtn->setToolTip(tr("Click to switch to regular speed limits"));
243 m_altSpeedsBtn->setDown(true);
245 else
247 m_altSpeedsBtn->setIcon(UIThemeManager::instance()->getIcon(QLatin1String("slow_off")));
248 m_altSpeedsBtn->setToolTip(tr("Click to switch to alternative speed limits"));
249 m_altSpeedsBtn->setDown(false);
251 refresh();
254 void StatusBar::capSpeed()
256 auto dialog = new SpeedLimitDialog {parentWidget()};
257 dialog->setAttribute(Qt::WA_DeleteOnClose);
258 dialog->open();