From 51fc672f59f390ce4ddf05448b63bdf11e80563b Mon Sep 17 00:00:00 2001 From: Philip Kaufmann Date: Mon, 1 Jun 2015 15:32:25 +0200 Subject: [PATCH] [Qt] disconnect peers from peers tab via context menu - It is now allowed to disconnect peers from peers tab via right-click context menu. Peers are not permanently banned! --- src/qt/guiutil.cpp | 13 +++++++++++++ src/qt/guiutil.h | 10 +++++++++- src/qt/rpcconsole.cpp | 34 +++++++++++++++++++++++++++++++++- src/qt/rpcconsole.h | 6 ++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index 4a1f728e1..581d2321b 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -265,6 +265,19 @@ void copyEntryData(QAbstractItemView *view, int column, int role) } } +QString getEntryData(QAbstractItemView *view, int column, int role) +{ + if(!view || !view->selectionModel()) + return QString(); + QModelIndexList selection = view->selectionModel()->selectedRows(column); + + if(!selection.isEmpty()) { + // Return first item + return (selection.at(0).data(role).toString()); + } + return QString(); +} + QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut) diff --git a/src/qt/guiutil.h b/src/qt/guiutil.h index bcbb540c3..55df64a25 100644 --- a/src/qt/guiutil.h +++ b/src/qt/guiutil.h @@ -64,6 +64,14 @@ namespace GUIUtil */ void copyEntryData(QAbstractItemView *view, int column, int role=Qt::EditRole); + /** Return a field of the currently selected entry as a QString. Does nothing if nothing + is selected. + @param[in] column Data column to extract from the model + @param[in] role Data role to extract from the model + @see TransactionView::copyLabel, TransactionView::copyAmount, TransactionView::copyAddress + */ + QString getEntryData(QAbstractItemView *view, int column, int role); + void setClipboard(const QString& str); /** Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix @@ -205,7 +213,7 @@ namespace GUIUtil #else typedef QProgressBar ProgressBar; #endif - + } // namespace GUIUtil #endif // BITCOIN_QT_GUIUTIL_H diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 29c971ec7..055824c19 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -25,6 +25,7 @@ #endif #include +#include #include #include #include @@ -205,7 +206,8 @@ RPCConsole::RPCConsole(QWidget *parent) : ui(new Ui::RPCConsole), clientModel(0), historyPtr(0), - cachedNodeid(-1) + cachedNodeid(-1), + contextMenu(0) { ui->setupUi(this); GUIUtil::restoreWindowGeometry("nRPCConsoleWindow", this->size(), this); @@ -305,10 +307,22 @@ void RPCConsole::setClientModel(ClientModel *model) ui->peerWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); ui->peerWidget->setSelectionBehavior(QAbstractItemView::SelectRows); ui->peerWidget->setSelectionMode(QAbstractItemView::SingleSelection); + ui->peerWidget->setContextMenuPolicy(Qt::CustomContextMenu); ui->peerWidget->setColumnWidth(PeerTableModel::Address, ADDRESS_COLUMN_WIDTH); ui->peerWidget->setColumnWidth(PeerTableModel::Subversion, SUBVERSION_COLUMN_WIDTH); ui->peerWidget->setColumnWidth(PeerTableModel::Ping, PING_COLUMN_WIDTH); + // create context menu actions + QAction* disconnectAction = new QAction(tr("&Disconnect Node"), this); + + // create context menu + contextMenu = new QMenu(); + contextMenu->addAction(disconnectAction); + + // context menu signals + connect(ui->peerWidget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showMenu(const QPoint&))); + connect(disconnectAction, SIGNAL(triggered()), this, SLOT(disconnectSelectedNode())); + // connect the peerWidget selection model to our peerSelected() handler connect(ui->peerWidget->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(peerSelected(const QItemSelection &, const QItemSelection &))); @@ -659,3 +673,21 @@ void RPCConsole::hideEvent(QHideEvent *event) // stop PeerTableModel auto refresh clientModel->getPeerTableModel()->stopAutoRefresh(); } + +void RPCConsole::showMenu(const QPoint& point) +{ + QModelIndex index = ui->peerWidget->indexAt(point); + if (index.isValid()) + contextMenu->exec(QCursor::pos()); +} + +void RPCConsole::disconnectSelectedNode() +{ + // Get currently selected peer address + QString strNode = GUIUtil::getEntryData(ui->peerWidget, 0, PeerTableModel::Address); + // Find the node, disconnect it and clear the selected node + if (CNode *bannedNode = FindNode(strNode.toStdString())) { + bannedNode->CloseSocketDisconnect(); + ui->peerWidget->selectionModel()->clearSelection(); + } +} diff --git a/src/qt/rpcconsole.h b/src/qt/rpcconsole.h index 8737be35d..767e9aaee 100644 --- a/src/qt/rpcconsole.h +++ b/src/qt/rpcconsole.h @@ -19,6 +19,7 @@ namespace Ui { } QT_BEGIN_NAMESPACE +class QMenu; class QItemSelection; QT_END_NAMESPACE @@ -57,6 +58,8 @@ private slots: void resizeEvent(QResizeEvent *event); void showEvent(QShowEvent *event); void hideEvent(QHideEvent *event); + /** Show custom context menu on Peers tab */ + void showMenu(const QPoint& point); public slots: void clear(); @@ -73,6 +76,8 @@ public slots: void peerSelected(const QItemSelection &selected, const QItemSelection &deselected); /** Handle updated peer information */ void peerLayoutChanged(); + /** Disconnect a selected node on the Peers tab */ + void disconnectSelectedNode(); signals: // For RPC command executor @@ -98,6 +103,7 @@ private: QStringList history; int historyPtr; NodeId cachedNodeid; + QMenu *contextMenu; }; #endif // BITCOIN_QT_RPCCONSOLE_H