AntiClipSettings/Settings.cpp

71 lines
2.4 KiB
C++
Raw Permalink Normal View History

2024-09-11 14:18:56 +08:00
#include "Settings.h"
#include "BoostLog.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <filesystem>
constexpr auto SettingsFilePath = "settings.xml";
Settings::Settings() {
if (!std::filesystem::exists(SettingsFilePath)) {
save();
}
}
void Settings::save() {
boost::property_tree::ptree ptree;
ptree.put("Application.DataCollection.ImageFormat", static_cast<int>(m_imageFormat));
std::ostringstream oss;
for (int i = 0; i < static_cast<int>(ImageFormat::None); i++) {
oss << i << ":" << static_cast<ImageFormat>(i) << " ";
}
ptree.put("Application.DataCollection.ImageFormat.<xmlcomment>", oss.str());
ptree.put("Application.DataCollection.ImageQuality", m_imageQuality);
ptree.put("Application.DataCollection.ImageQuality.<xmlcomment>", "0-100,仅对jpg有效");
for (auto &device : m_supportedDevices) {
ptree.add("Application.SupportedDevices.Model", device);
}
2024-09-11 14:18:56 +08:00
boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
boost::property_tree::write_xml(SettingsFilePath, ptree, std::locale(), settings);
}
void Settings::load() {
boost::property_tree::ptree ptree;
m_supportedDevices.clear();
2024-09-11 14:18:56 +08:00
try {
boost::property_tree::read_xml(SettingsFilePath, ptree);
m_imageFormat = static_cast<ImageFormat>(ptree.get<int>("Application.DataCollection.ImageFormat"));
m_imageQuality = ptree.get<int>("Application.DataCollection.ImageQuality");
for (auto &child : ptree.get_child("Application.SupportedDevices")) {
if (child.first != "Model") continue;
m_supportedDevices.push_back(child.second.get<std::string>(""));
}
2024-09-11 14:18:56 +08:00
} catch (...) {
LOG(error) << "parse " << SettingsFilePath << " failed.";
}
std::ostringstream oss;
oss << "[";
for (auto &device : m_supportedDevices) {
oss << device << ", ";
}
oss << "]";
LOG(info) << "image format: " << m_imageFormat << ", quality: " << m_imageQuality
<< ", supported devices: " << oss.str();
2024-09-11 14:18:56 +08:00
}
ImageFormat Settings::imageFormat() const {
return m_imageFormat;
}
int Settings::imageQuality() const {
return m_imageQuality;
}
std::list<std::string> Settings::supportedDevices() const {
return m_supportedDevices;
}