Skip to content

Commit fb6ba51

Browse files
committed
Add configure menu to locator widget
1 parent 5a0bcf0 commit fb6ba51

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/gui/locator/qgslocatorwidget.cpp

+54-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
#include "qgslocator.h"
2121
#include "qgsfilterlineedit.h"
2222
#include "qgsmapcanvas.h"
23-
#include <QLayout>
24-
#include <QCompleter>
2523
#include "qgsapplication.h"
2624
#include "qgslogger.h"
25+
#include <QLayout>
26+
#include <QCompleter>
27+
#include <QMenu>
2728

2829
QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
2930
: QWidget( parent )
@@ -33,7 +34,7 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
3334
, mResultsView( new QgsLocatorResultsView( this ) )
3435
{
3536
mLineEdit->setShowClearButton( true );
36-
mLineEdit->setShowSearchIcon( true );
37+
mLineEdit->setPlaceholderText( tr( "Type to locate (Ctrl+K)" ) );
3738

3839
resize( 200, 30 );
3940
QSizePolicy sizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
@@ -79,6 +80,9 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
7980
mPopupTimer.setInterval( 100 );
8081
mPopupTimer.setSingleShot( true );
8182
connect( &mPopupTimer, &QTimer::timeout, this, &QgsLocatorWidget::performSearch );
83+
mFocusTimer.setInterval( 110 );
84+
mFocusTimer.setSingleShot( true );
85+
connect( &mFocusTimer, &QTimer::timeout, this, &QgsLocatorWidget::triggerSearchAndShowList );
8286

8387
mLineEdit->installEventFilter( this );
8488
mResultsContainer->installEventFilter( this );
@@ -87,6 +91,17 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
8791
window()->installEventFilter( this );
8892

8993
mLocator->registerFilter( new QgsLocatorFilterFilter( this, this ) );
94+
95+
mMenu = new QMenu( this );
96+
QAction *menuAction = mLineEdit->addAction( QgsApplication::getThemeIcon( "/search.svg" ), QLineEdit::LeadingPosition );
97+
connect( menuAction, &QAction::triggered, this, [ = ]
98+
{
99+
mFocusTimer.stop();
100+
mResultsContainer->hide();
101+
mMenu->exec( QCursor::pos() );
102+
} );
103+
connect( mMenu, &QMenu::aboutToShow, this, &QgsLocatorWidget::configMenuAboutToShow );
104+
90105
}
91106

92107
QgsLocator *QgsLocatorWidget::locator()
@@ -197,12 +212,13 @@ bool QgsLocatorWidget::eventFilter( QObject *obj, QEvent *event )
197212
{
198213
if ( !mLineEdit->hasFocus() && !mResultsContainer->hasFocus() && !mResultsView->hasFocus() )
199214
{
215+
mFocusTimer.stop();
200216
mResultsContainer->hide();
201217
}
202218
}
203219
else if ( event->type() == QEvent::FocusIn && obj == mLineEdit )
204220
{
205-
triggerSearchAndShowList();
221+
mFocusTimer.start();
206222
}
207223
else if ( obj == window() && event->type() == QEvent::Resize )
208224
{
@@ -222,6 +238,40 @@ void QgsLocatorWidget::addResult( const QgsLocatorResult &result )
222238
}
223239
}
224240

241+
void QgsLocatorWidget::configMenuAboutToShow()
242+
{
243+
mMenu->clear();
244+
QMap< QString, QgsLocatorFilter *> filters = mLocator->prefixedFilters();
245+
QMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
246+
for ( ; fIt != filters.constEnd(); ++fIt )
247+
{
248+
QAction *action = new QAction( fIt.value()->displayName(), mMenu );
249+
connect( action, &QAction::triggered, this, [ = ]
250+
{
251+
QString currentText = mLineEdit->text();
252+
if ( currentText.isEmpty() )
253+
currentText = tr( "<type here>" );
254+
else
255+
{
256+
QStringList parts = currentText.split( ' ' );
257+
if ( parts.count() > 1 && mLocator->prefixedFilters().contains( parts.at( 0 ) ) )
258+
{
259+
parts.pop_front();
260+
currentText = parts.join( ' ' );
261+
}
262+
}
263+
264+
mLineEdit->setText( fIt.key() + ' ' + currentText );
265+
mLineEdit->setSelection( fIt.key().length() + 1, currentText.length() );
266+
} );
267+
mMenu->addAction( action );
268+
}
269+
mMenu->addSeparator();
270+
QAction *configAction = new QAction( trUtf8( "Configure…" ), mMenu );
271+
mMenu->addAction( configAction );
272+
273+
}
274+
225275
void QgsLocatorWidget::updateResults( const QString &text )
226276
{
227277
if ( mLocator->isRunning() )

src/gui/locator/qgslocatorwidget.h

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
8686
void triggerSearchAndShowList();
8787
void searchFinished();
8888
void addResult( const QgsLocatorResult &result );
89+
void configMenuAboutToShow();
8990

9091
private:
9192

@@ -96,11 +97,13 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
9697
QgsFloatingWidget *mResultsContainer = nullptr;
9798
QgsLocatorResultsView *mResultsView = nullptr;
9899
QgsMapCanvas *mMapCanvas = nullptr;
100+
QMenu *mMenu = nullptr;
99101

100102
QString mNextRequestedString;
101103
bool mHasQueuedRequest = false;
102104
bool mHasSelectedResult = false;
103105
QTimer mPopupTimer;
106+
QTimer mFocusTimer;
104107

105108
void updateResults( const QString &text );
106109
void acceptCurrentEntry();

0 commit comments

Comments
 (0)