2 Copyright (C) 2013-2018 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>
53 using boost::shared_ptr;
54 using boost::weak_ptr;
55 using boost::dynamic_pointer_cast;
57 using boost::optional;
59 /* 3 hours in 640 pixels */
60 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
61 int const Timeline::_minimum_pixels_per_track = 16;
63 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
64 : wxPanel (parent, wxID_ANY)
65 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
66 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
69 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
70 , _reels_view (new TimelineReelsView (*this, 32))
71 , _labels_view (new TimelineLabelsView (*this))
74 , _down_view_position (0)
81 , _pixels_per_track (48)
82 , _first_resize (true)
85 _labels_canvas->SetDoubleBuffered (true);
86 _main_canvas->SetDoubleBuffered (true);
89 wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
90 sizer->Add (_labels_canvas, 0, wxEXPAND);
91 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
92 sizer->Add (_main_canvas, 1, wxEXPAND);
95 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
96 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
97 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
98 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
99 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
100 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
101 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
102 _main_canvas->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&Timeline::scrolled, this, _1));
103 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&Timeline::scrolled, this, _1));
104 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&Timeline::scrolled, this, _1));
105 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&Timeline::scrolled, this, _1));
106 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&Timeline::scrolled, this, _1));
107 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&Timeline::scrolled, this, _1));
108 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
110 film_changed (Film::CONTENT);
112 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
114 _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
115 _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
118 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
122 Timeline::set_pixels_per_second (double pps)
124 _pixels_per_second = max (_minimum_pixels_per_second, pps);
128 Timeline::paint_labels ()
130 wxPaintDC dc (_labels_canvas);
132 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
138 _labels_canvas->GetViewStart (&vsx, &vsy);
139 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
141 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
147 Timeline::paint_main ()
149 wxPaintDC dc (_main_canvas);
150 _main_canvas->DoPrepareDC (dc);
152 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
158 _main_canvas->GetViewStart (&vsx, &vsy);
159 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
161 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
163 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
165 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
167 /* Find areas of overlap with other content views, so that we can plot them */
168 list<dcpomatic::Rect<int> > overlaps;
169 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
170 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
171 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
172 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
176 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
178 overlaps.push_back (r.get ());
182 i->paint (gc, overlaps);
186 /* Translate back as _down_point and _zoom_point do not take scroll into account */
187 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
188 gc->SetPen (*wxBLACK_PEN);
189 gc->SetBrush (*wxTRANSPARENT_BRUSH);
191 min (_down_point.x, _zoom_point->x),
192 min (_down_point.y, _zoom_point->y),
193 fabs (_down_point.x - _zoom_point->x),
194 fabs (_down_point.y - _zoom_point->y)
202 Timeline::film_changed (Film::Property p)
204 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
207 } else if (p == Film::CONTENT_ORDER) {
213 Timeline::recreate_views ()
215 shared_ptr<const Film> film = _film.lock ();
221 _views.push_back (_time_axis_view);
222 _views.push_back (_reels_view);
224 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
226 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
229 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
230 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
234 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
237 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
238 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
248 Timeline::film_content_changed (int property, bool frequent)
252 if (property == AudioContentProperty::STREAMS) {
254 } else if (!frequent) {
262 place (TimelineViewList& views, int& tracks)
264 int const base = tracks;
266 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
267 if (!dynamic_pointer_cast<T>(i)) {
271 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
275 shared_ptr<Content> content = cv->content();
276 DCPTimePeriod const content_period (content->position(), content->end());
279 TimelineViewList::iterator j = views.begin();
280 while (j != views.end()) {
281 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
287 shared_ptr<Content> test_content = test->content();
289 test->track() && test->track().get() == t &&
290 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
291 /* we have an overlap on track `t' */
299 if (j == views.end ()) {
300 /* no overlap on `t' */
306 tracks = max (tracks, t + 1);
309 return tracks - base;
312 /** Compare the mapped output channels of two TimelineViews, so we can into
313 * order of first mapped DCP channel.
315 struct AudioMappingComparator {
316 bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
318 shared_ptr<TimelineAudioContentView> cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
320 list<int> oc = cva->content()->audio->mapping().mapped_output_channels();
321 la = *min_element(begin(oc), end(oc));
324 shared_ptr<TimelineAudioContentView> cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
326 list<int> oc = cvb->content()->audio->mapping().mapped_output_channels();
327 lb = *min_element(begin(oc), end(oc));
334 Timeline::assign_tracks ()
337 Video (mono or left-eye)
350 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
351 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
359 bool have_3d = false;
360 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
361 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
366 /* Video on tracks 0 and maybe 1 (left and right eye) */
367 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
369 _tracks = max (_tracks, 2);
376 _tracks = max (_tracks, 1);
380 int const subtitle_tracks = place<TimelineSubtitleContentView> (_views, _tracks);
384 bool have_atmos = false;
385 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
386 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
390 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
391 cv->set_track (_tracks - 1);
400 /* Audio. We're sorting the views so that we get the audio views in order of increasing
404 TimelineViewList views = _views;
405 sort(views.begin(), views.end(), AudioMappingComparator());
406 int const audio_tracks = place<TimelineAudioContentView> (views, _tracks);
408 _labels_view->set_3d (have_3d);
409 _labels_view->set_audio_tracks (audio_tracks);
410 _labels_view->set_subtitle_tracks (subtitle_tracks);
411 _labels_view->set_atmos (have_atmos);
413 _time_axis_view->set_y (tracks());
414 _reels_view->set_y (8);
418 Timeline::tracks () const
424 Timeline::setup_scrollbars ()
426 shared_ptr<const Film> film = _film.lock ();
427 if (!film || !_pixels_per_second) {
431 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
433 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
434 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
435 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
436 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
439 shared_ptr<TimelineView>
440 Timeline::event_to_view (wxMouseEvent& ev)
442 /* Search backwards through views so that we find the uppermost one first */
443 TimelineViewList::reverse_iterator i = _views.rbegin();
444 Position<int> const p (ev.GetX(), ev.GetY());
445 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
446 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
450 if (i == _views.rend ()) {
451 return shared_ptr<TimelineView> ();
458 Timeline::left_down (wxMouseEvent& ev)
461 _down_point = ev.GetPosition ();
465 left_down_select (ev);
477 Timeline::left_down_select (wxMouseEvent& ev)
479 shared_ptr<TimelineView> view = event_to_view (ev);
480 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
485 _down_view = content_view;
486 _down_view_position = content_view->content()->position ();
489 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
490 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
495 if (!ev.ShiftDown ()) {
496 cv->set_selected (view == *i);
500 if (content_view && ev.ShiftDown ()) {
501 content_view->set_selected (!content_view->selected ());
507 /* Pre-compute the points that we might snap to */
508 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
509 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
510 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
514 _start_snaps.push_back (cv->content()->position());
515 _end_snaps.push_back (cv->content()->position());
516 _start_snaps.push_back (cv->content()->end());
517 _end_snaps.push_back (cv->content()->end());
519 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
520 _start_snaps.push_back (i);
524 /* Tell everyone that things might change frequently during the drag */
525 _down_view->content()->set_change_signals_frequent (true);
530 Timeline::left_up (wxMouseEvent& ev)
549 Timeline::left_up_select (wxMouseEvent& ev)
552 _down_view->content()->set_change_signals_frequent (false);
555 _content_panel->set_selection (selected_content ());
556 set_position_from_event (ev);
558 /* Clear up up the stuff we don't do during drag */
563 _start_snaps.clear ();
568 Timeline::left_up_zoom (wxMouseEvent& ev)
570 _zoom_point = ev.GetPosition ();
573 _main_canvas->GetViewStart (&vsx, &vsy);
575 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
576 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
578 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
579 /* Very small zoom rectangle: we assume it wasn't intentional */
583 DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
584 DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
585 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
587 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
588 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
589 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
592 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
593 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
594 _labels_canvas->Scroll (0, y);
596 _zoom_point = optional<wxPoint> ();
601 Timeline::set_pixels_per_track (int h)
603 _pixels_per_track = max(_minimum_pixels_per_track, h);
607 Timeline::mouse_moved (wxMouseEvent& ev)
611 mouse_moved_select (ev);
614 mouse_moved_zoom (ev);
624 Timeline::mouse_moved_select (wxMouseEvent& ev)
630 set_position_from_event (ev);
634 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
640 _zoom_point = ev.GetPosition ();
645 Timeline::right_down (wxMouseEvent& ev)
649 right_down_select (ev);
653 set_pixels_per_second (*_pixels_per_second / 2);
654 set_pixels_per_track (_pixels_per_track / 2);
666 Timeline::right_down_select (wxMouseEvent& ev)
668 shared_ptr<TimelineView> view = event_to_view (ev);
669 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
674 if (!cv->selected ()) {
676 cv->set_selected (true);
679 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
683 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
685 DCPTime const d = a - b;
686 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
687 nearest_distance = d;
692 Timeline::set_position_from_event (wxMouseEvent& ev)
694 if (!_pixels_per_second) {
698 double const pps = _pixels_per_second.get ();
700 wxPoint const p = ev.GetPosition();
703 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
704 before the drag is considered to have started.
706 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
717 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
721 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
722 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
725 optional<DCPTime> nearest_distance;
727 /* Find the nearest snap point */
729 BOOST_FOREACH (DCPTime i, _start_snaps) {
730 maybe_snap (i, new_position, nearest_distance);
733 BOOST_FOREACH (DCPTime i, _end_snaps) {
734 maybe_snap (i, new_end, nearest_distance);
737 if (nearest_distance) {
738 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
739 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
740 new_position += nearest_distance.get ();
745 if (new_position < DCPTime ()) {
746 new_position = DCPTime ();
749 _down_view->content()->set_position (new_position);
751 shared_ptr<Film> film = _film.lock ();
752 DCPOMATIC_ASSERT (film);
753 film->set_sequence (false);
757 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
759 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
762 shared_ptr<const Film>
763 Timeline::film () const
765 return _film.lock ();
771 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
773 _first_resize = false;
779 Timeline::clear_selection ()
781 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
782 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
784 cv->set_selected (false);
789 TimelineContentViewList
790 Timeline::selected_views () const
792 TimelineContentViewList sel;
794 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
795 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
796 if (cv && cv->selected()) {
805 Timeline::selected_content () const
808 TimelineContentViewList views = selected_views ();
810 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
811 sel.push_back ((*i)->content ());
818 Timeline::set_selection (ContentList selection)
820 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
821 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
823 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
829 Timeline::tracks_y_offset () const
831 return _reels_view->bbox().height + 4;
835 Timeline::width () const
837 return _main_canvas->GetVirtualSize().GetWidth();
841 Timeline::scrolled (wxScrollWinEvent& ev)
843 if (ev.GetOrientation() == wxVERTICAL) {
845 _main_canvas->GetViewStart (&x, &y);
846 _labels_canvas->Scroll (0, y);
852 Timeline::tool_clicked (Tool t)
869 Timeline::zoom_all ()
871 shared_ptr<Film> film = _film.lock ();
872 DCPOMATIC_ASSERT (film);
873 set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
874 set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
876 _main_canvas->Scroll (0, 0);
877 _labels_canvas->Scroll (0, 0);