2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "film_editor.h"
23 #include "timeline_time_axis_view.h"
24 #include "timeline_reels_view.h"
25 #include "timeline_labels_view.h"
26 #include "timeline_video_content_view.h"
27 #include "timeline_audio_content_view.h"
28 #include "timeline_subtitle_content_view.h"
29 #include "timeline_atmos_content_view.h"
30 #include "content_panel.h"
33 #include "lib/playlist.h"
34 #include "lib/image_content.h"
35 #include "lib/timer.h"
36 #include "lib/audio_content.h"
37 #include "lib/subtitle_content.h"
38 #include "lib/video_content.h"
39 #include "lib/atmos_mxf_content.h"
40 #include <wx/graphics.h>
41 #include <boost/weak_ptr.hpp>
42 #include <boost/foreach.hpp>
49 using boost::shared_ptr;
50 using boost::weak_ptr;
51 using boost::dynamic_pointer_cast;
53 using boost::optional;
55 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
56 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
59 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
60 , _reels_view (new TimelineReelsView (*this, 32))
61 , _labels_view (new TimelineLabelsView (*this))
64 , _down_view_position (0)
70 SetDoubleBuffered (true);
73 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
74 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
75 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
76 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
77 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
78 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
80 film_changed (Film::CONTENT);
82 SetMinSize (wxSize (640, tracks() * track_height() + 96));
84 _tracks_position = Position<int> (_labels_view->bbox().width, 32);
86 _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
87 _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
95 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
100 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
102 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
104 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
106 /* Find areas of overlap */
107 list<dcpomatic::Rect<int> > overlaps;
108 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
109 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
110 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
111 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
115 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
117 overlaps.push_back (r.get ());
121 i->paint (gc, overlaps);
128 Timeline::film_changed (Film::Property p)
130 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
133 } else if (p == Film::CONTENT_ORDER) {
139 Timeline::recreate_views ()
141 shared_ptr<const Film> film = _film.lock ();
147 _views.push_back (_time_axis_view);
148 _views.push_back (_reels_view);
149 _views.push_back (_labels_view);
151 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
153 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
156 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
157 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
161 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
164 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
165 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
170 setup_pixels_per_second ();
175 Timeline::film_content_changed (int property, bool frequent)
179 if (property == AudioContentProperty::STREAMS) {
181 } else if (!frequent) {
182 setup_pixels_per_second ();
189 place (TimelineViewList& views, int& tracks)
191 int const base = tracks;
193 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
194 if (!dynamic_pointer_cast<T>(i)) {
198 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
202 shared_ptr<Content> content = cv->content();
203 DCPTimePeriod const content_period (content->position(), content->end());
206 TimelineViewList::iterator j = views.begin();
207 while (j != views.end()) {
208 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
214 shared_ptr<Content> test_content = test->content();
216 test->track() && test->track().get() == t &&
217 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
218 /* we have an overlap on track `t' */
226 if (j == views.end ()) {
227 /* no overlap on `t' */
233 tracks = max (tracks, t + 1);
236 return tracks - base;
240 Timeline::assign_tracks ()
243 Video (mono or left-eye)
256 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
257 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
265 bool have_3d = false;
266 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
267 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
272 /* Video on tracks 0 and maybe 1 (left and right eye) */
273 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
275 _tracks = max (_tracks, 2);
282 _tracks = max (_tracks, 1);
286 int const subtitle_tracks = place<TimelineSubtitleContentView> (_views, _tracks);
290 bool have_atmos = false;
291 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
292 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
296 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
297 cv->set_track (_tracks - 1);
308 place<TimelineAudioContentView> (_views, _tracks);
310 _labels_view->set_3d (have_3d);
311 _labels_view->set_subtitle_tracks (subtitle_tracks);
312 _labels_view->set_atmos (have_atmos);
314 _time_axis_view->set_y (tracks() * track_height() + 64);
315 _reels_view->set_y (8);
319 Timeline::tracks () const
325 Timeline::setup_pixels_per_second ()
327 shared_ptr<const Film> film = _film.lock ();
328 if (!film || film->length() == DCPTime ()) {
332 _pixels_per_second = static_cast<double>(width() - tracks_position().x * 2) / film->length().seconds ();
335 shared_ptr<TimelineView>
336 Timeline::event_to_view (wxMouseEvent& ev)
338 /* Search backwards through views so that we find the uppermost one first */
339 TimelineViewList::reverse_iterator i = _views.rbegin();
340 Position<int> const p (ev.GetX(), ev.GetY());
341 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
342 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
346 if (i == _views.rend ()) {
347 return shared_ptr<TimelineView> ();
354 Timeline::left_down (wxMouseEvent& ev)
356 shared_ptr<TimelineView> view = event_to_view (ev);
357 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
362 _down_view = content_view;
363 _down_view_position = content_view->content()->position ();
366 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
367 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
372 if (!ev.ShiftDown ()) {
373 cv->set_selected (view == *i);
377 if (content_view && ev.ShiftDown ()) {
378 content_view->set_selected (!content_view->selected ());
382 _down_point = ev.GetPosition ();
386 /* Pre-compute the points that we might snap to */
387 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
388 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
389 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
393 _start_snaps.push_back (cv->content()->position());
394 _end_snaps.push_back (cv->content()->position());
395 _start_snaps.push_back (cv->content()->end());
396 _end_snaps.push_back (cv->content()->end());
398 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
399 _start_snaps.push_back (i);
403 /* Tell everyone that things might change frequently during the drag */
404 _down_view->content()->set_change_signals_frequent (true);
409 Timeline::left_up (wxMouseEvent& ev)
414 _down_view->content()->set_change_signals_frequent (false);
417 _content_panel->set_selection (selected_content ());
418 set_position_from_event (ev);
420 /* Clear up up the stuff we don't do during drag */
422 setup_pixels_per_second ();
425 _start_snaps.clear ();
430 Timeline::mouse_moved (wxMouseEvent& ev)
436 set_position_from_event (ev);
440 Timeline::right_down (wxMouseEvent& ev)
442 shared_ptr<TimelineView> view = event_to_view (ev);
443 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
448 if (!cv->selected ()) {
450 cv->set_selected (true);
453 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
457 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
459 DCPTime const d = a - b;
460 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
461 nearest_distance = d;
466 Timeline::set_position_from_event (wxMouseEvent& ev)
468 if (!_pixels_per_second) {
472 double const pps = _pixels_per_second.get ();
474 wxPoint const p = ev.GetPosition();
477 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
478 before the drag is considered to have started.
480 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
491 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
495 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
496 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
499 optional<DCPTime> nearest_distance;
501 /* Find the nearest snap point */
503 BOOST_FOREACH (DCPTime i, _start_snaps) {
504 maybe_snap (i, new_position, nearest_distance);
507 BOOST_FOREACH (DCPTime i, _end_snaps) {
508 maybe_snap (i, new_end, nearest_distance);
511 if (nearest_distance) {
512 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
513 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
514 new_position += nearest_distance.get ();
519 if (new_position < DCPTime ()) {
520 new_position = DCPTime ();
523 _down_view->content()->set_position (new_position);
525 shared_ptr<Film> film = _film.lock ();
526 DCPOMATIC_ASSERT (film);
527 film->set_sequence (false);
531 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
533 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
536 shared_ptr<const Film>
537 Timeline::film () const
539 return _film.lock ();
545 setup_pixels_per_second ();
549 Timeline::clear_selection ()
551 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
552 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
554 cv->set_selected (false);
559 TimelineContentViewList
560 Timeline::selected_views () const
562 TimelineContentViewList sel;
564 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
565 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
566 if (cv && cv->selected()) {
575 Timeline::selected_content () const
578 TimelineContentViewList views = selected_views ();
580 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
581 sel.push_back ((*i)->content ());
588 Timeline::set_selection (ContentList selection)
590 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
591 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
593 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());