From be60f9bff327ee56ba0d2db45696100e06e43eae Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 17 Feb 2014 10:42:38 -0500 Subject: [PATCH] gui: Split out collection manager tab This makes the code a little bit cleaner, since this tab is self contained. Signed-off-by: Anna Schumaker --- gui/collection_mgr.cpp | 146 ++++++++++++++++++++++++++++++++++++++++ gui/wires.cpp | 147 +---------------------------------------- include/ocarina.h | 6 +- 3 files changed, 153 insertions(+), 146 deletions(-) create mode 100644 gui/collection_mgr.cpp diff --git a/gui/collection_mgr.cpp b/gui/collection_mgr.cpp new file mode 100644 index 00000000..557b6426 --- /dev/null +++ b/gui/collection_mgr.cpp @@ -0,0 +1,146 @@ +/* + * Copyright 2014 (c) Anna Schumaker. + */ +#include +#include +#include + + +static class CollectionColumns : public Gtk::TreeModelColumnRecord { +public: + CollectionColumns() + { add(c_col_id); add(c_col_enabled); add(c_col_size); add(c_col_path); } + + Gtk::TreeModelColumn c_col_id; + Gtk::TreeModelColumn c_col_enabled; + Gtk::TreeModelColumn c_col_size; + Gtk::TreeModelColumn c_col_path; +} collection_cols; + +static Glib::RefPtr get_collection_list() +{ + return get_object("o_collection_dirs"); +} + +static void on_collection_ok() +{ + std::string path; + Gtk::FileChooserWidget *chooser; + + chooser = get_widget("o_collection_chooser"); + path = chooser->get_filename(); + library::add_path(path); + enable_idle(); +} + +static void on_collection_update() +{ + library :: update_all(); + enable_idle(); +} + +static void on_collection_import() +{ + library :: import(); + enable_idle(); +} + +static void on_collection_row_activated(const Gtk::TreePath &path, + Gtk::TreeViewColumn *col) +{ + Gtk::FileChooser *chooser = get_widget("o_collection_chooser"); + Glib::RefPtr list = get_collection_list(); + + Gtk::TreeModel::Row row = *(list->get_iter(path)); + Glib::ustring dir = row[collection_cols.c_col_path]; + chooser->set_current_folder(dir); +} + +#ifndef CONFIG_TEST +static +#endif /* CONFIG_TEST */ +void do_collection_delete() +{ + Gtk::TreePath path; + Gtk::TreeViewColumn *col; + Gtk::TreeView *treeview = get_widget("o_collection_treeview"); + + treeview->get_cursor(path, col); + if (path) { + Glib::RefPtr list = get_collection_list(); + Gtk::TreeModel::Row row = *(list->get_iter(path)); + library :: del_path(row[collection_cols.c_col_id]); + list->erase(row); + } +} + +static bool on_collection_key_pressed(GdkEventKey *event) +{ + std::string key = gdk_keyval_name(event->keyval); + if (key == "Delete") { + do_collection_delete(); + return true; + } + return false; +} + +#ifndef CONFIG_TEST +static +#endif /* CONFIG_TEST */ +void on_collection_toggled(const Glib::ustring &path) +{ + Glib::RefPtr list = get_collection_list(); + Gtk::TreeModel::Row row = *(list->get_iter(path)); + row[collection_cols.c_col_enabled] = !row[collection_cols.c_col_enabled]; + library :: set_enabled(row[collection_cols.c_col_id], + row[collection_cols.c_col_enabled]); +} + +static void on_library_add(unsigned int id, library :: Library *path) +{ + Gtk::TreeModel::Row row; + Glib::RefPtr list = get_collection_list(); + + row = *(list->append()); + row[collection_cols.c_col_id] = id; + row[collection_cols.c_col_enabled] = path->enabled; + row[collection_cols.c_col_size] = path->size; + row[collection_cols.c_col_path] = path->root_path; +} + +static void on_library_update(unsigned int id, library :: Library *path) +{ + Gtk::TreeModel::Row row; + Glib::RefPtr list = get_collection_list(); + Gtk::TreeModel::Children children = list->children(); + + for (Gtk::TreeModel::Children::iterator it = children.begin(); + it != children.end(); it++) { + row = *it; + if (row[collection_cols.c_col_id] == id) + row[collection_cols.c_col_size] = path->size; + } +} + +void collection_mgr_init() +{ + struct Callbacks *cb = get_callbacks(); + + Gtk::TreeView *treeview = get_widget("o_collection_treeview"); + Glib::RefPtr list = get_collection_list(); + Glib::RefPtr toggle; + + toggle = get_object("o_collection_toggle"); + + cb->on_library_add = on_library_add; + cb->on_library_update = on_library_update; + + connect_button("o_collection_ok", on_collection_ok); + connect_button("o_collection_update", on_collection_update); + connect_button("o_collection_import", on_collection_import); + + list->set_sort_column(collection_cols.c_col_path, Gtk::SORT_ASCENDING); + treeview->signal_row_activated().connect(sigc::ptr_fun(on_collection_row_activated)); + treeview->signal_key_press_event().connect(sigc::ptr_fun(on_collection_key_pressed)); + toggle->signal_toggled().connect(sigc::ptr_fun(on_collection_toggled)); +} diff --git a/gui/wires.cpp b/gui/wires.cpp index fc1b80d0..42558012 100644 --- a/gui/wires.cpp +++ b/gui/wires.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -15,12 +14,8 @@ static bool audio_playing = false; static Glib::RefPtr builder; static sigc::connection fav_connection; static sigc::connection ban_connection; -void enable_idle(); void enable_timeout(); -template -void get_object(const std::string &, Glib::RefPtr &); - /* @@ -202,125 +197,6 @@ static bool on_window_key_released(GdkEventKey *event) -/* - * Collection manager functions - */ -static class CollectionColumns : public Gtk::TreeModelColumnRecord { -public: - CollectionColumns() - { add(c_col_id); add(c_col_enabled); add(c_col_size); add(c_col_path); } - - Gtk::TreeModelColumn c_col_id; - Gtk::TreeModelColumn c_col_enabled; - Gtk::TreeModelColumn c_col_size; - Gtk::TreeModelColumn c_col_path; -} collection_cols; - -static void on_collection_ok() -{ - std::string path; - - Gtk::FileChooserWidget *chooser; - get_builder()->get_widget("o_collection_chooser", chooser); - path = chooser->get_filename(); - library::add_path(path); - enable_idle(); -} - -static void on_collection_update() -{ - library :: update_all(); - enable_idle(); -} - -static void on_collection_import() -{ - library :: import(); - enable_idle(); -} - -static void on_collection_row_activated(const Gtk::TreePath &path, - Gtk::TreeViewColumn *col) -{ - Gtk::FileChooser *chooser; - Glib::RefPtr list = get_object("o_collection_dirs"); - - builder->get_widget("o_collection_chooser", chooser); - - Gtk::TreeModel::Row row = *(list->get_iter(path)); - Glib::ustring dir = row[collection_cols.c_col_path]; - chooser->set_current_folder(dir); -} - -#ifndef CONFIG_TEST -static -#endif /* CONFIG_TEST */ -void do_collection_delete() -{ - Gtk::TreePath path; - Gtk::TreeViewColumn *col; - Gtk::TreeView *treeview; - - builder->get_widget("o_collection_treeview", treeview); - treeview->get_cursor(path, col); - if (path) { - Glib::RefPtr list = get_object("o_collection_dirs"); - Gtk::TreeModel::Row row = *(list->get_iter(path)); - library :: del_path(row[collection_cols.c_col_id]); - list->erase(row); - } -} - -static bool on_collection_key_pressed(GdkEventKey *event) -{ - std::string key = gdk_keyval_name(event->keyval); - if (key == "Delete") { - do_collection_delete(); - return true; - } - return false; -} - -#ifndef CONFIG_TEST -static -#endif /* CONFIG_TEST */ -void on_collection_toggled(const Glib::ustring &path) -{ - Glib::RefPtr list = get_object("o_collection_dirs"); - Gtk::TreeModel::Row row = *(list->get_iter(path)); - row[collection_cols.c_col_enabled] = !row[collection_cols.c_col_enabled]; - library :: set_enabled(row[collection_cols.c_col_id], - row[collection_cols.c_col_enabled]); -} - -static void on_library_add(unsigned int id, library :: Library *path) -{ - Gtk::TreeModel::Row row; - Glib::RefPtr list = get_object("o_collection_dirs"); - - row = *(list->append()); - row[collection_cols.c_col_id] = id; - row[collection_cols.c_col_enabled] = path->enabled; - row[collection_cols.c_col_size] = path->size; - row[collection_cols.c_col_path] = path->root_path; -} - -static void on_library_update(unsigned int id, library :: Library *path) -{ - Gtk::TreeModel::Row row; - Glib::RefPtr list = get_object("o_collection_dirs"); - Gtk::TreeModel::Children children = list->children(); - - for (Gtk::TreeModel::Children::iterator it = children.begin(); - it != children.end(); it++) { - row = *it; - if (row[collection_cols.c_col_id] == id) - row[collection_cols.c_col_size] = path->size; - } -} - - - /* * Idle func */ @@ -375,7 +251,7 @@ Glib::RefPtr &get_builder() return builder; } -static void connect_button(const std::string &name, void (*func)()) +void connect_button(const std::string &name, void (*func)()) { get_widget(name)->signal_clicked().connect(sigc::ptr_fun(func)); } @@ -384,9 +260,6 @@ Gtk::Window *connect_wires() { Gtk::Window *window; struct Callbacks *cb = get_callbacks(); - Glib::RefPtr list; - Glib::RefPtr toggle; - Gtk::TreeView *treeview; Gtk::SpinButton *count; Gtk::CheckButton *enabled; Gtk::Scale *position; @@ -424,23 +297,6 @@ Gtk::Window *connect_wires() window->set_can_focus(); - /* Collection manager */ - cb->on_library_add = on_library_add; - cb->on_library_update = on_library_update; - list = get_object("o_collection_dirs"); - list->set_sort_column(collection_cols.c_col_path, Gtk::SORT_ASCENDING); - - builder->get_widget("o_collection_treeview", treeview); - toggle = get_object("o_collection_toggle"); - treeview->signal_row_activated().connect(sigc::ptr_fun(on_collection_row_activated)); - treeview->signal_key_press_event().connect(sigc::ptr_fun(on_collection_key_pressed)); - toggle->signal_toggled().connect(sigc::ptr_fun(on_collection_toggled)); - - connect_button("o_collection_ok", on_collection_ok); - connect_button("o_collection_update", on_collection_update); - connect_button("o_collection_import", on_collection_import); - - /* Favorite and ban buttons */ builder->get_widget("o_ban", ban); builder->get_widget("o_favorite", fav); @@ -449,6 +305,7 @@ Gtk::Window *connect_wires() /* Set up other tabs */ + collection_mgr_init(); init_tabs(); return window; diff --git a/include/ocarina.h b/include/ocarina.h index fa84d83e..d2498b47 100644 --- a/include/ocarina.h +++ b/include/ocarina.h @@ -7,6 +7,9 @@ #include #include +/* collection_mgr.cpp */ +void collection_mgr_init(); + /* main.cpp */ Gtk::Window *ocarina_init(int *, char ***); @@ -56,8 +59,9 @@ void init_tabs2(); void cleanup_tabs(); /* wires.cpp */ +void enable_idle(); +void connect_button(const std::string &, void (*func)()); Gtk::Window *connect_wires(); -Gtk::Button *get_button(const std::string &); Glib::RefPtr &get_builder(); template