[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!
This commit is contained in:
Philip Kaufmann 2015-06-01 15:32:25 +02:00
parent f00b62391b
commit 51fc672f59
4 changed files with 61 additions and 2 deletions

View file

@ -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)

View file

@ -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

View file

@ -25,6 +25,7 @@
#endif
#include <QKeyEvent>
#include <QMenu>
#include <QScrollBar>
#include <QThread>
#include <QTime>
@ -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();
}
}

View file

@ -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