Add ';' shortcut for "Add comment" (#78)

* Add ';' shortcut for "Add comment"
This commit is contained in:
Properrr 2017-11-03 04:31:20 -07:00 committed by xarkes
parent 9dc51b9801
commit bfed7d43ed
6 changed files with 123 additions and 60 deletions

View File

@ -5,23 +5,101 @@
#include "dialogs/RenameDialog.h" #include "dialogs/RenameDialog.h"
#include "dialogs/XrefsDialog.h" #include "dialogs/XrefsDialog.h"
#include <QtCore> #include <QtCore>
#include <QShortcut>
DisassemblyContextMenu::DisassemblyContextMenu(RVA offset, QWidget *parent) : DisassemblyContextMenu::DisassemblyContextMenu(RVA offset, QWidget *parent) :
QMenu(parent), QMenu(parent),
offset(offset) offset(offset)
{
init();
}
DisassemblyContextMenu::DisassemblyContextMenu(QWidget*parent)
: QMenu(parent)
{
init();
}
void DisassemblyContextMenu::setOffset(RVA offset)
{
this->offset = offset;
}
QKeySequence DisassemblyContextMenu::getCommentSequence() const
{
return {";"};
}
QKeySequence DisassemblyContextMenu::getAddFlagSequence() const
{
return {}; //TODO insert correct sequence
}
QKeySequence DisassemblyContextMenu::getRenameSequence() const
{
return {}; //TODO insert correct sequence
}
QKeySequence DisassemblyContextMenu::getXRefSequence() const
{
return {Qt::Key_X};
}
QKeySequence DisassemblyContextMenu::getDisplayOptionsSequence() const
{
return {}; //TODO insert correct sequence
}
void DisassemblyContextMenu::init()
{ {
actionAddComment.setText("Add comment"); actionAddComment.setText("Add comment");
this->addAction(&actionAddComment); this->addAction(&actionAddComment);
actionAddComment.setShortcut(getCommentSequence());
actionAddFlag.setText("Add flag"); actionAddFlag.setText("Add flag");
this->addAction(&actionAddFlag); this->addAction(&actionAddFlag);
actionAddComment.setShortcut(getAddFlagSequence());
actionRename.setText("Rename"); actionRename.setText("Rename");
this->addAction(&actionRename); this->addAction(&actionRename);
actionAddComment.setShortcut(getRenameSequence());
actionXRefs.setText("Show xrefs"); actionXRefs.setText("Show xrefs");
this->addAction(&actionXRefs); this->addAction(&actionXRefs);
actionAddComment.setShortcut(getXRefSequence());
this->addSeparator(); this->addSeparator();
actionDisplayOptions.setText("Show options"); actionDisplayOptions.setText("Show options");
actionAddComment.setShortcut(getDisplayOptionsSequence());
this->addAction(&actionDisplayOptions); this->addAction(&actionDisplayOptions);
auto pWidget = parentWidget();
QShortcut *shortcut_dispOptions = new QShortcut(getDisplayOptionsSequence(), pWidget);
shortcut_dispOptions->setContext(Qt::WidgetWithChildrenShortcut);
connect(shortcut_dispOptions, &QShortcut::activated,
this, &DisassemblyContextMenu::on_actionDisplayOptions_triggered);
QShortcut *shortcut_x = new QShortcut(getXRefSequence(), pWidget);
shortcut_x->setContext(Qt::WidgetWithChildrenShortcut);
connect(shortcut_x, &QShortcut::activated,
this, &DisassemblyContextMenu::on_actionXRefs_triggered);
QShortcut *shortcut_comment = new QShortcut(getCommentSequence(), pWidget);
shortcut_comment->setContext(Qt::WidgetWithChildrenShortcut);
connect(shortcut_comment, &QShortcut::activated,
this, &DisassemblyContextMenu::on_actionAddComment_triggered);
QShortcut *shortcut_addFlag = new QShortcut(getAddFlagSequence(), pWidget);
shortcut_addFlag->setContext(Qt::WidgetWithChildrenShortcut);
connect(shortcut_addFlag, &QShortcut::activated,
this, &DisassemblyContextMenu::on_actionAddFlag_triggered);
QShortcut *shortcut_renameSequence = new QShortcut(getRenameSequence(), pWidget);
shortcut_renameSequence->setContext(Qt::WidgetWithChildrenShortcut);
connect(shortcut_renameSequence, &QShortcut::activated,
this, &DisassemblyContextMenu::on_actionRename_triggered);
connect(&actionAddComment, SIGNAL(triggered(bool)), this, SLOT(on_actionAddComment_triggered())); connect(&actionAddComment, SIGNAL(triggered(bool)), this, SLOT(on_actionAddComment_triggered()));
connect(&actionAddFlag, SIGNAL(triggered(bool)), this, SLOT(on_actionAddFlag_triggered())); connect(&actionAddFlag, SIGNAL(triggered(bool)), this, SLOT(on_actionAddFlag_triggered()));
connect(&actionRename, SIGNAL(triggered(bool)), this, SLOT(on_actionRename_triggered())); connect(&actionRename, SIGNAL(triggered(bool)), this, SLOT(on_actionRename_triggered()));
@ -29,10 +107,6 @@ DisassemblyContextMenu::DisassemblyContextMenu(RVA offset, QWidget *parent) :
connect(&actionDisplayOptions, SIGNAL(triggered()), this, SLOT(on_actionDisplayOptions_triggered())); connect(&actionDisplayOptions, SIGNAL(triggered()), this, SLOT(on_actionDisplayOptions_triggered()));
} }
DisassemblyContextMenu::~DisassemblyContextMenu()
{
}
void DisassemblyContextMenu::on_actionAddComment_triggered() void DisassemblyContextMenu::on_actionAddComment_triggered()
{ {
RAnalFunction *fcn = CutterCore::getInstance()->functionAt(offset); RAnalFunction *fcn = CutterCore::getInstance()->functionAt(offset);

View File

@ -3,13 +3,33 @@
#include "cutter.h" #include "cutter.h"
#include <QMenu> #include <QMenu>
#include <QKeySequence>
class DisassemblyContextMenu : public QMenu { class DisassemblyContextMenu : public QMenu {
Q_OBJECT Q_OBJECT
public: public:
DisassemblyContextMenu(RVA offset, QWidget *parent = nullptr); DisassemblyContextMenu(RVA offset, QWidget *parent = nullptr);
~DisassemblyContextMenu(); ~DisassemblyContextMenu() = default;
DisassemblyContextMenu(QWidget *parent = nullptr);
public slots:
void setOffset(RVA offset);
void on_actionAddComment_triggered();
void on_actionAddFlag_triggered();
void on_actionRename_triggered();
void on_actionXRefs_triggered();
void on_actionDisplayOptions_triggered();
private:
void init();
QKeySequence getCommentSequence() const;
QKeySequence getAddFlagSequence() const;
QKeySequence getRenameSequence() const;
QKeySequence getXRefSequence() const;
QKeySequence getDisplayOptionsSequence() const;
private: private:
RVA offset; RVA offset;
QAction actionAddComment; QAction actionAddComment;
@ -17,12 +37,5 @@ private:
QAction actionRename; QAction actionRename;
QAction actionXRefs; QAction actionXRefs;
QAction actionDisplayOptions; QAction actionDisplayOptions;
private slots:
void on_actionAddComment_triggered();
void on_actionAddFlag_triggered();
void on_actionRename_triggered();
void on_actionXRefs_triggered();
void on_actionDisplayOptions_triggered();
}; };
#endif // DISASSEMBLYCONTEXTMENU_H #endif // DISASSEMBLYCONTEXTMENU_H

View File

@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include <QPainter> #include <QPainter>
#include <QScrollBar> #include <QScrollBar>
#include <QShortcut>
#include <QClipboard> #include <QClipboard>
#include <QApplication> #include <QApplication>
#include <QMimeData> #include <QMimeData>
@ -29,7 +30,7 @@ DisassemblerGraphView::DisassemblerGraphView(QWidget *parent)
mHistoryLock(false), mHistoryLock(false),
layoutType(LayoutType::Medium), layoutType(LayoutType::Medium),
mGoto(nullptr), mGoto(nullptr),
mXrefDlg(nullptr) mMenu(new DisassemblyContextMenu(this))
{ {
this->status = "Loading..."; this->status = "Loading...";
@ -661,15 +662,16 @@ void DisassemblerGraphView::mousePressEvent(QMouseEvent* event)
//Update current instruction //Update current instruction
duint instr = this->getInstrForMouseEvent(event); duint instr = this->getInstrForMouseEvent(event);
if(instr != 0) if(instr != 0) {
this->cur_instr = instr; this->cur_instr = instr;
emit currentInstructionUpdated(instr);
}
this->viewport()->update(); this->viewport()->update();
if(event->button() == Qt::RightButton) if(event->button() == Qt::RightButton)
{ {
DisassemblyContextMenu cMenu(instr, this); mMenu->exec(event->globalPos()); //execute context menu
cMenu.exec(event->globalPos()); //execute context menu
} }
} }
else if(event->button() == Qt::LeftButton) else if(event->button() == Qt::LeftButton)
@ -1676,10 +1678,9 @@ void DisassemblerGraphView::addReferenceAction(QMenu* menu, duint addr)
void DisassemblerGraphView::setupContextMenu() void DisassemblerGraphView::setupContextMenu()
{ {
connect(this, &DisassemblerGraphView::currentInstructionUpdated,
mMenu, &DisassemblyContextMenu::setOffset);
// TODO make this prettier // TODO make this prettier
QShortcut *shortcut_x = new QShortcut(QKeySequence(Qt::Key_X), this);
shortcut_x->setContext(Qt::WidgetShortcut);
connect(shortcut_x, SIGNAL(activated()), this, SLOT(xrefSlot()));
QShortcut *shortcut_escape = new QShortcut(QKeySequence(Qt::Key_Escape), this); QShortcut *shortcut_escape = new QShortcut(QKeySequence(Qt::Key_Escape), this);
shortcut_escape->setContext(Qt::WidgetShortcut); shortcut_escape->setContext(Qt::WidgetShortcut);
@ -2030,14 +2031,6 @@ restart:
*/ */
} }
void DisassemblerGraphView::xrefSlot()
{
RVA addr = this->get_cursor_pos();
XrefsDialog *dialog = new XrefsDialog(this);
dialog->fillRefsForAddress(addr, RAddressString(addr), false);
dialog->exec();
}
void DisassemblerGraphView::decompileSlot() void DisassemblerGraphView::decompileSlot()
{ {
/*std::vector<SnowmanRange> ranges; /*std::vector<SnowmanRange> ranges;

View File

@ -29,6 +29,7 @@ class MenuBuilder;
class CachedFontMetrics; class CachedFontMetrics;
class GotoDialog; class GotoDialog;
class XrefBrowseDialog; class XrefBrowseDialog;
class DisassemblyContextMenu;
class DisassemblerGraphView : public QAbstractScrollArea class DisassemblerGraphView : public QAbstractScrollArea
{ {
@ -255,6 +256,7 @@ public:
signals: signals:
void displaySnowmanWidget(); void displaySnowmanWidget();
void currentInstructionUpdated(duint);
public slots: public slots:
void updateTimerEvent(); void updateTimerEvent();
@ -282,7 +284,6 @@ public slots:
void saveImageSlot(); void saveImageSlot();
void setCommentSlot(); void setCommentSlot();
void setLabelSlot(); void setLabelSlot();
void xrefSlot();
void decompileSlot(); void decompileSlot();
void seekPrev(); void seekPrev();
@ -358,7 +359,7 @@ private:
std::unordered_map<duint, duint> currentBlockMap; std::unordered_map<duint, duint> currentBlockMap;
//QBeaEngine disasm; //QBeaEngine disasm;
GotoDialog* mGoto; GotoDialog* mGoto;
XrefBrowseDialog* mXrefDlg; DisassemblyContextMenu* mMenu;
void addReferenceAction(QMenu* menu, duint addr); void addReferenceAction(QMenu* menu, duint addr);
}; };

View File

@ -1,6 +1,5 @@
#include "DisassemblyWidget.h" #include "DisassemblyWidget.h"
#include "menus/DisassemblyContextMenu.h" #include "menus/DisassemblyContextMenu.h"
#include "dialogs/XrefsDialog.h"
#include "utils/HexAsciiHighlighter.h" #include "utils/HexAsciiHighlighter.h"
#include "utils/HexHighlighter.h" #include "utils/HexHighlighter.h"
#include "utils/Configuration.h" #include "utils/Configuration.h"
@ -9,12 +8,14 @@
#include <QJsonArray> #include <QJsonArray>
#include <QJsonObject> #include <QJsonObject>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QRegularExpression>
DisassemblyWidget::DisassemblyWidget(QWidget *parent) : DisassemblyWidget::DisassemblyWidget(QWidget *parent)
QDockWidget(parent), : QDockWidget(parent)
mDisasScrollArea(new DisassemblyScrollArea(this)), , mCtxMenu(new DisassemblyContextMenu(this))
mDisasTextEdit(new DisassemblyTextEdit(this)) , mDisasScrollArea(new DisassemblyScrollArea(this))
, mDisasTextEdit(new DisassemblyTextEdit(this))
{ {
topOffset = bottomOffset = RVA_INVALID; topOffset = bottomOffset = RVA_INVALID;
@ -49,16 +50,9 @@ DisassemblyWidget::DisassemblyWidget(QWidget *parent) :
connect(mDisasTextEdit, SIGNAL(customContextMenuRequested(const QPoint &)), connect(mDisasTextEdit, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(showDisasContextMenu(const QPoint &))); this, SLOT(showDisasContextMenu(const QPoint &)));
// x to show XRefs
QShortcut *shortcut_x = new QShortcut(QKeySequence(Qt::Key_X), mDisasTextEdit);
shortcut_x->setContext(Qt::WidgetShortcut);
connect(shortcut_x, SIGNAL(activated()), this, SLOT(showXrefsDialog()));
maxLines = 0; maxLines = 0;
updateMaxLines(); updateMaxLines();
connect(mDisasScrollArea, SIGNAL(scrollLines(int)), this, SLOT(scrollInstructions(int))); connect(mDisasScrollArea, SIGNAL(scrollLines(int)), this, SLOT(scrollInstructions(int)));
connect(mDisasScrollArea, SIGNAL(disassemblyResized()), this, SLOT(updateMaxLines())); connect(mDisasScrollArea, SIGNAL(disassemblyResized()), this, SLOT(updateMaxLines()));
@ -243,12 +237,12 @@ void DisassemblyWidget::highlightCurrentLine()
cursor2.endEditBlock(); cursor2.endEditBlock();
mDisasTextEdit->setExtraSelections(extraSelections); mDisasTextEdit->setExtraSelections(extraSelections);
mCtxMenu->setOffset(readCurrentDisassemblyOffset());
} }
void DisassemblyWidget::showDisasContextMenu(const QPoint &pt) void DisassemblyWidget::showDisasContextMenu(const QPoint &pt)
{ {
DisassemblyContextMenu menu(this->readCurrentDisassemblyOffset(), mDisasTextEdit); mCtxMenu->exec(mDisasTextEdit->mapToGlobal(pt));
menu.exec(mDisasTextEdit->mapToGlobal(pt));
} }
RVA DisassemblyWidget::readCurrentDisassemblyOffset() RVA DisassemblyWidget::readCurrentDisassemblyOffset()
@ -398,6 +392,7 @@ void DisassemblyWidget::on_seekChanged(RVA offset)
this->raise(); this->raise();
} }
if (topOffset != RVA_INVALID && bottomOffset != RVA_INVALID if (topOffset != RVA_INVALID && bottomOffset != RVA_INVALID
&& offset >= topOffset && offset <= bottomOffset) && offset >= topOffset && offset <= bottomOffset)
{ {
@ -409,6 +404,7 @@ void DisassemblyWidget::on_seekChanged(RVA offset)
// otherwise scroll there // otherwise scroll there
refreshDisasm(offset); refreshDisasm(offset);
} }
mCtxMenu->setOffset(offset);
} }
void DisassemblyWidget::fontsUpdatedSlot() void DisassemblyWidget::fontsUpdatedSlot()
@ -417,22 +413,6 @@ void DisassemblyWidget::fontsUpdatedSlot()
refreshDisasm(); refreshDisasm();
} }
void DisassemblyWidget::showXrefsDialog()
{
// Get current offset
QTextCursor tc = mDisasTextEdit->textCursor();
tc.select(QTextCursor::LineUnderCursor);
QString lastline = tc.selectedText();
QString ele = lastline.split(" ", QString::SkipEmptyParts)[0];
if (ele.contains("0x"))
{
RVA addr = ele.toLongLong(0, 16);
XrefsDialog *dialog = new XrefsDialog(this);
dialog->fillRefsForAddress(addr, RAddressString(addr), false);
dialog->exec();
}
}
DisassemblyScrollArea::DisassemblyScrollArea(QWidget *parent) : QAbstractScrollArea(parent) DisassemblyScrollArea::DisassemblyScrollArea(QWidget *parent) : QAbstractScrollArea(parent)
{ {
} }

View File

@ -3,12 +3,14 @@
#include "cutter.h" #include "cutter.h"
#include <QDockWidget> #include <QDockWidget>
#include <QTextEdit>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QShortcut> #include <QShortcut>
class DisassemblyTextEdit; class DisassemblyTextEdit;
class DisassemblyScrollArea; class DisassemblyScrollArea;
class DisassemblyContextMenu;
class DisassemblyWidget : public QDockWidget class DisassemblyWidget : public QDockWidget
{ {
@ -24,7 +26,6 @@ public slots:
void on_seekChanged(RVA offset); void on_seekChanged(RVA offset);
void refreshDisasm(RVA offset = RVA_INVALID); void refreshDisasm(RVA offset = RVA_INVALID);
void fontsUpdatedSlot(); void fontsUpdatedSlot();
void showXrefsDialog();
private slots: private slots:
void scrollInstructions(int count); void scrollInstructions(int count);
@ -33,6 +34,7 @@ private slots:
void cursorPositionChanged(); void cursorPositionChanged();
private: private:
DisassemblyContextMenu *mCtxMenu;
DisassemblyScrollArea *mDisasScrollArea; DisassemblyScrollArea *mDisasScrollArea;
DisassemblyTextEdit *mDisasTextEdit; DisassemblyTextEdit *mDisasTextEdit;