Skip to content

Commit 2435dbc

Browse files
committed
Nicer results, always show an icon, show description
1 parent ea86049 commit 2435dbc

File tree

4 files changed

+69
-18
lines changed

4 files changed

+69
-18
lines changed

python/gui/locator/qgslocatorfilter.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class QgsLocatorResult
4242
String displayed for result.
4343
%End
4444

45+
QString description;
46+
%Docstring
47+
Descriptive text for result.
48+
%End
49+
4550
QVariant userData;
4651
%Docstring
4752
Custom reference or other data set by the filter.

src/gui/locator/qgslocatorfilter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class GUI_EXPORT QgsLocatorResult
6666
*/
6767
QString displayString;
6868

69+
/**
70+
* Descriptive text for result.
71+
*/
72+
QString description;
73+
6974
/**
7075
* Custom reference or other data set by the filter.
7176
*/

src/gui/locator/qgslocatorwidget.cpp

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
6666
mProxyModel = new QgsLocatorProxyModel( mLocatorModel );
6767
mProxyModel->setSourceModel( mLocatorModel );
6868
mResultsView->setModel( mProxyModel );
69+
mResultsView->setUniformRowHeights( true );
70+
mResultsView->setIconSize( QSize( 16, 16 ) );
6971
mResultsView->recalculateSize();
7072

7173
connect( mLocator, &QgsLocator::foundResult, this, &QgsLocatorWidget::addResult );
@@ -123,6 +125,14 @@ void QgsLocatorWidget::showList()
123125
mResultsContainer->raise();
124126
}
125127

128+
void QgsLocatorWidget::triggerSearchAndShowList()
129+
{
130+
if ( mProxyModel->rowCount() == 0 )
131+
performSearch();
132+
else
133+
showList();
134+
}
135+
126136
void QgsLocatorWidget::searchFinished()
127137
{
128138
if ( mHasQueuedRequest )
@@ -146,15 +156,15 @@ bool QgsLocatorWidget::eventFilter( QObject *obj, QEvent *event )
146156
case Qt::Key_Down:
147157
case Qt::Key_PageUp:
148158
case Qt::Key_PageDown:
149-
showList();
159+
triggerSearchAndShowList();
150160
mHasSelectedResult = true;
151161
QgsApplication::sendEvent( mResultsView, event );
152162
return true;
153163
case Qt::Key_Home:
154164
case Qt::Key_End:
155165
if ( keyEvent->modifiers() & Qt::ControlModifier )
156166
{
157-
showList();
167+
triggerSearchAndShowList();
158168
mHasSelectedResult = true;
159169
QgsApplication::sendEvent( mResultsView, event );
160170
return true;
@@ -192,7 +202,7 @@ bool QgsLocatorWidget::eventFilter( QObject *obj, QEvent *event )
192202
}
193203
else if ( event->type() == QEvent::FocusIn && obj == mLineEdit )
194204
{
195-
showList();
205+
triggerSearchAndShowList();
196206
}
197207
else if ( obj == window() && event->type() == QEvent::Resize )
198208
{
@@ -272,7 +282,7 @@ QgsLocatorContext QgsLocatorWidget::createContext()
272282
//
273283

274284
QgsLocatorModel::QgsLocatorModel( QObject *parent )
275-
: QAbstractListModel( parent )
285+
: QAbstractTableModel( parent )
276286
{}
277287

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

291301
int QgsLocatorModel::columnCount( const QModelIndex & ) const
292302
{
293-
return 1;
303+
return 2;
294304
}
295305

296306
QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
@@ -304,17 +314,37 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
304314
case Qt::DisplayRole:
305315
case Qt::EditRole:
306316
{
307-
if ( !mResults.at( index.row() ).filter )
308-
return mResults.at( index.row() ).result.displayString;
309-
else
310-
return mResults.at( index.row() ).filterTitle;
317+
switch ( index.column() )
318+
{
319+
case Name:
320+
if ( !mResults.at( index.row() ).filter )
321+
return mResults.at( index.row() ).result.displayString;
322+
else
323+
return mResults.at( index.row() ).filterTitle;
324+
case Description:
325+
if ( !mResults.at( index.row() ).filter )
326+
return mResults.at( index.row() ).result.description;
327+
else
328+
return QVariant();
329+
}
311330
}
312331

313332
case Qt::DecorationRole:
314-
if ( !mResults.at( index.row() ).filter )
315-
return mResults.at( index.row() ).result.icon;
316-
else
317-
return QVariant();
333+
switch ( index.column() )
334+
{
335+
case Name:
336+
if ( !mResults.at( index.row() ).filter )
337+
{
338+
QIcon icon = mResults.at( index.row() ).result.icon;
339+
if ( !icon.isNull() )
340+
return icon;
341+
return QgsApplication::getThemeIcon( "/search.svg" );
342+
}
343+
else
344+
return QVariant();
345+
case Description:
346+
return QVariant();
347+
}
318348

319349
case ResultDataRole:
320350
if ( !mResults.at( index.row() ).filter )
@@ -354,9 +384,9 @@ Qt::ItemFlags QgsLocatorModel::flags( const QModelIndex &index ) const
354384
{
355385
if ( !index.isValid() || index.row() < 0 || index.column() < 0 ||
356386
index.row() >= rowCount( QModelIndex() ) || index.column() >= columnCount( QModelIndex() ) )
357-
return QAbstractListModel::flags( index );
387+
return QAbstractTableModel::flags( index );
358388

359-
Qt::ItemFlags flags = QAbstractListModel::flags( index );
389+
Qt::ItemFlags flags = QAbstractTableModel::flags( index );
360390
if ( !mResults.at( index.row() ).filterTitle.isEmpty() )
361391
{
362392
flags = flags & ~( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
@@ -411,6 +441,9 @@ void QgsLocatorResultsView::recalculateSize()
411441
// resize the floating widget this is contained within
412442
parentWidget()->resize( newSize );
413443
QTreeView::resize( newSize );
444+
445+
header()->resizeSection( 0, width / 2 );
446+
header()->resizeSection( 1, 0 );
414447
}
415448

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

498531
QgsLocatorResult result;
499532
result.filter = this;
500-
result.displayString = QString( "%1 (%2)" ).arg( fIt.key(), fIt.value()->displayName() );
533+
result.displayString = fIt.key();
534+
result.description = fIt.value()->displayName();
501535
result.userData = fIt.key() + ' ';
502-
//result.icon = action->icon();
536+
result.icon = QgsApplication::getThemeIcon( "/search.svg" );
503537
emit resultFetched( result );
504538
}
505539
}

src/gui/locator/qgslocatorwidget.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
8383
void scheduleDelayedPopup();
8484
void performSearch();
8585
void showList();
86+
void triggerSearchAndShowList();
8687
void searchFinished();
8788
void addResult( const QgsLocatorResult &result );
8889

@@ -134,7 +135,7 @@ class QgsLocatorFilterFilter : public QgsLocatorFilter
134135
* An abstract list model for displaying the results in a QgsLocatorWidget.
135136
* \since QGIS 3.0
136137
*/
137-
class QgsLocatorModel : public QAbstractListModel
138+
class QgsLocatorModel : public QAbstractTableModel
138139
{
139140
Q_OBJECT
140141

@@ -150,6 +151,12 @@ class QgsLocatorModel : public QAbstractListModel
150151
ResultFilterNameRole,
151152
};
152153

154+
enum columnCount
155+
{
156+
Name = 0,
157+
Description
158+
};
159+
153160
/**
154161
* Constructor for QgsLocatorModel.
155162
*/

0 commit comments

Comments
 (0)