Skip to content

Commit e8d3ae9

Browse files
committedMay 17, 2017
Enable disabling filters via options dialog
1 parent ce66393 commit e8d3ae9

File tree

7 files changed

+107
-24
lines changed

7 files changed

+107
-24
lines changed
 

‎python/gui/locator/qgslocatorfilter.sip

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ class QgsLocatorFilter : QObject
173173
:rtype: bool
174174
%End
175175

176+
bool enabled() const;
177+
%Docstring
178+
Returns true if the filter is enabled.
179+
.. seealso:: setEnabled()
180+
:rtype: bool
181+
%End
182+
183+
void setEnabled( bool enabled );
184+
%Docstring
185+
Sets whether the filter is ``enabled``.
186+
.. seealso:: enabled()
187+
%End
188+
176189
signals:
177190

178191
void resultFetched( const QgsLocatorResult &result );

‎src/app/locator/qgslocatoroptionswidget.cpp

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,27 @@ QgsLocatorOptionsWidget::QgsLocatorOptionsWidget( QgsLocator *locator, QWidget *
2626

2727
mModel = new QgsLocatorFiltersModel( mLocator, this );
2828
mFiltersTreeView->setModel( mModel );
29+
30+
mFiltersTreeView->header()->setStretchLastSection( false );
31+
mFiltersTreeView->header()->setResizeMode( 0, QHeaderView::Stretch );
2932
}
3033

3134

3235
//
3336
// QgsLocatorFiltersModel
3437
//
3538

39+
#define HIDDEN_FILTER_OFFSET 1
40+
3641
QgsLocatorFiltersModel::QgsLocatorFiltersModel( QgsLocator *locator, QObject *parent )
3742
: QAbstractTableModel( parent )
3843
, mLocator( locator )
3944
{
40-
setHeaderData( Name, Qt::Horizontal, tr( "Filter" ) );
41-
setHeaderData( Prefix, Qt::Horizontal, tr( "Prefix" ) );
42-
setHeaderData( Active, Qt::Horizontal, tr( "Enabled" ) );
43-
setHeaderData( Default, Qt::Horizontal, tr( "Default" ) );
4445
}
4546

4647
int QgsLocatorFiltersModel::rowCount( const QModelIndex & ) const
4748
{
48-
return mLocator->filters().count();
49+
return mLocator->filters().count() - HIDDEN_FILTER_OFFSET;
4950
}
5051

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

7273
case Prefix:
73-
return mLocator->filters().at( index.row() )->prefix();
74+
return filterForIndex( index )->prefix();
7475

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

8889
case Active:
89-
return Qt::Checked;
90+
return filterForIndex( index )->enabled() ? Qt::Checked : Qt::Unchecked;
9091

9192
case Default:
92-
return mLocator->filters().at( index.row() )->useWithoutPrefix() ? Qt::Checked : Qt::Unchecked;
93+
return filterForIndex( index )->useWithoutPrefix() ? Qt::Checked : Qt::Unchecked;
9394
}
94-
95-
9695
}
9796

9897
return QVariant();
9998
}
10099

100+
bool QgsLocatorFiltersModel::setData( const QModelIndex &index, const QVariant &value, int role )
101+
{
102+
if ( !index.isValid() || index.row() < 0 || index.column() < 0 ||
103+
index.row() >= rowCount( QModelIndex() ) || index.column() >= columnCount( QModelIndex() ) )
104+
return false;
105+
106+
switch ( role )
107+
{
108+
case Qt::CheckStateRole:
109+
{
110+
switch ( index.column() )
111+
{
112+
case Name:
113+
case Prefix:
114+
return false;
115+
116+
case Active:
117+
{
118+
filterForIndex( index )->setEnabled( value.toInt() == Qt::Checked );
119+
emit dataChanged( index, index, QVector<int>() << Qt::EditRole << Qt::CheckStateRole );
120+
return true;
121+
}
122+
123+
case Default:
124+
{
125+
filterForIndex( index )->setUseWithoutPrefix( value.toInt() == Qt::Checked );
126+
emit dataChanged( index, index, QVector<int>() << Qt::EditRole << Qt::CheckStateRole );
127+
return true;
128+
}
129+
}
130+
}
131+
}
132+
return false;
133+
}
134+
101135
Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
102136
{
103137
if ( !index.isValid() || index.row() < 0 || index.column() < 0 ||
@@ -121,23 +155,30 @@ Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
121155
return flags;
122156
}
123157

124-
QVariant QgsLocatorFiltersModel::zheaderData( int section, Qt::Orientation orientation, int role ) const
158+
QVariant QgsLocatorFiltersModel::headerData( int section, Qt::Orientation orientation, int role ) const
125159
{
126-
if ( orientation == Qt::Horizontal && role == Qt::SizeHintRole )
160+
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
127161
{
128-
QSize size = QAbstractTableModel::headerData( section, orientation, role ).toSize();
129162
switch ( section )
130163
{
131164
case Name:
132-
break;
165+
return tr( "Filter" );
166+
133167
case Prefix:
168+
return tr( "Prefix" );
169+
134170
case Active:
171+
return tr( "Enabled" );
172+
135173
case Default:
136-
size.setWidth( 100 );
137-
break;
174+
return tr( "Default" );
138175
}
139-
return size;
140176
}
141177

142-
return QAbstractTableModel::headerData( section, orientation, role );
178+
return QVariant();
179+
}
180+
181+
QgsLocatorFilter *QgsLocatorFiltersModel::filterForIndex( const QModelIndex &index ) const
182+
{
183+
return mLocator->filters().at( index.row() + HIDDEN_FILTER_OFFSET );
143184
}

‎src/app/locator/qgslocatoroptionswidget.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ class QgsLocatorFiltersModel : public QAbstractTableModel
7373
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
7474
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
7575
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
76+
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
7677
Qt::ItemFlags flags( const QModelIndex &index ) const override;
77-
QVariant zheaderData( int section, Qt::Orientation orientation,
78-
int role = Qt::DisplayRole ) const;
78+
QVariant headerData( int section, Qt::Orientation orientation,
79+
int role = Qt::DisplayRole ) const override;
7980
private:
8081

82+
QgsLocatorFilter *filterForIndex( const QModelIndex &index ) const;
83+
8184
QgsLocator *mLocator = nullptr;
8285
};
8386

