Cleanup use of custom provider definitions, use common map_provider.hpp

This commit is contained in:
Dan Paulat 2023-04-30 14:47:30 -05:00
parent 6f40c75a33
commit c77c899040
3 changed files with 48 additions and 38 deletions

View file

@ -1,10 +1,12 @@
#include <scwx/qt/manager/settings_manager.hpp>
#include <scwx/qt/map/map_provider.hpp>
#include <scwx/qt/util/json.hpp>
#include <scwx/util/logger.hpp>
#include <filesystem>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <QDir>
#include <QStandardPaths>
@ -148,29 +150,31 @@ static void ValidateSettings()
auto& generalSettings = general_settings();
// Validate map provider
std::string mapProvider = generalSettings.map_provider().GetValue();
std::string mapProviderName = generalSettings.map_provider().GetValue();
std::string mapboxApiKey = generalSettings.mapbox_api_key().GetValue();
std::string maptilerApiKey = generalSettings.maptiler_api_key().GetValue();
if (mapProvider == "maptiler" && //
mapboxApiKey.size() > 1 && //
maptilerApiKey == "?")
{
logger_->info("Setting Map Provider to Mapbox based on API key settings");
map::MapProvider mapProvider = map::GetMapProvider(mapProviderName);
std::string mapApiKey = map::GetMapProviderApiKey(mapProvider);
generalSettings.map_provider().SetValue("mapbox");
settingsChanged = true;
}
else if (mapProvider == "mapbox" && //
maptilerApiKey.size() > 1 && //
mapboxApiKey == "?")
if (mapApiKey == "?")
{
for (map::MapProvider newProvider : map::MapProviderIterator())
{
if (mapProvider != newProvider &&
map::GetMapProviderApiKey(newProvider).size() > 1)
{
logger_->info(
"Setting Map Provider to MapTiler based on API key settings");
"Setting Map Provider to {} based on API key settings",
map::GetMapProviderName(newProvider));
generalSettings.map_provider().SetValue("maptiler");
std::string newProviderName {GetMapProviderName(newProvider)};
boost::to_lower(newProviderName);
generalSettings.map_provider().SetValue(newProviderName);
settingsChanged = true;
}
}
}
if (settingsChanged)
{

View file

@ -1,8 +1,11 @@
#include <scwx/qt/settings/general_settings.hpp>
#include <scwx/qt/settings/settings_container.hpp>
#include <scwx/qt/map/map_provider.hpp>
#include <array>
#include <boost/algorithm/string.hpp>
namespace scwx
{
namespace qt
@ -12,9 +15,6 @@ namespace settings
static const std::string logPrefix_ = "scwx::qt::settings::general_settings";
static const std::array<std::string, 2> kMapProviderValues_ {"mapbox",
"maptiler"};
class GeneralSettingsImpl
{
public:
@ -41,9 +41,15 @@ public:
mapProvider_.SetValidator(
[](const std::string& value)
{
return std::find(kMapProviderValues_.cbegin(),
kMapProviderValues_.cend(),
value) != kMapProviderValues_.cend();
return std::find_if(map::MapProviderIterator().begin(),
map::MapProviderIterator().end(),
[&value](map::MapProvider mapProvider)
{
std::string mapProviderName =
map::GetMapProviderName(mapProvider);
boost::to_lower(mapProviderName);
return value == mapProviderName;
}) != map::MapProviderIterator().end();
});
mapboxApiKey_.SetValidator([](const std::string& value)
{ return !value.empty(); });

View file

@ -5,6 +5,7 @@
#include <scwx/common/color_table.hpp>
#include <scwx/qt/config/radar_site.hpp>
#include <scwx/qt/manager/settings_manager.hpp>
#include <scwx/qt/map/map_provider.hpp>
#include <scwx/qt/settings/settings_interface.hpp>
#include <scwx/qt/ui/radar_site_dialog.hpp>
#include <scwx/qt/util/color.hpp>
@ -36,8 +37,6 @@ struct ColorTableConversions
float scale {1.0f};
};
static const std::array<std::string, 2> kMapProviders_ {"Mapbox", "MapTiler"};
static const std::array<std::pair<std::string, std::string>, 15>
kColorTableTypes_ {std::pair {"BR", "BR"},
std::pair {"BV", "BV"},
@ -320,29 +319,30 @@ void SettingsDialogImpl::SetupGeneralTab()
gridHeight_.SetEditWidget(self_->ui->gridHeightSpinBox);
gridHeight_.SetResetButton(self_->ui->resetGridHeightButton);
for (const auto& mapProvider : kMapProviders_)
for (const auto& mapProvider : map::MapProviderIterator())
{
self_->ui->mapProviderComboBox->addItem(
QString::fromStdString(mapProvider));
QString::fromStdString(map::GetMapProviderName(mapProvider)));
}
mapProvider_.SetSettingsVariable(generalSettings.map_provider());
mapProvider_.SetMapFromValueFunction(
[](const std::string& text) -> std::string
{
auto it = std::find_if(kMapProviders_.cbegin(),
kMapProviders_.cend(),
[&text](const std::string& mapProvider)
{ return boost::iequals(text, mapProvider); });
auto it = std::find_if(
map::MapProviderIterator().begin(),
map::MapProviderIterator().end(),
[&text](map::MapProvider mapProvider)
{ return boost::iequals(text, GetMapProviderName(mapProvider)); });
if (it == kMapProviders_.cend())
if (it != map::MapProviderIterator().end())
{
// Map provider label not found, return unknown
return "?";
// Return map provider label
return GetMapProviderName(*it);
}
// Return map provider label
return *it;
// Map provider label not found, return unknown
return "?";
});
mapProvider_.SetMapToValueFunction(
[](std::string text) -> std::string