Skip to content
Permalink
Browse files
Don't clear locator model immediately when a new search triggers
Instead delay the search until either a tiny timeout (100ms) or
the first new results come in, whichever happens first.

This avoids the list view clearing too frequently and giving
a distracting "flashing" appearance when users type quickly
into to locator bar.
  • Loading branch information
nyalldawson committed May 21, 2017
1 parent d55fa22 commit 1577a57a19ce18f8f0aece046e3a7e5b30087e6a
Showing with 47 additions and 5 deletions.
  1. +36 −5 src/gui/locator/qgslocatorwidget.cpp
  2. +11 −0 src/gui/locator/qgslocatorwidget.h
@@ -304,7 +304,7 @@ void QgsLocatorWidget::updateResults( const QString &text )
else
{
mHasSelectedResult = false;
mLocatorModel->clear();
mLocatorModel->deferredClear();
mLocator->fetchResults( text, createContext() );
}
}
@@ -350,16 +350,29 @@ QgsLocatorContext QgsLocatorWidget::createContext()

QgsLocatorModel::QgsLocatorModel( QObject *parent )
: QAbstractTableModel( parent )
{}
{
mDeferredClearTimer.setInterval( 100 );
mDeferredClearTimer.setSingleShot( true );
connect( &mDeferredClearTimer, &QTimer::timeout, this, &QgsLocatorModel::clear );
}

void QgsLocatorModel::clear()
{
mDeferredClearTimer.stop();
mDeferredClear = false;

beginResetModel();
mResults.clear();
mFoundResultsFromFilterNames.clear();
endResetModel();
}

void QgsLocatorModel::deferredClear()
{
mDeferredClear = true;
mDeferredClearTimer.start();
}

int QgsLocatorModel::rowCount( const QModelIndex & ) const
{
return mResults.size();
@@ -459,17 +472,29 @@ Qt::ItemFlags QgsLocatorModel::flags( const QModelIndex &index ) const
flags = flags & ~( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
}
return flags;

}

void QgsLocatorModel::addResult( const QgsLocatorResult &result )
{
mDeferredClearTimer.stop();
if ( mDeferredClear )
{
mFoundResultsFromFilterNames.clear();
}

int pos = mResults.size();
bool addingFilter = !result.filter->displayName().isEmpty() && !mFoundResultsFromFilterNames.contains( result.filter->name() );
if ( addingFilter )
mFoundResultsFromFilterNames << result.filter->name();

beginInsertRows( QModelIndex(), pos, pos + ( addingFilter ? 1 : 0 ) );
if ( mDeferredClear )
{
beginResetModel();
mResults.clear();
}
else
beginInsertRows( QModelIndex(), pos, pos + ( addingFilter ? 1 : 0 ) );

if ( addingFilter )
{
Entry entry;
@@ -480,7 +505,13 @@ void QgsLocatorModel::addResult( const QgsLocatorResult &result )
Entry entry;
entry.result = result;
mResults << entry;
endInsertRows();

if ( mDeferredClear )
endResetModel();
else
endInsertRows();

mDeferredClear = false;
}


@@ -180,9 +180,18 @@ class QgsLocatorModel : public QAbstractTableModel

/**
* Resets the model and clears all existing results.
* \see deferredClear()
*/
void clear();

/**
* Resets the model and clears all existing results after a short delay, or whenever the next result is added to the model
* (whichever occurs first). Using deferredClear() instead of clear() can avoid the visually distracting frequent clears
* which may occur if the model is being updated quickly multiple times as a result of users typing in a search query.
* \see deferredClear()
*/
void deferredClear();

int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
@@ -206,6 +215,8 @@ class QgsLocatorModel : public QAbstractTableModel

QList<Entry> mResults;
QSet<QString> mFoundResultsFromFilterNames;
bool mDeferredClear = false;
QTimer mDeferredClearTimer;
};

class QgsLocatorProxyModel : public QSortFilterProxyModel

0 comments on commit 1577a57

Please sign in to comment.