Change config file to json

This commit is contained in:
daleclack 2022-09-24 23:45:43 +08:00
parent 88af58a207
commit c0734c96bd
3 changed files with 31 additions and 19 deletions

View File

@ -11,7 +11,7 @@
<attribute name="label">Show Info</attribute>
</item>
<item>
<attribute name="label">Change Background</attribute>
<attribute name="label">Customization</attribute>
<attribute name="action">win.back</attribute>
</item>
<item>

View File

@ -416,12 +416,19 @@ void MyPrefs::btnapply_clicked()
}
// Open the file for configs
json data = json::parse(R"(
{
"height":1280,
"width":720
}
)");
std::fstream outfile;
outfile.open("config", std::ios_base::out);
outfile.open("config.json", std::ios_base::out);
if (outfile.is_open())
{
outfile << "width=" << width << std::endl;
outfile << "height=" << height << std::endl;
data["width"] = width;
data["height"] = height;
outfile<<data;
outfile.close();
}
}
@ -436,12 +443,13 @@ void MyPrefs::btnGet_clicked()
}
void MyPrefs::load_winsize_config(){
std::string height_str, width_str;
// Read values from a file
if (readCfgFile("config", "width", width_str) && readCfgFile("config", "height", height_str))
{
height = atoi(height_str.c_str());
width = atoi(width_str.c_str());
std::ifstream jsonfile("config.json");
if(jsonfile.is_open()){
json data = json::parse(jsonfile);
height = data["height"];
width = data["width"];
}else{
height = 720;
width = 1280;
}
}

View File

@ -2,7 +2,10 @@
#include <gtkmm.h>
#include <string>
#include "cfgfile/cfgfile.hh"
#include <fstream>
#include "../json_nlohmann/json.hpp"
using json = nlohmann::json;
class MyPrefs : public Gtk::Window
{
@ -81,12 +84,13 @@ private:
//Read Config from file without use the MyPrefs class
static inline void get_size_config(int &width, int &height){
std::string height_str, width_str;
// Read values from a file
if (readCfgFile("config", "width", width_str) && readCfgFile("config", "height", height_str))
{
height = atoi(height_str.c_str());
width = atoi(width_str.c_str());
std::ifstream jsonfile("config.json");
if(jsonfile.is_open()){
json data = json::parse(jsonfile);
height = data["height"];
width = data["width"];
}else{
height = 720;
width = 1280;
}
}