Skip to content

Commit

Permalink
Enable disabling filters via options dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 17, 2017
1 parent ce66393 commit e8d3ae9
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 24 deletions.
13 changes: 13 additions & 0 deletions python/gui/locator/qgslocatorfilter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ class QgsLocatorFilter : QObject
:rtype: bool
%End

bool enabled() const;
%Docstring
Returns true if the filter is enabled.
.. seealso:: setEnabled()
:rtype: bool
%End

void setEnabled( bool enabled );
%Docstring
Sets whether the filter is ``enabled``.
.. seealso:: enabled()
%End

signals:

void resultFetched( const QgsLocatorResult &result );
Expand Down
79 changes: 60 additions & 19 deletions src/app/locator/qgslocatoroptionswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,27 @@ QgsLocatorOptionsWidget::QgsLocatorOptionsWidget( QgsLocator *locator, QWidget *

mModel = new QgsLocatorFiltersModel( mLocator, this );
mFiltersTreeView->setModel( mModel );

mFiltersTreeView->header()->setStretchLastSection( false );
mFiltersTreeView->header()->setResizeMode( 0, QHeaderView::Stretch );
}


//
// QgsLocatorFiltersModel
//

#define HIDDEN_FILTER_OFFSET 1

QgsLocatorFiltersModel::QgsLocatorFiltersModel( QgsLocator *locator, QObject *parent )
: QAbstractTableModel( parent )
, mLocator( locator )
{
setHeaderData( Name, Qt::Horizontal, tr( "Filter" ) );
setHeaderData( Prefix, Qt::Horizontal, tr( "Prefix" ) );
setHeaderData( Active, Qt::Horizontal, tr( "Enabled" ) );
setHeaderData( Default, Qt::Horizontal, tr( "Default" ) );
}

int QgsLocatorFiltersModel::rowCount( const QModelIndex & ) const
{
return mLocator->filters().count();
return mLocator->filters().count() - HIDDEN_FILTER_OFFSET;
}

int QgsLocatorFiltersModel::columnCount( const QModelIndex & ) const
Expand All @@ -67,10 +68,10 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
switch ( index.column() )
{
case Name:
return mLocator->filters().at( index.row() )->displayName();
return filterForIndex( index )->displayName();

case Prefix:
return mLocator->filters().at( index.row() )->prefix();
return filterForIndex( index )->prefix();

case Active:
case Default:
Expand All @@ -86,18 +87,51 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
return QVariant();

case Active:
return Qt::Checked;
return filterForIndex( index )->enabled() ? Qt::Checked : Qt::Unchecked;

case Default:
return mLocator->filters().at( index.row() )->useWithoutPrefix() ? Qt::Checked : Qt::Unchecked;
return filterForIndex( index )->useWithoutPrefix() ? Qt::Checked : Qt::Unchecked;
}


}

return QVariant();
}

bool QgsLocatorFiltersModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
if ( !index.isValid() || index.row() < 0 || index.column() < 0 ||
index.row() >= rowCount( QModelIndex() ) || index.column() >= columnCount( QModelIndex() ) )
return false;

switch ( role )
{
case Qt::CheckStateRole:
{
switch ( index.column() )
{
case Name:
case Prefix:
return false;

case Active:
{
filterForIndex( index )->setEnabled( value.toInt() == Qt::Checked );
emit dataChanged( index, index, QVector<int>() << Qt::EditRole << Qt::CheckStateRole );
return true;
}

case Default:
{
filterForIndex( index )->setUseWithoutPrefix( value.toInt() == Qt::Checked );
emit dataChanged( index, index, QVector<int>() << Qt::EditRole << Qt::CheckStateRole );
return true;
}
}
}
}
return false;
}

Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
{
if ( !index.isValid() || index.row() < 0 || index.column() < 0 ||
Expand All @@ -121,23 +155,30 @@ Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
return flags;
}

QVariant QgsLocatorFiltersModel::zheaderData( int section, Qt::Orientation orientation, int role ) const
QVariant QgsLocatorFiltersModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if ( orientation == Qt::Horizontal && role == Qt::SizeHintRole )
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
{
QSize size = QAbstractTableModel::headerData( section, orientation, role ).toSize();
switch ( section )
{
case Name:
break;
return tr( "Filter" );

case Prefix:
return tr( "Prefix" );

case Active:
return tr( "Enabled" );

case Default:
size.setWidth( 100 );
break;
return tr( "Default" );
}
return size;
}

return QAbstractTableModel::headerData( section, orientation, role );
return QVariant();
}

