2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <wx/graphics.h>
22 #include <boost/weak_ptr.hpp>
24 #include "lib/playlist.h"
25 #include "film_editor.h"
32 using boost::shared_ptr;
33 using boost::weak_ptr;
34 using boost::dynamic_pointer_cast;
36 using boost::optional;
38 /** Parent class for components of the timeline (e.g. a piece of content or an axis) */
39 class View : public boost::noncopyable
50 void paint (wxGraphicsContext* g)
52 _last_paint_bbox = bbox ();
58 _timeline.force_redraw (_last_paint_bbox);
59 _timeline.force_redraw (bbox ());
62 virtual dcpomatic::Rect<int> bbox () const = 0;
65 virtual void do_paint (wxGraphicsContext *) = 0;
67 int time_x (DCPTime t) const
69 return _timeline.tracks_position().x + t.seconds() * _timeline.pixels_per_second ();
75 dcpomatic::Rect<int> _last_paint_bbox;
79 /** Parent class for views of pieces of content */
80 class ContentView : public View
83 ContentView (Timeline& tl, shared_ptr<Content> c)
89 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3));
92 dcpomatic::Rect<int> bbox () const
94 shared_ptr<const Film> film = _timeline.film ();
95 shared_ptr<const Content> content = _content.lock ();
96 if (!film || !content) {
97 return dcpomatic::Rect<int> ();
100 return dcpomatic::Rect<int> (
101 time_x (content->position ()) - 8,
103 content->length_after_trim().seconds() * _timeline.pixels_per_second() + 16,
104 _timeline.track_height() + 16
108 void set_selected (bool s) {
113 bool selected () const {
117 shared_ptr<Content> content () const {
118 return _content.lock ();
121 void set_track (int t) {
129 virtual wxString type () const = 0;
130 virtual wxColour colour () const = 0;
134 void do_paint (wxGraphicsContext* gc)
136 shared_ptr<const Film> film = _timeline.film ();
137 shared_ptr<const Content> cont = content ();
138 if (!film || !cont) {
142 DCPTime const position = cont->position ();
143 DCPTime const len = cont->length_after_trim ();
145 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
147 gc->SetPen (*wxBLACK_PEN);
149 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
151 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
153 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
156 wxGraphicsPath path = gc->CreatePath ();
157 path.MoveToPoint (time_x (position), y_pos (_track) + 4);
158 path.AddLineToPoint (time_x (position + len), y_pos (_track) + 4);
159 path.AddLineToPoint (time_x (position + len), y_pos (_track + 1) - 4);
160 path.AddLineToPoint (time_x (position), y_pos (_track + 1) - 4);
161 path.AddLineToPoint (time_x (position), y_pos (_track) + 4);
162 gc->StrokePath (path);
165 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->path_summary()).data(), type().data());
167 wxDouble name_height;
168 wxDouble name_descent;
169 wxDouble name_leading;
170 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
172 gc->Clip (wxRegion (time_x (position), y_pos (_track), len.seconds() * _timeline.pixels_per_second(), _timeline.track_height()));
173 gc->DrawText (name, time_x (position) + 12, y_pos (_track + 1) - name_height - 4);
177 int y_pos (int t) const
179 return _timeline.tracks_position().y + t * _timeline.track_height();
182 void content_changed (int p, bool frequent)
186 if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
191 _timeline.setup_pixels_per_second ();
192 _timeline.Refresh ();
196 boost::weak_ptr<Content> _content;
200 boost::signals2::scoped_connection _content_connection;
203 class AudioContentView : public ContentView
206 AudioContentView (Timeline& tl, shared_ptr<Content> c)
207 : ContentView (tl, c)
211 wxString type () const
216 wxColour colour () const
218 return wxColour (149, 121, 232, 255);
222 class VideoContentView : public ContentView
225 VideoContentView (Timeline& tl, shared_ptr<Content> c)
226 : ContentView (tl, c)
231 wxString type () const
233 if (dynamic_pointer_cast<FFmpegContent> (content ())) {
240 wxColour colour () const
242 return wxColour (242, 92, 120, 255);
246 class SubtitleContentView : public ContentView
249 SubtitleContentView (Timeline& tl, shared_ptr<Content> c)
250 : ContentView (tl, c)
254 wxString type () const
256 return _("subtitles");
259 wxColour colour () const
261 return wxColour (163, 255, 154, 255);
265 class TimeAxisView : public View
268 TimeAxisView (Timeline& tl, int y)
273 dcpomatic::Rect<int> bbox () const
275 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
286 void do_paint (wxGraphicsContext* gc)
288 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
290 double mark_interval = rint (128 / _timeline.pixels_per_second ());
291 if (mark_interval > 5) {
292 mark_interval -= int (rint (mark_interval)) % 5;
294 if (mark_interval > 10) {
295 mark_interval -= int (rint (mark_interval)) % 10;
297 if (mark_interval > 60) {
298 mark_interval -= int (rint (mark_interval)) % 60;
300 if (mark_interval > 3600) {
301 mark_interval -= int (rint (mark_interval)) % 3600;
304 if (mark_interval < 1) {
308 wxGraphicsPath path = gc->CreatePath ();
309 path.MoveToPoint (_timeline.x_offset(), _y);
310 path.AddLineToPoint (_timeline.width(), _y);
311 gc->StrokePath (path);
313 /* Time in seconds */
315 while ((t.seconds() * _timeline.pixels_per_second()) < _timeline.width()) {
316 wxGraphicsPath path = gc->CreatePath ();
317 path.MoveToPoint (time_x (t), _y - 4);
318 path.AddLineToPoint (time_x (t), _y + 4);
319 gc->StrokePath (path);
321 double tc = t.seconds ();
322 int const h = tc / 3600;
324 int const m = tc / 60;
328 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
331 wxDouble str_descent;
332 wxDouble str_leading;
333 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
335 int const tx = _timeline.x_offset() + t.seconds() * _timeline.pixels_per_second();
336 if ((tx + str_width) < _timeline.width()) {
337 gc->DrawText (str, time_x (t), _y + 16);
340 t += DCPTime::from_seconds (mark_interval);
349 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
350 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
353 , _time_axis_view (new TimeAxisView (*this, 32))
355 , _pixels_per_second (0)
357 , _down_view_position (0)
358 , _first_move (false)
363 SetDoubleBuffered (true);
366 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
367 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
368 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
369 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
370 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
371 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
375 SetMinSize (wxSize (640, tracks() * track_height() + 96));
377 _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
385 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
390 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
392 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
400 Timeline::playlist_changed ()
404 shared_ptr<const Film> fl = _film.lock ();
410 _views.push_back (_time_axis_view);
412 ContentList content = fl->playlist()->content ();
414 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
415 if (dynamic_pointer_cast<VideoContent> (*i)) {
416 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i)));
418 if (dynamic_pointer_cast<AudioContent> (*i)) {
419 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i)));
421 if (dynamic_pointer_cast<SubtitleContent> (*i)) {
422 _views.push_back (shared_ptr<View> (new SubtitleContentView (*this, *i)));
427 setup_pixels_per_second ();
432 Timeline::assign_tracks ()
434 list<shared_ptr<VideoContentView> > video;
435 list<shared_ptr<AudioContentView> > audio;
436 list<shared_ptr<SubtitleContentView> > subtitle;
438 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
439 shared_ptr<VideoContentView> v = dynamic_pointer_cast<VideoContentView> (*i);
444 shared_ptr<AudioContentView> a = dynamic_pointer_cast<AudioContentView> (*i);
449 shared_ptr<SubtitleContentView> s = dynamic_pointer_cast<SubtitleContentView> (*i);
451 subtitle.push_back (s);
456 if (!video.empty ()) {
457 for (list<shared_ptr<VideoContentView> >::iterator i = video.begin(); i != video.end(); ++i) {
458 (*i)->set_track (_tracks);
463 if (!subtitle.empty ()) {
464 for (list<shared_ptr<SubtitleContentView> >::iterator i = subtitle.begin(); i != subtitle.end(); ++i) {
465 (*i)->set_track (_tracks);
470 int const audio_start = _tracks;
472 for (list<shared_ptr<AudioContentView> >::iterator i = audio.begin(); i != audio.end(); ++i) {
473 shared_ptr<Content> acv_content = (*i)->content();
477 list<shared_ptr<AudioContentView> >::iterator j = audio.begin ();
478 while (j != audio.end()) {
479 if ((*j)->track() == t) {
480 bool const no_overlap =
481 (acv_content->position() < (*j)->content()->position() && acv_content->end() < (*j)->content()->position()) ||
482 (acv_content->position() > (*j)->content()->end() && acv_content->end() > (*j)->content()->end());
485 /* we have an overlap on track `t' */
494 if (j == audio.end ()) {
495 /* no overlap on `t' */
501 _tracks = max (_tracks, t + 1);
504 _time_axis_view->set_y (tracks() * track_height() + 32);
508 Timeline::tracks () const
514 Timeline::setup_pixels_per_second ()
516 shared_ptr<const Film> film = _film.lock ();
517 if (!film || film->length() == DCPTime ()) {
521 _pixels_per_second = static_cast<double>(width() - x_offset() * 2) / film->length().seconds ();
525 Timeline::event_to_view (wxMouseEvent& ev)
527 ViewList::iterator i = _views.begin();
528 Position<int> const p (ev.GetX(), ev.GetY());
529 while (i != _views.end() && !(*i)->bbox().contains (p)) {
533 if (i == _views.end ()) {
534 return shared_ptr<View> ();
541 Timeline::left_down (wxMouseEvent& ev)
543 shared_ptr<View> view = event_to_view (ev);
544 shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
549 _down_view = content_view;
550 _down_view_position = content_view->content()->position ();
553 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
554 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
559 if (!ev.ShiftDown ()) {
560 cv->set_selected (view == *i);
564 _film_editor->set_selection (cv->content ());
568 if (content_view && ev.ShiftDown ()) {
569 content_view->set_selected (!content_view->selected ());
573 _down_point = ev.GetPosition ();
577 _down_view->content()->set_change_signals_frequent (true);
582 Timeline::left_up (wxMouseEvent& ev)
587 _down_view->content()->set_change_signals_frequent (false);
590 set_position_from_event (ev);
594 Timeline::mouse_moved (wxMouseEvent& ev)
600 set_position_from_event (ev);
604 Timeline::right_down (wxMouseEvent& ev)
606 shared_ptr<View> view = event_to_view (ev);
607 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
612 if (!cv->selected ()) {
614 cv->set_selected (true);
617 _menu.popup (_film, selected_content (), ev.GetPosition ());
621 Timeline::set_position_from_event (wxMouseEvent& ev)
623 wxPoint const p = ev.GetPosition();
626 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
627 before the drag is considered to have started.
629 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
640 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / _pixels_per_second);
645 DCPTime nearest_distance = DCPTime::max ();
646 DCPTime nearest_new_position = DCPTime::max ();
648 /* Find the nearest content edge; this is inefficient */
649 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
650 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
651 if (!cv || cv == _down_view) {
656 /* Snap starts to ends */
657 DCPTime const d = DCPTime (cv->content()->end() - new_position).abs ();
658 if (first || d < nearest_distance) {
659 nearest_distance = d;
660 nearest_new_position = cv->content()->end();
665 /* Snap ends to starts */
666 DCPTime const d = DCPTime (
667 cv->content()->position() - (new_position + _down_view->content()->length_after_trim())
670 if (d < nearest_distance) {
671 nearest_distance = d;
672 nearest_new_position = cv->content()->position() - _down_view->content()->length_after_trim ();
680 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
681 if (nearest_distance < DCPTime::from_seconds ((width() / pixels_per_second()) / 32)) {
682 new_position = nearest_new_position;
687 if (new_position < DCPTime ()) {
688 new_position = DCPTime ();
691 _down_view->content()->set_position (new_position);
693 shared_ptr<Film> film = _film.lock ();
695 film->set_sequence_video (false);
699 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
701 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
704 shared_ptr<const Film>
705 Timeline::film () const
707 return _film.lock ();
713 setup_pixels_per_second ();
717 Timeline::clear_selection ()
719 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
720 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
722 cv->set_selected (false);
727 Timeline::ContentViewList
728 Timeline::selected_views () const
732 for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
733 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
734 if (cv && cv->selected()) {
743 Timeline::selected_content () const
746 ContentViewList views = selected_views ();
748 for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
749 sel.push_back ((*i)->content ());