From c61dca75044703b3a04b79a3e520e41bb37b4cd4 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 17 Jan 2015 13:18:19 -0500 Subject: [PATCH] gui: Various collection manager cleanups Signed-off-by: Anna Schumaker --- gui/manager.cpp | 184 +++++++++++++++++++++++------------------------- 1 file changed, 89 insertions(+), 95 deletions(-) diff --git a/gui/manager.cpp b/gui/manager.cpp index 190ca243..c5a6b219 100644 --- a/gui/manager.cpp +++ b/gui/manager.cpp @@ -9,6 +9,14 @@ #include +static Gtk::Button *c_add; +static Gtk::Button *c_update; +static Gtk::FileChooserWidget *c_chooser; +static Gtk::ProgressBar *c_progress; +static Gtk::TreeView *c_treeview; +static Glib::RefPtr c_list; +static Glib::RefPtr c_toggle; + static class CollectionColumns : public Gtk::TreeModelColumnRecord { public: Gtk::TreeModelColumn c_id; @@ -17,38 +25,66 @@ public: Gtk::TreeModelColumn c_path; CollectionColumns() - { - add(c_id); - add(c_enabled); - add(c_size); - add(c_path); - } + { add(c_id); add(c_enabled); add(c_size); add(c_path); } } c_cols; -static Gtk::FileChooserWidget *c_chooser; -static Gtk::ProgressBar *c_progress; -static Gtk::TreeView *c_treeview; -static Glib::RefPtr c_list; - - -static Library *find_library(const Gtk::TreeModel::Row &row) -{ - return tags :: get_library(row[c_cols.c_id]); -} static void list_path(Library *lib) { Gtk::TreeModel::Row row; - if (!lib) - return; - row = *(c_list->append()); + if (lib) { + row = *(c_list->append()); + row[c_cols.c_id] = lib->index(); + row[c_cols.c_enabled] = lib->enabled(); + row[c_cols.c_size] = lib->size(); + row[c_cols.c_path] = lib->primary_key(); + } +} - row[c_cols.c_id] = lib->index(); - row[c_cols.c_enabled] = lib->enabled(); - row[c_cols.c_size] = lib->size(); - row[c_cols.c_path] = lib->primary_key(); +static Library *get_library_and_row(const Gtk::TreePath &path, + Gtk::TreeModel::Row &row) +{ + row = *(c_list->get_iter(path)); + return tags :: get_library(row[c_cols.c_id]); +} + +static Library *get_library(const Gtk::TreePath &path) +{ + Gtk::TreeModel::Row row; + return get_library_and_row(path, row); +} + +static Library *current_library_and_row(Gtk::TreeModel::Row &row) +{ + Gtk::TreePath path; + Gtk::TreeViewColumn *col; + + c_treeview->get_cursor(path, col); + if (path.empty()) + return NULL; + return get_library_and_row(path, row); +} + +static Library *current_library() +{ + Gtk::TreeModel::Row row; + return current_library_and_row(row); +} + + + +void update_paths() +{ + Library *lib; + Gtk::TreeModel::Children::iterator it; + + for (it = c_list->children().begin(); it != c_list->children().end(); it++) { + lib = tags :: get_library((*it)[c_cols.c_id]); + if (lib) + (*it)[c_cols.c_size] = lib->size(); + } } static void remove_banned_tracks() @@ -63,48 +99,7 @@ static void remove_banned_tracks() library :: get_queue()->del(tags :: get_track(*it)); } -void del_path(const Gtk::TreePath &path) -{ - Gtk::TreeModel::Row row = *(c_list->get_iter(path)); - Library *lib = find_library(row); - library :: remove(lib); - c_list->erase(row); -} -void update_paths() -{ - Library *lib; - Gtk::TreeModel::Children::iterator it; - - for (it = c_list->children().begin(); it != c_list->children().end(); it++) { - lib = find_library(*it); - if (lib) - (*it)[c_cols.c_size] = lib->size(); - } -} - -void toggle_path_enabled(const Gtk::TreePath &path) -{ - Gtk::TreeModel::Row row = *(c_list->get_iter(path)); - Library *lib = find_library(row); - row[c_cols.c_enabled] = !row[c_cols.c_enabled]; - library :: set_enabled(lib, row[c_cols.c_enabled]); - - if (row[c_cols.c_enabled]) - remove_banned_tracks(); -} -static void cd_chooser(const Glib::ustring &dir) -{ - if (c_chooser->get_current_folder() != dir) - c_chooser->set_current_folder(dir); -} - -static bool find_cur_path(Gtk::TreePath &path) -{ - Gtk::TreeViewColumn *col; - c_treeview->get_cursor(path, col); - return !path.empty(); -} static bool on_idle() { @@ -120,8 +115,7 @@ static bool on_idle() return ret; } - -static void on_ok() +static void on_add() { list_path(library :: add(c_chooser->get_filename())); lib :: idle(on_idle); @@ -135,64 +129,64 @@ static void on_update() static void on_row_activated(const Gtk::TreePath &path, Gtk::TreeViewColumn *col) { - Gtk::TreeModel::Row row = *(c_list->get_iter(path)); - library :: update(find_library(row)); + library :: update(get_library(path)); lib :: idle(on_idle); } static void on_cursor_changed() { - Gtk::TreePath path; - if (find_cur_path(path)) { - Gtk::TreeModel::Row row = *(c_list->get_iter(path)); - cd_chooser(row[c_cols.c_path]); - } + Library *lib = current_library(); + if (lib && (c_chooser->get_current_folder() != lib->primary_key())) + c_chooser->set_current_folder(lib->primary_key()); } static bool on_key_pressed(GdkEventKey *event) { - Gtk::TreePath path; + Library *lib; + Gtk::TreeModel::Row row; if (lib :: key_name(event) != "Delete") return false; - if (find_cur_path(path)) - del_path(path); + lib = current_library_and_row(row); + library :: remove(lib); + c_list->erase(row); return true; } -static void on_toggled(const Glib::ustring &path) +static void on_toggled(const Glib::ustring &str) { - toggle_path_enabled(Gtk::TreePath(path)); + Gtk::TreeModel::Row row; + Library *lib = get_library_and_row(Gtk::TreePath(str), row); + + library :: set_enabled(lib, !lib->enabled()); + row[c_cols.c_enabled] = lib->enabled(); + if (lib->enabled()) + remove_banned_tracks(); } void manager :: init() { - Library *library; - Glib::RefPtr toggle; + c_add = lib :: get_widget("colmgr_add"); + c_update = lib :: get_widget("colmgr_update"); - c_treeview = lib :: get_widget("colmgr_treeview"); - c_progress = lib :: get_widget("o_idle_progress"); c_chooser = lib :: get_widget("colmgr_chooser"); - toggle = lib :: get_object("colmgr_toggle"); + c_progress = lib :: get_widget("o_idle_progress"); + c_toggle = lib :: get_object("colmgr_toggle"); + c_list = lib :: get_object("colmgr_list"); + c_treeview = lib :: get_widget("colmgr_treeview"); - connect_button("colmgr_add", on_ok); - connect_button("colmgr_update", on_update); + c_add->signal_clicked().connect(sigc::ptr_fun(on_add)); + c_update->signal_clicked().connect(sigc::ptr_fun(on_update)); + c_toggle->signal_toggled().connect(sigc::ptr_fun(on_toggled)); + c_list->set_sort_column(c_cols.c_path, Gtk::SORT_ASCENDING); c_treeview->signal_row_activated().connect(sigc::ptr_fun(on_row_activated)); c_treeview->signal_cursor_changed().connect(sigc::ptr_fun(on_cursor_changed)); c_treeview->signal_key_press_event().connect(sigc::ptr_fun(on_key_pressed)); - toggle->signal_toggled().connect(sigc::ptr_fun(on_toggled)); - - c_list = lib :: get_object("colmgr_list"); - c_list->set_sort_column(c_cols.c_path, Gtk::SORT_ASCENDING); - - for (unsigned int i = 0; i < tags :: library_size(); i++) { - library = tags :: get_library(i); - if (library) - list_path(library); - } + for (unsigned int i = 0; i < tags :: library_size(); i++) + list_path(tags :: get_library(i)); }