Factor out and simplify the search by components in options tree
authorJulien "_FrnchFrgg_" RIVAUD <frnchfrgg@free.fr>
Tue, 26 Jul 2016 05:42:54 +0000 (07:42 +0200)
committerJulien "_FrnchFrgg_" RIVAUD <frnchfrgg@free.fr>
Tue, 26 Jul 2016 06:02:40 +0000 (08:02 +0200)
gtk2_ardour/option_editor.cc
gtk2_ardour/option_editor.h

index f19babaae46b37ae126b58d328be77bfa15a9c83..9a21e600a0f56a4aa891650816f342301741ac2e 100644 (file)
@@ -533,99 +533,63 @@ OptionEditor::treeview_row_selected ()
        }
 }
 
-void
-OptionEditor::add_path_to_treeview (std::string const & pn, Gtk::Widget& widget)
+TreeModel::iterator
+OptionEditor::find_path_in_treemodel (std::string const & pn, bool create_missing)
 {
-       option_treeview.set_model (Glib::RefPtr<TreeStore>());
-
-       if (pn.find ('/') == std::string::npos) {
-               /* new top level item in tree */
-
-               TreeModel::iterator new_row = option_tree->append ();
-               TreeModel::Row row = *new_row;
-               row[option_columns.name] = pn;
-               row[option_columns.widget] = &widget;
-
-       } else {
-
-               /* find parent */
-
-               /* split page name, which is actually a path, into each
-                * component
-                */
-
-               std::vector<std::string> components;
-               split (pn, components, '/');
-
-               /* start with top level children */
-
-               typedef Gtk::TreeModel::Children type_children; //minimise code length.
-               type_children children = option_tree->children();
-
-               /* foreach path component ... */
-
-               for (std::vector<std::string>::const_iterator s = components.begin(); s != components.end(); ) {
-
-                       bool component_found = false;
-
-                       type_children::iterator iter;
-
-                       /* look through the children at this level */
+       /* split page name, which is actually a path, into each component */
 
-                       for (iter = children.begin(); iter != children.end(); ++iter) {
-                               Gtk::TreeModel::Row row = *iter;
+       std::vector<std::string> components;
+       split (pn, components, '/');
 
-                               std::string row_name = row[option_columns.name];
-                               if (row_name == (*s)) {
+       /* start with top level children */
 
-                                       /* found it */
+       TreeModel::Children children = option_tree->children();
+       TreeModel::iterator iter;
 
-                                       component_found = true;
+       /* foreach path component ... */
 
-                                       /* reset children to point to
-                                        * the children of this row
-                                        */
+       for (std::vector<std::string>::const_iterator s = components.begin(); s != components.end(); ++s) {
 
-                                       children = row.children();
-                                       break;
-                               }
+               for (iter = children.begin(); iter != children.end(); ++iter) {
+                       TreeModel::Row row = *iter;
+                       const std::string row_name = row[option_columns.name];
+                       if (row_name == (*s)) {
+                               break;
                        }
+               }
 
-                       if (!component_found) {
-
-                               /* missing component. If it is the last
-                                * one, append a real page. Otherwise
-                                * just put an entry in the tree model
-                                * since it is a navigational/sorting
-                                * component.
-                                */
-
-                               TreeModel::iterator new_row = option_tree->append (children);
-                               TreeModel::Row row = *new_row;
+               if (iter == children.end()) {
+                       /* the current component is missing; bail out or create it */
+                       if (!create_missing) {
+                               return option_tree->get_iter(TreeModel::Path(""));
+                       } else {
+                               iter = option_tree->append (children);
+                               TreeModel::Row row = *iter;
                                row[option_columns.name] = *s;
+                               row[option_columns.widget] = 0;
+                       }
+               }
 
-                               ++s;
+               /* from now on, iter points to a valid row, either the one we found or a new one */
+               /* set children to the row's children to continue searching */
+               children = (*iter)->children ();
 
-                               if (s == components.end()) {
-                                       row[option_columns.widget] = &widget;
-                               } else {
-                                       row[option_columns.widget] = 0;
-                               }
+       }
 
-                               children = row.children ();
+       return iter;
+}
 
-                       } else {
+void
+OptionEditor::add_path_to_treeview (std::string const & pn, Gtk::Widget& widget)
+{
+       option_treeview.set_model (Glib::RefPtr<TreeStore>());
 
-                               /* component found, just move on to the
-                                * next one. children has already been
-                                * reset appropriately.
-                                */
+       TreeModel::iterator row_iter = find_path_in_treemodel(pn, true);
 
-                               ++s;
-                       }
-               }
+       assert(row_iter);
 
-       }
+       TreeModel::Row row = *row_iter;
+       row[option_columns.widget] = &widget;
 
        option_treeview.set_model (option_tree);
        option_treeview.expand_all ();
index 53c671287831a4288383490cf2abd5a933715644..b5d44d577ca048d3b94fb44cc8ddda13bb0c1fdd 100644 (file)
@@ -723,6 +723,8 @@ private:
        std::map<std::string, OptionEditorPage*> _pages;
 
        void add_path_to_treeview (std::string const &, Gtk::Widget&);
+       Gtk::TreeModel::iterator find_path_in_treemodel (std::string const & pn,
+                                                        bool create_missing = false);
        void treeview_row_selected ();
 };