Skip to content

Commit

Permalink
Display session paused state in main window title bar
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Apr 25, 2024
1 parent d60a59d commit ee91fb9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
70 changes: 45 additions & 25 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,18 @@ namespace

MainWindow::MainWindow(IGUIApplication *app, const WindowState initialState, const QString &titleSuffix)
: GUIApplicationComponent(app)
, m_ui(new Ui::MainWindow)
, m_storeExecutionLogEnabled(EXECUTIONLOG_SETTINGS_KEY(u"Enabled"_s))
, m_storeDownloadTrackerFavicon(SETTINGS_KEY(u"DownloadTrackerFavicon"_s))
, m_storeExecutionLogTypes(EXECUTIONLOG_SETTINGS_KEY(u"Types"_s), Log::MsgType::ALL)
, m_ui {new Ui::MainWindow}
, m_downloadRate {Utils::Misc::friendlyUnit(0, true)}
, m_uploadRate {Utils::Misc::friendlyUnit(0, true)}
, m_storeExecutionLogEnabled {EXECUTIONLOG_SETTINGS_KEY(u"Enabled"_s)}
, m_storeDownloadTrackerFavicon {SETTINGS_KEY(u"DownloadTrackerFavicon"_s)}
, m_storeExecutionLogTypes {EXECUTIONLOG_SETTINGS_KEY(u"Types"_s), Log::MsgType::ALL}
#ifdef Q_OS_MACOS
, m_badger(std::make_unique<MacUtils::Badger>())
, m_badger {std::make_unique<MacUtils::Badger>()}
#endif // Q_OS_MACOS
{
m_ui->setupUi(this);

setTitleSuffix(titleSuffix);

Preferences *const pref = Preferences::instance();
m_uiLocked = pref->isUILocked();
m_displaySpeedInTitle = pref->speedInTitleBar();
Expand All @@ -145,6 +145,8 @@ MainWindow::MainWindow(IGUIApplication *app, const WindowState initialState, con
setWindowIcon(UIThemeManager::instance()->getIcon(u"qbittorrent"_s));
#endif // Q_OS_MACOS

setTitleSuffix(titleSuffix);

#if (defined(Q_OS_UNIX))
m_ui->actionOptions->setText(tr("Preferences"));
#endif
Expand Down Expand Up @@ -182,11 +184,13 @@ MainWindow::MainWindow(IGUIApplication *app, const WindowState initialState, con
{
m_ui->actionPauseSession->setVisible(false);
m_ui->actionResumeSession->setVisible(true);
refreshWindowTitle();
});
connect(BitTorrent::Session::instance(), &BitTorrent::Session::resumed, this, [this]
{
m_ui->actionPauseSession->setVisible(true);
m_ui->actionResumeSession->setVisible(false);
refreshWindowTitle();
});

auto *lockMenu = new QMenu(m_ui->menuView);
Expand Down Expand Up @@ -338,7 +342,7 @@ MainWindow::MainWindow(IGUIApplication *app, const WindowState initialState, con
// Configure BT session according to options
loadPreferences();

connect(BitTorrent::Session::instance(), &BitTorrent::Session::statsUpdated, this, &MainWindow::reloadSessionStats);
connect(BitTorrent::Session::instance(), &BitTorrent::Session::statsUpdated, this, &MainWindow::loadSessionStats);
connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentsUpdated, this, &MainWindow::reloadTorrentStats);

// Accept drag 'n drops
Expand Down Expand Up @@ -542,7 +546,7 @@ void MainWindow::setTitleSuffix(const QString &suffix)
m_windowTitle = QStringLiteral("qBittorrent " QBT_VERSION)
+ (!suffix.isEmpty() ? (separator + suffix) : QString());

setWindowTitle(m_windowTitle);
refreshWindowTitle();
}

void MainWindow::addToolbarContextMenu()
Expand Down Expand Up @@ -1500,28 +1504,24 @@ void MainWindow::loadPreferences()
qDebug("GUI settings loaded");
}

