diff options
author | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2017-09-14 17:29:34 +0200 |
---|---|---|
committer | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2017-09-19 06:59:04 +0000 |
commit | af3416a9de180173adca8f18d8bbc439d18e521c (patch) | |
tree | 2d4db952c5d9a551ec6a51e09b8e96da04f5ae97 /src/plugins/cppeditor/cppeditorwidget.cpp | |
parent | 656b9f918540e7e95d0f599415a6f122a417fbbe (diff) |
CppEditor: Refactor CppEditorWidget::contextMenuEvent
No behavior change.
* Extract some functions
* Add some clarifying comments here and there
* Add QTC_CHECK()
* Remove QLatin1String
* foreach -> for
* Const-correctness
Change-Id: Ie6ccc987ef959c5295a4d3225aac0dbad144f91d
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
Diffstat (limited to 'src/plugins/cppeditor/cppeditorwidget.cpp')
-rw-r--r-- | src/plugins/cppeditor/cppeditorwidget.cpp | 78 |
1 files changed, 48 insertions, 30 deletions
diff --git a/src/plugins/cppeditor/cppeditorwidget.cpp b/src/plugins/cppeditor/cppeditorwidget.cpp index fb25a9edd7..f0f02934d9 100644 --- a/src/plugins/cppeditor/cppeditorwidget.cpp +++ b/src/plugins/cppeditor/cppeditorwidget.cpp @@ -743,51 +743,69 @@ void CppEditorWidget::processKeyNormally(QKeyEvent *e) TextEditorWidget::keyPressEvent(e); } -void CppEditorWidget::contextMenuEvent(QContextMenuEvent *e) +static void addRefactoringActions(QMenu *menu, AssistInterface *iface) { - // ### enable - // updateSemanticInfo(m_semanticHighlighter->semanticInfo(currentSource())); + if (!iface || !menu) + return; - QPointer<QMenu> menu(new QMenu(this)); + using Processor = QScopedPointer<IAssistProcessor>; + using Proposal = QScopedPointer<IAssistProposal>; + using Model = QScopedPointer<GenericProposalModel>; + + const Processor processor(CppEditorPlugin::instance()->quickFixProvider()->createProcessor()); + const Proposal proposal(processor->perform(iface)); // OK, perform() takes ownership of iface. + if (proposal) { + Model model(static_cast<GenericProposalModel *>(proposal->model())); + for (int index = 0; index < model->size(); ++index) { + const auto item = static_cast<AssistProposalItem *>(model->proposalItem(index)); + const QuickFixOperation::Ptr op = item->data().value<QuickFixOperation::Ptr>(); + const QAction *action = menu->addAction(op->description()); + QObject::connect(action, &QAction::triggered, menu, [op] { op->perform(); }); + } + } +} - ActionContainer *mcontext = ActionManager::actionContainer(Constants::M_CONTEXT); - QMenu *contextMenu = mcontext->menu(); +QMenu *CppEditorWidget::createRefactorMenu(QWidget *parent) const +{ + auto *menu = new QMenu(tr("&Refactor"), parent); + menu->addAction(ActionManager::command(Constants::RENAME_SYMBOL_UNDER_CURSOR)->action()); - QMenu *quickFixMenu = new QMenu(tr("&Refactor"), menu); - quickFixMenu->addAction(ActionManager::command(Constants::RENAME_SYMBOL_UNDER_CURSOR)->action()); + // ### enable + // updateSemanticInfo(m_semanticHighlighter->semanticInfo(currentSource())); if (isSemanticInfoValidExceptLocalUses()) { d->m_useSelectionsUpdater.update(CppUseSelectionsUpdater::Synchronous); - AssistInterface *interface = createAssistInterface(QuickFix, ExplicitlyInvoked); - if (interface) { - QScopedPointer<IAssistProcessor> processor( - CppEditorPlugin::instance()->quickFixProvider()->createProcessor()); - QScopedPointer<IAssistProposal> proposal(processor->perform(interface)); - if (!proposal.isNull()) { - auto model = static_cast<GenericProposalModel *>(proposal->model()); - for (int index = 0; index < model->size(); ++index) { - auto item = static_cast<AssistProposalItem *>(model->proposalItem(index)); - QuickFixOperation::Ptr op = item->data().value<QuickFixOperation::Ptr>(); - QAction *action = quickFixMenu->addAction(op->description()); - connect(action, &QAction::triggered, this, [op] { op->perform(); }); - } - delete model; - } - } + addRefactoringActions(menu, createAssistInterface(QuickFix, ExplicitlyInvoked)); } - foreach (QAction *action, contextMenu->actions()) { + return menu; +} + +static void appendCustomContextMenuActionsAndMenus(QMenu *menu, QMenu *refactorMenu) +{ + bool isRefactoringMenuAdded = false; + const QMenu *contextMenu = ActionManager::actionContainer(Constants::M_CONTEXT)->menu(); + for (QAction *action : contextMenu->actions()) { menu->addAction(action); - if (action->objectName() == QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT)) - menu->addMenu(quickFixMenu); + if (action->objectName() == Constants::M_REFACTORING_MENU_INSERTION_POINT) { + isRefactoringMenuAdded = true; + menu->addMenu(refactorMenu); + } } + QTC_CHECK(isRefactoringMenuAdded); +} + +void CppEditorWidget::contextMenuEvent(QContextMenuEvent *e) +{ + const QPointer<QMenu> menu(new QMenu(this)); + + appendCustomContextMenuActionsAndMenus(menu, createRefactorMenu(menu)); appendStandardContextMenuActions(menu); menu->exec(e->globalPos()); - if (!menu) - return; - delete menu; + if (menu) + delete menu; // OK, menu was not already deleted by closed editor widget. } void CppEditorWidget::keyPressEvent(QKeyEvent *e) |