diff --git a/scwx-qt/source/scwx/qt/model/layer_model.cpp b/scwx-qt/source/scwx/qt/model/layer_model.cpp index 6966baf0..a074dc86 100644 --- a/scwx-qt/source/scwx/qt/model/layer_model.cpp +++ b/scwx-qt/source/scwx/qt/model/layer_model.cpp @@ -2,8 +2,10 @@ #include #include #include +#include #include +#include #include #include @@ -14,6 +16,8 @@ #include #include #include +#include +#include namespace scwx { @@ -34,6 +38,11 @@ static constexpr std::size_t kMapCount_ = 4u; static const QString kMimeFormat {"application/x.scwx-layer-model"}; +static const std::string kTypeName_ {"type"}; +static const std::string kDescriptionName_ {"description"}; +static const std::string kMovableName_ {"movable"}; +static const std::string kDisplayedName_ {"displayed"}; + typedef std:: variant LayerDescription; @@ -76,9 +85,40 @@ public: ~Impl() = default; void AddPlacefile(const std::string& name); + void InitializeLayerSettings(); + void WriteLayerSettings(); + + friend void tag_invoke(boost::json::value_from_tag, + boost::json::value& jv, + const LayerInfo& record) + { + std::string description {}; + + if (std::holds_alternative(record.description_)) + { + description = awips::GetPhenomenonCode( + std::get(record.description_)); + } + else if (std::holds_alternative(record.description_)) + { + description = + types::GetLayerName(std::get(record.description_)); + } + else if (std::holds_alternative(record.description_)) + { + description = std::get(record.description_); + } + + jv = {{kTypeName_, types::GetLayerTypeName(record.type_)}, + {kDescriptionName_, description}, + {kMovableName_, record.movable_}, + {kDisplayedName_, boost::json::value_from(record.displayed_)}}; + } LayerModel* self_; + std::string layerSettingsPath_ {}; + std::shared_ptr placefileManager_ { manager::PlacefileManager::Instance()}; @@ -107,8 +147,41 @@ LayerModel::LayerModel(QObject* parent) : &manager::PlacefileManager::PlacefileUpdated, this, &LayerModel::HandlePlacefileUpdate); + + p->InitializeLayerSettings(); +} + +LayerModel::~LayerModel() +{ + // Write layer settings on shutdown + p->WriteLayerSettings(); +}; + +void LayerModel::Impl::InitializeLayerSettings() +{ + std::string appDataPath { + QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + .toStdString()}; + + if (!std::filesystem::exists(appDataPath)) + { + if (!std::filesystem::create_directories(appDataPath)) + { + logger_->error("Unable to create application data directory: \"{}\"", + appDataPath); + } + } + + layerSettingsPath_ = appDataPath + "/layers.json"; +} + +void LayerModel::Impl::WriteLayerSettings() +{ + logger_->info("Saving layer settings"); + + auto layerJson = boost::json::value_from(layers_); + util::json::WriteJsonFile(layerSettingsPath_, layerJson); } -LayerModel::~LayerModel() = default; int LayerModel::rowCount(const QModelIndex& parent) const {