add new methods to ARDOUR::CoreSelection to centralize selecting next/prev stripables
authorPaul Davis <paul@linuxaudiosystems.com>
Fri, 24 Aug 2018 18:03:04 +0000 (14:03 -0400)
committerPaul Davis <paul@linuxaudiosystems.com>
Fri, 24 Aug 2018 18:04:18 +0000 (14:04 -0400)
libs/ardour/ardour/selection.h
libs/ardour/selection.cc

index 8da0692761c451fb8a7ef2e853a9775db60f940b..0c9493139a5bec0f732394868560424d7b340bf2 100644 (file)
 #include "pbd/i18n.h"
 
 #include "ardour/presentation_info.h"
+#include "ardour/types.h"
 
 namespace ARDOUR {
 
 class AutomationControl;
+class RouteGroup;
 class Session;
 class Stripable;
 class VCAManager;
@@ -48,6 +50,12 @@ class LIBARDOUR_API CoreSelection : public PBD::Stateful {
        void add (boost::shared_ptr<Stripable>, boost::shared_ptr<AutomationControl>);
        void remove (boost::shared_ptr<Stripable>, boost::shared_ptr<AutomationControl>);
        void set (boost::shared_ptr<Stripable>, boost::shared_ptr<AutomationControl>);
+       void set (StripableList&);
+
+       void select_next_stripable (bool mixer_order, bool routes_only);
+       void select_prev_stripable (bool mixer_order, bool routes_only);
+       bool select_stripable_and_maybe_group (boost::shared_ptr<Stripable> s, bool with_group, bool routes_only, RouteGroup*);
+
        void clear_stripables();
 
        bool selected (boost::shared_ptr<const Stripable>) const;
@@ -107,6 +115,11 @@ class LIBARDOUR_API CoreSelection : public PBD::Stateful {
        SelectedStripables _stripables;
 
        void send_selection_change ();
+
+       template<typename IterTypeForward, typename IterTypeCore>
+               void select_adjacent_stripable (bool mixer_order, bool routes_only,
+                                               IterTypeCore (StripableList::*begin_method)(),
+                                               IterTypeCore (StripableList::*end_method)());
 };
 
 } // namespace ARDOUR
index 5bd05b99f754d529dbd484e035b6defa147d0767..a2c2d16735ab5c1b7ccc803176a26f3c2af54152 100644 (file)
@@ -24,6 +24,8 @@
 
 #include "ardour/automation_control.h"
 #include "ardour/debug.h"
+#include "ardour/route.h"
+#include "ardour/route_group.h"
 #include "ardour/selection.h"
 #include "ardour/session.h"
 #include "ardour/stripable.h"
@@ -49,6 +51,146 @@ CoreSelection::~CoreSelection ()
 {
 }
 
+template<typename IterTypeForward, typename IterTypeCore>
+void
+CoreSelection::select_adjacent_stripable (bool mixer_order, bool routes_only,
+                                          IterTypeCore (StripableList::*begin_method)(),
+                                          IterTypeCore (StripableList::*end_method)())
+{
+       if (_stripables.empty()) {
+
+               /* Pick first acceptable */
+
+               StripableList stripables;
+               session.get_stripables (stripables);
+               stripables.sort (ARDOUR::Stripable::Sorter (mixer_order));
+
+               for (IterTypeForward s = (stripables.*begin_method)(); s != (stripables.*end_method)(); ++s) {
+                       if (select_stripable_and_maybe_group (*s, true, routes_only, 0)) {
+                               break;
+                       }
+               }
+
+               return;
+       }
+
+       /* fetch the current selection so that we can get the most recently selected */
+       StripableAutomationControls selected;
+       get_stripables (selected);
+       boost::shared_ptr<Stripable> last_selected = selected.back().stripable;
+
+       /* Get all stripables and sort into the appropriate ordering */
+       StripableList stripables;
+       session.get_stripables (stripables);
+       stripables.sort (ARDOUR::Stripable::Sorter (mixer_order));
+
+
+       /* Check for a possible selection-affecting route group */
+
+       RouteGroup* group = 0;
+       boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (last_selected);
+
+       if (r && r->route_group() && r->route_group()->is_select()) {
+               group = r->route_group();
+       }
+
+       bool select_me = false;
+
+       for (IterTypeCore i = (stripables.*begin_method)(); i != (stripables.*end_method)(); ++i) {
+
+               if (select_me) {
+
+                       if (!this->selected (*i)) { /* not currently selected */
+                               if (select_stripable_and_maybe_group (*i, true, routes_only, group)) {
+                                       return;
+                               }
+                       }
+               }
+
+               if ((*i) == last_selected) {
+                       select_me = true;
+               }
+       }
+
+       /* no previous, wrap around ... find first usable stripable from the back
+       */
+
+       for (IterTypeCore s = (stripables.*begin_method)(); s != (stripables.*end_method)(); ++s) {
+
+               r = boost::dynamic_pointer_cast<Route> (*s);
+
+               if ((!routes_only || r) && !(*s)->is_monitor()) {
+                       if (select_stripable_and_maybe_group (*s, true, routes_only, 0)) {
+                               return;
+                       }
+               }
+       }
+}
+
+void
+CoreSelection::select_next_stripable (bool mixer_order, bool routes_only)
+{
+       select_adjacent_stripable<StripableList::iterator> (mixer_order, routes_only, &StripableList::begin, &StripableList::end);
+}
+
+void
+CoreSelection::select_prev_stripable (bool mixer_order, bool routes_only)
+{
+       select_adjacent_stripable<StripableList::reverse_iterator> (mixer_order, routes_only, &StripableList::rbegin, &StripableList::rend);
+}
+
+
+bool
+CoreSelection::select_stripable_and_maybe_group (boost::shared_ptr<Stripable> s, bool with_group, bool routes_only, RouteGroup* group)
+{
+       boost::shared_ptr<Route> r;
+       StripableList sl;
+
+       if (s->is_hidden()) {
+               return false;
+       }
+
+       if ((r = boost::dynamic_pointer_cast<Route> (s))) {
+
+               if (!r->active()) {
+                       return false;
+               }
+
+               if (with_group) {
+
+                       if (!group || !r->route_group() || r->route_group() != group) {
+
+                               if (r->route_group() && r->route_group()->is_select()) {
+                                       boost::shared_ptr<RouteList> rl = r->route_group()->route_list ();
+                                       for (RouteList::iterator ri = rl->begin(); ri != rl->end(); ++ri) {
+                                               if (*ri != r) {
+                                                       sl.push_back (*ri);
+                                               }
+                                       }
+                               }
+
+                               /* it is important to make the "primary" stripable being selected the last in this
+                                * list
+                                */
+
+                               sl.push_back (s);
+                               set (sl);
+                               return true;
+                       }
+
+               } else {
+                       set (s, boost::shared_ptr<AutomationControl>());
+                       return true;
+               }
+
+       } else if (!routes_only) {
+               set (s, boost::shared_ptr<AutomationControl>());
+               return true;
+       }
+
+       return false;
+}
+
 void
 CoreSelection::toggle (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
 {
@@ -61,6 +203,63 @@ CoreSelection::toggle (boost::shared_ptr<Stripable> s, boost::shared_ptr<Automat
        }
 }
 
+void
+CoreSelection::set (StripableList& sl)
+{
+       bool send = false;
+       boost::shared_ptr<AutomationControl> no_control;
+
+       std::vector<boost::shared_ptr<Stripable> > removed;
+
+       {
+               Glib::Threads::RWLock::WriterLock lm (_lock);
+
+               removed.reserve (_stripables.size());
+
+               for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
+                       boost::shared_ptr<Stripable> sp = session.stripable_by_id ((*x).stripable);
+                       if (sp) {
+                               removed.push_back (sp);
+                       }
+               }
+
+               _stripables.clear ();
+
+               for (StripableList::iterator s = sl.begin(); s != sl.end(); ++s) {
+
+                       SelectedStripable ss (*s, no_control, g_atomic_int_add (&selection_order, 1));
+
+                       if (_stripables.insert (ss).second) {
+                               DEBUG_TRACE (DEBUG::Selection, string_compose ("set:added %1 to s/c selection\n", (*s)->name()));
+                               send = true;
+                       } else {
+                               DEBUG_TRACE (DEBUG::Selection, string_compose ("%1 already in s/c selection\n", (*s)->name()));
+                       }
+               }
+       }
+
+       if (send || !removed.empty()) {
+
+               send_selection_change ();
+
+               /* send per-object signal to notify interested parties
+                  the selection status has changed
+               */
+
+               PropertyChange pc (Properties::selected);
+
+               for (std::vector<boost::shared_ptr<Stripable> >::iterator s = removed.begin(); s != removed.end(); ++s) {
+                       (*s)->presentation_info().PropertyChanged (pc);
+               }
+
+               for (StripableList::iterator s = sl.begin(); s != sl.end(); ++s) {
+                       (*s)->presentation_info().PropertyChanged (pc);
+               }
+
+       }
+
+}
+
 void
 CoreSelection::add (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
 {