Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / statusbar.cpp
blob8eef15e7baf2c204d6e5cc225eb3a1bbe1350cdf
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 inherits("QMacStyle") and this becomes false.
52 setStyleSheet(u"QStatusBar::item { border-width: 0; }"_s);
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(u"firewalled"_s));
67 m_connecStatusLblIcon->setToolTip(u"<b>%1</b><br><i>%2</i>"_s.arg(tr("Connection status:")
68 , tr("No direct connections. This may indicate network configuration problems.")));
69 connect(m_connecStatusLblIcon, &QAbstractButton::clicked, this, &StatusBar::connectionButtonClicked);
71 m_dlSpeedLbl = new QPushButton(this);
72 m_dlSpeedLbl->setIcon(UIThemeManager::instance()->getIcon(u"downloading"_s, u"downloading_small"_s));
73 connect(m_dlSpeedLbl, &QAbstractButton::clicked, this, &StatusBar::capSpeed);
74 m_dlSpeedLbl->setFlat(true);
75 m_dlSpeedLbl->setFocusPolicy(Qt::NoFocus);
76 m_dlSpeedLbl->setCursor(Qt::PointingHandCursor);
77 m_dlSpeedLbl->setStyleSheet(u"text-align:left;"_s);
78 m_dlSpeedLbl->setMinimumWidth(200);
80 m_upSpeedLbl = new QPushButton(this);
81 m_upSpeedLbl->setIcon(UIThemeManager::instance()->getIcon(u"upload"_s, u"seeding"_s));
82 connect(m_upSpeedLbl, &QAbstractButton::clicked, this, &StatusBar::capSpeed);
83 m_upSpeedLbl->setFlat(true);
84 m_upSpeedLbl->setFocusPolicy(Qt::NoFocus);
85 m_upSpeedLbl->setCursor(Qt::PointingHandCursor);
86 m_upSpeedLbl->setStyleSheet(u"text-align:left;"_s);
87 m_upSpeedLbl->setMinimumWidth(200);
89 m_DHTLbl = new QLabel(tr("DHT: %1 nodes").arg(0), this);
90 m_DHTLbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
92 m_altSpeedsBtn = new QPushButton(this);
93 m_altSpeedsBtn->setFlat(true);
94 m_altSpeedsBtn->setFocusPolicy(Qt::NoFocus);
95 m_altSpeedsBtn->setCursor(Qt::PointingHandCursor);
96 updateAltSpeedsBtn(session->isAltGlobalSpeedLimitEnabled());
97 connect(m_altSpeedsBtn, &QAbstractButton::clicked, this, &StatusBar::alternativeSpeedsButtonClicked);
99 // Because on some platforms the default icon size is bigger
100 // and it will result in taller/fatter statusbar, even if the
101 // icons are actually 16x16
102 m_connecStatusLblIcon->setIconSize(Utils::Gui::smallIconSize());
103 m_dlSpeedLbl->setIconSize(Utils::Gui::smallIconSize());
104 m_upSpeedLbl->setIconSize(Utils::Gui::smallIconSize());
105 m_altSpeedsBtn->setIconSize(QSize(Utils::Gui::mediumIconSize().width(), Utils::Gui::smallIconSize().height()));
107 // Set to the known maximum width(plus some padding)
108 // so the speed widgets will take the rest of the space
109 m_connecStatusLblIcon->setMaximumWidth(Utils::Gui::largeIconSize().width());
110 m_altSpeedsBtn->setMaximumWidth(Utils::Gui::largeIconSize().width());
112 QFrame *statusSep1 = new QFrame(this);
113 statusSep1->setFrameStyle(QFrame::VLine);
114 #ifndef Q_OS_MACOS
115 statusSep1->setFrameShadow(QFrame::Raised);
116 #endif
117 QFrame *statusSep2 = new QFrame(this);
118 statusSep2->setFrameStyle(QFrame::VLine);
119 #ifndef Q_OS_MACOS
120 statusSep2->setFrameShadow(QFrame::Raised);
121 #endif
122 QFrame *statusSep3 = new QFrame(this);
123 statusSep3->setFrameStyle(QFrame::VLine);
124 #ifndef Q_OS_MACOS
125 statusSep3->setFrameShadow(QFrame::Raised);
126 #endif
127 QFrame *statusSep4 = new QFrame(this);
128 statusSep4->setFrameStyle(QFrame::VLine);
129 #ifndef Q_OS_MACOS
130 statusSep4->setFrameShadow(QFrame::Raised);
131 #endif
132 layout->addWidget(m_DHTLbl);
133 layout->addWidget(statusSep1);
134 layout->addWidget(m_connecStatusLblIcon);
135 layout->addWidget(statusSep2);
136 layout->addWidget(m_altSpeedsBtn);
137 layout->addWidget(statusSep4);
138 layout->addWidget(m_dlSpeedLbl);
139 layout->addWidget(statusSep3);
140 layout->addWidget(m_upSpeedLbl);
142 addPermanentWidget(container);
143 setStyleSheet(u"QWidget {margin: 0;}"_s);
144 container->adjustSize();
145 adjustSize();
146 // Is DHT enabled
147 m_DHTLbl->setVisible(session->isDHTEnabled());
148 refresh();
149 connect(session, &BitTorrent::Session::statsUpdated, this, &StatusBar::refresh);
152 StatusBar::~StatusBar()
154 qDebug() << Q_FUNC_INFO;
157 void StatusBar::showRestartRequired()
159 // Restart required notification
160 const QString restartText = tr("qBittorrent needs to be restarted!");
162 const QPixmap pixmap = style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(Utils::Gui::smallIconSize());
163 auto *restartIconLbl = new QLabel(this);
164 restartIconLbl->setPixmap(pixmap);
165 restartIconLbl->setToolTip(restartText);
166 insertWidget(0, restartIconLbl);
168 auto *restartLbl = new QLabel(this);
169 restartLbl->setText(restartText);
170 insertWidget(1, restartLbl);
173 void StatusBar::updateConnectionStatus()
175 const BitTorrent::SessionStatus &sessionStatus = BitTorrent::Session::instance()->status();
177 if (!BitTorrent::Session::instance()->isListening())
179 m_connecStatusLblIcon->setIcon(UIThemeManager::instance()->getIcon(u"disconnected"_s));
180 const QString tooltip = u"<b>%1</b><br>%2"_s.arg(tr("Connection Status:"), tr("Offline. This usually means that qBittorrent failed to listen on the selected port for incoming connections."));
181 m_connecStatusLblIcon->setToolTip(tooltip);
183 else
185 if (sessionStatus.hasIncomingConnections)
187 // Connection OK
188 m_connecStatusLblIcon->setIcon(UIThemeManager::instance()->getIcon(u"connected"_s));
189 const QString tooltip = u"<b>%1</b><br>%2"_s.arg(tr("Connection Status:"), tr("Online"));
190 m_connecStatusLblIcon->setToolTip(tooltip);
192 else
194 m_connecStatusLblIcon->setIcon(UIThemeManager::instance()->getIcon(u"firewalled"_s));
195 const QString tooltip = u"<b>%1</b><br><i>%2</i>"_s.arg(tr("Connection Status:"), tr("No direct connections. This may indicate network configuration problems."));
196 m_connecStatusLblIcon->setToolTip(tooltip);
201 void StatusBar::updateDHTNodesNumber()
203 if (BitTorrent::Session::instance()->isDHTEnabled())
205 m_DHTLbl->setVisible(true);
206 m_DHTLbl->setText(tr("DHT: %1 nodes")
207 .arg(BitTorrent::Session::instance()->status().dhtNodes));
209 else
211 m_DHTLbl->setVisible(false);
215 void StatusBar::updateSpeedLabels()
217 const BitTorrent::SessionStatus &sessionStatus = BitTorrent::Session::instance()->status();
219 QString dlSpeedLbl = Utils::Misc::friendlyUnit(sessionStatus.payloadDownloadRate, true);
220 const int dlSpeedLimit = BitTorrent::Session::instance()->downloadSpeedLimit();
221 if (dlSpeedLimit > 0)
222 dlSpeedLbl += u" [" + Utils::Misc::friendlyUnit(dlSpeedLimit, true) + u']';
223 dlSpeedLbl += u" (" + Utils::Misc::friendlyUnit(sessionStatus.totalPayloadDownload) + u')';
224 m_dlSpeedLbl->setText(dlSpeedLbl);
226 QString upSpeedLbl = Utils::Misc::friendlyUnit(sessionStatus.payloadUploadRate, true);
227 const int upSpeedLimit = BitTorrent::Session::instance()->uploadSpeedLimit();
228 if (upSpeedLimit > 0)
229 upSpeedLbl += u" [" + Utils::Misc::friendlyUnit(upSpeedLimit, true) + u']';
230 upSpeedLbl += u" (" + Utils::Misc::friendlyUnit(sessionStatus.totalPayloadUpload) + u')';
231 m_upSpeedLbl->setText(upSpeedLbl);
234 void StatusBar::refresh()
236 updateConnectionStatus();
237 updateDHTNodesNumber();
238 updateSpeedLabels();
241 void StatusBar::updateAltSpeedsBtn(bool alternative)
243 if (alternative)
245 m_altSpeedsBtn->setIcon(UIThemeManager::instance()->getIcon(u"slow"_s));
246 m_altSpeedsBtn->setToolTip(tr("Click to switch to regular speed limits"));
247 m_altSpeedsBtn->setDown(true);
249 else
251 m_altSpeedsBtn->setIcon(UIThemeManager::instance()->getIcon(u"slow_off"_s));
252 m_altSpeedsBtn->setToolTip(tr("Click to switch to alternative speed limits"));
253 m_altSpeedsBtn->setDown(false);
255 refresh();
258 void StatusBar::capSpeed()
260 auto *dialog = new SpeedLimitDialog {parentWidget()};
261 dialog->setAttribute(Qt::WA_DeleteOnClose);
262 dialog->open();