From 1cef190d2966e6150b60a3128096768b87a1c4f2 Mon Sep 17 00:00:00 2001 From: karliss Date: Wed, 2 Oct 2019 14:22:04 +0300 Subject: [PATCH] Use AddressableItemContextMenu in Registers widgtet. (#1794) * Use AddressableItemContextMenu in Registers widgtet. * Hide shortcuts becasue they can't be used in register widget. --- src/widgets/RegistersWidget.cpp | 25 +++++++++++++++++++++++-- src/widgets/RegistersWidget.h | 5 ++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/widgets/RegistersWidget.cpp b/src/widgets/RegistersWidget.cpp index 27d4ea18..106461a8 100644 --- a/src/widgets/RegistersWidget.cpp +++ b/src/widgets/RegistersWidget.cpp @@ -10,7 +10,8 @@ RegistersWidget::RegistersWidget(MainWindow *main, QAction *action) : CutterDockWidget(main, action), - ui(new Ui::RegistersWidget) + ui(new Ui::RegistersWidget), + addressContextMenu(this, main) { ui->setupUi(this); @@ -24,6 +25,12 @@ RegistersWidget::RegistersWidget(MainWindow *main, QAction *action) : connect(Core(), &CutterCore::refreshAll, this, &RegistersWidget::updateContents); connect(Core(), &CutterCore::registersChanged, this, &RegistersWidget::updateContents); + + // Hide shortcuts because there is no way of selecting an item and triger them + for (auto &action : addressContextMenu.actions()) { + action->setShortcut(QKeySequence()); + // setShortcutVisibleInContextMenu(false) doesn't work + } } RegistersWidget::~RegistersWidget() = default; @@ -63,6 +70,14 @@ void RegistersWidget::setRegisterGrid() registerEditValue = new QLineEdit; registerEditValue->setFixedWidth(140); registerEditValue->setFont(Config()->getFont()); + registerLabel->setContextMenuPolicy(Qt::CustomContextMenu); + connect(registerLabel, &QWidget::customContextMenuRequested, this, [this, registerEditValue, registerLabel](QPoint p){ + openContextMenu(registerLabel->mapToGlobal(p), registerEditValue->text()); + }); + registerEditValue->setContextMenuPolicy(Qt::CustomContextMenu); + connect(registerEditValue, &QWidget::customContextMenuRequested, this, [this, registerEditValue](QPoint p){ + openContextMenu(registerEditValue->mapToGlobal(p), registerEditValue->text()); + }); // add label and register value to grid registerLayout->addWidget(registerLabel, i, col); registerLayout->addWidget(registerEditValue, i, col + 1); @@ -98,9 +113,15 @@ void RegistersWidget::setRegisterGrid() registerEditValue->setText(regValue); i++; // decide if we should change column - if (i >= registerLen / numCols + 1) { + if (i >= (registerLen + numCols - 1) / numCols) { i = 0; col += 2; } } } + +void RegistersWidget::openContextMenu(QPoint point, QString address) +{ + addressContextMenu.setTarget(address.toULongLong(nullptr, 16)); + addressContextMenu.exec(point); +} diff --git a/src/widgets/RegistersWidget.h b/src/widgets/RegistersWidget.h index 0b3752fe..ec78197c 100644 --- a/src/widgets/RegistersWidget.h +++ b/src/widgets/RegistersWidget.h @@ -8,6 +8,7 @@ #include "core/Cutter.h" #include "CutterDockWidget.h" +#include "menus/AddressableItemContextMenu.h" class MainWindow; @@ -26,11 +27,13 @@ public: private slots: void updateContents(); void setRegisterGrid(); + void openContextMenu(QPoint point, QString address); private: std::unique_ptr ui; QGridLayout *registerLayout = new QGridLayout; + AddressableItemContextMenu addressContextMenu; int numCols = 2; int registerLen = 0; RefreshDeferrer *refreshDeferrer; -}; \ No newline at end of file +};