Skip to content
Permalink
Browse files

Enable disabling filters via options dialog

  • Loading branch information
nyalldawson committed May 17, 2017
1 parent ce66393 commit e8d3ae92644eba1cc35546d86bdce6afdbbcd4cb
@@ -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 );
@@ -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
@@ -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:
@@ -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 ||
@@ -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 );
}
@@ -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;
};

@@ -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 );
@@ -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;
}
}
@@ -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;
@@ -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:

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

private:

bool mEnabled = true;
bool mUseWithoutPrefix = true;

};
@@ -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, [ = ]
{
@@ -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;

0 comments on commit e8d3ae9

Please sign in to comment.
You can’t perform that action at this time.