Skip to content

Commit c57e12a

Browse files
committed
Merge pull request #6217
51fc672 [Qt] disconnect peers from peers tab via context menu (Philip Kaufmann)
2 parents defa4fc + 51fc672 commit c57e12a

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

src/qt/guiutil.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,19 @@ void copyEntryData(QAbstractItemView *view, int column, int role)
265265
}
266266
}
267267

268+
QString getEntryData(QAbstractItemView *view, int column, int role)
269+
{
270+
if(!view || !view->selectionModel())
271+
return QString();
272+
QModelIndexList selection = view->selectionModel()->selectedRows(column);
273+
274+
if(!selection.isEmpty()) {
275+
// Return first item
276+
return (selection.at(0).data(role).toString());
277+
}
278+
return QString();
279+
}
280+
268281
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir,
269282
const QString &filter,
270283
QString *selectedSuffixOut)

src/qt/guiutil.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ namespace GUIUtil
6464
*/
6565
void copyEntryData(QAbstractItemView *view, int column, int role=Qt::EditRole);
6666

67+
/** Return a field of the currently selected entry as a QString. Does nothing if nothing
68+
is selected.
69+
@param[in] column Data column to extract from the model
70+
@param[in] role Data role to extract from the model
71+
@see TransactionView::copyLabel, TransactionView::copyAmount, TransactionView::copyAddress
72+
*/
73+
QString getEntryData(QAbstractItemView *view, int column, int role);
74+
6775
void setClipboard(const QString& str);
6876

6977
/** Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix
@@ -205,7 +213,7 @@ namespace GUIUtil
205213
#else
206214
typedef QProgressBar ProgressBar;
207215
#endif
208-
216+
209217
} // namespace GUIUtil
210218

211219
#endif // BITCOIN_QT_GUIUTIL_H

src/qt/rpcconsole.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#endif
2626

2727
#include <QKeyEvent>
28+
#include <QMenu>
2829
#include <QScrollBar>
2930
#include <QThread>
3031
#include <QTime>
@@ -205,7 +206,8 @@ RPCConsole::RPCConsole(QWidget *parent) :
205206
ui(new Ui::RPCConsole),
206207
clientModel(0),
207208
historyPtr(0),
208-
cachedNodeid(-1)
209+
cachedNodeid(-1),
210+
contextMenu(0)
209211
{
210212
ui->setupUi(this);
211213
GUIUtil::restoreWindowGeometry("nRPCConsoleWindow", this->size(), this);
@@ -305,10 +307,22 @@ void RPCConsole::setClientModel(ClientModel *model)
305307
ui->peerWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
306308
ui->peerWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
307309
ui->peerWidget->setSelectionMode(QAbstractItemView::SingleSelection);
310+
ui->peerWidget->setContextMenuPolicy(Qt::CustomContextMenu);
308311
ui->peerWidget->setColumnWidth(PeerTableModel::Address, ADDRESS_COLUMN_WIDTH);
309312
ui->peerWidget->setColumnWidth(PeerTableModel::Subversion, SUBVERSION_COLUMN_WIDTH);
310313
ui->peerWidget->setColumnWidth(PeerTableModel::Ping, PING_COLUMN_WIDTH);
311314

315+
// create context menu actions
316+
QAction* disconnectAction = new QAction(tr("&Disconnect Node"), this);
317+
318+
// create context menu
319+
contextMenu = new QMenu();
320+
contextMenu->addAction(disconnectAction);
321+
322+
// context menu signals
323+
connect(ui->peerWidget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showMenu(const QPoint&)));
324+
connect(disconnectAction, SIGNAL(triggered()), this, SLOT(disconnectSelectedNode()));
325+
312326
// connect the peerWidget selection model to our peerSelected() handler
313327
connect(ui->peerWidget->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
314328
this, SLOT(peerSelected(const QItemSelection &, const QItemSelection &)));
@@ -659,3 +673,21 @@ void RPCConsole::hideEvent(QHideEvent *event)
659673
// stop PeerTableModel auto refresh
660674
clientModel->getPeerTableModel()->stopAutoRefresh();
661675
}
676+
677+
void RPCConsole::showMenu(const QPoint& point)
678+
{
679+
QModelIndex index = ui->peerWidget->indexAt(point);
680+
if (index.isValid())
681+
contextMenu->exec(QCursor::pos());
682+
}
683+
684+
void RPCConsole::disconnectSelectedNode()
685+
{
686+
// Get currently selected peer address
687+
QString strNode = GUIUtil::getEntryData(ui->peerWidget, 0, PeerTableModel::Address);
688+
// Find the node, disconnect it and clear the selected node
689+
if (CNode *bannedNode = FindNode(strNode.toStdString())) {
690+
bannedNode->CloseSocketDisconnect();
691+
ui->peerWidget->selectionModel()->clearSelection();
692+
}
693+
}

src/qt/rpcconsole.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Ui {
1919
}
2020

2121
QT_BEGIN_NAMESPACE
22+
class QMenu;
2223
class QItemSelection;
2324
QT_END_NAMESPACE
2425

@@ -57,6 +58,8 @@ private slots:
5758
void resizeEvent(QResizeEvent *event);
5859
void showEvent(QShowEvent *event);
5960
void hideEvent(QHideEvent *event);
61+
/** Show custom context menu on Peers tab */
62+
void showMenu(const QPoint& point);
6063

6164
public slots:
6265
void clear();
@@ -73,6 +76,8 @@ public slots:
7376
void peerSelected(const QItemSelection &selected, const QItemSelection &deselected);
7477
/** Handle updated peer information */
7578
void peerLayoutChanged();
79+
/** Disconnect a selected node on the Peers tab */
80+
void disconnectSelectedNode();
7681

7782
signals:
7883
// For RPC command executor
@@ -98,6 +103,7 @@ public slots:
98103
QStringList history;
99104
int historyPtr;
100105
NodeId cachedNodeid;
106+
QMenu *contextMenu;
101107
};
102108

103109
#endif // BITCOIN_QT_RPCCONSOLE_H

0 commit comments

Comments
 (0)