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_text_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/text_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>
52 using boost::shared_ptr;
53 using boost::weak_ptr;
54 using boost::dynamic_pointer_cast;
56 using boost::optional;
58 /* 3 hours in 640 pixels */
59 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
60 int const Timeline::_minimum_pixels_per_track = 16;
62 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
63 : wxPanel (parent, wxID_ANY)
64 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
65 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
68 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
69 , _reels_view (new TimelineReelsView (*this, 32))
70 , _labels_view (new TimelineLabelsView (*this))
73 , _down_view_position (0)
80 , _pixels_per_track (48)
81 , _first_resize (true)
84 _labels_canvas->SetDoubleBuffered (true);
85 _main_canvas->SetDoubleBuffered (true);
88 wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
89 sizer->Add (_labels_canvas, 0, wxEXPAND);
90 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
91 sizer->Add (_main_canvas, 1, wxEXPAND);
94 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
95 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
96 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
97 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
98 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
99 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
100 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
101 _main_canvas->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&Timeline::scrolled, this, _1));
102 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&Timeline::scrolled, this, _1));
103 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&Timeline::scrolled, this, _1));
104 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&Timeline::scrolled, this, _1));
105 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&Timeline::scrolled, this, _1));
106 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&Timeline::scrolled, this, _1));
107 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
109 film_change (CHANGE_TYPE_DONE, Film::CONTENT);
111 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
113 _film_changed_connection = film->Change.connect (bind (&Timeline::film_change, this, _1, _2));
114 _film_content_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
117 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
121 Timeline::set_pixels_per_second (double pps)
123 _pixels_per_second = max (_minimum_pixels_per_second, pps);
127 Timeline::paint_labels ()
129 wxPaintDC dc (_labels_canvas);
131 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
137 _labels_canvas->GetViewStart (&vsx, &vsy);
138 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
140 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
146 Timeline::paint_main ()
148 wxPaintDC dc (_main_canvas);
149 _main_canvas->DoPrepareDC (dc);
151 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
157 _main_canvas->GetViewStart (&vsx, &vsy);
158 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
160 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
162 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
164 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
166 /* Find areas of overlap with other content views, so that we can plot them */
167 list<dcpomatic::Rect<int> > overlaps;
168 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
169 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
170 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
171 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
175 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
177 overlaps.push_back (r.get ());
181 i->paint (gc, overlaps);
185 /* Translate back as _down_point and _zoom_point do not take scroll into account */
186 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
187 gc->SetPen (*wxBLACK_PEN);
188 gc->SetBrush (*wxTRANSPARENT_BRUSH);
190 min (_down_point.x, _zoom_point->x),
191 min (_down_point.y, _zoom_point->y),
192 abs (_down_point.x - _zoom_point->x),
193 abs (_down_point.y - _zoom_point->y)
201 Timeline::film_change (ChangeType type, Film::Property p)
203 if (type != CHANGE_TYPE_DONE) {
207 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
210 } else if (p == Film::CONTENT_ORDER) {
216 Timeline::recreate_views ()
218 shared_ptr<const Film> film = _film.lock ();
224 _views.push_back (_time_axis_view);
225 _views.push_back (_reels_view);
227 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
229 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
232 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
233 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
236 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
237 _views.push_back (shared_ptr<TimelineView> (new TimelineTextContentView (*this, i, j)));
240 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
241 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
251 Timeline::film_content_change (ChangeType type, int property, bool frequent)
253 if (type != CHANGE_TYPE_DONE) {
259 if (property == AudioContentProperty::STREAMS) {
261 } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
262 _reels_view->force_redraw ();
263 } else if (!frequent) {
271 place (TimelineViewList& views, int& tracks)
273 int const base = tracks;
275 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
276 if (!dynamic_pointer_cast<T>(i)) {
280 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
284 shared_ptr<Content> content = cv->content();
285 DCPTimePeriod const content_period (content->position(), content->end());
288 TimelineViewList::iterator j = views.begin();
289 while (j != views.end()) {
290 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
296 shared_ptr<Content> test_content = test->content();
298 test->track() && test->track().get() == t &&
299 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
300 /* we have an overlap on track `t' */
308 if (j == views.end ()) {
309 /* no overlap on `t' */
315 tracks = max (tracks, t + 1);
318 return tracks - base;
321 /** Compare the mapped output channels of two TimelineViews, so we can into
322 * order of first mapped DCP channel.
324 struct AudioMappingComparator {
325 bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
327 shared_ptr<TimelineAudioContentView> cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
329 list<int> oc = cva->content()->audio->mapping().mapped_output_channels();
330 la = *min_element(boost::begin(oc), boost::end(oc));
333 shared_ptr<TimelineAudioContentView> cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
335 list<int> oc = cvb->content()->audio->mapping().mapped_output_channels();
336 lb = *min_element(boost::begin(oc), boost::end(oc));
343 Timeline::assign_tracks ()
346 Video (mono or left-eye)
359 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
360 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
368 bool have_3d = false;
369 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
370 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
375 /* Video on tracks 0 and maybe 1 (left and right eye) */
376 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
378 _tracks = max (_tracks, 2);
385 _tracks = max (_tracks, 1);
389 int const text_tracks = place<TimelineTextContentView> (_views, _tracks);
393 bool have_atmos = false;
394 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
395 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
399 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
400 cv->set_track (_tracks - 1);
409 /* Audio. We're sorting the views so that we get the audio views in order of increasing
413 TimelineViewList views = _views;
414 sort(views.begin(), views.end(), AudioMappingComparator());
415 int const audio_tracks = place<TimelineAudioContentView> (views, _tracks);
417 _labels_view->set_3d (have_3d);
418 _labels_view->set_audio_tracks (audio_tracks);
419 _labels_view->set_text_tracks (text_tracks);
420 _labels_view->set_atmos (have_atmos);
422 _time_axis_view->set_y (tracks());
423 _reels_view->set_y (8);
427 Timeline::tracks () const
433 Timeline::setup_scrollbars ()
435 shared_ptr<const Film> film = _film.lock ();
436 if (!film || !_pixels_per_second) {
440 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
442 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
443 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
444 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
445 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
448 shared_ptr<TimelineView>
449 Timeline::event_to_view (wxMouseEvent& ev)
451 /* Search backwards through views so that we find the uppermost one first */
452 TimelineViewList::reverse_iterator i = _views.rbegin();
453 Position<int> const p (ev.GetX(), ev.GetY());
454 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
455 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
459 if (i == _views.rend ()) {
460 return shared_ptr<TimelineView> ();
467 Timeline::left_down (wxMouseEvent& ev)
470 _down_point = ev.GetPosition ();
474 left_down_select (ev);
486 Timeline::left_down_select (wxMouseEvent& ev)
488 shared_ptr<TimelineView> view = event_to_view (ev);
489 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
494 _down_view = content_view;
495 _down_view_position = content_view->content()->position ();
498 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
499 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
504 if (!ev.ShiftDown ()) {
505 cv->set_selected (view == *i);
509 if (content_view && ev.ShiftDown ()) {
510 content_view->set_selected (!content_view->selected ());
516 /* Pre-compute the points that we might snap to */
517 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
518 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
519 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
523 _start_snaps.push_back (cv->content()->position());
524 _end_snaps.push_back (cv->content()->position());
525 _start_snaps.push_back (cv->content()->end());
526 _end_snaps.push_back (cv->content()->end());
528 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
529 _start_snaps.push_back (i);
533 /* Tell everyone that things might change frequently during the drag */
534 _down_view->content()->set_change_signals_frequent (true);
539 Timeline::left_up (wxMouseEvent& ev)
558 Timeline::left_up_select (wxMouseEvent& ev)
561 _down_view->content()->set_change_signals_frequent (false);
564 _content_panel->set_selection (selected_content ());
565 set_position_from_event (ev);
567 /* Clear up up the stuff we don't do during drag */
572 _start_snaps.clear ();
577 Timeline::left_up_zoom (wxMouseEvent& ev)
579 _zoom_point = ev.GetPosition ();
582 _main_canvas->GetViewStart (&vsx, &vsy);
584 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
585 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
587 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
588 /* Very small zoom rectangle: we assume it wasn't intentional */
592 DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
593 DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
594 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
596 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
597 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
598 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
601 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
602 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
603 _labels_canvas->Scroll (0, y);
605 _zoom_point = optional<wxPoint> ();
610 Timeline::set_pixels_per_track (int h)
612 _pixels_per_track = max(_minimum_pixels_per_track, h);
616 Timeline::mouse_moved (wxMouseEvent& ev)
620 mouse_moved_select (ev);
623 mouse_moved_zoom (ev);
633 Timeline::mouse_moved_select (wxMouseEvent& ev)
639 set_position_from_event (ev);
643 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
649 _zoom_point = ev.GetPosition ();
654 Timeline::right_down (wxMouseEvent& ev)
658 right_down_select (ev);
662 set_pixels_per_second (*_pixels_per_second / 2);
663 set_pixels_per_track (_pixels_per_track / 2);
675 Timeline::right_down_select (wxMouseEvent& ev)
677 shared_ptr<TimelineView> view = event_to_view (ev);
678 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
683 if (!cv->selected ()) {
685 cv->set_selected (true);
688 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
692 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
694 DCPTime const d = a - b;
695 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
696 nearest_distance = d;
701 Timeline::set_position_from_event (wxMouseEvent& ev)
703 if (!_pixels_per_second) {
707 double const pps = _pixels_per_second.get ();
709 wxPoint const p = ev.GetPosition();
712 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
713 before the drag is considered to have started.
715 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
726 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
730 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
731 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
734 optional<DCPTime> nearest_distance;
736 /* Find the nearest snap point */
738 BOOST_FOREACH (DCPTime i, _start_snaps) {
739 maybe_snap (i, new_position, nearest_distance);
742 BOOST_FOREACH (DCPTime i, _end_snaps) {
743 maybe_snap (i, new_end, nearest_distance);
746 if (nearest_distance) {
747 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
748 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
749 new_position += nearest_distance.get ();
754 if (new_position < DCPTime ()) {
755 new_position = DCPTime ();
758 _down_view->content()->set_position (new_position);
760 shared_ptr<Film> film = _film.lock ();
761 DCPOMATIC_ASSERT (film);
762 film->set_sequence (false);
766 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
768 _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
771 shared_ptr<const Film>
772 Timeline::film () const
774 return _film.lock ();
780 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
782 _first_resize = false;
788 Timeline::clear_selection ()
790 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
791 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
793 cv->set_selected (false);
798 TimelineContentViewList
799 Timeline::selected_views () const
801 TimelineContentViewList sel;
803 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
804 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
805 if (cv && cv->selected()) {
814 Timeline::selected_content () const
817 TimelineContentViewList views = selected_views ();
819 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
820 sel.push_back ((*i)->content ());
827 Timeline::set_selection (ContentList selection)
829 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
830 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
832 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
838 Timeline::tracks_y_offset () const
840 return _reels_view->bbox().height + 4;
844 Timeline::width () const
846 return _main_canvas->GetVirtualSize().GetWidth();
850 Timeline::scrolled (wxScrollWinEvent& ev)
852 if (ev.GetOrientation() == wxVERTICAL) {
854 _main_canvas->GetViewStart (&x, &y);
855 _labels_canvas->Scroll (0, y);
861 Timeline::tool_clicked (Tool t)
878 Timeline::zoom_all ()
880 shared_ptr<Film> film = _film.lock ();
881 DCPOMATIC_ASSERT (film);
882 set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
883 set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
885 _main_canvas->Scroll (0, 0);
886 _labels_canvas->Scroll (0, 0);