From d97040744744a1b69e85dad5d5221ae793f07b79 Mon Sep 17 00:00:00 2001 From: tedjk Date: Thu, 19 Mar 2015 13:30:32 +0100 Subject: [PATCH] config ready to test on source, starting with keybindings --- juci/CMakeLists.txt | 4 ++- juci/config.cc | 25 +++++++++++++++ juci/config.h | 30 ++++++++++++++++++ juci/config.json | 46 ++++++++++++++++++---------- juci/keybindings.cc | 25 ++++++++++----- juci/keybindings.h | 24 +++++++++++++-- juci/notebook.cc | 10 +++--- juci/notebook.h | 5 ++- juci/source.cc | 74 +++++++++++++++++++++++++++++---------------- juci/source.h | 28 ++++++----------- juci/window.cc | 13 +++----- juci/window.h | 10 ++++-- 12 files changed, 206 insertions(+), 88 deletions(-) create mode 100644 juci/config.cc create mode 100644 juci/config.h diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index ec9f2ac..84e5973 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -72,10 +72,12 @@ endif() # name of the executable on Windows will be example.exe add_executable(${project_name} #list of every needed file to create the executable - juci.cc + juci.cc keybindings menu source + config.h + config.cc sourcefile.h sourcefile.cc window diff --git a/juci/config.cc b/juci/config.cc new file mode 100644 index 0000000..9a93184 --- /dev/null +++ b/juci/config.cc @@ -0,0 +1,25 @@ +#include "config.h" + +MainConfig::MainConfig() { + 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(){ + 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"); + for ( auto &i : syntax_json ) + source_cfg_.InsertTag(i.first, i.second.get_value()); + + for ( auto &i : colors_json ) + source_cfg_.InsertType(i.first, i.second.get_value()); +} + +void MainConfig::GenerateKeybindings(){ +} diff --git a/juci/config.h b/juci/config.h new file mode 100644 index 0000000..56eb476 --- /dev/null +++ b/juci/config.h @@ -0,0 +1,30 @@ +#include +#include +#include +#include "source.h" +#include "keybindings.h" + class MainConfig { + public: + MainConfig(); + const Source::Config& source_cfg() {return source_cfg_; } + const Keybindings::Config& keybindings_cfg() {return 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 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 6af5298..c27f2b9 100644 --- a/juci/config.json +++ b/juci/config.json @@ -1,19 +1,33 @@ { - "colors": { - "text_color": "#333333", - "string" : "#CC0000", - "namespace_ref" : "#990099", - "type" : "#0066FF", - "keyword": "blue", - "comment": "grey", - "own": "pink" - }, - "syntax": { - "43": "type", - "46": "namespace_ref", - "109": "string", - "702": "keyword", - "703": "own", - "705": "comment" + "source": { + "colors": { + "text_color": "#333333", + "string": "#CC0000", + "namespace_ref": "#990099", + "type": "#0066FF", + "keyword": "blue", + "comment": "grey", + "own": "pink" + }, + "syntax": { + "43": "type", + "46": "namespace_ref", + "109": "string", + "702": "keyword", + "703": "own", + "705": "comment" } + }, + "example": { + "key": "value", + "key2": [ + "val1", + "val2", + 3 + ], + "key3": "value" + }, + "keybindings": { + "path" : "keybindings.xml" + } } diff --git a/juci/keybindings.cc b/juci/keybindings.cc index 234d9d9..48860e7 100644 --- a/juci/keybindings.cc +++ b/juci/keybindings.cc @@ -1,7 +1,8 @@ #include "keybindings.h" -Keybindings::Model::Model() { - menu_ui_string_ = +Keybindings::Model::Model(const Keybindings::Config &config) : + menu_ui_string_(config.menu_xml()) { + /* menu_ui_string_ = " " " " " " @@ -27,15 +28,15 @@ Keybindings::Model::Model() { " " " " " " - // " " - // " " - // " " + // " " + // " " + // " " " " " " " " " " " " - " "; + " ";*/ hidden_ui_string_ = " " @@ -43,12 +44,13 @@ Keybindings::Model::Model() { " " " " " "; -}; +} Keybindings::Model::~Model() { } -Keybindings::Controller::Controller() { +Keybindings::Controller::Controller(const 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(); @@ -75,3 +77,10 @@ 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()) { +} + +void Keybindings::Config::SetMenu(std::string &menu_xml) { + menu_xml_ = menu_xml; +} diff --git a/juci/keybindings.h b/juci/keybindings.h index adf3283..8ad9b22 100644 --- a/juci/keybindings.h +++ b/juci/keybindings.h @@ -6,9 +6,22 @@ #include "gtkmm.h" namespace Keybindings { + + class Config{ + public: + Config(const Config &original); + Config(); + const std::string& menu_xml() const {return menu_xml_;} + void SetMenu(std::string &menu_xml); + + private: + std::string menu_xml_; + std::string hidden_ui_string_; + };//Config + class Model { public: - Model(); + Model(const Keybindings::Config &config); virtual ~Model(); std::string menu_ui_string(){return menu_ui_string_;} std::string hidden_ui_string(){return hidden_ui_string_;} @@ -16,9 +29,10 @@ namespace Keybindings { std::string menu_ui_string_; std::string hidden_ui_string_; }; // Model + class Controller { public: - Controller(); + Controller(const Keybindings::Config &config); virtual ~Controller(); Glib::RefPtr action_group_menu() { return action_group_menu_; @@ -40,8 +54,14 @@ namespace Keybindings { Glib::RefPtr ui_manager_hidden_; Glib::RefPtr action_group_hidden_; // private: + Keybindings::Config config_; Keybindings::Model model_; + + + };//Controller + + } #endif // JUCI_KEYBINDINGS_H_ diff --git a/juci/notebook.cc b/juci/notebook.cc index 371de09..45f662b 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -12,16 +12,16 @@ Gtk::Box& Notebook::View::view() { view_.pack_start(notebook_); return view_; } -Notebook::Controller::Controller(Keybindings::Controller& keybindings){ +Notebook::Controller::Controller(Keybindings::Controller& keybindings, + const Source::Config &source_cfg) { scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); - source_vec_.push_back(new Source::Controller); + source_vec_.push_back(new Source::Controller(source_cfg)); scrolledwindow_vec_.back()->add(source_vec_.back()->view()); source_vec_.back()->OnNewEmptyFile(); notebook().append_page(*scrolledwindow_vec_.back(), "juCi++"); notebook().set_focus_child(*scrolledwindow_vec_.back()); refClipboard = Gtk::Clipboard::get(); - keybindings.action_group_menu()->add(Gtk::Action::create("FileMenu", Gtk::Stock::FILE)); /* File->New files */ @@ -114,7 +114,7 @@ Gtk::Box& Notebook::Controller::entry_view(){ } void Notebook::Controller::OnNewPage(std::string name) { scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); - source_vec_.push_back(new Source::Controller); + source_vec_.push_back(new Source::Controller(source_config_)); scrolledwindow_vec_.back()->add(source_vec_.back()->view()); source_vec_.back()->OnNewEmptyFile(); notebook().append_page(*scrolledwindow_vec_.back(), name); @@ -158,7 +158,7 @@ void Notebook::Controller::OnEditCut() { void Notebook::Controller::OnOpenFile(std::string path) { scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); - source_vec_.push_back(new Source::Controller); + source_vec_.push_back(new Source::Controller(source_config_)); scrolledwindow_vec_.back()->add(source_vec_.back()->view()); source_vec_.back()->OnOpenFile(path); unsigned pos = path.find_last_of("/\\"); diff --git a/juci/notebook.h b/juci/notebook.h index 1692270..f5a4e0f 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -18,13 +18,14 @@ namespace Notebook { View(); Gtk::Box& view(); Gtk::Notebook& notebook() { return notebook_; } + protected: Gtk::Box view_; Gtk::Notebook notebook_; }; class Controller { public: - Controller(Keybindings::Controller& keybindings); + Controller(Keybindings::Controller& keybindings,const Source::Config& config); Gtk::Box& view(); Gtk::Box& entry_view(); void OnNewPage(std::string name); @@ -50,7 +51,9 @@ namespace Notebook { void OnEditCut(); void OnEditSearch(); void Search(bool forward); + Source::Config& source_config() { return source_config_; } private: + Source::Config source_config_; bool is_new_file; Gtk::TextIter search_match_end_; Gtk::TextIter search_match_start_; diff --git a/juci/source.cc b/juci/source.cc index a7a95c9..34ed54d 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -29,8 +29,8 @@ string Source::View::GetLine(const Gtk::TextIter &begin) { // Source::View::ApplyTheme() // Applies theme in textview -void Source::View::ApplyTheme(const Source::Theme &theme) { - for (auto &item : theme.tagtable()) { +void Source::View::ApplyConfig(const Source::Config &config) { + for (auto &item : config.tagtable()) { // std::cout << "Apply: f: " << item.first << ", s: " << // item.second << std::endl; get_buffer()->create_tag(item.first)->property_foreground() = item.second; @@ -38,13 +38,13 @@ void Source::View::ApplyTheme(const Source::Theme &theme) { } void Source::View::OnOpenFile(std::vector &locations, - const Source::Theme &theme) { - ApplyTheme(theme); + const Source::Config &config) { + ApplyConfig(config); Glib::RefPtr buffer = get_buffer(); for (auto &loc : locations) { string type = std::to_string(loc.kind()); try { - theme.typetable().at(type); + config.typetable().at(type); } catch (std::exception) { continue; } @@ -66,40 +66,51 @@ void Source::View::OnOpenFile(std::vector &locations, Gtk::TextIter end_iter = buffer->get_iter_at_line_offset(linum_end, end); // std::cout << get_buffer()->get_text(begin_iter, end_iter) << std::endl; if (begin_iter.get_line() == end_iter.get_line()) { - buffer->apply_tag_by_name(theme.typetable().at(type), + buffer->apply_tag_by_name(config.typetable().at(type), begin_iter, end_iter); } } } -// Source::View::Theme::tagtable() + + +// Source::View::Config::Config(Config &config) +// copy-constructor +Source::Config::Config(const Source::Config &original) { + SetTagTable(original.tagtable()); + SetTypeTable(original.typetable()); +} + +Source::Config::Config(){} + +// Source::View::Config::tagtable() // returns a const refrence to the tagtable -const std::unordered_map& Source::Theme::tagtable() const { +const std::unordered_map& Source::Config::tagtable() const { return tagtable_; } -// Source::View::Theme::tagtable() +// Source::View::Config::tagtable() // returns a const refrence to the tagtable -const std::unordered_map& Source::Theme::typetable() const { +const std::unordered_map& Source::Config::typetable() const { return typetable_; } -void Source::Theme::InsertTag(const string &key, const string &value) { +void Source::Config::InsertTag(const string &key, const string &value) { tagtable_[key] = value; } -// Source::View::Theme::SetTagTable() +// Source::View::Config::SetTagTable() // sets the tagtable for the view -void Source::Theme::SetTypeTable( +void Source::Config::SetTypeTable( const std::unordered_map &typetable) { typetable_ = typetable; } -void Source::Theme::InsertType(const string &key, const string &value) { +void Source::Config::InsertType(const string &key, const string &value) { typetable_[key] = value; } -// Source::View::Theme::SetTagTable() +// Source::View::Config::SetTagTable() // sets the tagtable for the view -void Source::Theme::SetTagTable( +void Source::Config::SetTagTable( const std::unordered_map &tagtable) { tagtable_ = tagtable; } @@ -107,8 +118,7 @@ void Source::Theme::SetTagTable( /////////////// //// Model //// /////////////// -Source::Model::Model() : - theme_() { +/*Source::Model::Model() { std::cout << "Model constructor run" << std::endl; boost::property_tree::ptree pt; boost::property_tree::json_parser::read_json("config.json", pt); @@ -116,19 +126,23 @@ Source::Model::Model() : boost::property_tree::ptree props = pt.get_child(i.first); for (auto &pi : props) { if (i.first.compare("syntax")) { // checks the config-file - theme_.InsertTag(pi.first, pi.second.get_value()); + config_.InsertTag(pi.first, pi.second.get_value()); // std::cout << "inserting tag. " << pi.first << pi.second.get_value() << std::endl; } if (i.first.compare("colors")) { // checks the config-file - theme_.InsertType(pi.first, pi.second.get_value()); + config_.InsertType(pi.first, pi.second.get_value()); // std::cout << "inserting type. " << pi.first << pi.second.get_value() << std::endl; } } - } + } +}*/ + +Source::Model::Model(const Source::Config &config) : + config_(config) { } -Source::Theme& Source::Model::theme() { - return theme_; +Source::Config& Source::Model::config() { + return config_; } const string Source::Model::filepath() { @@ -149,12 +163,20 @@ SetSourceLocations(const std::vector &locations) { // Source::Controller::Controller() // Constructor for Controller -Source::Controller::Controller() { - //std::cout << "Controller constructor run" << std::endl; +Source::Controller::Controller(const Source::Config &config) : + model_(config) { view().get_buffer()->signal_changed().connect([this](){ this->OnLineEdit(); }); } + +/*Source::Controller::Controller(){ + //std::cout << "Controller constructor run" << std::endl; + + view().get_buffer()->signal_changed().connect([this](){ + this->OnLineEdit(); + }); + }*/ // Source::Controller::view() // return shared_ptr to the view Source::View& Source::Controller::view() { @@ -185,7 +207,7 @@ void Source::Controller::OnOpenFile(const string &filename) { int offset = view().get_buffer()->end().get_line_offset(); Clang::TranslationUnit tu(filename.c_str(), linums, offset); model().SetSourceLocations(tu.getSourceLocations()); - view().OnOpenFile(model().getSourceLocations(), model().theme()); + view().OnOpenFile(model().getSourceLocations(), model().config()); } Glib::RefPtr Source::Controller::buffer(){ diff --git a/juci/source.h b/juci/source.h index 171759b..315e2df 100644 --- a/juci/source.h +++ b/juci/source.h @@ -11,18 +11,10 @@ using std::string; namespace Source { - class Config() { - // læs json - - // - - private: - - } - - class Config { public: + Config(const Config &original); + Config(); const std::unordered_map& tagtable() const; const std::unordered_map& typetable() const; void SetTagTable(const std::unordered_map &tagtable); @@ -33,24 +25,24 @@ namespace Source { private: std::unordered_map tagtable_; std::unordered_map typetable_; - string background_; - }; // class Theme + }; // class Config class View : public Gtk::TextView { public: View(); string UpdateLine(); - void ApplyTheme(const Theme &theme); + void ApplyConfig(const Config &config); void OnOpenFile(std::vector &locations, - const Theme &theme); + const Config &config); private: string GetLine(const Gtk::TextIter &begin); }; // class View class Model{ public: - Model(); - Theme& theme(); + Model(const Source::Config &config); + //Model(); + Config& config(); const string filepath(); void SetFilePath(const string &filepath); void SetSourceLocations( @@ -60,13 +52,14 @@ namespace Source { } private: - Theme theme_; + Source::Config config_; string filepath_; std::vector locations_; }; class Controller { public: + Controller(const Source::Config &config); Controller(); View& view(); Model& model(); @@ -77,7 +70,6 @@ namespace Source { private: void OnLineEdit(); void OnSaveFile(); - protected: View view_; Model model_; diff --git a/juci/window.cc b/juci/window.cc index 05014e1..77572cf 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -2,11 +2,13 @@ Window::Window() : window_box_(Gtk::ORIENTATION_VERTICAL), - notebook_(keybindings_), - menu_(keybindings_){ + main_config_(), + notebook_(keybindings_(main_config().keybindings_cfg()), main_config().source_cfg()), + menu_(keybindings_(main_config().keybindings_cfg())) { set_title("juCi++"); set_default_size(600, 400); add(window_box_); + keybindings_.action_group_menu()->add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT), [this]() { @@ -34,12 +36,6 @@ Window::Window() : } // Window constructor void Window::OnWindowHide(){ - //TODO forgie: find out how to 'remove' the pointers - //TODO forgie: Make shared_ptr - //libjuci::PluginApi::notebook_ = - // std::shared_ptr(nullptr); - // libjuci::PluginApi::menu_ = - // std::shared_ptr(nullptr); hide(); } @@ -90,5 +86,4 @@ void Window::OnOpenFile() { break; } } - } diff --git a/juci/window.h b/juci/window.h index eac2412..12a1967 100644 --- a/juci/window.h +++ b/juci/window.h @@ -4,21 +4,27 @@ #include #include "gtkmm.h" #include "api.h" +#include "config.h" #include + class Window : public Gtk::Window { public: Window(); + MainConfig& main_config() {return main_config_;} Gtk::Box window_box_; //private: + MainConfig main_config_; Keybindings::Controller keybindings_; Menu::Controller menu_; Notebook::Controller notebook_; + + private: //signal handlers void OnWindowHide(); void OnOpenFile(); -}; -#endif // JUCI_WINDOW_H_ +}; +#endif // JUCI_WINDOW_H