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> <attribute name="label">Show Info</attribute>
</item> </item>
<item> <item>
<attribute name="label">Change Background</attribute> <attribute name="label">Customization</attribute>
<attribute name="action">win.back</attribute> <attribute name="action">win.back</attribute>
</item> </item>
<item> <item>

View File

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

View File

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