QgsLocatorFilter *QgsLocatorFiltersModel::filterForIndex( const QModelIndex &index ) const
{
return mLocator->filters().at( index.row() + HIDDEN_FILTER_OFFSET );
}
7 changes: 5 additions & 2 deletions src/app/locator/qgslocatoroptionswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ class QgsLocatorFiltersModel : public QAbstractTableModel
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;
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
Qt::ItemFlags flags( const QModelIndex &index ) const override;
QVariant zheaderData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const;
QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const override;
private:

QgsLocatorFilter *filterForIndex( const QModelIndex &index ) const;

QgsLocator *mLocator = nullptr;
};

Expand Down
4 changes: 2 additions & 2 deletions src/gui/locator/qgslocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
if ( searchString.indexOf( ' ' ) > 0 )
{
QString prefix = searchString.left( searchString.indexOf( ' ' ) );
if ( mPrefixedFilters.contains( prefix ) )
if ( mPrefixedFilters.contains( prefix ) && mPrefixedFilters.value( prefix )->enabled() )
{
mActiveFilters << mPrefixedFilters.value( prefix );
searchString = searchString.mid( prefix.length() + 1 );
Expand All @@ -109,7 +109,7 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
{
Q_FOREACH ( QgsLocatorFilter *filter, mFilters )
{
if ( filter->useWithoutPrefix() )
if ( filter->useWithoutPrefix() && filter->enabled() )
mActiveFilters << filter;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/gui/locator/qgslocatorfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ bool QgsLocatorFilter::stringMatches( const QString &candidate, const QString &s
return candidate.contains( search, Qt::CaseInsensitive );
}

bool QgsLocatorFilter::enabled() const
{
return mEnabled;
}

void QgsLocatorFilter::setEnabled( bool enabled )
{
mEnabled = enabled;
}

bool QgsLocatorFilter::useWithoutPrefix() const
{
return mUseWithoutPrefix;
Expand Down
13 changes: 13 additions & 0 deletions src/gui/locator/qgslocatorfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ class GUI_EXPORT QgsLocatorFilter : public QObject
*/
static bool stringMatches( const QString &candidate, const QString &search );

/**
* Returns true if the filter is enabled.
* \see setEnabled()
*/
bool enabled() const;

/**
* Sets whether the filter is \a enabled.
* \see enabled()
*/
void setEnabled( bool enabled );

signals:

/**
Expand All @@ -200,6 +212,7 @@ class GUI_EXPORT QgsLocatorFilter : public QObject

private:

bool mEnabled = true;
bool mUseWithoutPrefix = true;

};
Expand Down
5 changes: 4 additions & 1 deletion src/gui/locator/qgslocatorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ void QgsLocatorWidget::configMenuAboutToShow()
QMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
for ( ; fIt != filters.constEnd(); ++fIt )
{
if ( !fIt.value()->enabled() )
continue;

QAction *action = new QAction( fIt.value()->displayName(), mMenu );
connect( action, &QAction::triggered, this, [ = ]
{
Expand Down Expand Up @@ -576,7 +579,7 @@ void QgsLocatorFilterFilter::fetchResults( const QString &string, const QgsLocat
if ( feedback->isCanceled() )
return;

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

QgsLocatorResult result;
Expand Down

0 comments on commit e8d3ae9

Please sign in to comment.