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 <eike.ziller@qt.io>
This commit is contained in:
Jarek Kobus
2023-04-11 18:05:31 +02:00
parent abf7dd23ed
commit 07b1e1c5fe
3 changed files with 27 additions and 10 deletions

View File

@@ -26,6 +26,14 @@ namespace Internal { class Locator; }
class ILocatorFilter; class ILocatorFilter;
class AcceptResult
{
public:
QString newText;
int selectionStart = -1;
int selectionLength = 0;
};
class LocatorFilterEntry class LocatorFilterEntry
{ {
public: public:
@@ -77,6 +85,7 @@ public:
, displayName(name) , displayName(name)
{} {}
using Acceptor = std::function<AcceptResult()>;
/* backpointer to creating filter */ /* backpointer to creating filter */
ILocatorFilter *filter = nullptr; ILocatorFilter *filter = nullptr;
/* displayed string */ /* displayed string */
@@ -87,8 +96,12 @@ public:
QString extraInfo; QString extraInfo;
/* additional tooltip */ /* additional tooltip */
QString 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 */ /* 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 */ /* icon to display along with the entry */
std::optional<QIcon> displayIcon; std::optional<QIcon> displayIcon;
/* file path, if the entry is related to a file, is used e.g. for resolving a file icon */ /* file path, if the entry is related to a file, is used e.g. for resolving a file icon */

View File

@@ -10,6 +10,7 @@
#include "locatorsearchutils.h" #include "locatorsearchutils.h"
#include "../actionmanager/actionmanager.h" #include "../actionmanager/actionmanager.h"
#include "../coreplugintr.h" #include "../coreplugintr.h"
#include "../editormanager/editormanager.h"
#include "../icore.h" #include "../icore.h"
#include "../modemanager.h" #include "../modemanager.h"
@@ -1003,18 +1004,22 @@ void LocatorWidget::acceptEntry(int row)
if (!index.isValid()) if (!index.isValid())
return; return;
const LocatorFilterEntry entry = m_locatorModel->data(index, LocatorEntryRole).value<LocatorFilterEntry>(); const LocatorFilterEntry entry = m_locatorModel->data(index, LocatorEntryRole).value<LocatorFilterEntry>();
Q_ASSERT(entry.filter != nullptr);
QString newText;
int selectionStart = -1;
int selectionLength = 0;
QWidget *focusBeforeAccept = QApplication::focusWidget(); QWidget *focusBeforeAccept = QApplication::focusWidget();
entry.filter->accept(entry, &newText, &selectionStart, &selectionLength); AcceptResult result;
if (newText.isEmpty()) { 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(); emit hidePopup();
if (QApplication::focusWidget() == focusBeforeAccept) if (QApplication::focusWidget() == focusBeforeAccept)
resetFocus(m_previousFocusWidget, isInMainWindow()); resetFocus(m_previousFocusWidget, isInMainWindow());
} else { } else {
showText(newText, selectionStart, selectionLength); showText(result.newText, result.selectionStart, result.selectionLength);
} }
} }

View File

@@ -28,8 +28,7 @@ namespace Internal {
class LocatorModel; class LocatorModel;
class CompletionList; class CompletionList;
class LocatorWidget class LocatorWidget : public QWidget
: public QWidget
{ {
Q_OBJECT Q_OBJECT