‎src/gui/locator/qgslocator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
9999
if ( searchString.indexOf( ' ' ) > 0 )
100100
{
101101
QString prefix = searchString.left( searchString.indexOf( ' ' ) );
102-
if ( mPrefixedFilters.contains( prefix ) )
102+
if ( mPrefixedFilters.contains( prefix ) && mPrefixedFilters.value( prefix )->enabled() )
103103
{
104104
mActiveFilters << mPrefixedFilters.value( prefix );
105105
searchString = searchString.mid( prefix.length() + 1 );
@@ -109,7 +109,7 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
109109
{
110110
Q_FOREACH ( QgsLocatorFilter *filter, mFilters )
111111
{
112-
if ( filter->useWithoutPrefix() )
112+
if ( filter->useWithoutPrefix() && filter->enabled() )
113113
mActiveFilters << filter;
114114
}
115115
}

‎src/gui/locator/qgslocatorfilter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ bool QgsLocatorFilter::stringMatches( const QString &candidate, const QString &s
3030
return candidate.contains( search, Qt::CaseInsensitive );
3131
}
3232

33+
bool QgsLocatorFilter::enabled() const
34+
{
35+
return mEnabled;
36+
}
37+
38+
void QgsLocatorFilter::setEnabled( bool enabled )
39+
{
40+
mEnabled = enabled;
41+
}
42+
3343
bool QgsLocatorFilter::useWithoutPrefix() const
3444
{
3545
return mUseWithoutPrefix;

‎src/gui/locator/qgslocatorfilter.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ class GUI_EXPORT QgsLocatorFilter : public QObject
190190
*/
191191
static bool stringMatches( const QString &candidate, const QString &search );
192192

193+
/**
194+
* Returns true if the filter is enabled.
195+
* \see setEnabled()
196+
*/
197+
bool enabled() const;
198+
199+
/**
200+
* Sets whether the filter is \a enabled.
201+
* \see enabled()
202+
*/
203+
void setEnabled( bool enabled );
204+
193205
signals:
194206

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

201213
private:
202214

215+
bool mEnabled = true;
203216
bool mUseWithoutPrefix = true;
204217

205218
};

‎src/gui/locator/qgslocatorwidget.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ void QgsLocatorWidget::configMenuAboutToShow()
245245
QMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
246246
for ( ; fIt != filters.constEnd(); ++fIt )
247247
{
248+
if ( !fIt.value()->enabled() )
249+
continue;
250+
248251
QAction *action = new QAction( fIt.value()->displayName(), mMenu );
249252
connect( action, &QAction::triggered, this, [ = ]
250253
{
@@ -576,7 +579,7 @@ void QgsLocatorFilterFilter::fetchResults( const QString &string, const QgsLocat
576579
if ( feedback->isCanceled() )
577580
return;
578581

579-
if ( fIt.value() == this || !fIt.value() )
582+
if ( fIt.value() == this || !fIt.value() || !fIt.value()->enabled() )
580583
continue;
581584

582585
QgsLocatorResult result;

0 commit comments

Comments
 (0)
Please sign in to comment.