20
20
#include " qgslocator.h"
21
21
#include " qgsfilterlineedit.h"
22
22
#include " qgsmapcanvas.h"
23
- #include < QLayout>
24
- #include < QCompleter>
25
23
#include " qgsapplication.h"
26
24
#include " qgslogger.h"
25
+ #include < QLayout>
26
+ #include < QCompleter>
27
+ #include < QMenu>
27
28
28
29
QgsLocatorWidget::QgsLocatorWidget ( QWidget *parent )
29
30
: QWidget( parent )
@@ -33,7 +34,7 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
33
34
, mResultsView( new QgsLocatorResultsView( this ) )
34
35
{
35
36
mLineEdit ->setShowClearButton ( true );
36
- mLineEdit ->setShowSearchIcon ( true );
37
+ mLineEdit ->setPlaceholderText ( tr ( " Type to locate (Ctrl+K) " ) );
37
38
38
39
resize ( 200 , 30 );
39
40
QSizePolicy sizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
@@ -79,6 +80,9 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
79
80
mPopupTimer .setInterval ( 100 );
80
81
mPopupTimer .setSingleShot ( true );
81
82
connect ( &mPopupTimer , &QTimer::timeout, this , &QgsLocatorWidget::performSearch );
83
+ mFocusTimer .setInterval ( 110 );
84
+ mFocusTimer .setSingleShot ( true );
85
+ connect ( &mFocusTimer , &QTimer::timeout, this , &QgsLocatorWidget::triggerSearchAndShowList );
82
86
83
87
mLineEdit ->installEventFilter ( this );
84
88
mResultsContainer ->installEventFilter ( this );
@@ -87,6 +91,17 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
87
91
window ()->installEventFilter ( this );
88
92
89
93
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
+
90
105
}
91
106
92
107
QgsLocator *QgsLocatorWidget::locator ()
@@ -197,12 +212,13 @@ bool QgsLocatorWidget::eventFilter( QObject *obj, QEvent *event )
197
212
{
198
213
if ( !mLineEdit ->hasFocus () && !mResultsContainer ->hasFocus () && !mResultsView ->hasFocus () )
199
214
{
215
+ mFocusTimer .stop ();
200
216
mResultsContainer ->hide ();
201
217
}
202
218
}
203
219
else if ( event->type () == QEvent::FocusIn && obj == mLineEdit )
204
220
{
205
- triggerSearchAndShowList ();
221
+ mFocusTimer . start ();
206
222
}
207
223
else if ( obj == window () && event->type () == QEvent::Resize )
208
224
{
@@ -222,6 +238,40 @@ void QgsLocatorWidget::addResult( const QgsLocatorResult &result )
222
238
}
223
239
}
224
240
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
+
225
275
void QgsLocatorWidget::updateResults ( const QString &text )
226
276
{
227
277
if ( mLocator ->isRunning () )
0 commit comments