Disable "add peers" menu items instead of hiding it

Menu item in disabled state can show tool tip to help user understand
why it is unavailable.
Related issue: #15785.
PR #15787.
This commit is contained in:
Chocobo1 2021-12-06 13:54:38 +08:00 committed by GitHub
parent e3fe66d3ec
commit 6ccc92020c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 28 deletions

View File

@ -262,44 +262,54 @@ void PeerListWidget::updatePeerCountryResolutionState()
void PeerListWidget::showPeerListMenu(const QPoint &)
{
BitTorrent::Torrent *const torrent = m_properties->getCurrentTorrent();
BitTorrent::Torrent *torrent = m_properties->getCurrentTorrent();
if (!torrent) return;
QMenu *menu = new QMenu(this);
auto *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);
menu->setToolTipsVisible(true);
// Add Peer Action
// Do not allow user to add peers in a private torrent
if (!torrent->isQueued() && !torrent->isChecking() && !torrent->isPrivate())
QAction *addNewPeer = menu->addAction(UIThemeManager::instance()->getIcon("user-group-new"), tr("Add peers...")
, this, [this, torrent]()
{
menu->addAction(UIThemeManager::instance()->getIcon("user-group-new"), tr("Add a new peer...")
, this, [this, torrent]()
const QVector<BitTorrent::PeerAddress> peersList = PeersAdditionDialog::askForPeers(this);
const int peerCount = std::count_if(peersList.cbegin(), peersList.cend(), [torrent](const BitTorrent::PeerAddress &peer)
{
const QVector<BitTorrent::PeerAddress> peersList = PeersAdditionDialog::askForPeers(this);
const int peerCount = std::count_if(peersList.cbegin(), peersList.cend(), [torrent](const BitTorrent::PeerAddress &peer)
{
return torrent->connectPeer(peer);
});
if (peerCount < peersList.length())
QMessageBox::information(this, tr("Adding peers"), tr("Some peers cannot be added. Check the Log for details."));
else if (peerCount > 0)
QMessageBox::information(this, tr("Adding peers"), tr("Peers are added to this torrent."));
return torrent->connectPeer(peer);
});
}
if (peerCount < peersList.length())
QMessageBox::information(this, tr("Adding peers"), tr("Some peers cannot be added. Check the Log for details."));
else if (peerCount > 0)
QMessageBox::information(this, tr("Adding peers"), tr("Peers are added to this torrent."));
});
QAction *copyPeers = menu->addAction(UIThemeManager::instance()->getIcon("edit-copy"), tr("Copy IP:port")
, this, &PeerListWidget::copySelectedPeers);
menu->addSeparator();
QAction *banPeers = menu->addAction(UIThemeManager::instance()->getIcon("user-group-delete"), tr("Ban peer permanently")
, this, &PeerListWidget::banSelectedPeers);
if (!selectionModel()->selectedRows().isEmpty())
// disable actions
const auto disableAction = [](QAction *action, const QString &tooltip)
{
menu->addAction(UIThemeManager::instance()->getIcon("edit-copy"), tr("Copy IP:port")
, this, &PeerListWidget::copySelectedPeers);
menu->addSeparator();
menu->addAction(UIThemeManager::instance()->getIcon("user-group-delete"), tr("Ban peer permanently")
, this, &PeerListWidget::banSelectedPeers);
action->setEnabled(false);
action->setToolTip(tooltip);
};
if (torrent->isPrivate())
disableAction(addNewPeer, tr("Cannot add peers to a private torrent"));
else if (torrent->isChecking())
disableAction(addNewPeer, tr("Cannot add peers when the torrent is checking"));
else if (torrent->isQueued())
disableAction(addNewPeer, tr("Cannot add peers when the torrent is queued"));
if (selectionModel()->selectedRows().isEmpty())
{
const QString tooltip = tr("No peer was selected");
disableAction(copyPeers, tooltip);
disableAction(banPeers, tooltip);
}
if (menu->isEmpty())
delete menu;
else
menu->popup(QCursor::pos());
menu->popup(QCursor::pos());
}
void PeerListWidget::banSelectedPeers()

View File

@ -203,7 +203,7 @@
<li><a href="#CopyTrackerUrl" id="CopyTrackerUrl"><img src="icons/edit-copy.svg" alt="QBT_TR(Copy tracker URL)QBT_TR[CONTEXT=TrackerListWidget]" /> QBT_TR(Copy tracker URL)QBT_TR[CONTEXT=TrackerListWidget]</a></li>
</ul>
<ul id="torrentPeersMenu" class="contextMenu">
<li><a href="#addPeer"><img src="icons/list-add.svg" alt="QBT_TR(Add a new peer...)QBT_TR[CONTEXT=PeerListWidget]" /> QBT_TR(Add a new peer...)QBT_TR[CONTEXT=PeerListWidget]</a></li>
<li><a href="#addPeer"><img src="icons/list-add.svg" alt="QBT_TR(Add peers...)QBT_TR[CONTEXT=PeerListWidget]" /> QBT_TR(Add peers...)QBT_TR[CONTEXT=PeerListWidget]</a></li>
<li><a href="#copyPeer" id="CopyPeerInfo"><img src="icons/edit-copy.svg" alt="QBT_TR(Copy IP:port)QBT_TR[CONTEXT=PeerListWidget]" /> QBT_TR(Copy IP:port)QBT_TR[CONTEXT=PeerListWidget]</a></li>
<li class="separator"><a href="#banPeer"><img src="icons/user-group-delete.svg" alt="QBT_TR(Ban peer permanently)QBT_TR[CONTEXT=PeerListWidget]" /> QBT_TR(Ban peer permanently)QBT_TR[CONTEXT=PeerListWidget]</a></li>
</ul>