void MainWindow::reloadSessionStats()
void MainWindow::loadSessionStats()
{
const BitTorrent::SessionStatus &status = BitTorrent::Session::instance()->status();
const QString downloadRate = Utils::Misc::friendlyUnit(status.payloadDownloadRate, true);
const QString uploadRate = Utils::Misc::friendlyUnit(status.payloadUploadRate, true);
const auto *btSession = BitTorrent::Session::instance();
const BitTorrent::SessionStatus &status = btSession->status();
const QString m_downloadRate = Utils::Misc::friendlyUnit(status.payloadDownloadRate, true);
const QString m_uploadRate = Utils::Misc::friendlyUnit(status.payloadUploadRate, true);

// update global information
#ifdef Q_OS_MACOS
m_badger->updateSpeed(status.payloadDownloadRate, status.payloadUploadRate);
#else
const auto toolTip = u"%1\n%2"_s.arg(
tr("DL speed: %1", "e.g: Download speed: 10 KiB/s").arg(downloadRate)
, tr("UP speed: %1", "e.g: Upload speed: 10 KiB/s").arg(uploadRate));
tr("DL speed: %1", "e.g: Download speed: 10 KiB/s").arg(m_downloadRate)
, tr("UP speed: %1", "e.g: Upload speed: 10 KiB/s").arg(m_uploadRate));
app()->desktopIntegration()->setToolTip(toolTip); // tray icon
#endif // Q_OS_MACOS

if (m_displaySpeedInTitle)
{
const QString title = tr("[D: %1, U: %2] %3", "D = Download; U = Upload; %3 is the rest of the window title")
.arg(downloadRate, uploadRate, m_windowTitle);
setWindowTitle(title);
}
refreshWindowTitle();
}

void MainWindow::reloadTorrentStats(const QVector<BitTorrent::Torrent *> &torrents)
Expand Down Expand Up @@ -1625,10 +1625,7 @@ void MainWindow::on_actionSpeedInTitleBar_triggered()
{
m_displaySpeedInTitle = static_cast<QAction *>(sender())->isChecked();
Preferences::instance()->showSpeedInTitleBar(m_displaySpeedInTitle);
if (m_displaySpeedInTitle)
reloadSessionStats();
else
setWindowTitle(m_windowTitle);
refreshWindowTitle();
}

void MainWindow::on_actionRSSReader_triggered()
Expand Down Expand Up @@ -1893,6 +1890,29 @@ void MainWindow::applyTransferListFilter()
m_transferListWidget->applyFilter(m_columnFilterEdit->text(), m_columnFilterComboBox->currentData().value<TransferListModel::Column>());
}

void MainWindow::refreshWindowTitle()
{
const auto *btSession = BitTorrent::Session::instance();
if (btSession->isPaused())
{
const QString title = tr("[PAUSED] %1", "%1 is the rest of the window title").arg(m_windowTitle);
setWindowTitle(title);
}
else
{
if (m_displaySpeedInTitle)
{
const QString title = tr("[D: %1, U: %2] %3", "D = Download; U = Upload; %3 is the rest of the window title")
.arg(m_downloadRate, m_uploadRate, m_windowTitle);
setWindowTitle(title);
}
else
{
setWindowTitle(m_windowTitle);
}
}
}

#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
void MainWindow::checkProgramUpdate(const bool invokedByUser)
{
Expand Down
10 changes: 7 additions & 3 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private slots:
void displayRSSTab();
void displayExecutionLogTab();
void toggleFocusBetweenLineEdits();
void reloadSessionStats();
void loadSessionStats();
void reloadTorrentStats(const QVector<BitTorrent::Torrent *> &torrents);
void loadPreferences();
void optionsSaved();
Expand Down Expand Up @@ -203,14 +203,18 @@ private slots:
void showStatusBar(bool show);
void showFiltersSidebar(bool show);
void applyTransferListFilter();
void refreshWindowTitle();

Ui::MainWindow *m_ui = nullptr;

QFileSystemWatcher *m_executableWatcher = nullptr;
// GUI related
QString m_windowTitle;
QString m_downloadRate;
QString m_uploadRate;
bool m_posInitialized = false;
bool m_neverShown = true;

QFileSystemWatcher *m_executableWatcher = nullptr;
// GUI related
QPointer<QTabWidget> m_tabs;
QPointer<StatusBar> m_statusBar;
QPointer<OptionsDialog> m_options;
Expand Down

0 comments on commit ee91fb9

Please sign in to comment.