Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nominatim Geocoder Locator filter - add countryCodes configuration option #60719

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
44 changes: 44 additions & 0 deletions src/app/locator/qgsnominatimlocatorfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#include "qgsnominatimlocatorfilter.h"
#include "moc_qgsnominatimlocatorfilter.cpp"
#include "qgsgeocoder.h"
#include "qgslocatorfilter.h"
#include "qgsnominatimgeocoder.h"
#include "qgssettings.h"
#include "qgsmessagebaritem.h"
#include "qgsmessagebar.h"
Expand All @@ -33,6 +36,17 @@ QgsNominatimLocatorFilter::QgsNominatimLocatorFilter( QgsGeocoderInterface *geoc
setUseWithoutPrefix( false );
}

void QgsNominatimLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback )
{
QgsSettings settings;
QString countryCodes = settings.value( "locator_filters/nominatim_geocoder/country_codes", "", QgsSettings::App ).toString().trimmed();

QgsNominatimGeocoder *nominatimGeocoder = dynamic_cast<QgsNominatimGeocoder *>( geocoder() );
nominatimGeocoder->setCountryCodes( countryCodes );

QgsAbstractGeocoderLocatorFilter::fetchResults( string, context, feedback );
}

void QgsNominatimLocatorFilter::triggerResult( const QgsLocatorResult &result )
{
QgsSettings settings;
Expand All @@ -50,3 +64,33 @@ void QgsNominatimLocatorFilter::triggerResult( const QgsLocatorResult &result )
}
QgsGeocoderLocatorFilter::triggerResult( result );
}

void QgsNominatimLocatorFilter::openConfigWidget( QWidget *parent )
{
auto dlg = std::make_unique<QDialog>( parent );
dlg->setWindowTitle( "Nominatim Geocoder Country Codes" );

QGridLayout *layout = new QGridLayout;
layout->setSizeConstraint( QLayout::SetFixedSize );
QLabel *label = new QLabel( tr( "Two letter Country Codes (comma-separated)" ) );
QLineEdit *countryCodesEdit = new QLineEdit( dlg.get() );

// Load existing settings
QgsSettings settings;
countryCodesEdit->setText( settings.value( "locator_filters/nominatim_geocoder/country_codes", "", QgsSettings::App ).toString() );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use new settings tree API here and in other places?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know, I used the code from other Locator filters as example. Like this


I can check this, but not sure what will be better. Is approach I used deprecated?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not deprecated, but IMHO for new code we should use new API. Also with this API new setting will be nicely exposed in the advanced settings editor.


layout->addWidget( label );
layout->addWidget( countryCodesEdit );
QDialogButtonBox *buttonbBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, dlg.get() );
layout->addWidget( buttonbBox );
dlg->setLayout( layout );

// Save settings when dialog accepted
connect( buttonbBox, &QDialogButtonBox::accepted, dlg.get(), [&]() {
settings.setValue( "locator_filters/nominatim_geocoder/country_codes", countryCodesEdit->text().trimmed(), QgsSettings::App );
dlg->accept();
} );

connect( buttonbBox, &QDialogButtonBox::rejected, dlg.get(), &QDialog::reject );
dlg->exec();
}
3 changes: 3 additions & 0 deletions src/app/locator/qgsnominatimlocatorfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class APP_EXPORT QgsNominatimLocatorFilter : public QgsGeocoderLocatorFilter
public:
QgsNominatimLocatorFilter( QgsGeocoderInterface *geocoder, QgsMapCanvas *canvas );

void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
void triggerResult( const QgsLocatorResult &result ) override;
bool hasConfigWidget() const override { return true; }
void openConfigWidget( QWidget *parent ) override;
};

#endif // QGSNOMINATIMLOCATORFILTERS_H