Skip to content

Commit

Permalink
Show a list of shortcut prefixes in locator bar if no text is entered
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 17, 2017
1 parent 11036e4 commit ea86049
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 8 deletions.
9 changes: 9 additions & 0 deletions python/gui/locator/qgslocator.sip
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,18 @@ class QgsLocator : QObject
QList< QgsLocatorFilter *> filters();
%Docstring
Returns the list of filters registered in the locator.
.. seealso:: prefixedFilters()
:rtype: list of QgsLocatorFilter
%End

QMap< QString, QgsLocatorFilter *> prefixedFilters() const;
%Docstring
Returns a map of prefix to filter, for all registered filters
with valid prefixes.
.. seealso:: filters()
:rtype: QMap< str, QgsLocatorFilter *>
%End

void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback = 0 );
%Docstring
Triggers the background fetching of filter results for a specified search ``string``.
Expand Down
4 changes: 2 additions & 2 deletions src/app/locator/qgsinbuiltlocatorfilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class QgsLayerTreeLocatorFilter : public QgsLocatorFilter

QgsLayerTreeLocatorFilter( QObject *parent = nullptr );
virtual QString name() const override { return QStringLiteral( "layertree" ); }
virtual QString displayName() const override { return tr( "Project layers" ); }
virtual QString displayName() const override { return tr( "Project Layers" ); }
virtual Priority priority() const override { return Highest; }
QString prefix() const override { return QStringLiteral( "l" ); }

Expand All @@ -46,7 +46,7 @@ class QgsLayoutLocatorFilter : public QgsLocatorFilter

QgsLayoutLocatorFilter( QObject *parent = nullptr );
virtual QString name() const override { return QStringLiteral( "layouts" ); }
virtual QString displayName() const override { return tr( "Project layouts" ); }
virtual QString displayName() const override { return tr( "Project Layouts" ); }
virtual Priority priority() const override { return Highest; }
QString prefix() const override { return QStringLiteral( "pl" ); }

Expand Down
5 changes: 5 additions & 0 deletions src/gui/locator/qgslocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ QList<QgsLocatorFilter *> QgsLocator::filters()
return mFilters;
}

QMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
{
return mPrefixedFilters;
}

void QgsLocator::registerFilter( QgsLocatorFilter *filter )
{
mFilters.append( filter );
Expand Down
8 changes: 8 additions & 0 deletions src/gui/locator/qgslocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,17 @@ class GUI_EXPORT QgsLocator : public QObject

/**
* Returns the list of filters registered in the locator.
* \see prefixedFilters()
*/
QList< QgsLocatorFilter *> filters();

/**
* Returns a map of prefix to filter, for all registered filters
* with valid prefixes.
* \see filters()
*/
QMap< QString, QgsLocatorFilter *> prefixedFilters() const;

/**
* Triggers the background fetching of filter results for a specified search \a string.
* The \a context argument encapsulates the context relating to the search (such as a map
Expand Down
51 changes: 45 additions & 6 deletions src/gui/locator/qgslocatorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
mResultsView->installEventFilter( this );
installEventFilter( this );
window()->installEventFilter( this );

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

QgsLocator *QgsLocatorWidget::locator()
Expand Down Expand Up @@ -204,7 +206,10 @@ void QgsLocatorWidget::addResult( const QgsLocatorResult &result )
bool selectFirst = !mHasSelectedResult || mProxyModel->rowCount() == 0;
mLocatorModel->addResult( result );
if ( selectFirst )
mResultsView->setCurrentIndex( mProxyModel->index( 1, 0 ) );
{
int row = mProxyModel->flags( mProxyModel->index( 0, 0 ) ) & Qt::ItemIsSelectable ? 0 : 1;
mResultsView->setCurrentIndex( mProxyModel->index( row, 0 ) );
}
}

void QgsLocatorWidget::updateResults( const QString &text )
Expand All @@ -223,10 +228,7 @@ void QgsLocatorWidget::updateResults( const QString &text )
{
mHasSelectedResult = false;
mLocatorModel->clear();
if ( !text.isEmpty() )
{
mLocator->fetchResults( text, createContext() );
}
mLocator->fetchResults( text, createContext() );
}
}

Expand Down Expand Up @@ -366,7 +368,7 @@ Qt::ItemFlags QgsLocatorModel::flags( const QModelIndex &index ) const
void QgsLocatorModel::addResult( const QgsLocatorResult &result )
{
int pos = mResults.size();
bool addingFilter = !mFoundResultsFromFilterNames.contains( result.filter->name() );
bool addingFilter = !result.filter->displayName().isEmpty() && !mFoundResultsFromFilterNames.contains( result.filter->name() );
if ( addingFilter )
mFoundResultsFromFilterNames << result.filter->name();

Expand Down Expand Up @@ -469,3 +471,40 @@ bool QgsLocatorProxyModel::lessThan( const QModelIndex &left, const QModelIndex
rightFilter = sourceModel()->data( right, Qt::DisplayRole ).toString();
return QString::localeAwareCompare( leftFilter, rightFilter ) < 0;
}

QgsLocatorFilterFilter::QgsLocatorFilterFilter( QgsLocatorWidget *locator, QObject *parent )
: QgsLocatorFilter( parent )
, mLocator( locator )
{}

void QgsLocatorFilterFilter::fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback )
{
if ( !string.isEmpty() )
{
//only shows results when nothing typed
return;
}

QMap< QString, QgsLocatorFilter *> filters = mLocator->locator()->prefixedFilters();
QMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
for ( ; fIt != filters.constEnd(); ++fIt )
{
if ( feedback->isCanceled() )
return;

if ( fIt.value() == this || !fIt.value() )
continue;

QgsLocatorResult result;
result.filter = this;
result.displayString = QString( "%1 (%2)" ).arg( fIt.key(), fIt.value()->displayName() );
result.userData = fIt.key() + ' ';
//result.icon = action->icon();
emit resultFetched( result );
}
}

void QgsLocatorFilterFilter::triggerResult( const QgsLocatorResult &result )
{
mLocator->search( result.userData.toString() );
}
17 changes: 17 additions & 0 deletions src/gui/locator/qgslocatorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget

///@cond PRIVATE

class QgsLocatorFilterFilter : public QgsLocatorFilter
{
public:

QgsLocatorFilterFilter( QgsLocatorWidget *widget, QObject *parent = nullptr );

virtual QString name() const override { return QStringLiteral( "filters" );}
virtual QString displayName() const override { return QString(); }
virtual Priority priority() const { return static_cast< QgsLocatorFilter::Priority>( -1 ); /** shh, we cheat!**/ }
virtual void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
virtual void triggerResult( const QgsLocatorResult &result ) override;

private:

QgsLocatorWidget *mLocator = nullptr;
};

/**
* \class QgsLocatorModel
* \ingroup gui
Expand Down

0 comments on commit ea86049

Please sign in to comment.