From 07b1e1c5fe03806f7c8d78e1ae72ebd38a1b9849 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Tue, 11 Apr 2023 18:05:31 +0200 Subject: [PATCH] LocatorFilterEntry: Add Acceptor field Add also AcceptResult structure, returned by acceptor. The Acceptor, being a member of LocatorFilterEntry, is going to be used instead of virtual ILocatorFilter::accept() method. By default, when no Acceptor is provided, the locator widget is going to call EditorManager::openEditor() with a LocatorFilterEntry instance. Change-Id: Ic9697492738d65fd1331bbd0872bc374285c4e53 Reviewed-by: Eike Ziller --- .../coreplugin/locator/ilocatorfilter.h | 15 ++++++++++++++- .../coreplugin/locator/locatorwidget.cpp | 19 ++++++++++++------- .../coreplugin/locator/locatorwidget.h | 3 +-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/plugins/coreplugin/locator/ilocatorfilter.h b/src/plugins/coreplugin/locator/ilocatorfilter.h index 888d7b391e1..3babe87516c 100644 --- a/src/plugins/coreplugin/locator/ilocatorfilter.h +++ b/src/plugins/coreplugin/locator/ilocatorfilter.h @@ -26,6 +26,14 @@ namespace Internal { class Locator; } class ILocatorFilter; +class AcceptResult +{ +public: + QString newText; + int selectionStart = -1; + int selectionLength = 0; +}; + class LocatorFilterEntry { public: @@ -77,6 +85,7 @@ public: , displayName(name) {} + using Acceptor = std::function; /* backpointer to creating filter */ ILocatorFilter *filter = nullptr; /* displayed string */ @@ -87,8 +96,12 @@ public: QString extraInfo; /* additional tooltip */ QString toolTip; + /* called by locator widget on accept. By default, when acceptor is empty, + EditorManager::openEditor(LocatorFilterEntry) will be used instead. */ + Acceptor acceptor; /* can be used by the filter to save more information about the entry */ - QVariant internalData; // DON'T USE IN NEW CODE, IT'S GOING TO BE REMOVED, SOON... + /* Replaced by acceptor - DON'T USE IN NEW CODE, IT'S GOING TO BE REMOVED, SOON... */ + QVariant internalData; /* icon to display along with the entry */ std::optional displayIcon; /* file path, if the entry is related to a file, is used e.g. for resolving a file icon */ diff --git a/src/plugins/coreplugin/locator/locatorwidget.cpp b/src/plugins/coreplugin/locator/locatorwidget.cpp index 7bb381ac01b..bd31b69fe13 100644 --- a/src/plugins/coreplugin/locator/locatorwidget.cpp +++ b/src/plugins/coreplugin/locator/locatorwidget.cpp @@ -10,6 +10,7 @@ #include "locatorsearchutils.h" #include "../actionmanager/actionmanager.h" #include "../coreplugintr.h" +#include "../editormanager/editormanager.h" #include "../icore.h" #include "../modemanager.h" @@ -1003,18 +1004,22 @@ void LocatorWidget::acceptEntry(int row) if (!index.isValid()) return; const LocatorFilterEntry entry = m_locatorModel->data(index, LocatorEntryRole).value(); - Q_ASSERT(entry.filter != nullptr); - QString newText; - int selectionStart = -1; - int selectionLength = 0; QWidget *focusBeforeAccept = QApplication::focusWidget(); - entry.filter->accept(entry, &newText, &selectionStart, &selectionLength); - if (newText.isEmpty()) { + AcceptResult result; + if (entry.acceptor) { + result = entry.acceptor(); + } else if (entry.filter) { + entry.filter->accept(entry, &result.newText, &result.selectionStart, + &result.selectionLength); + } else { + EditorManager::openEditor(entry); + } + if (result.newText.isEmpty()) { emit hidePopup(); if (QApplication::focusWidget() == focusBeforeAccept) resetFocus(m_previousFocusWidget, isInMainWindow()); } else { - showText(newText, selectionStart, selectionLength); + showText(result.newText, result.selectionStart, result.selectionLength); } } diff --git a/src/plugins/coreplugin/locator/locatorwidget.h b/src/plugins/coreplugin/locator/locatorwidget.h index eef5a144147..efc90c3a517 100644 --- a/src/plugins/coreplugin/locator/locatorwidget.h +++ b/src/plugins/coreplugin/locator/locatorwidget.h @@ -28,8 +28,7 @@ namespace Internal { class LocatorModel; class CompletionList; -class LocatorWidget - : public QWidget +class LocatorWidget : public QWidget { Q_OBJECT