Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add configure menu to locator widget
  • Loading branch information
nyalldawson committed May 17, 2017
1 parent 5a0bcf0 commit fb6ba51
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
58 changes: 54 additions & 4 deletions src/gui/locator/qgslocatorwidget.cpp
Expand Up @@ -20,10 +20,11 @@
#include "qgslocator.h"
#include "qgsfilterlineedit.h"
#include "qgsmapcanvas.h"
#include <QLayout>
#include <QCompleter>
#include "qgsapplication.h"
#include "qgslogger.h"
#include <QLayout>
#include <QCompleter>
#include <QMenu>

QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
: QWidget( parent )
Expand All @@ -33,7 +34,7 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
, mResultsView( new QgsLocatorResultsView( this ) )
{
mLineEdit->setShowClearButton( true );
mLineEdit->setShowSearchIcon( true );
mLineEdit->setPlaceholderText( tr( "Type to locate (Ctrl+K)" ) );

resize( 200, 30 );
QSizePolicy sizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
Expand Down Expand Up @@ -79,6 +80,9 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
mPopupTimer.setInterval( 100 );
mPopupTimer.setSingleShot( true );
connect( &mPopupTimer, &QTimer::timeout, this, &QgsLocatorWidget::performSearch );
mFocusTimer.setInterval( 110 );
mFocusTimer.setSingleShot( true );
connect( &mFocusTimer, &QTimer::timeout, this, &QgsLocatorWidget::triggerSearchAndShowList );

mLineEdit->installEventFilter( this );
mResultsContainer->installEventFilter( this );
Expand All @@ -87,6 +91,17 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
window()->installEventFilter( this );

mLocator->registerFilter( new QgsLocatorFilterFilter( this, this ) );

mMenu = new QMenu( this );
QAction *menuAction = mLineEdit->addAction( QgsApplication::getThemeIcon( "/search.svg" ), QLineEdit::LeadingPosition );
connect( menuAction, &QAction::triggered, this, [ = ]
{
mFocusTimer.stop();
mResultsContainer->hide();
mMenu->exec( QCursor::pos() );
} );
connect( mMenu, &QMenu::aboutToShow, this, &QgsLocatorWidget::configMenuAboutToShow );

}

QgsLocator *QgsLocatorWidget::locator()
Expand Down Expand Up @@ -197,12 +212,13 @@ bool QgsLocatorWidget::eventFilter( QObject *obj, QEvent *event )
{
if ( !mLineEdit->hasFocus() && !mResultsContainer->hasFocus() && !mResultsView->hasFocus() )
{
mFocusTimer.stop();
mResultsContainer->hide();
}
}
else if ( event->type() == QEvent::FocusIn && obj == mLineEdit )
{
triggerSearchAndShowList();
mFocusTimer.start();
}
else if ( obj == window() && event->type() == QEvent::Resize )
{
Expand All @@ -222,6 +238,40 @@ void QgsLocatorWidget::addResult( const QgsLocatorResult &result )
}
}

void QgsLocatorWidget::configMenuAboutToShow()
{
mMenu->clear();
QMap< QString, QgsLocatorFilter *> filters = mLocator->prefixedFilters();
QMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
for ( ; fIt != filters.constEnd(); ++fIt )
{
QAction *action = new QAction( fIt.value()->displayName(), mMenu );
connect( action, &QAction::triggered, this, [ = ]
{
QString currentText = mLineEdit->text();
if ( currentText.isEmpty() )
currentText = tr( "<type here>" );
else
{
QStringList parts = currentText.split( ' ' );
if ( parts.count() > 1 && mLocator->prefixedFilters().contains( parts.at( 0 ) ) )
{
parts.pop_front();
currentText = parts.join( ' ' );
}
}

mLineEdit->setText( fIt.key() + ' ' + currentText );
mLineEdit->setSelection( fIt.key().length() + 1, currentText.length() );
} );
mMenu->addAction( action );
}
mMenu->addSeparator();
QAction *configAction = new QAction( trUtf8( "Configure…" ), mMenu );
mMenu->addAction( configAction );

}

void QgsLocatorWidget::updateResults( const QString &text )
{
if ( mLocator->isRunning() )
Expand Down
3 changes: 3 additions & 0 deletions src/gui/locator/qgslocatorwidget.h
Expand Up @@ -86,6 +86,7 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
void triggerSearchAndShowList();
void searchFinished();
void addResult( const QgsLocatorResult &result );
void configMenuAboutToShow();

private:

Expand All @@ -96,11 +97,13 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
QgsFloatingWidget *mResultsContainer = nullptr;
QgsLocatorResultsView *mResultsView = nullptr;
QgsMapCanvas *mMapCanvas = nullptr;
QMenu *mMenu = nullptr;

QString mNextRequestedString;
bool mHasQueuedRequest = false;
bool mHasSelectedResult = false;
QTimer mPopupTimer;
QTimer mFocusTimer;

void updateResults( const QString &text );
void acceptCurrentEntry();
Expand Down

0 comments on commit fb6ba51

Please sign in to comment.