fix duplicating multiple selected regions - fixes #6202
[ardour.git] / libs / ardour / playlist.cc
index 77666977fc279687b16bed36a83712ab6e73b7a6..868be8da193d898f44565d77873175bfab8ca7b4 100644 (file)
@@ -557,6 +557,7 @@ Playlist::notify_region_added (boost::shared_ptr<Region> r)
                pending_contents_change = false;
                RegionAdded (boost::weak_ptr<Region> (r)); /* EMIT SIGNAL */
                ContentsChanged (); /* EMIT SIGNAL */
+               
        }
 }
 
@@ -611,37 +612,41 @@ Playlist::flush_notifications (bool from_undo)
                */
        }
 
-       if (((regions_changed || pending_contents_change) && !in_set_state) || pending_layering) {
+       /* notify about contents/region changes first so that layering changes
+        * in a UI will take place on the new contents.
+        */
+
+       if (regions_changed || pending_contents_change) {
+               pending_layering = true;
+               ContentsChanged (); /* EMIT SIGNAL */
+       }
+       
+       for (s = pending_adds.begin(); s != pending_adds.end(); ++s) {
+               (*s)->clear_changes ();
+               RegionAdded (boost::weak_ptr<Region> (*s)); /* EMIT SIGNAL */
+       }
+       
+       if ((regions_changed && !in_set_state) || pending_layering) {
                relayer ();
        }
+       
+       coalesce_and_check_crossfades (crossfade_ranges);
+       
+       if (!pending_range_moves.empty ()) {
+               /* We don't need to check crossfades for these as pending_bounds has
+                  already covered it.
+               */
+               RangesMoved (pending_range_moves, from_undo);
+       }
+       
+       if (!pending_region_extensions.empty ()) {
+               RegionsExtended (pending_region_extensions);
+       }
 
-        if (regions_changed || pending_contents_change) {
-                pending_contents_change = false;
-                ContentsChanged (); /* EMIT SIGNAL */
-        }
-
-        for (s = pending_adds.begin(); s != pending_adds.end(); ++s) {
-                (*s)->clear_changes ();
-                RegionAdded (boost::weak_ptr<Region> (*s)); /* EMIT SIGNAL */
-        }
-
-        coalesce_and_check_crossfades (crossfade_ranges);
-
-        if (!pending_range_moves.empty ()) {
-                /* We don't need to check crossfades for these as pending_bounds has
-                   already covered it.
-                */
-                RangesMoved (pending_range_moves, from_undo);
-        }
-
-        if (!pending_region_extensions.empty ()) {
-                RegionsExtended (pending_region_extensions);
-        }
-
-        clear_pending ();
+       clear_pending ();
 
-        in_flush = false;
- }
+       in_flush = false;
+}
 
  void
  Playlist::clear_pending ()
@@ -1239,6 +1244,13 @@ Playlist::flush_notifications (bool from_undo)
 
  void
  Playlist::duplicate (boost::shared_ptr<Region> region, framepos_t position, float times)
+ {
+        duplicate(region, position, region->length(), times);
+ }
+
+/** @param gap from the beginning of the region to the next beginning */
+ void
+ Playlist::duplicate (boost::shared_ptr<Region> region, framepos_t position, framecnt_t gap, float times)
  {
         times = fabs (times);
 
@@ -1250,7 +1262,7 @@ Playlist::flush_notifications (bool from_undo)
                 boost::shared_ptr<Region> copy = RegionFactory::create (region, true);
                 add_region_internal (copy, pos);
                 set_layer (copy, DBL_MAX);
-                pos += region->length();
+                pos += gap;
         }
 
         if (floor (times) != times) {
@@ -1637,6 +1649,8 @@ Playlist::region_bounds_changed (const PropertyChange& what_changed, boost::shar
                 save = true;
         }
 
+        mark_session_dirty ();
+     
         return save;
  }
 
