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 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
57 : wxPanel (parent, wxID_ANY)
58 , _labels_panel (new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
59 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
62 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
63 , _reels_view (new TimelineReelsView (*this, 32))
64 , _labels_view (new TimelineLabelsView (*this))
67 , _down_view_position (0)
76 _labels_panel->SetDoubleBuffered (true);
77 _main_canvas->SetDoubleBuffered (true);
80 wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
81 sizer->Add (_labels_panel, 0, wxEXPAND);
82 _labels_panel->SetMinSize (wxSize (_labels_view->bbox().width, -1));
83 sizer->Add (_main_canvas, 1, wxEXPAND);
86 _labels_panel->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
87 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
88 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
89 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
90 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
91 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
92 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
94 film_changed (Film::CONTENT);
96 SetMinSize (wxSize (640, 4 * track_height() + 96));
98 _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
99 _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
101 _pixels_per_second = max (0.01, static_cast<double>(640) / film->length().seconds ());
104 _main_canvas->EnableScrolling (true, true);
108 Timeline::paint_labels ()
110 wxPaintDC dc (_labels_panel);
112 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
117 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
123 Timeline::paint_main ()
125 wxPaintDC dc (_main_canvas);
126 _main_canvas->DoPrepareDC (dc);
128 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
134 _main_canvas->GetViewStart (&vsx, &vsy);
135 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
137 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
139 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
141 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
143 /* Find areas of overlap with other content views, so that we can plot them */
144 list<dcpomatic::Rect<int> > overlaps;
145 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
146 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
147 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
148 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
152 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
154 overlaps.push_back (r.get ());
158 i->paint (gc, overlaps);
162 /* Translate back as _down_point and _zoom_point do not take scroll into account */
163 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
164 gc->SetPen (*wxBLACK_PEN);
165 gc->SetBrush (*wxTRANSPARENT_BRUSH);
167 min (_down_point.x, _zoom_point->x),
168 min (_down_point.y, _zoom_point->y),
169 fabs (_down_point.x - _zoom_point->x),
170 fabs (_down_point.y - _zoom_point->y)
178 Timeline::film_changed (Film::Property p)
180 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
183 } else if (p == Film::CONTENT_ORDER) {
189 Timeline::recreate_views ()
191 shared_ptr<const Film> film = _film.lock ();
197 _views.push_back (_time_axis_view);
198 _views.push_back (_reels_view);
200 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
202 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
205 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
206 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
210 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
213 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
214 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
224 Timeline::film_content_changed (int property, bool frequent)
228 if (property == AudioContentProperty::STREAMS) {
230 } else if (!frequent) {
238 place (TimelineViewList& views, int& tracks)
240 int const base = tracks;
242 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
243 if (!dynamic_pointer_cast<T>(i)) {
247 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
251 shared_ptr<Content> content = cv->content();
252 DCPTimePeriod const content_period (content->position(), content->end());
255 TimelineViewList::iterator j = views.begin();
256 while (j != views.end()) {
257 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
263 shared_ptr<Content> test_content = test->content();
265 test->track() && test->track().get() == t &&
266 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
267 /* we have an overlap on track `t' */
275 if (j == views.end ()) {
276 /* no overlap on `t' */
282 tracks = max (tracks, t + 1);
285 return tracks - base;
289 Timeline::assign_tracks ()
292 Video (mono or left-eye)
305 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
306 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
314 bool have_3d = false;
315 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
316 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
321 /* Video on tracks 0 and maybe 1 (left and right eye) */
322 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
324 _tracks = max (_tracks, 2);
331 _tracks = max (_tracks, 1);
335 int const subtitle_tracks = place<TimelineSubtitleContentView> (_views, _tracks);
339 bool have_atmos = false;
340 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
341 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
345 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
346 cv->set_track (_tracks - 1);
357 place<TimelineAudioContentView> (_views, _tracks);
359 _labels_view->set_3d (have_3d);
360 _labels_view->set_subtitle_tracks (subtitle_tracks);
361 _labels_view->set_atmos (have_atmos);
363 _time_axis_view->set_y (tracks() * track_height() + 64);
364 _reels_view->set_y (8);
368 Timeline::tracks () const
374 Timeline::setup_scrollbars ()
376 shared_ptr<const Film> film = _film.lock ();
377 if (!film || !_pixels_per_second) {
380 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), tracks() * track_height() + 96);
381 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
384 shared_ptr<TimelineView>
385 Timeline::event_to_view (wxMouseEvent& ev)
387 /* Search backwards through views so that we find the uppermost one first */
388 TimelineViewList::reverse_iterator i = _views.rbegin();
389 Position<int> const p (ev.GetX(), ev.GetY());
390 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
391 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
395 if (i == _views.rend ()) {
396 return shared_ptr<TimelineView> ();
403 Timeline::left_down (wxMouseEvent& ev)
406 _down_point = ev.GetPosition ();
410 left_down_select (ev);
419 Timeline::left_down_select (wxMouseEvent& ev)
421 shared_ptr<TimelineView> view = event_to_view (ev);
422 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
427 _down_view = content_view;
428 _down_view_position = content_view->content()->position ();
431 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
432 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
437 if (!ev.ShiftDown ()) {
438 cv->set_selected (view == *i);
442 if (content_view && ev.ShiftDown ()) {
443 content_view->set_selected (!content_view->selected ());
449 /* Pre-compute the points that we might snap to */
450 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
451 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
452 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
456 _start_snaps.push_back (cv->content()->position());
457 _end_snaps.push_back (cv->content()->position());
458 _start_snaps.push_back (cv->content()->end());
459 _end_snaps.push_back (cv->content()->end());
461 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
462 _start_snaps.push_back (i);
466 /* Tell everyone that things might change frequently during the drag */
467 _down_view->content()->set_change_signals_frequent (true);
472 Timeline::left_up (wxMouseEvent& ev)
487 Timeline::left_up_select (wxMouseEvent& ev)
490 _down_view->content()->set_change_signals_frequent (false);
493 _content_panel->set_selection (selected_content ());
494 set_position_from_event (ev);
496 /* Clear up up the stuff we don't do during drag */
501 _start_snaps.clear ();
506 Timeline::left_up_zoom (wxMouseEvent& ev)
508 _zoom_point = ev.GetPosition ();
511 _main_canvas->GetViewStart (&vsx, &vsy);
513 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
514 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
516 DCPTime time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
517 DCPTime time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
518 _pixels_per_second = GetSize().GetWidth() / (time_right.seconds() - time_left.seconds());
520 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, wxDefaultCoord);
522 _zoom_point = optional<wxPoint> ();
527 Timeline::mouse_moved (wxMouseEvent& ev)
531 mouse_moved_select (ev);
534 mouse_moved_zoom (ev);
540 Timeline::mouse_moved_select (wxMouseEvent& ev)
546 set_position_from_event (ev);
550 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
556 _zoom_point = ev.GetPosition ();
561 Timeline::right_down (wxMouseEvent& ev)
565 right_down_select (ev);
569 _pixels_per_second = *_pixels_per_second / 2;
576 Timeline::right_down_select (wxMouseEvent& ev)
578 shared_ptr<TimelineView> view = event_to_view (ev);
579 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
584 if (!cv->selected ()) {
586 cv->set_selected (true);
589 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
593 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
595 DCPTime const d = a - b;
596 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
597 nearest_distance = d;
602 Timeline::set_position_from_event (wxMouseEvent& ev)
604 if (!_pixels_per_second) {
608 double const pps = _pixels_per_second.get ();
610 wxPoint const p = ev.GetPosition();
613 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
614 before the drag is considered to have started.
616 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
627 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
631 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
632 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
635 optional<DCPTime> nearest_distance;
637 /* Find the nearest snap point */
639 BOOST_FOREACH (DCPTime i, _start_snaps) {
640 maybe_snap (i, new_position, nearest_distance);
643 BOOST_FOREACH (DCPTime i, _end_snaps) {
644 maybe_snap (i, new_end, nearest_distance);
647 if (nearest_distance) {
648 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
649 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
650 new_position += nearest_distance.get ();
655 if (new_position < DCPTime ()) {
656 new_position = DCPTime ();
659 _down_view->content()->set_position (new_position);
661 shared_ptr<Film> film = _film.lock ();
662 DCPOMATIC_ASSERT (film);
663 film->set_sequence (false);
667 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
669 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
672 shared_ptr<const Film>
673 Timeline::film () const
675 return _film.lock ();
685 Timeline::clear_selection ()
687 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
688 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
690 cv->set_selected (false);
695 TimelineContentViewList
696 Timeline::selected_views () const
698 TimelineContentViewList sel;
700 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
701 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
702 if (cv && cv->selected()) {
711 Timeline::selected_content () const
714 TimelineContentViewList views = selected_views ();
716 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
717 sel.push_back ((*i)->content ());
724 Timeline::set_selection (ContentList selection)
726 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
727 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
729 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());