Skip to content

Commit

Permalink
Add context menu to status bar that allows to switch servers (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
equeim committed Dec 6, 2023
1 parent c2a24bb commit 512e880
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### Added
- Support of xdg-activation protocol on Wayland with Qt < 6.3
- Option to open torrent's file or download directory on double click
- Right click on status bar opens menu to quickly connect to different server

### Changed
- "Open" and "Show in file manager" actions now show error dialog if file/directory does not exist,
Expand Down
13 changes: 10 additions & 3 deletions src/ui/screens/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,16 @@ namespace tremotesf {

mWindow->setCentralWidget(&mSplitter);

mWindow->setStatusBar(new MainWindowStatusBar(mViewModel.rpc()));
auto* const statusBar = new MainWindowStatusBar(mViewModel.rpc());
mWindow->setStatusBar(statusBar);
if (!Settings::instance()->isStatusBarVisible()) {
mWindow->statusBar()->hide();
statusBar->hide();
}
QObject::connect(statusBar, &MainWindowStatusBar::showConnectionSettingsDialog, this, [this] {
showSingleInstanceDialog<ConnectionSettingsDialog>([this] {
return new ConnectionSettingsDialog(mWindow);
});
});

setupActions();

Expand Down Expand Up @@ -341,7 +347,8 @@ namespace tremotesf {
QAction mAddTorrentFileAction{
QIcon::fromTheme("list-add"_l1),
//: Menu item
qApp->translate("tremotesf", "&Add Torrent File...")};
qApp->translate("tremotesf", "&Add Torrent File...")
};
QAction mAddTorrentLinkAction{
QIcon::fromTheme("insert-link"_l1),
//: Menu item
Expand Down
49 changes: 47 additions & 2 deletions src/ui/screens/mainwindow/mainwindowstatusbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

#include "mainwindowstatusbar.h"

#include <QActionGroup>
#include <QCoreApplication>
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QMenu>

#include <KSeparator>

#include "log/log.h"
#include "rpc/serverstats.h"
#include "rpc/servers.h"
#include "rpc/rpc.h"
Expand All @@ -20,6 +23,9 @@ namespace tremotesf {
MainWindowStatusBar::MainWindowStatusBar(const Rpc* rpc, QWidget* parent) : QStatusBar(parent), mRpc(rpc) {
setSizeGripEnabled(false);

setContextMenuPolicy(Qt::CustomContextMenu);
QObject::connect(this, &QWidget::customContextMenuRequested, this, &MainWindowStatusBar::showContextMenu);

auto container = new QWidget(this);
addPermanentWidget(container, 1);

Expand Down Expand Up @@ -138,9 +144,48 @@ namespace tremotesf {
void MainWindowStatusBar::updateStatusLabels() {
mStatusLabel->setText(mRpc->status().toString());
if (mRpc->error() != RpcError::NoError) {
setToolTip(mRpc->errorMessage());
mStatusLabel->setToolTip(mRpc->errorMessage());
} else {
setToolTip({});
mStatusLabel->setToolTip({});
}
}

void MainWindowStatusBar::showContextMenu() {
auto* const menu = new QMenu(this);
auto* const group = new QActionGroup(menu);
group->setExclusive(true);
menu->setAttribute(Qt::WA_DeleteOnClose, true);
const auto servers = Servers::instance()->servers();
const auto currentServerName = Servers::instance()->currentServerName();
for (const auto& server : servers) {
auto* const action = menu->addAction(server.name);
action->setCheckable(true);
group->addAction(action);
if (server.name == currentServerName) {
action->setChecked(true);
}
}
menu->addSeparator();
auto* const connectionSettingsAction = menu->addAction(
QIcon::fromTheme("network-server"_l1),
qApp->translate("tremotesf", "&Connection Settings")
);
QObject::connect(menu, &QMenu::triggered, this, [this, connectionSettingsAction](QAction* action) {
if (action == connectionSettingsAction) {
emit showConnectionSettingsDialog();
} else {
const auto selectedName = action->text();
const auto servers = Servers::instance()->servers();
const auto found = std::find_if(servers.begin(), servers.end(), [&](const auto& server) {
return server.name == selectedName;
});
if (found != servers.end()) {
Servers::instance()->setCurrentServer(selectedName);
} else {
logWarning("Selected server {} which no longer exists", selectedName);
}
}
});
menu->popup(QCursor::pos());
}
}
4 changes: 4 additions & 0 deletions src/ui/screens/mainwindow/mainwindowstatusbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace tremotesf {
void updateLayout();
void updateServerLabel();
void updateStatusLabels();
void showContextMenu();

const Rpc* mRpc{};
QLabel* mNoServersErrorImage{};
Expand All @@ -35,6 +36,9 @@ namespace tremotesf {
KSeparator* mThirdSeparator{};
QLabel* mUploadSpeedImage{};
QLabel* mUploadSpeedLabel{};

signals:
void showConnectionSettingsDialog();
};
}

Expand Down

0 comments on commit 512e880

Please sign in to comment.