Xe-Release/XeRelease_Gtkmm4/src/MyPrefs.cc

304 lines
8.1 KiB
C++
Raw Normal View History

2022-07-26 13:08:45 +08:00
#include "MyPrefs.hh"
2021-06-23 22:26:32 +08:00
2022-07-26 13:01:38 +08:00
MyPrefs::MyPrefs(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_builder)
: Gtk::Box(cobject),
2022-07-24 18:23:10 +08:00
ref_Glade(ref_builder)
2021-06-23 22:26:32 +08:00
{
2022-07-24 18:23:10 +08:00
// Get Widgets
2022-12-06 23:58:44 +08:00
entry_lts = ref_builder->get_widget<Gtk::Entry>("entry_lts");
entry_stable = ref_builder->get_widget<Gtk::Entry>("entry_stable");
entry_dev = ref_builder->get_widget<Gtk::Entry>("entry_dev");
entry_path = ref_builder->get_widget<Gtk::Entry>("entry_path");
btnpath = ref_builder->get_widget<Gtk::Button>("btnpath");
btnok = ref_builder->get_widget<Gtk::Button>("btn_ok");
btncancel = ref_builder->get_widget<Gtk::Button>("btn_cancel");
2022-07-24 18:23:10 +08:00
// Connect Signal
2022-07-26 13:01:38 +08:00
btnpath->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnpath_clicked));
btnok->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnok_clicked));
2022-07-26 13:34:28 +08:00
btncancel->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnreset_clicked));
2021-06-23 22:26:32 +08:00
}
2022-07-26 13:01:38 +08:00
void MyPrefs::btnok_clicked()
2022-07-24 18:23:10 +08:00
{
// Save Configs to a file
2022-07-26 13:01:38 +08:00
Glib::ustring config;
std::fstream outfile;
2022-07-24 18:23:10 +08:00
2022-07-26 13:01:38 +08:00
// Initalize Config for path
2022-12-18 22:05:12 +08:00
switch (get_os_type())
2022-07-26 13:01:38 +08:00
{
2022-12-18 22:05:12 +08:00
case OS_Type::Linux:
2022-07-26 13:01:38 +08:00
config_unix = entry_path->get_text();
2022-12-18 22:05:12 +08:00
break;
case OS_Type::Windows:
2022-07-26 13:01:38 +08:00
config_win32 = entry_path->get_text();
2022-12-18 22:05:12 +08:00
break;
case OS_Type::Darwin:
config_darwin = entry_path->get_text();
break;
2022-07-26 13:01:38 +08:00
}
2022-12-18 22:05:12 +08:00
// if (get_os_type() == OS_Type::Linux)
// {
// config_unix = entry_path->get_text();
// }
// else
// {
// config_win32 = entry_path->get_text();
// }
2022-07-26 13:01:38 +08:00
// Open the config file
outfile.open("xe_config.json", std::ios_base::out);
/*OutPut contents to the file
Simple Contents of xe_config:
2022-07-26 13:34:28 +08:00
{
"Longterm":"x.x",
"Stable":"x.x",
"Develop":"x.x",
2022-12-18 22:05:12 +08:00
"dark_mode":false,
2022-12-28 15:20:35 +08:00
"background":0
2022-07-26 13:34:28 +08:00
"Release_Path_Unix":"",
2022-12-18 22:05:12 +08:00
"Release_Path_Win32":"",
"Release_Path_Darwin":""
2022-07-26 13:34:28 +08:00
}
2022-07-26 13:01:38 +08:00
*/
if (outfile.is_open())
{
2022-07-26 13:34:28 +08:00
// BackUp the original data
data_backup = data;
2022-07-26 13:01:38 +08:00
// Create json object
json out_data = json::parse(R"(
2022-07-24 18:23:10 +08:00
{
"Longterm":"",
"Stable":"",
"Develop":"",
"Release_Path_Unix":"",
2022-12-18 22:05:12 +08:00
"Release_Path_Win32":"",
"Release_Path_Darwin":"",
2022-12-28 15:20:35 +08:00
"dark_mode":false,
"background":2
2022-07-24 18:23:10 +08:00
}
)");
2022-07-26 13:01:38 +08:00
// Load config to json file
out_data["Longterm"] = entry_lts->get_text();
out_data["Stable"] = entry_stable->get_text();
out_data["Develop"] = entry_dev->get_text();
out_data["Release_Path_Unix"] = config_unix;
out_data["Release_Path_Win32"] = config_win32;
2022-12-18 22:05:12 +08:00
out_data["Release_Path_Darwin"] = config_darwin;
out_data["dark_mode"] = dark_mode;
2022-12-28 15:20:35 +08:00
out_data["background"] = background_id;
2022-07-26 13:01:38 +08:00
outfile << out_data;
2022-07-26 13:34:28 +08:00
// Set Current json data
data = out_data;
// Show Dialog
msg_dialog1.Init("Config File Saved!");
2023-04-09 22:41:48 +08:00
msg_dialog1.present();
2021-06-23 22:26:32 +08:00
}
2022-07-26 13:01:38 +08:00
outfile.close();
2021-06-23 22:26:32 +08:00
}
2021-06-23 21:03:44 +08:00
2022-07-26 13:34:28 +08:00
void MyPrefs::btnreset_clicked()
{
// Restore the backup data
data = data_backup;
// Reset content of entries
reset_entries();
// Show Dialog
msg_dialog1.Init("Config Reseted!\n Press \"OK\" to save.");
2023-04-09 22:41:48 +08:00
msg_dialog1.present();
2022-07-26 13:34:28 +08:00
}
2022-07-26 13:01:38 +08:00
void MyPrefs::init_json_data(json &data1)
2022-07-26 11:00:41 +08:00
{
// Read Configs
// Open json file
if (!data1.empty())
{
2022-07-26 13:34:28 +08:00
data = data1;
// Set the content of entry
reset_entries();
}
}
void MyPrefs::reset_entries()
{
std::string config_longterm, config_stable, config_devel;
// Read json data
config_longterm = data["Longterm"];
config_stable = data["Stable"];
config_devel = data["Develop"];
config_unix = data["Release_Path_Unix"];
config_win32 = data["Release_Path_Win32"];
2022-12-18 22:05:12 +08:00
config_darwin = data["Release_Path_Darwin"];
dark_mode = data["dark_mode"];
2022-12-28 15:20:35 +08:00
background_id = data["background"];
2022-07-26 13:34:28 +08:00
// Set text from json file data
entry_lts->set_text(config_longterm);
entry_stable->set_text(config_stable);
entry_dev->set_text(config_devel);
// Use different path for Linux filesystem and Windows
2022-12-18 22:05:12 +08:00
switch (get_os_type())
2022-07-26 13:34:28 +08:00
{
2022-12-18 22:05:12 +08:00
case OS_Type::Linux:
2022-07-26 13:34:28 +08:00
entry_path->set_text(config_unix);
2022-12-18 22:05:12 +08:00
break;
case OS_Type::Windows:
2022-07-26 13:34:28 +08:00
entry_path->set_text(config_win32);
2022-12-18 22:05:12 +08:00
break;
case OS_Type::Darwin:
entry_path->set_text(config_darwin);
break;
2022-07-26 11:00:41 +08:00
}
2022-12-18 22:05:12 +08:00
// if (get_os_type() == OS_Type::Linux)
// {
// entry_path->set_text(config_unix);
// }
// else
// {
// entry_path->set_text(config_win32);
// }
2022-07-26 11:00:41 +08:00
}
2022-07-26 13:01:38 +08:00
MyPrefs *MyPrefs::create()
2022-07-24 18:23:10 +08:00
{
2022-07-26 13:01:38 +08:00
// Create the config widget
2022-07-24 18:23:10 +08:00
auto builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/prefs.ui");
2022-07-26 13:01:38 +08:00
MyPrefs *box = nullptr;
2022-12-06 23:58:44 +08:00
box = Gtk::Builder::get_widget_derived<MyPrefs>(builder, "prefs");
2022-07-26 13:01:38 +08:00
return box;
}
2021-09-20 10:46:08 +08:00
2022-07-26 13:34:28 +08:00
void MyPrefs::set_parent_win(Gtk::Window *parent)
{
2022-07-26 13:01:38 +08:00
parent_win = parent;
2022-07-26 13:34:28 +08:00
msg_dialog1.set_transient_for(*parent);
2021-09-20 10:46:08 +08:00
}
2023-04-13 12:35:15 +08:00
void MyPrefs::set_dark_mode(bool dark_mode_enabled)
{
2022-12-19 08:04:27 +08:00
// Put the config of dark mode to the class
dark_mode = dark_mode_enabled;
}
2023-04-13 12:35:15 +08:00
void MyPrefs::save_config_now()
{
2022-12-19 08:04:27 +08:00
// Save config when the dark mode config is modified
btnok_clicked();
}
2022-07-26 13:01:38 +08:00
void MyPrefs::btnpath_clicked()
2022-07-24 18:23:10 +08:00
{
// Create a Dialog
2022-07-26 13:01:38 +08:00
dialog = Gtk::FileChooserNative::create("Select a folder", *parent_win,
2022-12-06 23:58:44 +08:00
Gtk::FileChooser::Action::SELECT_FOLDER, "OK", "Cancel");
2022-07-24 18:23:10 +08:00
2022-07-26 13:01:38 +08:00
dialog->signal_response().connect(sigc::mem_fun(*this, &MyPrefs::dialog_response));
2021-12-31 22:49:10 +08:00
dialog->show();
}
2022-07-26 13:01:38 +08:00
void MyPrefs::dialog_response(int response_id)
2022-07-24 18:23:10 +08:00
{
2022-12-06 23:58:44 +08:00
if (response_id == Gtk::ResponseType::ACCEPT)
2022-07-24 18:23:10 +08:00
{
2022-12-06 23:58:44 +08:00
auto file = dialog->get_file();
Glib::ustring path = file->get_path();
2021-12-31 22:49:10 +08:00
entry_path->set_text(path);
2022-12-06 23:58:44 +08:00
file.reset();
2021-12-31 22:49:10 +08:00
}
dialog.reset();
}
2021-06-23 21:03:44 +08:00
MsgBox::MsgBox(Gtk::Window &parent)
2023-04-13 12:35:15 +08:00
: vbox(Gtk::Orientation::VERTICAL, 5),
hbox(Gtk::Orientation::HORIZONTAL, 5),
btn_box(Gtk::Orientation::HORIZONTAL, 5),
btn_ok("OK")
2021-06-23 21:03:44 +08:00
{
2022-07-24 18:23:10 +08:00
// Initalize MsgBox
2021-06-23 21:03:44 +08:00
set_icon_name("Xe-Release");
2022-07-24 18:23:10 +08:00
set_default_size(300, 150);
2023-04-13 12:35:15 +08:00
// add_button("OK", Gtk::ResponseType::OK);
2021-06-23 21:03:44 +08:00
set_transient_for(parent);
2023-04-13 12:35:15 +08:00
// Add Message and icon
2022-12-06 23:58:44 +08:00
image.set_from_icon_name("Xe-Release");
2022-12-12 15:47:43 +08:00
image.set_icon_size(Gtk::IconSize::LARGE);
image.set_size_request(64, 64);
2023-04-13 12:35:15 +08:00
// vbox = get_content_area();
2022-12-06 23:58:44 +08:00
hbox.append(image);
hbox.append(msg_label);
2023-04-13 12:35:15 +08:00
// Add message box to the main box
2022-12-06 23:58:44 +08:00
hbox.set_expand();
hbox.set_halign(Gtk::Align::FILL);
hbox.set_valign(Gtk::Align::FILL);
2023-04-13 12:35:15 +08:00
vbox.append(hbox);
vbox.set_margin(5);
// Add button
btn_box.append(btn_ok);
btn_box.set_halign(Gtk::Align::END);
btn_ok.set_halign(Gtk::Align::END);
btn_ok.signal_clicked().connect(sigc::mem_fun(*this, &MsgBox::on_response));
vbox.append(btn_box);
set_child(vbox);
2021-06-23 21:03:44 +08:00
}
2022-07-26 13:34:28 +08:00
MsgBox::MsgBox()
2023-04-13 12:35:15 +08:00
: vbox(Gtk::Orientation::VERTICAL, 5),
hbox(Gtk::Orientation::HORIZONTAL, 5),
btn_box(Gtk::Orientation::HORIZONTAL, 5),
btn_ok("OK")
2022-07-26 13:34:28 +08:00
{
2023-04-13 12:35:15 +08:00
// Initalize MsgBox
2022-07-26 13:34:28 +08:00
set_icon_name("Xe-Release");
set_default_size(300, 150);
2023-04-13 12:35:15 +08:00
// add_button("OK", Gtk::ResponseType::OK);
// set_transient_for(parent);
// Add Message and icon
2022-12-06 23:58:44 +08:00
image.set_from_icon_name("Xe-Release");
2022-12-12 15:47:43 +08:00
image.set_icon_size(Gtk::IconSize::LARGE);
image.set_size_request(64, 64);
2023-04-13 12:35:15 +08:00
// vbox = get_content_area();
2022-12-06 23:58:44 +08:00
hbox.append(image);
hbox.append(msg_label);
2023-04-13 12:35:15 +08:00
// Add message box to the main box
2022-12-06 23:58:44 +08:00
hbox.set_expand();
hbox.set_halign(Gtk::Align::FILL);
hbox.set_valign(Gtk::Align::FILL);
2023-04-13 12:35:15 +08:00
vbox.append(hbox);
vbox.set_margin(5);
// Add button
btn_box.append(btn_ok);
btn_box.set_halign(Gtk::Align::END);
btn_ok.set_halign(Gtk::Align::END);
btn_ok.signal_clicked().connect(sigc::mem_fun(*this, &MsgBox::on_response));
vbox.append(btn_box);
set_child(vbox);
2022-07-26 13:34:28 +08:00
}
2022-07-24 18:23:10 +08:00
void MsgBox::Init(Glib::ustring msg)
{
2021-06-23 21:03:44 +08:00
msg_label.set_label(msg);
}
2023-04-13 12:35:15 +08:00
void MsgBox::on_response()
2022-07-24 18:23:10 +08:00
{
2021-06-23 21:03:44 +08:00
hide();
}