Add copy to DisassemblyWidget

This commit is contained in:
Florian Märkl 2017-12-02 16:43:21 +01:00
parent dfc80a3b9b
commit 903f2042e6
3 changed files with 47 additions and 2 deletions

View File

@ -10,6 +10,8 @@
DisassemblyContextMenu::DisassemblyContextMenu(QWidget *parent)
: QMenu(parent),
offset(0),
canCopy(false),
actionCopy(this),
actionAddComment(this),
actionAddFlag(this),
actionRename(this),
@ -24,6 +26,11 @@ DisassemblyContextMenu::DisassemblyContextMenu(QWidget *parent)
actionSetBaseSyscall(this),
actionSetBaseString(this)
{
actionCopy.setText(tr("Copy"));
this->addAction(&actionCopy);
actionCopy.setShortcut(getCopySequence());
copySeparator = addSeparator();
actionAddComment.setText(tr("Add Comment"));
this->addAction(&actionAddComment);
actionAddComment.setShortcut(getCommentSequence());
@ -67,6 +74,11 @@ DisassemblyContextMenu::DisassemblyContextMenu(QWidget *parent)
auto pWidget = parentWidget();
QShortcut *shortcut_Copy = new QShortcut(getCopySequence(), pWidget);
shortcut_Copy->setContext(Qt::WidgetWithChildrenShortcut);
connect(shortcut_Copy, &QShortcut::activated,
this, &DisassemblyContextMenu::on_actionCopy_triggered);
QShortcut *shortcut_dispOptions = new QShortcut(getDisplayOptionsSequence(), pWidget);
shortcut_dispOptions->setContext(Qt::WidgetWithChildrenShortcut);
connect(shortcut_dispOptions, &QShortcut::activated,
@ -92,6 +104,8 @@ DisassemblyContextMenu::DisassemblyContextMenu(QWidget *parent)
connect(shortcut_renameSequence, &QShortcut::activated,
this, &DisassemblyContextMenu::on_actionRename_triggered);
connect(&actionCopy, SIGNAL(triggered(bool)), this, SLOT(on_actionCopy_triggered()));
connect(&actionAddComment, SIGNAL(triggered(bool)), this, SLOT(on_actionAddComment_triggered()));
connect(&actionAddFlag, SIGNAL(triggered(bool)), this, SLOT(on_actionAddFlag_triggered()));
connect(&actionRename, SIGNAL(triggered(bool)), this, SLOT(on_actionRename_triggered()));
@ -115,6 +129,11 @@ void DisassemblyContextMenu::setOffset(RVA offset)
this->offset = offset;
}
void DisassemblyContextMenu::setCanCopy(bool enabled)
{
this->canCopy = enabled;
}
void DisassemblyContextMenu::aboutToShowSlot()
{
// check if set immediate base menu makes sense
@ -133,6 +152,14 @@ void DisassemblyContextMenu::aboutToShowSlot()
{
actionAddComment.setText(tr("Edit Comment"));
}
actionCopy.setVisible(canCopy);
copySeparator->setVisible(canCopy);
}
QKeySequence DisassemblyContextMenu::getCopySequence() const
{
return QKeySequence::Copy;
}
QKeySequence DisassemblyContextMenu::getCommentSequence() const
@ -160,6 +187,11 @@ QKeySequence DisassemblyContextMenu::getDisplayOptionsSequence() const
return {}; //TODO insert correct sequence
}
void DisassemblyContextMenu::on_actionCopy_triggered()
{
emit copy();
}
void DisassemblyContextMenu::on_actionAddComment_triggered()
{
QJsonObject disasObject = Core()->cmdj("pdj 1 @ " + QString::number(offset)).array().first().toObject();

View File

@ -13,12 +13,18 @@ public:
DisassemblyContextMenu(QWidget *parent = nullptr);
~DisassemblyContextMenu() = default;
signals:
void copy();
public slots:
void setOffset(RVA offset);
void setCanCopy(bool enabled);
private slots:
void aboutToShowSlot();
void on_actionCopy_triggered();
void on_actionAddComment_triggered();
void on_actionAddFlag_triggered();
void on_actionRename_triggered();
@ -35,6 +41,7 @@ private slots:
void on_actionSetBaseString_triggered();
private:
QKeySequence getCopySequence() const;
QKeySequence getCommentSequence() const;
QKeySequence getAddFlagSequence() const;
QKeySequence getRenameSequence() const;
@ -42,6 +49,10 @@ private:
QKeySequence getDisplayOptionsSequence() const;
RVA offset;
bool canCopy;
QAction actionCopy;
QAction *copySeparator;
QAction actionAddComment;
QAction actionAddFlag;

View File

@ -103,6 +103,8 @@ DisassemblyWidget::DisassemblyWidget(QWidget *parent)
refreshDisasm(Core()->getOffset());
});
connect(mCtxMenu, SIGNAL(copy()), mDisasTextEdit, SLOT(copy()));
// Dirty
QShortcut *shortcut_escape = new QShortcut(QKeySequence(Qt::Key_Escape), this);
shortcut_escape->setContext(Qt::WidgetShortcut);
@ -402,9 +404,9 @@ void DisassemblyWidget::cursorPositionChanged()
RVA offset = readCurrentDisassemblyOffset();
Core()->seek(offset);
highlightCurrentLine();
mCtxMenu->setCanCopy(mDisasTextEdit->textCursor().hasSelection());
}
bool DisassemblyWidget::eventFilter(QObject *obj, QEvent *event)
{
if ((obj == mDisasTextEdit || obj == mDisasTextEdit->viewport()) && event->type() == QEvent::MouseButtonDblClick)
@ -547,7 +549,7 @@ void DisassemblyTextEdit::mousePressEvent(QMouseEvent *event)
{
QPlainTextEdit::mousePressEvent(event);
if (event->button() == Qt::RightButton)
if (event->button() == Qt::RightButton && !textCursor().hasSelection())
{
setTextCursor(cursorForPosition(event->pos()));
}