Skip to content

Commit

Permalink
Nicer results, always show an icon, show description
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 17, 2017
1 parent ea86049 commit 2435dbc
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 18 deletions.
5 changes: 5 additions & 0 deletions python/gui/locator/qgslocatorfilter.sip
Expand Up @@ -42,6 +42,11 @@ class QgsLocatorResult
String displayed for result.
%End

QString description;
%Docstring
Descriptive text for result.
%End

QVariant userData;
%Docstring
Custom reference or other data set by the filter.
Expand Down
5 changes: 5 additions & 0 deletions src/gui/locator/qgslocatorfilter.h
Expand Up @@ -66,6 +66,11 @@ class GUI_EXPORT QgsLocatorResult
*/
QString displayString;

/**
* Descriptive text for result.
*/
QString description;

/**
* Custom reference or other data set by the filter.
*/
Expand Down
68 changes: 51 additions & 17 deletions src/gui/locator/qgslocatorwidget.cpp
Expand Up @@ -66,6 +66,8 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
mProxyModel = new QgsLocatorProxyModel( mLocatorModel );
mProxyModel->setSourceModel( mLocatorModel );
mResultsView->setModel( mProxyModel );
mResultsView->setUniformRowHeights( true );
mResultsView->setIconSize( QSize( 16, 16 ) );
mResultsView->recalculateSize();

connect( mLocator, &QgsLocator::foundResult, this, &QgsLocatorWidget::addResult );
Expand Down Expand Up @@ -123,6 +125,14 @@ void QgsLocatorWidget::showList()
mResultsContainer->raise();
}

void QgsLocatorWidget::triggerSearchAndShowList()
{
if ( mProxyModel->rowCount() == 0 )
performSearch();
else
showList();
}

void QgsLocatorWidget::searchFinished()
{
if ( mHasQueuedRequest )
Expand All @@ -146,15 +156,15 @@ bool QgsLocatorWidget::eventFilter( QObject *obj, QEvent *event )
case Qt::Key_Down:
case Qt::Key_PageUp:
case Qt::Key_PageDown:
showList();
triggerSearchAndShowList();
mHasSelectedResult = true;
QgsApplication::sendEvent( mResultsView, event );
return true;
case Qt::Key_Home:
case Qt::Key_End:
if ( keyEvent->modifiers() & Qt::ControlModifier )
{
showList();
triggerSearchAndShowList();
mHasSelectedResult = true;
QgsApplication::sendEvent( mResultsView, event );
return true;
Expand Down Expand Up @@ -192,7 +202,7 @@ bool QgsLocatorWidget::eventFilter( QObject *obj, QEvent *event )
}
else if ( event->type() == QEvent::FocusIn && obj == mLineEdit )
{
showList();
triggerSearchAndShowList();
}
else if ( obj == window() && event->type() == QEvent::Resize )
{
Expand Down Expand Up @@ -272,7 +282,7 @@ QgsLocatorContext QgsLocatorWidget::createContext()
//

QgsLocatorModel::QgsLocatorModel( QObject *parent )
: QAbstractListModel( parent )
: QAbstractTableModel( parent )
{}

void QgsLocatorModel::clear()
Expand All @@ -290,7 +300,7 @@ int QgsLocatorModel::rowCount( const QModelIndex & ) const

int QgsLocatorModel::columnCount( const QModelIndex & ) const
{
return 1;
return 2;
}

QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
Expand All @@ -304,17 +314,37 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
case Qt::DisplayRole:
case Qt::EditRole:
{
if ( !mResults.at( index.row() ).filter )
return mResults.at( index.row() ).result.displayString;
else
return mResults.at( index.row() ).filterTitle;
switch ( index.column() )
{
case Name:
if ( !mResults.at( index.row() ).filter )
return mResults.at( index.row() ).result.displayString;
else
return mResults.at( index.row() ).filterTitle;
case Description:
if ( !mResults.at( index.row() ).filter )
return mResults.at( index.row() ).result.description;
else
return QVariant();
}
}

case Qt::DecorationRole:
if ( !mResults.at( index.row() ).filter )
return mResults.at( index.row() ).result.icon;
else
return QVariant();
switch ( index.column() )
{
case Name:
if ( !mResults.at( index.row() ).filter )
{
QIcon icon = mResults.at( index.row() ).result.icon;
if ( !icon.isNull() )
return icon;
return QgsApplication::getThemeIcon( "/search.svg" );
}
else
return QVariant();
case Description:
return QVariant();
}

case ResultDataRole:
if ( !mResults.at( index.row() ).filter )
Expand Down Expand Up @@ -354,9 +384,9 @@ Qt::ItemFlags QgsLocatorModel::flags( const QModelIndex &index ) const
{
if ( !index.isValid() || index.row() < 0 || index.column() < 0 ||
index.row() >= rowCount( QModelIndex() ) || index.column() >= columnCount( QModelIndex() ) )
return QAbstractListModel::flags( index );
return QAbstractTableModel::flags( index );

Qt::ItemFlags flags = QAbstractListModel::flags( index );
Qt::ItemFlags flags = QAbstractTableModel::flags( index );
if ( !mResults.at( index.row() ).filterTitle.isEmpty() )
{
flags = flags & ~( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
Expand Down Expand Up @@ -411,6 +441,9 @@ void QgsLocatorResultsView::recalculateSize()
// resize the floating widget this is contained within
parentWidget()->resize( newSize );
QTreeView::resize( newSize );

header()->resizeSection( 0, width / 2 );
header()->resizeSection( 1, 0 );
}

void QgsLocatorResultsView::selectNextResult()
Expand Down Expand Up @@ -497,9 +530,10 @@ void QgsLocatorFilterFilter::fetchResults( const QString &string, const QgsLocat

QgsLocatorResult result;
result.filter = this;
result.displayString = QString( "%1 (%2)" ).arg( fIt.key(), fIt.value()->displayName() );
result.displayString = fIt.key();
result.description = fIt.value()->displayName();
result.userData = fIt.key() + ' ';
//result.icon = action->icon();
result.icon = QgsApplication::getThemeIcon( "/search.svg" );
emit resultFetched( result );
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/gui/locator/qgslocatorwidget.h
Expand Up @@ -83,6 +83,7 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
void scheduleDelayedPopup();
void performSearch();
void showList();
void triggerSearchAndShowList();
void searchFinished();
void addResult( const QgsLocatorResult &result );

Expand Down Expand Up @@ -134,7 +135,7 @@ class QgsLocatorFilterFilter : public QgsLocatorFilter
* An abstract list model for displaying the results in a QgsLocatorWidget.
* \since QGIS 3.0
*/
class QgsLocatorModel : public QAbstractListModel
class QgsLocatorModel : public QAbstractTableModel
{
Q_OBJECT

Expand All @@ -150,6 +151,12 @@ class QgsLocatorModel : public QAbstractListModel
ResultFilterNameRole,
};

enum columnCount
{
Name = 0,
Description
};

/**
* Constructor for QgsLocatorModel.
*/
Expand Down

0 comments on commit 2435dbc

Please sign in to comment.