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>
50 using boost::shared_ptr;
51 using boost::weak_ptr;
52 using boost::dynamic_pointer_cast;
54 using boost::optional;
56 /* 3 hours in 640 pixels */
57 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
58 int const Timeline::_minimum_pixels_per_track = 16;
60 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
61 : wxPanel (parent, wxID_ANY)
62 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
63 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
66 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
67 , _reels_view (new TimelineReelsView (*this, 32))
68 , _labels_view (new TimelineLabelsView (*this))
71 , _down_view_position (0)
78 , _pixels_per_track (48)
79 , _first_resize (true)
82 _labels_canvas->SetDoubleBuffered (true);
83 _main_canvas->SetDoubleBuffered (true);
86 wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
87 sizer->Add (_labels_canvas, 0, wxEXPAND);
88 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
89 sizer->Add (_main_canvas, 1, wxEXPAND);
92 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
93 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
94 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
95 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
96 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
97 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
98 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
99 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
101 film_changed (Film::CONTENT);
103 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
105 _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
106 _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
109 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
113 Timeline::set_pixels_per_second (double pps)
115 _pixels_per_second = max (_minimum_pixels_per_second, pps);
119 Timeline::paint_labels ()
121 wxPaintDC dc (_labels_canvas);
123 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
129 _labels_canvas->GetViewStart (&vsx, &vsy);
130 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
132 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
138 Timeline::paint_main ()
140 wxPaintDC dc (_main_canvas);
141 _main_canvas->DoPrepareDC (dc);
143 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
149 _main_canvas->GetViewStart (&vsx, &vsy);
150 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
152 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
154 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
156 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
158 /* Find areas of overlap with other content views, so that we can plot them */
159 list<dcpomatic::Rect<int> > overlaps;
160 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
161 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
162 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
163 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
167 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
169 overlaps.push_back (r.get ());
173 i->paint (gc, overlaps);
177 /* Translate back as _down_point and _zoom_point do not take scroll into account */
178 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
179 gc->SetPen (*wxBLACK_PEN);
180 gc->SetBrush (*wxTRANSPARENT_BRUSH);
182 min (_down_point.x, _zoom_point->x),
183 min (_down_point.y, _zoom_point->y),
184 fabs (_down_point.x - _zoom_point->x),
185 fabs (_down_point.y - _zoom_point->y)
193 Timeline::film_changed (Film::Property p)
195 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
198 } else if (p == Film::CONTENT_ORDER) {
204 Timeline::recreate_views ()
206 shared_ptr<const Film> film = _film.lock ();
212 _views.push_back (_time_axis_view);
213 _views.push_back (_reels_view);
215 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
217 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
220 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
221 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
225 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
228 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
229 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
239 Timeline::film_content_changed (int property, bool frequent)
243 if (property == AudioContentProperty::STREAMS) {
245 } else if (!frequent) {
253 place (TimelineViewList& views, int& tracks)
255 int const base = tracks;
257 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
258 if (!dynamic_pointer_cast<T>(i)) {
262 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
266 shared_ptr<Content> content = cv->content();
267 DCPTimePeriod const content_period (content->position(), content->end());
270 TimelineViewList::iterator j = views.begin();
271 while (j != views.end()) {
272 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
278 shared_ptr<Content> test_content = test->content();
280 test->track() && test->track().get() == t &&
281 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
282 /* we have an overlap on track `t' */
290 if (j == views.end ()) {
291 /* no overlap on `t' */
297 tracks = max (tracks, t + 1);
300 return tracks - base;
304 Timeline::assign_tracks ()
307 Video (mono or left-eye)
320 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
321 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
329 bool have_3d = false;
330 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
331 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
336 /* Video on tracks 0 and maybe 1 (left and right eye) */
337 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
339 _tracks = max (_tracks, 2);
346 _tracks = max (_tracks, 1);
350 int const subtitle_tracks = place<TimelineSubtitleContentView> (_views, _tracks);
354 bool have_atmos = false;
355 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
356 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
360 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
361 cv->set_track (_tracks - 1);
372 place<TimelineAudioContentView> (_views, _tracks);
374 _labels_view->set_3d (have_3d);
375 _labels_view->set_subtitle_tracks (subtitle_tracks);
376 _labels_view->set_atmos (have_atmos);
378 _time_axis_view->set_y (tracks());
379 _reels_view->set_y (8);
383 Timeline::tracks () const
389 Timeline::setup_scrollbars ()
391 shared_ptr<const Film> film = _film.lock ();
392 if (!film || !_pixels_per_second) {
395 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, tracks() * pixels_per_track() + 96);
396 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
397 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height);
398 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
401 shared_ptr<TimelineView>
402 Timeline::event_to_view (wxMouseEvent& ev)
404 /* Search backwards through views so that we find the uppermost one first */
405 TimelineViewList::reverse_iterator i = _views.rbegin();
406 Position<int> const p (ev.GetX(), ev.GetY());
407 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
408 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
412 if (i == _views.rend ()) {
413 return shared_ptr<TimelineView> ();
420 Timeline::left_down (wxMouseEvent& ev)
423 _down_point = ev.GetPosition ();
427 left_down_select (ev);
439 Timeline::left_down_select (wxMouseEvent& ev)
441 shared_ptr<TimelineView> view = event_to_view (ev);
442 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
447 _down_view = content_view;
448 _down_view_position = content_view->content()->position ();
451 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
452 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
457 if (!ev.ShiftDown ()) {
458 cv->set_selected (view == *i);
462 if (content_view && ev.ShiftDown ()) {
463 content_view->set_selected (!content_view->selected ());
469 /* Pre-compute the points that we might snap to */
470 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
471 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
472 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
476 _start_snaps.push_back (cv->content()->position());
477 _end_snaps.push_back (cv->content()->position());
478 _start_snaps.push_back (cv->content()->end());
479 _end_snaps.push_back (cv->content()->end());
481 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
482 _start_snaps.push_back (i);
486 /* Tell everyone that things might change frequently during the drag */
487 _down_view->content()->set_change_signals_frequent (true);
492 Timeline::left_up (wxMouseEvent& ev)
511 Timeline::left_up_select (wxMouseEvent& ev)
514 _down_view->content()->set_change_signals_frequent (false);
517 _content_panel->set_selection (selected_content ());
518 set_position_from_event (ev);
520 /* Clear up up the stuff we don't do during drag */
525 _start_snaps.clear ();
530 Timeline::left_up_zoom (wxMouseEvent& ev)
532 _zoom_point = ev.GetPosition ();
535 _main_canvas->GetViewStart (&vsx, &vsy);
537 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
538 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
540 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
541 /* Very small zoom rectangle: we assume it wasn't intentional */
545 DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
546 DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
547 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
549 double const tracks_top = double(top_left.y) / _pixels_per_track;
550 double const tracks_bottom = double(bottom_right.y) / _pixels_per_track;
551 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
554 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, tracks_top * _pixels_per_track / _y_scroll_rate);
555 _labels_canvas->Scroll (0, tracks_top * _pixels_per_track / _y_scroll_rate);
557 _zoom_point = optional<wxPoint> ();
562 Timeline::set_pixels_per_track (int h)
564 _pixels_per_track = max(_minimum_pixels_per_track, h);
568 Timeline::mouse_moved (wxMouseEvent& ev)
572 mouse_moved_select (ev);
575 mouse_moved_zoom (ev);
585 Timeline::mouse_moved_select (wxMouseEvent& ev)
591 set_position_from_event (ev);
595 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
601 _zoom_point = ev.GetPosition ();
606 Timeline::right_down (wxMouseEvent& ev)
610 right_down_select (ev);
614 set_pixels_per_second (*_pixels_per_second / 2);
615 set_pixels_per_track (_pixels_per_track / 2);
627 Timeline::right_down_select (wxMouseEvent& ev)
629 shared_ptr<TimelineView> view = event_to_view (ev);
630 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
635 if (!cv->selected ()) {
637 cv->set_selected (true);
640 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
644 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
646 DCPTime const d = a - b;
647 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
648 nearest_distance = d;
653 Timeline::set_position_from_event (wxMouseEvent& ev)
655 if (!_pixels_per_second) {
659 double const pps = _pixels_per_second.get ();
661 wxPoint const p = ev.GetPosition();
664 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
665 before the drag is considered to have started.
667 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
678 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
682 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
683 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
686 optional<DCPTime> nearest_distance;
688 /* Find the nearest snap point */
690 BOOST_FOREACH (DCPTime i, _start_snaps) {
691 maybe_snap (i, new_position, nearest_distance);
694 BOOST_FOREACH (DCPTime i, _end_snaps) {
695 maybe_snap (i, new_end, nearest_distance);
698 if (nearest_distance) {
699 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
700 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
701 new_position += nearest_distance.get ();
706 if (new_position < DCPTime ()) {
707 new_position = DCPTime ();
710 _down_view->content()->set_position (new_position);
712 shared_ptr<Film> film = _film.lock ();
713 DCPOMATIC_ASSERT (film);
714 film->set_sequence (false);
718 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
720 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
723 shared_ptr<const Film>
724 Timeline::film () const
726 return _film.lock ();
732 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
734 _first_resize = false;
740 Timeline::clear_selection ()
742 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
743 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
745 cv->set_selected (false);
750 TimelineContentViewList
751 Timeline::selected_views () const
753 TimelineContentViewList sel;
755 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
756 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
757 if (cv && cv->selected()) {
766 Timeline::selected_content () const
769 TimelineContentViewList views = selected_views ();
771 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
772 sel.push_back ((*i)->content ());
779 Timeline::set_selection (ContentList 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 (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
790 Timeline::tracks_y_offset () const
792 return _reels_view->bbox().height + 4;
796 Timeline::width () const
798 return _main_canvas->GetVirtualSize().GetWidth();
802 Timeline::scrolled (wxScrollWinEvent& ev)
804 if (ev.GetOrientation() == wxVERTICAL) {
805 _labels_canvas->Scroll (0, ev.GetPosition ());
811 Timeline::tool_clicked (Tool t)
828 Timeline::zoom_all ()
830 shared_ptr<Film> film = _film.lock ();
831 DCPOMATIC_ASSERT (film);
832 set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
833 set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
835 _main_canvas->Scroll (0, 0);
836 _labels_canvas->Scroll (0, 0);