Skip to content

Commit

Permalink
Make locator settings persistent
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 17, 2017
1 parent e8d3ae9 commit 7635b44
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
51 changes: 43 additions & 8 deletions src/app/locator/qgslocatoroptionswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* *
***************************************************************************/


#include "qgslocatoroptionswidget.h"
#include "qgssettings.h"

QgsLocatorOptionsWidget::QgsLocatorOptionsWidget( QgsLocator *locator, QWidget *parent )
: QWidget( parent )
Expand All @@ -31,6 +31,11 @@ QgsLocatorOptionsWidget::QgsLocatorOptionsWidget( QgsLocator *locator, QWidget *
mFiltersTreeView->header()->setResizeMode( 0, QHeaderView::Stretch );
}

void QgsLocatorOptionsWidget::commitChanges()
{
mModel->commitChanges();
}


//
// QgsLocatorFiltersModel
Expand Down Expand Up @@ -87,10 +92,20 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
return QVariant();

case Active:
return filterForIndex( index )->enabled() ? Qt::Checked : Qt::Unchecked;
{
QgsLocatorFilter *filter = filterForIndex( index );
if ( mEnabledChanges.contains( filter ) )
return mEnabledChanges.value( filter ) ? Qt::Checked : Qt::Unchecked;
else
return filterForIndex( index )->enabled() ? Qt::Checked : Qt::Unchecked;
}

case Default:
return filterForIndex( index )->useWithoutPrefix() ? Qt::Checked : Qt::Unchecked;
QgsLocatorFilter *filter = filterForIndex( index );
if ( mDefaultChanges.contains( filter ) )
return mDefaultChanges.value( filter ) ? Qt::Checked : Qt::Unchecked;
else
return filterForIndex( index )->useWithoutPrefix() ? Qt::Checked : Qt::Unchecked;
}
}

Expand All @@ -107,6 +122,7 @@ bool QgsLocatorFiltersModel::setData( const QModelIndex &index, const QVariant &
{
case Qt::CheckStateRole:
{
bool checked = static_cast< Qt::CheckState >( value.toInt() ) == Qt::Checked;
switch ( index.column() )
{
case Name:
Expand All @@ -115,15 +131,15 @@ bool QgsLocatorFiltersModel::setData( const QModelIndex &index, const QVariant &

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

case Default:
{
filterForIndex( index )->setUseWithoutPrefix( value.toInt() == Qt::Checked );
emit dataChanged( index, index, QVector<int>() << Qt::EditRole << Qt::CheckStateRole );
mDefaultChanges.insert( filterForIndex( index ), checked );
emit dataChanged( index, index );
return true;
}
}
Expand All @@ -148,7 +164,6 @@ Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
case Active:
case Default:
flags = flags | Qt::ItemIsUserCheckable;
flags = flags | Qt::ItemIsEditable;
break;
}

Expand Down Expand Up @@ -178,6 +193,26 @@ QVariant QgsLocatorFiltersModel::headerData( int section, Qt::Orientation orient
return QVariant();
}

void QgsLocatorFiltersModel::commitChanges()
{
QgsSettings settings;

QHash< QgsLocatorFilter *, bool >::const_iterator it = mEnabledChanges.constBegin();
for ( ; it != mEnabledChanges.constEnd(); ++it )
{
QgsLocatorFilter *filter = it.key();
settings.setValue( QStringLiteral( "locator_filters/enabled_%1" ).arg( filter->name() ), it.value(), QgsSettings::Section::Gui );
filter->setEnabled( it.value() );
}
it = mDefaultChanges.constBegin();
for ( ; it != mDefaultChanges.constEnd(); ++it )
{
QgsLocatorFilter *filter = it.key();
settings.setValue( QStringLiteral( "locator_filters/default_%1" ).arg( filter->name() ), it.value(), QgsSettings::Section::Gui );
filter->setUseWithoutPrefix( it.value() );
}
}

QgsLocatorFilter *QgsLocatorFiltersModel::filterForIndex( const QModelIndex &index ) const
{
return mLocator->filters().at( index.row() + HIDDEN_FILTER_OFFSET );
Expand Down
14 changes: 14 additions & 0 deletions src/app/locator/qgslocatoroptionswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class QgsLocatorOptionsWidget : public QWidget, private Ui::QgsLocatorOptionsWid

QgsLocatorOptionsWidget( QgsLocator *locator, QWidget *parent = nullptr );

public slots:

void commitChanges();

private:

QgsLocator *mLocator = nullptr;
Expand Down Expand Up @@ -77,11 +81,21 @@ class QgsLocatorFiltersModel : public QAbstractTableModel
Qt::ItemFlags flags( const QModelIndex &index ) const override;
QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const override;

public slots:

void commitChanges();

private:

QgsLocatorFilter *filterForIndex( const QModelIndex &index ) const;

QgsLocator *mLocator = nullptr;

// changes are defered to support cancelation
QHash< QgsLocatorFilter *, bool > mEnabledChanges;
QHash< QgsLocatorFilter *, bool > mDefaultChanges;

};

#endif // QGSLOCATOROPTIONSWIDGET_H
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,8 @@ void QgsOptions::saveOptions()

saveDefaultDatumTransformations();

mLocatorOptionsWidget->commitChanges();

Q_FOREACH ( QgsOptionsPageWidget *widget, mAdditionalOptionWidgets )
{
widget->apply();
Expand Down
9 changes: 9 additions & 0 deletions src/gui/locator/qgslocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgslocator.h"
#include "qgssettings.h"
#include <QtConcurrent>
#include <functional>

Expand Down Expand Up @@ -72,6 +73,14 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter )
mPrefixedFilters.insert( filter->prefix(), filter );
}
}

// restore settings
QgsSettings settings;
bool enabled = settings.value( QStringLiteral( "locator_filters/enabled_%1" ).arg( filter->name() ), true, QgsSettings::Section::Gui ).toBool();
bool byDefault = settings.value( QStringLiteral( "locator_filters/default_%1" ).arg( filter->name() ), filter->useWithoutPrefix(), QgsSettings::Section::Gui ).toBool();

filter->setEnabled( enabled );
filter->setUseWithoutPrefix( byDefault );
}

void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback )
Expand Down

0 comments on commit 7635b44

Please sign in to comment.