From 2362a4a01cb0c73ba43fd3f53a5e92c7873f5f30 Mon Sep 17 00:00:00 2001 From: tedjk Date: Fri, 20 Mar 2015 13:47:25 +0100 Subject: [PATCH] added config to source, notebook, menu and keybindings. --- juci/config.cc | 27 +++++++++++++++---- juci/config.h | 19 +++++++------- juci/config.json | 9 ++++--- juci/keybindings.cc | 64 ++++++++++++++++----------------------------- juci/keybindings.h | 31 +++++++++++----------- juci/menu.cc | 12 ++------- juci/notebook.cc | 11 +++++--- juci/notebook.h | 5 ++-- juci/window.cc | 5 ++-- juci/window.h | 7 ++--- 10 files changed, 92 insertions(+), 98 deletions(-) diff --git a/juci/config.cc b/juci/config.cc index 9a93184..5aaeead 100644 --- a/juci/config.cc +++ b/juci/config.cc @@ -1,16 +1,17 @@ #include "config.h" -MainConfig::MainConfig() { + +MainConfig::MainConfig() : +keybindings_cfg_(), source_cfg_() { boost::property_tree::json_parser::read_json("config.json", cfg_); GenerateSource(); GenerateKeybindings(); - // keybindings_cfg_ = cfg_.get_child("keybindings"); // notebook_cfg_ = cfg_.get_child("notebook"); // menu_cfg_ = cfg_.get_child("menu"); } -void MainConfig::GenerateSource(){ +void MainConfig::GenerateSource() { boost::property_tree::ptree source_json = cfg_.get_child("source"); boost::property_tree::ptree syntax_json = source_json.get_child("syntax"); boost::property_tree::ptree colors_json = source_json.get_child("colors"); @@ -18,8 +19,24 @@ void MainConfig::GenerateSource(){ source_cfg_.InsertTag(i.first, i.second.get_value()); for ( auto &i : colors_json ) - source_cfg_.InsertType(i.first, i.second.get_value()); + source_cfg_.InsertType(i.first, i.second.get_value()); } -void MainConfig::GenerateKeybindings(){ +void MainConfig::GenerateKeybindings() { + string line; + std::ifstream menu_xml("menu.xml"); + if (menu_xml.is_open()) { + while (getline(menu_xml, line)) { + keybindings_cfg_.AppendXml(line); + } + } + boost::property_tree::ptree keys_json = cfg_.get_child("keybindings"); + for (auto &i : keys_json) + keybindings_cfg_.key_map()[i.first] = i.second.get_value(); +} +Keybindings::Config& MainConfig::keybindings_cfg() { + return keybindings_cfg_; +} +const Source::Config& MainConfig::source_cfg() { + return source_cfg_; } diff --git a/juci/config.h b/juci/config.h index 56eb476..4b40506 100644 --- a/juci/config.h +++ b/juci/config.h @@ -1,30 +1,31 @@ #include #include #include -#include "source.h" +#include #include "keybindings.h" +#include "source.h" + class MainConfig { - public: + public: MainConfig(); - const Source::Config& source_cfg() {return source_cfg_; } - const Keybindings::Config& keybindings_cfg() {return keybindings_cfg_; } - + const Source::Config& source_cfg(); + Keybindings::Config& keybindings_cfg(); + void PrintMenu(); //boost::property_tree::ptree& source_cfg(); //boost::property_tree::ptree& keybindings_cfg(); //boost::property_tree::ptree& notebookk_cfg(); //boost::property_tree::ptree& menu_cfg(); - + boost::property_tree::ptree cfg_; // boost::property_tree::ptree sourcecfg_; - // boost::property_tree::ptree keybindings_cfg_; + boost::property_tree::ptree key_tree_; // boost::property_tree::ptree notebook_cfg_; // boost::property_tree::ptree menu_cfg_; - + Source::Config source_cfg_; Keybindings::Config keybindings_cfg_; void GenerateSource(); void GenerateKeybindings(); }; - diff --git a/juci/config.json b/juci/config.json index c27f2b9..4bd8f75 100644 --- a/juci/config.json +++ b/juci/config.json @@ -18,6 +18,12 @@ "705": "comment" } }, + "keybindings": { + "split_window": "s", + "new_h_file": "h", + "new_cc_file": "c", + "close_tab": "w" + }, "example": { "key": "value", "key2": [ @@ -26,8 +32,5 @@ 3 ], "key3": "value" - }, - "keybindings": { - "path" : "keybindings.xml" } } diff --git a/juci/keybindings.cc b/juci/keybindings.cc index 48860e7..b7b8a36 100644 --- a/juci/keybindings.cc +++ b/juci/keybindings.cc @@ -1,63 +1,29 @@ #include "keybindings.h" -Keybindings::Model::Model(const Keybindings::Config &config) : +Keybindings::Model::Model(Keybindings::Config &config) : menu_ui_string_(config.menu_xml()) { - /* menu_ui_string_ = - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - // " " - // " " - // " " - " " - " " - " " - " " - " " - " ";*/ - - hidden_ui_string_ = + /* hidden_ui_string_ = " " " " " " " " - " "; + " ";*/ } Keybindings::Model::~Model() { } -Keybindings::Controller::Controller(const Keybindings::Config &config) : +Keybindings::Controller::Controller(Keybindings::Config &config) : config_(config), model_(config) { action_group_menu_ = Gtk::ActionGroup::create(); ui_manager_menu_ = Gtk::UIManager::create(); action_group_hidden_ = Gtk::ActionGroup::create(); ui_manager_hidden_ = Gtk::UIManager::create(); } + Keybindings::Controller::~Controller() { } + void Keybindings::Controller::BuildMenu() { try { ui_manager_menu_->add_ui_from_string(model_.menu_ui_string()); @@ -77,10 +43,24 @@ void Keybindings::Controller::BuildHiddenMenu() { ui_manager_hidden_->insert_action_group(action_group_hidden_); } -Keybindings::Config::Config(const Keybindings::Config &original) : - menu_xml_(original.menu_xml()) { +Keybindings::Config::Config(Keybindings::Config &original) { + SetMenu(original.menu_xml()); + SetKeyMap(original.key_map()); +} + +Keybindings::Config::Config() { + menu_xml_ = ""; + std::unordered_map key_map(); +} + +void Keybindings::Config::AppendXml(std::string &child) { + menu_xml_ += child; } void Keybindings::Config::SetMenu(std::string &menu_xml) { menu_xml_ = menu_xml; } + +void Keybindings::Config::SetKeyMap(std::unordered_map &key_map) { + key_map_ = key_map; +} diff --git a/juci/keybindings.h b/juci/keybindings.h index 8ad9b22..8273a38 100644 --- a/juci/keybindings.h +++ b/juci/keybindings.h @@ -2,29 +2,34 @@ #ifndef JUCI_KEYBINDINGS_H_ #define JUCI_KEYBINDINGS_H_ -#include "iostream" +#include #include "gtkmm.h" +#include +//#include "config.h" //TODO :: remove? namespace Keybindings { - class Config{ + class Config { public: - Config(const Config &original); + Config(Config &original); Config(); - const std::string& menu_xml() const {return menu_xml_;} + std::string& menu_xml() { return menu_xml_; } + std::unordered_map& key_map() { return key_map_; } + void AppendXml(std::string &child); void SetMenu(std::string &menu_xml); - + void SetKeyMap(std::unordered_map &key_map); private: + std::unordered_map key_map_; std::string menu_xml_; std::string hidden_ui_string_; };//Config class Model { public: - Model(const Keybindings::Config &config); + Model(Keybindings::Config &config); virtual ~Model(); - std::string menu_ui_string(){return menu_ui_string_;} - std::string hidden_ui_string(){return hidden_ui_string_;} + std::string menu_ui_string() { return menu_ui_string_; } + std::string hidden_ui_string() { return hidden_ui_string_; } //private: std::string menu_ui_string_; std::string hidden_ui_string_; @@ -32,7 +37,7 @@ namespace Keybindings { class Controller { public: - Controller(const Keybindings::Config &config); + explicit Controller(Keybindings::Config &config); virtual ~Controller(); Glib::RefPtr action_group_menu() { return action_group_menu_; @@ -55,13 +60,7 @@ namespace Keybindings { Glib::RefPtr action_group_hidden_; // private: Keybindings::Config config_; - Keybindings::Model model_; - - - + Keybindings::Model model_; };//Controller - - } - #endif // JUCI_KEYBINDINGS_H_ diff --git a/juci/menu.cc b/juci/menu.cc index db504a4..e3e0368 100644 --- a/juci/menu.cc +++ b/juci/menu.cc @@ -28,21 +28,13 @@ Menu::Controller::Controller(Keybindings::Controller& keybindings) : "_Window")); keybindings_.action_group_menu()->add(Gtk::Action::create("WindowSplitWindow", "Split window"), - Gtk::AccelKey("S"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["split_window"]),//"S"), [this]() { OnWindowSplitWindow(); }); keybindings_.action_group_menu()->add(Gtk::Action::create("PluginMenu", "_Plugins")); - //Moved to ApiServiceProvider - /*keybindings_.action_group_menu() - ->add(Gtk::Action::create("PluginSnippet", "Snippet")); - keybindings_.action_group_menu()->add(Gtk::Action::create("PluginAddSnippet", - "Add snippet"), - Gtk::AccelKey("space"), - [this]() { - OnPluginAddSnippet(); - });*/ keybindings_.action_group_menu()->add(Gtk::Action::create("HelpMenu", Gtk::Stock::HELP)); keybindings_.action_group_menu()->add(Gtk::Action::create("HelpAbout", diff --git a/juci/notebook.cc b/juci/notebook.cc index 45f662b..c54b477 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -36,21 +36,24 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings, }); keybindings.action_group_menu()->add(Gtk::Action::create("FileNewCC", "New cc file"), - Gtk::AccelKey("c"), + Gtk::AccelKey(keybindings.config_ + .key_map()["new_cc_file"]), [this]() { is_new_file = true; OnFileNewCCFile(); }); keybindings.action_group_menu()->add(Gtk::Action::create("FileNewH", "New h file"), - Gtk::AccelKey("h"), + Gtk::AccelKey(keybindings.config_ + .key_map()["new_h_file"]), [this]() { is_new_file = true; OnFileNewHeaderFile(); }); keybindings.action_group_menu()->add(Gtk::Action::create("WindowCloseTab", "Close tab"), - Gtk::AccelKey("w"), + Gtk::AccelKey(keybindings.config_ + .key_map()["close_tab"]), [this]() { OnCloseCurrentPage(); }); @@ -59,7 +62,7 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings, [this]() { is_new_file = false; OnEditSearch(); - //TODO(Oyvang, Zalox, Forgi)Create function OnEditFind(); + //TODO(Oyvang, Zalox, Forgi)Create function OnEditFind(); }); keybindings.action_group_menu()->add(Gtk::Action::create("EditCopy", Gtk::Stock::COPY), diff --git a/juci/notebook.h b/juci/notebook.h index f5a4e0f..90243e6 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -25,7 +25,8 @@ namespace Notebook { }; class Controller { public: - Controller(Keybindings::Controller& keybindings,const Source::Config& config); + Controller(Keybindings::Controller& keybindings, + const Source::Config& config); Gtk::Box& view(); Gtk::Box& entry_view(); void OnNewPage(std::string name); @@ -51,7 +52,7 @@ namespace Notebook { void OnEditCut(); void OnEditSearch(); void Search(bool forward); - Source::Config& source_config() { return source_config_; } + const Source::Config& source_config() { return source_config_; } private: Source::Config source_config_; bool is_new_file; diff --git a/juci/window.cc b/juci/window.cc index 77572cf..efd34b1 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -3,8 +3,9 @@ Window::Window() : window_box_(Gtk::ORIENTATION_VERTICAL), main_config_(), - notebook_(keybindings_(main_config().keybindings_cfg()), main_config().source_cfg()), - menu_(keybindings_(main_config().keybindings_cfg())) { + keybindings_(main_config_.keybindings_cfg()), + notebook_( keybindings(), main_config_.source_cfg()), + menu_( keybindings() ) { set_title("juCi++"); set_default_size(600, 400); add(window_box_); diff --git a/juci/window.h b/juci/window.h index 12a1967..c95e632 100644 --- a/juci/window.h +++ b/juci/window.h @@ -1,8 +1,6 @@ #ifndef JUCI_WINDOW_H_ #define JUCI_WINDOW_H_ -#include -#include "gtkmm.h" #include "api.h" #include "config.h" #include @@ -11,7 +9,7 @@ class Window : public Gtk::Window { public: Window(); - MainConfig& main_config() {return main_config_;} + MainConfig& main_config() { return main_config_; } Gtk::Box window_box_; //private: MainConfig main_config_; @@ -19,12 +17,11 @@ public: Menu::Controller menu_; Notebook::Controller notebook_; - + Keybindings::Controller& keybindings() { return keybindings_; } private: //signal handlers void OnWindowHide(); void OnOpenFile(); - }; #endif // JUCI_WINDOW_H