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>
34 #include <QHBoxLayout>
36 #include <QPushButton>
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"
46 StatusBar::StatusBar(QWidget
*parent
)
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; }"_qs
);
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"_qs
));
67 m_connecStatusLblIcon
->setToolTip(u
"<b>%1</b><br><i>%2</i>"_qs
.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"_qs
));
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;"_qs
);
78 m_dlSpeedLbl
->setMinimumWidth(200);
80 m_upSpeedLbl
= new QPushButton(this);
81 m_upSpeedLbl
->setIcon(UIThemeManager::instance()->getIcon(u
"upload"_qs
));
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;"_qs
);
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
);
115 statusSep1
->setFrameShadow(QFrame::Raised
);
117 QFrame
*statusSep2
= new QFrame(this);
118 statusSep2
->setFrameStyle(QFrame::VLine
);
120 statusSep2
->setFrameShadow(QFrame::Raised
);
122 QFrame
*statusSep3
= new QFrame(this);
123 statusSep3
->setFrameStyle(QFrame::VLine
);
125 statusSep3
->setFrameShadow(QFrame::Raised
);
127 QFrame
*statusSep4
= new QFrame(this);
128 statusSep4
->setFrameStyle(QFrame::VLine
);
130 statusSep4
->setFrameShadow(QFrame::Raised
);
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;}"_qs
);
144 container
->adjustSize();
147 m_DHTLbl
->setVisible(session
->isDHTEnabled());
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!");
161 QLabel
*restartIconLbl
= new QLabel(this);
162 restartIconLbl
->setPixmap(style()->standardPixmap(QStyle::SP_MessageBoxWarning
));
163 restartIconLbl
->setToolTip(restartText
);
164 insertWidget(0, restartIconLbl
);
166 QLabel
*restartLbl
= new QLabel(this);
167 restartLbl
->setText(restartText
);
168 insertWidget(1, restartLbl
);
171 void StatusBar::updateConnectionStatus()
173 const BitTorrent::SessionStatus
&sessionStatus
= BitTorrent::Session::instance()->status();
175 if (!BitTorrent::Session::instance()->isListening())
177 m_connecStatusLblIcon
->setIcon(UIThemeManager::instance()->getIcon(u
"disconnected"_qs
));
178 const QString tooltip
= u
"<b>%1</b><br>%2"_qs
.arg(tr("Connection Status:"), tr("Offline. This usually means that qBittorrent failed to listen on the selected port for incoming connections."));
179 m_connecStatusLblIcon
->setToolTip(tooltip
);
183 if (sessionStatus
.hasIncomingConnections
)
186 m_connecStatusLblIcon
->setIcon(UIThemeManager::instance()->getIcon(u
"connected"_qs
));
187 const QString tooltip
= u
"<b>%1</b><br>%2"_qs
.arg(tr("Connection Status:"), tr("Online"));
188 m_connecStatusLblIcon
->setToolTip(tooltip
);
192 m_connecStatusLblIcon
->setIcon(UIThemeManager::instance()->getIcon(u
"firewalled"_qs
));
193 const QString tooltip
= u
"<b>%1</b><br><i>%2</i>"_qs
.arg(tr("Connection Status:"), tr("No direct connections. This may indicate network configuration problems."));
194 m_connecStatusLblIcon
->setToolTip(tooltip
);
199 void StatusBar::updateDHTNodesNumber()
201 if (BitTorrent::Session::instance()->isDHTEnabled())
203 m_DHTLbl
->setVisible(true);
204 m_DHTLbl
->setText(tr("DHT: %1 nodes")
205 .arg(BitTorrent::Session::instance()->status().dhtNodes
));
209 m_DHTLbl
->setVisible(false);
213 void StatusBar::updateSpeedLabels()
215 const BitTorrent::SessionStatus
&sessionStatus
= BitTorrent::Session::instance()->status();
217 QString dlSpeedLbl
= Utils::Misc::friendlyUnit(sessionStatus
.payloadDownloadRate
, true);
218 const int dlSpeedLimit
= BitTorrent::Session::instance()->downloadSpeedLimit();
219 if (dlSpeedLimit
> 0)
220 dlSpeedLbl
+= u
" [" + Utils::Misc::friendlyUnit(dlSpeedLimit
, true) + u
']';
221 dlSpeedLbl
+= u
" (" + Utils::Misc::friendlyUnit(sessionStatus
.totalPayloadDownload
) + u
')';
222 m_dlSpeedLbl
->setText(dlSpeedLbl
);
224 QString upSpeedLbl
= Utils::Misc::friendlyUnit(sessionStatus
.payloadUploadRate
, true);
225 const int upSpeedLimit
= BitTorrent::Session::instance()->uploadSpeedLimit();
226 if (upSpeedLimit
> 0)
227 upSpeedLbl
+= u
" [" + Utils::Misc::friendlyUnit(upSpeedLimit
, true) + u
']';
228 upSpeedLbl
+= u
" (" + Utils::Misc::friendlyUnit(sessionStatus
.totalPayloadUpload
) + u
')';
229 m_upSpeedLbl
->setText(upSpeedLbl
);
232 void StatusBar::refresh()
234 updateConnectionStatus();
235 updateDHTNodesNumber();
239 void StatusBar::updateAltSpeedsBtn(bool alternative
)
243 m_altSpeedsBtn
->setIcon(UIThemeManager::instance()->getIcon(u
"slow"_qs
));
244 m_altSpeedsBtn
->setToolTip(tr("Click to switch to regular speed limits"));
245 m_altSpeedsBtn
->setDown(true);
249 m_altSpeedsBtn
->setIcon(UIThemeManager::instance()->getIcon(u
"slow_off"_qs
));
250 m_altSpeedsBtn
->setToolTip(tr("Click to switch to alternative speed limits"));
251 m_altSpeedsBtn
->setDown(false);
256 void StatusBar::capSpeed()
258 auto dialog
= new SpeedLimitDialog
{parentWidget()};
259 dialog
->setAttribute(Qt::WA_DeleteOnClose
);