Rewrite sync between dropdown and list

This commit is contained in:
daleclack 2024-01-18 18:26:51 +08:00
parent 7f1206ddd4
commit df219869fb
4 changed files with 155 additions and 107 deletions

View File

@ -14,6 +14,8 @@ MyPrefs::MyPrefs(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_
btncancel = ref_builder->get_widget<Gtk::Button>("btn_cancel");
// Connect Signal
btnadd->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnadd_clicked));
btnremove->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnremove_clicked));
btnpath->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnpath_clicked));
btnok->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnok_clicked));
btncancel->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnreset_clicked));
@ -22,10 +24,28 @@ MyPrefs::MyPrefs(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_
ver_list = Gio::ListStore<ModelColumns>::create();
selection = Gtk::NoSelection::create(ver_list);
// List content for test
ver_list->append(ModelColumns::create("Longterm", "5.15"));
ver_list->append(ModelColumns::create("Stable", "9.1"));
ver_list->append(ModelColumns::create("Develop", "-1"));
version_view.set_model(selection);
// Add Column View
version_sw->set_child(version_view);
// Add branch column
branch_factory = Gtk::SignalListItemFactory::create();
branch_factory->signal_bind().connect(sigc::mem_fun(*this, &MyPrefs::bind_branch));
branch_factory->signal_setup().connect(sigc::mem_fun(*this, &MyPrefs::setup_branch));
branch_column = Gtk::ColumnViewColumn::create("Branch", branch_factory);
version_view.append_column(branch_column);
// Add version column
version_factory = Gtk::SignalListItemFactory::create();
version_factory->signal_bind().connect(sigc::mem_fun(*this, &MyPrefs::bind_version));
version_factory->signal_setup().connect(sigc::mem_fun(*this, &MyPrefs::setup_version));
version_column = Gtk::ColumnViewColumn::create("Version", version_factory);
version_view.append_column(version_column);
}
void MyPrefs::btnok_clicked()
@ -47,15 +67,6 @@ void MyPrefs::btnok_clicked()
config_darwin = entry_path->get_text();
break;
}
// if (get_os_type() == OS_Type::Linux)
// {
// config_unix = entry_path->get_text();
// }
// else
// {
// config_win32 = entry_path->get_text();
// }
// Open the config file
outfile.open("xe_config.json", std::ios_base::out);
/*OutPut contents to the file
@ -121,18 +132,6 @@ void MyPrefs::btnreset_clicked()
msg_dialog1.present();
}
void MyPrefs::init_json_data(json &data1)
{
// Read Configs
// Open json file
if (!data1.empty())
{
data = data1;
// Set the content of entry
reset_entries();
}
}
void MyPrefs::reset_entries()
{
str_vec branchs_vec, versions_vec;
@ -158,14 +157,6 @@ void MyPrefs::reset_entries()
entry_path->set_text(config_darwin);
break;
}
// if (get_os_type() == OS_Type::Linux)
// {
// entry_path->set_text(config_unix);
// }
// else
// {
// entry_path->set_text(config_win32);
// }
}
MyPrefs *MyPrefs::create()
@ -185,18 +176,6 @@ void MyPrefs::set_parent_win(Gtk::Window *parent)
msg_dialog1.set_transient_for(*parent);
}
void MyPrefs::set_dark_mode(bool dark_mode_enabled)
{
// Put the config of dark mode to the class
dark_mode = dark_mode_enabled;
}
void MyPrefs::save_config_now()
{
// Save config when the dark mode config is modified
btnok_clicked();
}
void MyPrefs::btnpath_clicked()
{
// Create a Dialog
@ -269,3 +248,34 @@ void MyPrefs::bind_version(const Glib::RefPtr<Gtk::ListItem> &item)
Glib::Binding::bind_property(item1->property_version(), entry->property_text(),
Glib::Binding::Flags::BIDIRECTIONAL);
}
void MyPrefs::btnadd_clicked()
{
// Append a item to the list
ver_list->append(ModelColumns::create("<empty>", "<empty>"));
}
void MyPrefs::btnremove_clicked()
{
// Get Position of item
auto pos = ver_list->get_n_items() - 1;
// Remove item
ver_list->remove(pos);
}
int MyPrefs::get_background_id()
{
// The id for background
return background_id;
}
MyListStore MyPrefs::get_model()
{
// The store for the dropdown and list
return ver_list;
}
void MyPrefs::save_config_now()
{
}

View File

@ -9,6 +9,7 @@
using json = nlohmann::json;
typedef std::vector<std::string> str_vec;
typedef Glib::RefPtr<Gio::ListStore<ModelColumns>> MyListStore;
class MyPrefs : public Gtk::Box
{
@ -16,9 +17,9 @@ public:
MyPrefs(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_builder);
static MyPrefs *create();
void set_parent_win(Gtk::Window *parent);
void init_json_data(json &data1);
void save_config_now();
void set_dark_mode(bool dark_mode_enabled);
int get_background_id();
MyListStore get_model();
int background_id = 3;
private:
@ -35,7 +36,7 @@ private:
bool dark_mode;
// The Column View for versions
Glib::RefPtr<Gio::ListStore<ModelColumns>> ver_list;
MyListStore ver_list;
Glib::RefPtr<Gtk::NoSelection> selection;
// Factory to renderer branch string
@ -59,6 +60,8 @@ private:
// Signal Handlers
Glib::RefPtr<Gtk::FileChooserNative> dialog;
void btnadd_clicked();
void btnremove_clicked();
void btnpath_clicked();
void dialog_response(int response_id);
void btnok_clicked();

View File

@ -41,12 +41,20 @@ MyWin::MyWin()
snprintf(api_version, 57, "Xe Api Version:%d", xeapi1(local));
api_label.set_label(api_version);
// Get perferences
prefs = MyPrefs::create();
back_id = prefs->get_background_id();
// Initalize combobox
combo_list = Gtk::StringList::create();
combo_list->append("Longterm");
combo_list->append("Stable");
combo_list->append("Development");
drop_down.set_model(combo_list);
drop_list = prefs->get_model();
drop_down.set_model(drop_list);
// Add factory for dropdown
drop_factory = Gtk::SignalListItemFactory::create();
drop_factory->signal_bind().connect(sigc::mem_fun(*this, &MyWin::bind_drop));
drop_factory->signal_setup().connect(sigc::mem_fun(*this, &MyWin::setup_drop));
drop_down.set_factory(drop_factory);
drop_down.set_list_factory(drop_factory);
drop_down.set_selected(1);
// Add Main Controls
@ -64,10 +72,8 @@ MyWin::MyWin()
stack1.add(overlay, "main_page", "WelCome");
// Add Config Page
prefs = MyPrefs::create();
prefs->set_parent_win(this);
load_config();
prefs->init_json_data(data);
// load_config();
cfg_box.append(*prefs);
// cfg_box.set_hexpand();
cfg_box.set_halign(Gtk::Align::CENTER);
@ -241,36 +247,36 @@ void MyWin::background3()
void MyWin::config_dialog()
{
load_config();
prefs->init_json_data(data);
// load_config();
// prefs->init_json_data(data);
stack1.set_visible_child(cfg_box);
}
void MyWin::load_config()
{
// Load/Reload json config file
// void MyWin::load_config()
// {
// // Load/Reload json config file
// Open json file
std::ifstream json_file("xe_config.json");
if (json_file.is_open())
{
// Read data from json file
data = json::parse(json_file);
config_longterm = data["Longterm"];
config_stable = data["Stable"];
config_devel = data["Develop"];
back_id = data["background"];
}
else
{
msg_dialog.Init("The config doesn't exist!\nPlease use \"Config\" menu to set releases");
msg_dialog.present();
back_id = 3;
return;
}
json_config_init(data);
json_file.close();
}
// // Open json file
// std::ifstream json_file("xe_config.json");
// if (json_file.is_open())
// {
// // Read data from json file
// data = json::parse(json_file);
// config_longterm = data["Longterm"];
// config_stable = data["Stable"];
// config_devel = data["Develop"];
// back_id = data["background"];
// }
// else
// {
// msg_dialog.Init("The config doesn't exist!\nPlease use \"Config\" menu to set releases");
// msg_dialog.present();
// back_id = 3;
// return;
// }
// json_config_init(data);
// json_file.close();
// }
void MyWin::on_window_hide(Gtk::Window *window)
{
@ -282,32 +288,32 @@ void MyWin::main_releases()
// Get Selection
int version = drop_down.get_selected();
char str[57];
// Get Version string
auto item = drop_list->get_item(version);
auto message = item->get_version_str();
msg_dialog.Init(message);
msg_dialog.present();
// Get Configs
load_config();
switch (version) // Use Selection to Perform
{
case Releases::LTS:
msg_dialog.Init("The longterm build is diasbled!");
msg_dialog.present();
longterm(local, config_longterm.c_str(), str);
msg_dialog.Init(str);
msg_dialog.present();
break;
case Releases::Stable:
msg_dialog.Init("The stable build is diasbled!");
msg_dialog.present();
stable(local, config_stable.c_str(), str);
msg_dialog.Init(str);
msg_dialog.present();
break;
case Releases::Dev:
msg_dialog.Init("The development build is diasbled!");
msg_dialog.present();
develop(local, config_devel.c_str(), str);
msg_dialog.Init(str);
msg_dialog.present();
break;
}
// load_config();
// switch (version) // Use Selection to Perform
// {
// case Releases::LTS:
// longterm(local, config_longterm.c_str(), str);
// msg_dialog.Init(str);
// msg_dialog.present();
// break;
// case Releases::Stable:
// stable(local, config_stable.c_str(), str);
// msg_dialog.Init(str);
// msg_dialog.present();
// break;
// case Releases::Dev:
// develop(local, config_devel.c_str(), str);
// msg_dialog.Init(str);
// msg_dialog.present();
// break;
// }
}
void MyWin::check_toggled()
@ -316,11 +322,34 @@ void MyWin::check_toggled()
prefs->save_config_now();
}
void MyWin::setup_drop(const Glib::RefPtr<Gtk::ListItem> &item)
{
// Set label for item
item->set_child(*Gtk::make_managed<Gtk::Label>());
}
void MyWin::bind_drop(const Glib::RefPtr<Gtk::ListItem> &item)
{
// Get position
auto pos = item->get_position();
// Get label widget
auto label = dynamic_cast<Gtk::Label *>(item->get_child());
if (!label)
{
return;
}
// Set text to the label
auto item1 = drop_list->get_item(pos);
label->set_text(item1->get_branch_str());
}
void MyWin::about_dialog()
{
char *version, *copyright;
// The Gtkmm Version
version = g_strdup_printf("17.0\nRunning Against Gtkmm %d.%d.%d\n2023 Update Summary edition",
version = g_strdup_printf("17.0\nRunning Against Gtkmm %d.%d.%d\n",
GTKMM_MAJOR_VERSION,
GTKMM_MINOR_VERSION,
GTKMM_MICRO_VERSION);

View File

@ -19,11 +19,17 @@ private:
Gtk::Overlay back_overlay, overlay;
Gtk::Box btn_box, cfg_box;
Gtk::Label api_label;
// Gtk::ComboBoxText combo;
Glib::RefPtr<Gtk::StringList> combo_list;
Gtk::DropDown drop_down;
Gtk::Button btn_ver;
// ListStore for dropdown
MyListStore drop_list;
// Factory to renderer string for dropdown
Glib::RefPtr<Gtk::SignalListItemFactory> drop_factory;
void setup_drop(const Glib::RefPtr<Gtk::ListItem> &item);
void bind_drop(const Glib::RefPtr<Gtk::ListItem> &item);
// TitleBar and menu
Gtk::HeaderBar header;
Gtk::MenuButton menubtn;