Skip to content

Commit 56c131d

Browse files
committed
Add copy/paste to breakpoints widget.
1 parent 685fdc4 commit 56c131d

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

ui/breakpointswidget.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ limitations under the License.
1717
#include <QPainter>
1818
#include <QHeaderView>
1919
#include <QFileInfo>
20+
#include <QClipboard>
21+
#include <QGuiApplication>
22+
#include <QKeyEvent>
23+
#include <QStringList>
24+
#include <algorithm>
2025
#include "breakpointswidget.h"
2126
#include "ui.h"
2227
#include "menus.h"
@@ -229,12 +234,16 @@ QSize DebugBreakpointsItemDelegate::sizeHint(const QStyleOptionViewItem& option,
229234
DebugBreakpointsWidget::DebugBreakpointsWidget(ViewFrame* view, BinaryViewRef data, Menu* menu):
230235
QTableView(view), m_view(view)
231236
{
237+
setFocusPolicy(Qt::StrongFocus);
238+
232239
m_controller = DebuggerController::GetController(data);
233240
if (!m_controller)
234241
return;
235242

236243
m_model = new DebugBreakpointsListModel(this, view);
237244
setModel(m_model);
245+
if (viewport())
246+
viewport()->setFocusPolicy(Qt::StrongFocus);
238247
setSelectionBehavior(QAbstractItemView::SelectItems);
239248
setSelectionMode(QAbstractItemView::ExtendedSelection);
240249

@@ -272,6 +281,10 @@ DebugBreakpointsWidget::DebugBreakpointsWidget(ViewFrame* view, BinaryViewRef da
272281
m_actionHandler.bindAction(
273282
jumpToBreakpointActionName, UIAction([&]() { jump(); }, [&]() { return selectionNotEmpty(); }));
274283

284+
m_menu->addAction("Copy", "Options", MENU_ORDER_NORMAL);
285+
m_actionHandler.bindAction(
286+
"Copy", UIAction([&]() { copySelection(); }, [&]() { return selectionNotEmpty(); }), HighActionPriority);
287+
275288
QString addBreakpointActionName = QString::fromStdString("Add Breakpoint...");
276289
UIAction::registerAction(addBreakpointActionName);
277290
m_menu->addAction(addBreakpointActionName, "Options", MENU_ORDER_NORMAL);
@@ -305,13 +318,54 @@ void DebugBreakpointsWidget::contextMenuEvent(QContextMenuEvent* event)
305318
}
306319

307320

321+
void DebugBreakpointsWidget::keyPressEvent(QKeyEvent* event)
322+
{
323+
if (event && event->matches(QKeySequence::Copy))
324+
{
325+
copySelection();
326+
event->accept();
327+
return;
328+
}
329+
330+
QTableView::keyPressEvent(event);
331+
}
332+
333+
308334
bool DebugBreakpointsWidget::selectionNotEmpty()
309335
{
310336
QModelIndexList sel = selectionModel()->selectedIndexes();
311337
return (!sel.empty()) && sel[0].isValid();
312338
}
313339

314340

341+
void DebugBreakpointsWidget::copySelection()
342+
{
343+
if (!model() || !selectionModel())
344+
return;
345+
346+
QModelIndexList rows = selectionModel()->selectedRows();
347+
if (rows.empty())
348+
return;
349+
350+
std::sort(rows.begin(), rows.end(), [](const QModelIndex& a, const QModelIndex& b) { return a.row() < b.row(); });
351+
352+
QStringList lines;
353+
for (const QModelIndex& rowIndex : rows)
354+
{
355+
QStringList cells;
356+
for (int column = 0; column < model()->columnCount(); column++)
357+
{
358+
QModelIndex idx = model()->index(rowIndex.row(), column);
359+
cells << model()->data(idx, Qt::DisplayRole).toString();
360+
}
361+
lines << cells.join("\t");
362+
}
363+
364+
if (QClipboard* clipboard = QGuiApplication::clipboard())
365+
clipboard->setText(lines.join("\n"));
366+
}
367+
368+
315369
void DebugBreakpointsWidget::jump()
316370
{
317371
QModelIndexList sel = selectionModel()->selectedRows();

ui/breakpointswidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
#include <QModelIndex>
2222
#include <QTableView>
2323
#include <QStyledItemDelegate>
24+
#include <QKeyEvent>
2425
#include "inttypes.h"
2526
#include "binaryninjaapi.h"
2627
#include "viewframe.h"
@@ -127,11 +128,13 @@ class DebugBreakpointsWidget : public QTableView
127128
Menu* m_menu;
128129

129130
bool selectionNotEmpty();
131+
void copySelection();
130132

131133
//void shouldBeVisible()
132134
//virtual void notifyFontChanged() override;
133135

134136
virtual void contextMenuEvent(QContextMenuEvent* event) override;
137+
virtual void keyPressEvent(QKeyEvent* event) override;
135138

136139
public:
137140
DebugBreakpointsWidget(ViewFrame* view, BinaryViewRef data, Menu* menu);

0 commit comments

Comments
 (0)