Enable disabling filters via options dialog

This commit is contained in:
Nyall Dawson 2017-05-17 18:35:31 +10:00
parent ce66393636
commit e8d3ae9264
7 changed files with 107 additions and 24 deletions

View File

@ -173,6 +173,19 @@ class QgsLocatorFilter : QObject
:rtype: bool :rtype: bool
%End %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: signals:
void resultFetched( const QgsLocatorResult &result ); void resultFetched( const QgsLocatorResult &result );

View File

@ -26,6 +26,9 @@ QgsLocatorOptionsWidget::QgsLocatorOptionsWidget( QgsLocator *locator, QWidget *
mModel = new QgsLocatorFiltersModel( mLocator, this ); mModel = new QgsLocatorFiltersModel( mLocator, this );
mFiltersTreeView->setModel( mModel ); mFiltersTreeView->setModel( mModel );
mFiltersTreeView->header()->setStretchLastSection( false );
mFiltersTreeView->header()->setResizeMode( 0, QHeaderView::Stretch );
} }
@ -33,19 +36,17 @@ QgsLocatorOptionsWidget::QgsLocatorOptionsWidget( QgsLocator *locator, QWidget *
// QgsLocatorFiltersModel // QgsLocatorFiltersModel
// //
#define HIDDEN_FILTER_OFFSET 1
QgsLocatorFiltersModel::QgsLocatorFiltersModel( QgsLocator *locator, QObject *parent ) QgsLocatorFiltersModel::QgsLocatorFiltersModel( QgsLocator *locator, QObject *parent )
: QAbstractTableModel( parent ) : QAbstractTableModel( parent )
, mLocator( locator ) , 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 int QgsLocatorFiltersModel::rowCount( const QModelIndex & ) const
{ {
return mLocator->filters().count(); return mLocator->filters().count() - HIDDEN_FILTER_OFFSET;
} }
int QgsLocatorFiltersModel::columnCount( const QModelIndex & ) const int QgsLocatorFiltersModel::columnCount( const QModelIndex & ) const
@ -67,10 +68,10 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
switch ( index.column() ) switch ( index.column() )
{ {
case Name: case Name:
return mLocator->filters().at( index.row() )->displayName(); return filterForIndex( index )->displayName();
case Prefix: case Prefix:
return mLocator->filters().at( index.row() )->prefix(); return filterForIndex( index )->prefix();
case Active: case Active:
case Default: case Default:
@ -86,18 +87,51 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
return QVariant(); return QVariant();
case Active: case Active:
return Qt::Checked; return filterForIndex( index )->enabled() ? Qt::Checked : Qt::Unchecked;
case Default: case Default:
return mLocator->filters().at( index.row() )->useWithoutPrefix() ? Qt::Checked : Qt::Unchecked; return filterForIndex( index )->useWithoutPrefix() ? Qt::Checked : Qt::Unchecked;
} }
} }
return QVariant(); 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 Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
{ {
if ( !index.isValid() || index.row() < 0 || index.column() < 0 || if ( !index.isValid() || index.row() < 0 || index.column() < 0 ||
@ -121,23 +155,30 @@ Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
return flags; 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 ) switch ( section )
{ {
case Name: case Name:
break; return tr( "Filter" );
case Prefix: case Prefix:
return tr( "Prefix" );
case Active: case Active:
return tr( "Enabled" );
case Default: case Default:
size.setWidth( 100 ); return tr( "Default" );
break;
} }
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 );
} }

View File

@ -73,11 +73,14 @@ class QgsLocatorFiltersModel : public QAbstractTableModel
int rowCount( const QModelIndex &parent = QModelIndex() ) const override; int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
int columnCount( 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; 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; Qt::ItemFlags flags( const QModelIndex &index ) const override;
QVariant zheaderData( int section, Qt::Orientation orientation, QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const; int role = Qt::DisplayRole ) const override;
private: private:
QgsLocatorFilter *filterForIndex( const QModelIndex &index ) const;
QgsLocator *mLocator = nullptr; QgsLocator *mLocator = nullptr;
}; };

View File

@ -99,7 +99,7 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
if ( searchString.indexOf( ' ' ) > 0 ) if ( searchString.indexOf( ' ' ) > 0 )
{ {
QString prefix = searchString.left( searchString.indexOf( ' ' ) ); QString prefix = searchString.left( searchString.indexOf( ' ' ) );
if ( mPrefixedFilters.contains( prefix ) ) if ( mPrefixedFilters.contains( prefix ) && mPrefixedFilters.value( prefix )->enabled() )
{ {
mActiveFilters << mPrefixedFilters.value( prefix ); mActiveFilters << mPrefixedFilters.value( prefix );
searchString = searchString.mid( prefix.length() + 1 ); searchString = searchString.mid( prefix.length() + 1 );
@ -109,7 +109,7 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
{ {
Q_FOREACH ( QgsLocatorFilter *filter, mFilters ) Q_FOREACH ( QgsLocatorFilter *filter, mFilters )
{ {
if ( filter->useWithoutPrefix() ) if ( filter->useWithoutPrefix() && filter->enabled() )
mActiveFilters << filter; mActiveFilters << filter;
} }
} }

View File

@ -30,6 +30,16 @@ bool QgsLocatorFilter::stringMatches( const QString &candidate, const QString &s
return candidate.contains( search, Qt::CaseInsensitive ); return candidate.contains( search, Qt::CaseInsensitive );
} }
bool QgsLocatorFilter::enabled() const
{
return mEnabled;
}
void QgsLocatorFilter::setEnabled( bool enabled )
{
mEnabled = enabled;
}
bool QgsLocatorFilter::useWithoutPrefix() const bool QgsLocatorFilter::useWithoutPrefix() const
{ {
return mUseWithoutPrefix; return mUseWithoutPrefix;

View File

@ -190,6 +190,18 @@ class GUI_EXPORT QgsLocatorFilter : public QObject
*/ */
static bool stringMatches( const QString &candidate, const QString &search ); 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: signals:
/** /**
@ -200,6 +212,7 @@ class GUI_EXPORT QgsLocatorFilter : public QObject
private: private:
bool mEnabled = true;
bool mUseWithoutPrefix = true; bool mUseWithoutPrefix = true;
}; };

View File

@ -245,6 +245,9 @@ void QgsLocatorWidget::configMenuAboutToShow()
QMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin(); QMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
for ( ; fIt != filters.constEnd(); ++fIt ) for ( ; fIt != filters.constEnd(); ++fIt )
{ {
if ( !fIt.value()->enabled() )
continue;
QAction *action = new QAction( fIt.value()->displayName(), mMenu ); QAction *action = new QAction( fIt.value()->displayName(), mMenu );
connect( action, &QAction::triggered, this, [ = ] connect( action, &QAction::triggered, this, [ = ]
{ {
@ -576,7 +579,7 @@ void QgsLocatorFilterFilter::fetchResults( const QString &string, const QgsLocat
if ( feedback->isCanceled() ) if ( feedback->isCanceled() )
return; return;
if ( fIt.value() == this || !fIt.value() ) if ( fIt.value() == this || !fIt.value() || !fIt.value()->enabled() )
continue; continue;
QgsLocatorResult result; QgsLocatorResult result;