diff --git a/src/menus/DisassemblyContextMenu.cpp b/src/menus/DisassemblyContextMenu.cpp index 8fda3c0f..d4e6b972 100644 --- a/src/menus/DisassemblyContextMenu.cpp +++ b/src/menus/DisassemblyContextMenu.cpp @@ -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(); diff --git a/src/menus/DisassemblyContextMenu.h b/src/menus/DisassemblyContextMenu.h index 18113ed3..b7ca617e 100644 --- a/src/menus/DisassemblyContextMenu.h +++ b/src/menus/DisassemblyContextMenu.h @@ -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; diff --git a/src/widgets/DisassemblyWidget.cpp b/src/widgets/DisassemblyWidget.cpp index 43c2fd77..07baeab3 100644 --- a/src/widgets/DisassemblyWidget.cpp +++ b/src/widgets/DisassemblyWidget.cpp @@ -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())); }