Lua bindings to access editor selection + region selection bindings
authorRobin Gareus <robin@gareus.org>
Thu, 23 Feb 2017 21:31:03 +0000 (22:31 +0100)
committerRobin Gareus <robin@gareus.org>
Thu, 23 Feb 2017 21:32:32 +0000 (22:32 +0100)
gtk2_ardour/editor.cc
gtk2_ardour/editor.h
gtk2_ardour/editor_selection.cc
gtk2_ardour/luainstance.cc
gtk2_ardour/public_editor.h
scripts/select_every_2nd_region.lua [new file with mode: 0644]

index bb769e30ef9251ffd309921374f688b31b178184..ba07222bdf8d694aaa0e4f9302a5e7dc7e01ebf3 100644 (file)
@@ -5087,6 +5087,24 @@ Editor::get_regions_corresponding_to (boost::shared_ptr<Region> region, vector<R
        }
 }
 
+RegionView*
+Editor::get_regionview_from_region (boost::shared_ptr<Region> region) const
+{
+       for (TrackViewList::const_iterator i = track_views.begin(); i != track_views.end(); ++i) {
+               RouteTimeAxisView* tatv;
+               if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
+                       if (!tatv->track()) {
+                               continue;
+                       }
+                       RegionView* marv = tatv->view()->find_view (region);
+                       if (marv) {
+                               return marv;
+                       }
+               }
+       }
+       return NULL;
+}
+
 void
 Editor::show_rhythm_ferret ()
 {
index 458753d638a891da9461b210cdcd4d1578ef15e3..01ef86874eee4f8a6b79832c13fa394e51118d34 100644 (file)
@@ -257,6 +257,8 @@ class Editor : public PublicEditor, public PBD::ScopedConnectionList, public ARD
        bool get_selection_extents (framepos_t &start, framepos_t &end) const;  // the time extents of the current selection, whether Range, Region(s), Control Points, or Notes
        Selection& get_cut_buffer() const { return *cut_buffer; }
 
+       void set_selection (std::list<Selectable*>, Selection::Operation);
+
        bool extend_selection_to_track (TimeAxisView&);
 
        void play_selection ();
@@ -732,6 +734,8 @@ class Editor : public PublicEditor, public PBD::ScopedConnectionList, public ARD
 
        void get_equivalent_regions (RegionView* rv, std::vector<RegionView*> &, PBD::PropertyID) const;
        RegionSelection get_equivalent_regions (RegionSelection &, PBD::PropertyID) const;
+       RegionView* get_regionview_from_region (boost::shared_ptr<ARDOUR::Region>) const;
+
        void mapover_tracks (sigc::slot<void,RouteTimeAxisView&,uint32_t> sl, TimeAxisView*, PBD::PropertyID) const;
        void mapover_tracks_with_unique_playlists (sigc::slot<void,RouteTimeAxisView&,uint32_t> sl, TimeAxisView*, PBD::PropertyID) const;
 
index d214d48a320b42127c3a8c761276e270b451fe33..8c6b16f51d02c11bfa2362e8b2500a8cf083c6cd 100644 (file)
@@ -893,6 +893,30 @@ out:
        return commit;
 }
 
+void
+Editor::set_selection (std::list<Selectable*> s, Selection::Operation op)
+{
+       if (s.empty()) {
+               return;
+       }
+       begin_reversible_selection_op (X_("set selection"));
+       switch (op) {
+               case Selection::Toggle:
+                       selection->toggle (s);
+                       break;
+               case Selection::Set:
+                       selection->set (s);
+                       break;
+               case Selection::Extend:
+                       selection->add (s);
+                       break;
+               case Selection::Add:
+                       selection->add (s);
+                       break;
+       }
+
+       commit_reversible_selection_op () ;
+}
 
 void
 Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> region, Selection::Operation op)
index 1a8bf12d62c8290f8e6379eb2f35a4571eb77f81..c8a4fe9e3b09f9033d1f7e72fee18555d6d85acd 100644 (file)
 #include "luainstance.h"
 #include "luasignal.h"
 #include "marker.h"
+#include "region_view.h"
 #include "processor_box.h"
 #include "time_axis_view.h"
+#include "time_axis_view_item.h"
 #include "selection.h"
 #include "script_selector.h"
 #include "timers.h"
@@ -582,8 +584,17 @@ LuaInstance::register_classes (lua_State* L)
                .endClass ()
 #endif
 
+               .beginClass <Selectable> ("Selectable")
+               .endClass ()
+               .deriveClass <TimeAxisViewItem, Selectable> ("TimeAxisViewItem")
+               .endClass ()
+               .deriveClass <RegionView, TimeAxisViewItem> ("RegionView")
+               .endClass ()
+
+               .beginStdCPtrList <Selectable> ("SelectionList")
+               .endClass ()
+
                .beginClass <RegionSelection> ("RegionSelection")
-               .addFunction ("clear_all", &RegionSelection::clear_all)
                .addFunction ("start", &RegionSelection::start)
                .addFunction ("end_frame", &RegionSelection::end_frame)
                .addFunction ("n_midi_regions", &RegionSelection::n_midi_regions)
@@ -646,6 +657,8 @@ LuaInstance::register_classes (lua_State* L)
                .addFunction ("get_cut_buffer", &PublicEditor::get_cut_buffer)
                .addRefFunction ("get_selection_extents", &PublicEditor::get_selection_extents)
 
+               .addFunction ("set_selection", &PublicEditor::set_selection)
+
                .addFunction ("play_selection", &PublicEditor::play_selection)
                .addFunction ("play_with_preroll", &PublicEditor::play_with_preroll)
                .addFunction ("maybe_locate_with_edit_preroll", &PublicEditor::maybe_locate_with_edit_preroll)
@@ -691,6 +704,8 @@ LuaInstance::register_classes (lua_State* L)
                .addFunction ("set_selected_mixer_strip", &PublicEditor::set_selected_mixer_strip)
                .addFunction ("hide_track_in_display", &PublicEditor::hide_track_in_display)
 #endif
+
+               .addFunction ("get_regionview_from_region", &PublicEditor::get_regionview_from_region)
                .addFunction ("set_stationary_playhead", &PublicEditor::set_stationary_playhead)
                .addFunction ("stationary_playhead", &PublicEditor::stationary_playhead)
                .addFunction ("set_follow_playhead", &PublicEditor::set_follow_playhead)
@@ -783,6 +798,13 @@ LuaInstance::register_classes (lua_State* L)
                .addConst ("PunchOut", ArdourMarker::Type(ArdourMarker::PunchOut))
                .endNamespace ()
 
+               .beginNamespace ("SelectionOp")
+               .addConst ("Toggle", Selection::Operation(Selection::Toggle))
+               .addConst ("Set", Selection::Operation(Selection::Set))
+               .addConst ("Extend", Selection::Operation(Selection::Extend))
+               .addConst ("Add", Selection::Operation(Selection::Add))
+               .endNamespace ()
+
                .endNamespace (); // end ArdourUI
 
        // Editing Symbols
index 5cfedb11f14e7c47d4dfe02aadcfd753e956cd4f..62cb9f2bcb14b4c3b86458c3b26dd36ac3bf2806 100644 (file)
@@ -204,9 +204,13 @@ class PublicEditor : public Gtkmm2ext::Tabbable {
        virtual framepos_t playhead_cursor_sample () const = 0;
        virtual double sample_to_pixel (framepos_t frame) const = 0;
        virtual double sample_to_pixel_unrounded (framepos_t frame) const = 0;
+
        virtual Selection& get_selection () const = 0;
        virtual bool get_selection_extents (framepos_t &start, framepos_t &end) const = 0;
        virtual Selection& get_cut_buffer () const = 0;
+
+       virtual void set_selection (std::list<Selectable*>, Selection::Operation) = 0;
+
        virtual bool extend_selection_to_track (TimeAxisView&) = 0;
        virtual void play_selection () = 0;
        virtual void play_with_preroll () = 0;
@@ -348,6 +352,7 @@ class PublicEditor : public Gtkmm2ext::Tabbable {
        virtual RouteTimeAxisView* get_route_view_by_route_id (const PBD::ID& id) const = 0;
 
        virtual void get_equivalent_regions (RegionView* rv, std::vector<RegionView*>&, PBD::PropertyID) const = 0;
+       virtual RegionView* get_regionview_from_region (boost::shared_ptr<ARDOUR::Region>) const = 0;
 
        sigc::signal<void> ZoomChanged;
        sigc::signal<void> Realized;
diff --git a/scripts/select_every_2nd_region.lua b/scripts/select_every_2nd_region.lua
new file mode 100644 (file)
index 0000000..4e6d156
--- /dev/null
@@ -0,0 +1,42 @@
+ardour {
+       ["type"]    = "EditorAction",
+       name        = "Region Select/2",
+       license     = "MIT",
+       author      = "Ardour Team",
+       description = [[select every 2nd region on all selected tracks]]
+}
+
+-- select every 2nd region on all selected tracks
+function factory () return function ()
+
+       local sl = ArdourUI.SelectionList () -- empty selection list
+
+       local sel = Editor:get_selection () -- get current selection
+       -- for each selected track/bus..
+       for route in sel.tracks:routelist ():iter () do
+               -- consider only tracks
+               local track = route:to_track ()
+               if track:isnil() then
+                       goto continue
+               end
+
+               local skip = false;
+               -- iterate over all regions of the given track
+               for region in track:playlist():region_list():iter() do
+                       if skip then
+                               -- skip every 2nd region
+                               skip = false;
+                       else
+                               skip = true;
+                               -- get RegionView (GUI object to be selected)
+                               local rv = Editor:get_regionview_from_region (region)
+                               -- add it to the list of Objects to be selected
+                               sl:push_back (rv);
+                       end
+               end
+               ::continue::
+       end
+
+       -- set/replace current selection in the editor
+       Editor:set_selection (sl, ArdourUI.SelectionOp.Set);
+end end