@@ -2328,7 +2342,10 @@ struct RelayerSort {
 void
 Playlist::set_layer (boost::shared_ptr<Region> region, double new_layer)
 {
-       /* Remove the layer we are setting from our region list, and sort it */
+       /* Remove the layer we are setting from our region list, and sort it
+       *  using the layer indeces.
+       */
+
        RegionList copy = regions.rlist();
        copy.remove (region);
        copy.sort (RelayerSort ());
@@ -2351,13 +2368,18 @@ void
 Playlist::setup_layering_indices (RegionList const & regions)
 {
        uint64_t j = 0;
-       list<Evoral::Range<framepos_t> > xf;
 
        for (RegionList::const_iterator k = regions.begin(); k != regions.end(); ++k) {
                (*k)->set_layering_index (j++);
        }
 }
 
+struct LaterHigherSort {
+       bool operator () (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
+               return a->position() < b->position();
+       }
+};
+
 /** Take the layering indices of each of our regions, compute the layers
  *  that they should be on, and write the layers back to the regions.
  */
@@ -2392,9 +2414,16 @@ Playlist::relayer ()
        vector<vector<RegionList> > layers;
        layers.push_back (vector<RegionList> (divisions));
 
-       /* Sort our regions into layering index order */
+       /* Sort our regions into layering index order (for manual layering) or position order (for later is higher)*/
        RegionList copy = regions.rlist();
-       copy.sort (RelayerSort ());
+       switch (Config->get_layer_model()) {
+               case LaterHigher:
+                       copy.sort (LaterHigherSort ());
+                       break;
+               case Manual:
+                       copy.sort (RelayerSort ());
+                       break;
+       }
 
        DEBUG_TRACE (DEBUG::Layering, "relayer() using:\n");
        for (RegionList::iterator i = copy.begin(); i != copy.end(); ++i) {
@@ -2596,6 +2625,29 @@ Playlist::region_use_count (boost::shared_ptr<Region> r) const
                }
        }
 
+       RegionFactory::CompoundAssociations& cassocs (RegionFactory::compound_associations());
+       for (RegionFactory::CompoundAssociations::iterator it = cassocs.begin(); it != cassocs.end(); ++it) {
+               /* check if region is used in a compound */
+               if (it->second == r) {
+                       /* region is referenced as 'original' of a compound */
+                       ++cnt;
+                       break;
+               }
+               if (r->whole_file() && r->max_source_level() > 0) {
+                       /* region itself ia a compound.
+                        * the compound regions are not referenced -> check regions inside compound
+                        */
+                       const SourceList& sl = r->sources();
+                       for (SourceList::const_iterator s = sl.begin(); s != sl.end(); ++s) {
+                               boost::shared_ptr<PlaylistSource> ps = boost::dynamic_pointer_cast<PlaylistSource>(*s);
+                               if (!ps) continue;
+                               if (ps->playlist()->region_use_count(it->first)) {
+                                       // break out of both loops
+                                       return ++cnt;
+                               }
+                       }
+               }
+       }
        return cnt;
 }
 
@@ -3038,6 +3090,7 @@ Playlist::uncombine (boost::shared_ptr<Region> target)
                }
 
                boost::shared_ptr<Region> original (ca->second);
+               cassocs.erase(ca);
                bool modified_region;
 
                if (i == rl.begin()) {
@@ -3135,6 +3188,9 @@ Playlist::uncombine (boost::shared_ptr<Region> target)
        for (vector<boost::shared_ptr<Region> >::iterator i = originals.begin(); i != originals.end(); ++i) {
                add_region ((*i), (*i)->position());
                set_layer((*i), (*i)->layer());
+               if (!RegionFactory::region_by_id((*i)->id())) {
+                       RegionFactory::map_add(*i);
+               }
        }
 
        in_partition = false;
@@ -3190,6 +3246,7 @@ restart:
                                continue;
                        }
 
+                       // XXX i->from can be > i->to - is this right? coverage() will return OverlapNone in this case
                        if (Evoral::coverage (i->from, i->to, j->from, j->to) != Evoral::OverlapNone) {
                                i->from = min (i->from, j->from);
                                i->to = max (i->to, j->to);