Skip to content

Commit

Permalink
Merge pull request #6217
Browse files Browse the repository at this point in the history
51fc672 [Qt] disconnect peers from peers tab via context menu (Philip Kaufmann)
  • Loading branch information
laanwj committed Jun 9, 2015
2 parents defa4fc + 51fc672 commit c57e12a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion src/qt/guiutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -205,7 +213,7 @@ namespace GUIUtil
#else
typedef QProgressBar ProgressBar;
#endif

} // namespace GUIUtil

#endif // BITCOIN_QT_GUIUTIL_H
34 changes: 33 additions & 1 deletion src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#endif

#include <QKeyEvent>
#include <QMenu>
#include <QScrollBar>
#include <QThread>
#include <QTime>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 &)));
Expand Down Expand Up @@ -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();
}
}
6 changes: 6 additions & 0 deletions src/qt/rpcconsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Ui {
}

QT_BEGIN_NAMESPACE
class QMenu;
class QItemSelection;
QT_END_NAMESPACE

Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -98,6 +103,7 @@ public slots:
QStringList history;
int historyPtr;
NodeId cachedNodeid;
QMenu *contextMenu;
};

#endif // BITCOIN_QT_RPCCONSOLE_H

0 comments on commit c57e12a

Please sign in to comment.