Commit af3416a9 authored by Nikolai Kosjar's avatar Nikolai Kosjar

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's avatarTim Jenssen <tim.jenssen@qt.io>
parent 656b9f91
......@@ -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)
......
......@@ -129,6 +129,8 @@ private:
unsigned documentRevision() const;
QMenu *createRefactorMenu(QWidget *parent) const;
TextEditor::RefactorMarkers refactorMarkersWithoutClangMarkers() const;
CppTools::RefactoringEngineInterface *refactoringEngine() const;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment