2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "film_editor.h"
22 #include "timeline_time_axis_view.h"
23 #include "timeline_reels_view.h"
24 #include "timeline_labels_view.h"
25 #include "timeline_video_content_view.h"
26 #include "timeline_audio_content_view.h"
27 #include "timeline_subtitle_content_view.h"
28 #include "content_panel.h"
31 #include "lib/playlist.h"
32 #include "lib/image_content.h"
33 #include "lib/timer.h"
34 #include "lib/audio_content.h"
35 #include "lib/subtitle_content.h"
36 #include <wx/graphics.h>
37 #include <boost/weak_ptr.hpp>
38 #include <boost/foreach.hpp>
45 using boost::shared_ptr;
46 using boost::weak_ptr;
47 using boost::dynamic_pointer_cast;
49 using boost::optional;
51 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
52 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
55 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
56 , _reels_view (new TimelineReelsView (*this, 32))
57 , _labels_view (new TimelineLabelsView (*this))
60 , _down_view_position (0)
66 SetDoubleBuffered (true);
69 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
70 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
71 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
72 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
73 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
74 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
76 film_changed (Film::CONTENT);
78 SetMinSize (wxSize (640, tracks() * track_height() + 96));
80 _tracks_position = Position<int> (_labels_view->bbox().width, 8);
82 _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
83 _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
91 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
97 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
99 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
101 /* Find areas of overlap */
102 list<dcpomatic::Rect<int> > overlaps;
103 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
104 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
105 if (!ic || !jc || i == j || ic->track() != jc->track() || !ic->active() || !jc->active()) {
109 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
111 overlaps.push_back (r.get ());
115 i->paint (gc, overlaps);
122 Timeline::film_changed (Film::Property p)
124 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
127 } else if (p == Film::CONTENT_ORDER) {
133 Timeline::recreate_views ()
135 shared_ptr<const Film> film = _film.lock ();
141 _views.push_back (_time_axis_view);
142 _views.push_back (_reels_view);
143 _views.push_back (_labels_view);
145 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
146 if (dynamic_pointer_cast<VideoContent> (i)) {
147 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
150 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (i);
151 if (ac && !ac->audio_mapping().mapped_output_channels().empty ()) {
152 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
155 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (i);
156 if (sc && sc->has_subtitles ()) {
157 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, sc)));
162 setup_pixels_per_second ();
167 Timeline::film_content_changed (int property, bool frequent)
171 if (property == AudioContentProperty::AUDIO_STREAMS) {
173 } else if (!frequent) {
174 setup_pixels_per_second ();
180 Timeline::assign_tracks ()
182 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
183 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
189 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
190 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
195 if (dynamic_pointer_cast<TimelineVideoContentView> (*i)) {
196 /* Video on track 0 */
198 _tracks = max (_tracks, 1);
200 } else if (dynamic_pointer_cast<TimelineSubtitleContentView> (*i)) {
201 /* Subtitles on track 1 */
203 _tracks = max (_tracks, 2);
207 /* Audio on tracks 2 and up */
210 shared_ptr<Content> content = cv->content();
211 DCPTimePeriod content_period (content->position(), content->end());
214 TimelineViewList::iterator j = _views.begin();
215 while (j != _views.end()) {
216 shared_ptr<TimelineAudioContentView> test = dynamic_pointer_cast<TimelineAudioContentView> (*j);
222 shared_ptr<Content> test_content = test->content();
224 if (test && test->track() && test->track().get() == t) {
225 if (content_period.overlaps (DCPTimePeriod(test_content->position(), test_content->end()))) {
226 /* we have an overlap on track `t' */
235 if (j == _views.end ()) {
236 /* no overlap on `t' */
242 _tracks = max (_tracks, t + 1);
245 _time_axis_view->set_y (tracks() * track_height() + 64);
246 _reels_view->set_y (tracks() * track_height() + 32);
250 Timeline::tracks () const
256 Timeline::setup_pixels_per_second ()
258 shared_ptr<const Film> film = _film.lock ();
259 if (!film || film->length() == DCPTime ()) {
263 _pixels_per_second = static_cast<double>(width() - tracks_position().x * 2) / film->length().seconds ();
266 shared_ptr<TimelineView>
267 Timeline::event_to_view (wxMouseEvent& ev)
269 /* Search backwards through views so that we find the uppermost one first */
270 TimelineViewList::reverse_iterator i = _views.rbegin();
271 Position<int> const p (ev.GetX(), ev.GetY());
272 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
273 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
277 if (i == _views.rend ()) {
278 return shared_ptr<TimelineView> ();
285 Timeline::left_down (wxMouseEvent& ev)
287 shared_ptr<TimelineView> view = event_to_view (ev);
288 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
293 _down_view = content_view;
294 _down_view_position = content_view->content()->position ();
297 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
298 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
303 if (!ev.ShiftDown ()) {
304 cv->set_selected (view == *i);
308 if (content_view && ev.ShiftDown ()) {
309 content_view->set_selected (!content_view->selected ());
313 _down_point = ev.GetPosition ();
317 /* Pre-compute the points that we might snap to */
318 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
319 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
320 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
324 _start_snaps.push_back (cv->content()->position());
325 _end_snaps.push_back (cv->content()->position());
326 _start_snaps.push_back (cv->content()->end());
327 _end_snaps.push_back (cv->content()->end());
329 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
330 _start_snaps.push_back (i);
334 /* Tell everyone that things might change frequently during the drag */
335 _down_view->content()->set_change_signals_frequent (true);
340 Timeline::left_up (wxMouseEvent& ev)
345 _down_view->content()->set_change_signals_frequent (false);
346 _content_panel->set_selection (_down_view->content ());
349 set_position_from_event (ev);
351 /* Clear up up the stuff we don't do during drag */
353 setup_pixels_per_second ();
356 _start_snaps.clear ();
361 Timeline::mouse_moved (wxMouseEvent& ev)
367 set_position_from_event (ev);
371 Timeline::right_down (wxMouseEvent& ev)
373 shared_ptr<TimelineView> view = event_to_view (ev);
374 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
379 if (!cv->selected ()) {
381 cv->set_selected (true);
384 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
388 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
390 DCPTime const d = a - b;
391 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
392 nearest_distance = d;
397 Timeline::set_position_from_event (wxMouseEvent& ev)
399 if (!_pixels_per_second) {
403 double const pps = _pixels_per_second.get ();
405 wxPoint const p = ev.GetPosition();
408 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
409 before the drag is considered to have started.
411 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
422 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
426 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
427 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
430 optional<DCPTime> nearest_distance;
432 /* Find the nearest snap point */
434 BOOST_FOREACH (DCPTime i, _start_snaps) {
435 maybe_snap (i, new_position, nearest_distance);
438 BOOST_FOREACH (DCPTime i, _end_snaps) {
439 maybe_snap (i, new_end, nearest_distance);
442 if (nearest_distance) {
443 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
444 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
445 new_position += nearest_distance.get ();
450 if (new_position < DCPTime ()) {
451 new_position = DCPTime ();
454 _down_view->content()->set_position (new_position);
456 shared_ptr<Film> film = _film.lock ();
457 DCPOMATIC_ASSERT (film);
458 film->set_sequence (false);
462 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
464 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
467 shared_ptr<const Film>
468 Timeline::film () const
470 return _film.lock ();
476 setup_pixels_per_second ();
480 Timeline::clear_selection ()
482 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
483 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
485 cv->set_selected (false);
490 TimelineContentViewList
491 Timeline::selected_views () const
493 TimelineContentViewList sel;
495 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
496 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
497 if (cv && cv->selected()) {
506 Timeline::selected_content () const
509 TimelineContentViewList views = selected_views ();
511 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
512 sel.push_back ((*i)->content ());
519 Timeline::set_selection (ContentList selection)
521 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
522 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
524 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());