2 Copyright (C) 2013-2014 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 "lib/image_content.h"
26 #include "film_editor.h"
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35 using boost::dynamic_pointer_cast;
37 using boost::optional;
40 * @brief Parent class for components of the timeline (e.g. a piece of content or an axis).
42 class View : public boost::noncopyable
53 void paint (wxGraphicsContext* g)
55 _last_paint_bbox = bbox ();
61 _timeline.force_redraw (_last_paint_bbox);
62 _timeline.force_redraw (bbox ());
65 virtual dcpomatic::Rect<int> bbox () const = 0;
68 virtual void do_paint (wxGraphicsContext *) = 0;
70 int time_x (DCPTime t) const
72 return _timeline.tracks_position().x + t.seconds() * _timeline.pixels_per_second().get_value_or (0);
78 dcpomatic::Rect<int> _last_paint_bbox;
82 /** @class ContentView
83 * @brief Parent class for views of pieces of content.
85 class ContentView : public View
88 ContentView (Timeline& tl, shared_ptr<Content> c)
93 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3));
96 dcpomatic::Rect<int> bbox () const
100 shared_ptr<const Film> film = _timeline.film ();
101 shared_ptr<const Content> content = _content.lock ();
102 if (!film || !content) {
103 return dcpomatic::Rect<int> ();
106 return dcpomatic::Rect<int> (
107 time_x (content->position ()) - 8,
108 y_pos (_track.get()) - 8,
109 content->length_after_trim().seconds() * _timeline.pixels_per_second().get_value_or(0) + 16,
110 _timeline.track_height() + 16
114 void set_selected (bool s) {
119 bool selected () const {
123 shared_ptr<Content> content () const {
124 return _content.lock ();
127 void set_track (int t) {
131 void unset_track () {
132 _track = boost::optional<int> ();
135 optional<int> track () const {
139 virtual wxString type () const = 0;
140 virtual wxColour background_colour () const = 0;
141 virtual wxColour foreground_colour () const = 0;
145 void do_paint (wxGraphicsContext* gc)
149 shared_ptr<const Film> film = _timeline.film ();
150 shared_ptr<const Content> cont = content ();
151 if (!film || !cont) {
155 DCPTime const position = cont->position ();
156 DCPTime const len = cont->length_after_trim ();
158 wxColour selected (background_colour().Red() / 2, background_colour().Green() / 2, background_colour().Blue() / 2);
160 gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 4, wxPENSTYLE_SOLID));
162 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
164 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (background_colour(), wxBRUSHSTYLE_SOLID));
167 wxGraphicsPath path = gc->CreatePath ();
168 path.MoveToPoint (time_x (position), y_pos (_track.get()) + 4);
169 path.AddLineToPoint (time_x (position + len), y_pos (_track.get()) + 4);
170 path.AddLineToPoint (time_x (position + len), y_pos (_track.get() + 1) - 4);
171 path.AddLineToPoint (time_x (position), y_pos (_track.get() + 1) - 4);
172 path.AddLineToPoint (time_x (position), y_pos (_track.get()) + 4);
173 gc->StrokePath (path);
176 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->summary()).data(), type().data());
178 wxDouble name_height;
179 wxDouble name_descent;
180 wxDouble name_leading;
181 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
183 gc->Clip (wxRegion (time_x (position), y_pos (_track.get()), len.seconds() * _timeline.pixels_per_second().get_value_or(0), _timeline.track_height()));
184 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT, foreground_colour ()));
185 gc->DrawText (name, time_x (position) + 12, y_pos (_track.get() + 1) - name_height - 4);
189 int y_pos (int t) const
191 return _timeline.tracks_position().y + t * _timeline.track_height();
194 void content_changed (int p, bool frequent)
198 if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
203 _timeline.setup_pixels_per_second ();
204 _timeline.Refresh ();
208 boost::weak_ptr<Content> _content;
209 optional<int> _track;
212 boost::signals2::scoped_connection _content_connection;
215 class AudioContentView : public ContentView
218 AudioContentView (Timeline& tl, shared_ptr<Content> c)
219 : ContentView (tl, c)
223 wxString type () const
228 wxColour background_colour () const
230 return wxColour (149, 121, 232, 255);
233 wxColour foreground_colour () const
235 return wxColour (0, 0, 0, 255);
239 class VideoContentView : public ContentView
242 VideoContentView (Timeline& tl, shared_ptr<Content> c)
243 : ContentView (tl, c)
248 wxString type () const
250 if (dynamic_pointer_cast<ImageContent> (content ()) && content()->number_of_paths() == 1) {
257 wxColour background_colour () const
259 return wxColour (242, 92, 120, 255);
262 wxColour foreground_colour () const
264 return wxColour (0, 0, 0, 255);
268 class SubtitleContentView : public ContentView
271 SubtitleContentView (Timeline& tl, shared_ptr<SubtitleContent> c)
272 : ContentView (tl, c)
273 , _subtitle_content (c)
277 wxString type () const
279 return _("subtitles");
282 wxColour background_colour () const
284 shared_ptr<SubtitleContent> sc = _subtitle_content.lock ();
285 if (!sc || !sc->use_subtitles ()) {
286 return wxColour (210, 210, 210, 128);
289 return wxColour (163, 255, 154, 255);
292 wxColour foreground_colour () const
294 shared_ptr<SubtitleContent> sc = _subtitle_content.lock ();
295 if (!sc || !sc->use_subtitles ()) {
296 return wxColour (180, 180, 180, 128);
299 return wxColour (0, 0, 0, 255);
302 boost::weak_ptr<SubtitleContent> _subtitle_content;
305 class TimeAxisView : public View
308 TimeAxisView (Timeline& tl, int y)
313 dcpomatic::Rect<int> bbox () const
315 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
326 void do_paint (wxGraphicsContext* gc)
328 if (!_timeline.pixels_per_second()) {
332 double const pps = _timeline.pixels_per_second().get ();
334 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
336 double mark_interval = rint (128 / pps);
337 if (mark_interval > 5) {
338 mark_interval -= int (rint (mark_interval)) % 5;
340 if (mark_interval > 10) {
341 mark_interval -= int (rint (mark_interval)) % 10;
343 if (mark_interval > 60) {
344 mark_interval -= int (rint (mark_interval)) % 60;
346 if (mark_interval > 3600) {
347 mark_interval -= int (rint (mark_interval)) % 3600;
350 if (mark_interval < 1) {
354 wxGraphicsPath path = gc->CreatePath ();
355 path.MoveToPoint (_timeline.x_offset(), _y);
356 path.AddLineToPoint (_timeline.width(), _y);
357 gc->StrokePath (path);
359 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
361 /* Time in seconds */
363 while ((t.seconds() * pps) < _timeline.width()) {
364 wxGraphicsPath path = gc->CreatePath ();
365 path.MoveToPoint (time_x (t), _y - 4);
366 path.AddLineToPoint (time_x (t), _y + 4);
367 gc->StrokePath (path);
369 double tc = t.seconds ();
370 int const h = tc / 3600;
372 int const m = tc / 60;
376 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
379 wxDouble str_descent;
380 wxDouble str_leading;
381 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
383 int const tx = _timeline.x_offset() + t.seconds() * pps;
384 if ((tx + str_width) < _timeline.width()) {
385 gc->DrawText (str, time_x (t), _y + 16);
388 t += DCPTime::from_seconds (mark_interval);
397 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
398 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
399 , _content_panel (cp)
401 , _time_axis_view (new TimeAxisView (*this, 32))
404 , _down_view_position (0)
405 , _first_move (false)
410 SetDoubleBuffered (true);
413 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
414 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
415 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
416 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
417 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
418 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
422 SetMinSize (wxSize (640, tracks() * track_height() + 96));
424 _playlist_changed_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
425 _playlist_content_changed_connection = film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_content_changed, this, _2));
433 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
438 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
446 Timeline::playlist_changed ()
450 shared_ptr<const Film> fl = _film.lock ();
456 _views.push_back (_time_axis_view);
458 ContentList content = fl->playlist()->content ();
460 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
461 if (dynamic_pointer_cast<VideoContent> (*i)) {
462 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i)));
464 if (dynamic_pointer_cast<AudioContent> (*i)) {
465 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i)));
468 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (*i);
469 if (sc && sc->has_subtitles ()) {
470 _views.push_back (shared_ptr<View> (new SubtitleContentView (*this, sc)));
475 setup_pixels_per_second ();
480 Timeline::playlist_content_changed (int property)
484 if (property == ContentProperty::POSITION) {
486 setup_pixels_per_second ();
492 Timeline::assign_tracks ()
494 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
495 shared_ptr<ContentView> c = dynamic_pointer_cast<ContentView> (*i);
501 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
502 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
507 shared_ptr<Content> content = cv->content();
511 ViewList::iterator j = _views.begin();
512 while (j != _views.end()) {
513 shared_ptr<ContentView> test = dynamic_pointer_cast<ContentView> (*j);
519 shared_ptr<Content> test_content = test->content();
521 if (test && test->track() && test->track().get() == t) {
522 bool const no_overlap =
523 (content->position() < test_content->position() && content->end() < test_content->position()) ||
524 (content->position() > test_content->end() && content->end() > test_content->end());
527 /* we have an overlap on track `t' */
536 if (j == _views.end ()) {
537 /* no overlap on `t' */
543 _tracks = max (_tracks, t + 1);
546 _time_axis_view->set_y (tracks() * track_height() + 32);
550 Timeline::tracks () const
556 Timeline::setup_pixels_per_second ()
558 shared_ptr<const Film> film = _film.lock ();
559 if (!film || film->length() == DCPTime ()) {
563 _pixels_per_second = static_cast<double>(width() - x_offset() * 2) / film->length().seconds ();
567 Timeline::event_to_view (wxMouseEvent& ev)
569 ViewList::iterator i = _views.begin();
570 Position<int> const p (ev.GetX(), ev.GetY());
571 while (i != _views.end() && !(*i)->bbox().contains (p)) {
575 if (i == _views.end ()) {
576 return shared_ptr<View> ();
583 Timeline::left_down (wxMouseEvent& ev)
585 shared_ptr<View> view = event_to_view (ev);
586 shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
591 _down_view = content_view;
592 _down_view_position = content_view->content()->position ();
595 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
596 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
601 if (!ev.ShiftDown ()) {
602 cv->set_selected (view == *i);
606 _content_panel->set_selection (cv->content ());
610 if (content_view && ev.ShiftDown ()) {
611 content_view->set_selected (!content_view->selected ());
615 _down_point = ev.GetPosition ();
619 _down_view->content()->set_change_signals_frequent (true);
624 Timeline::left_up (wxMouseEvent& ev)
629 _down_view->content()->set_change_signals_frequent (false);
632 set_position_from_event (ev);
636 Timeline::mouse_moved (wxMouseEvent& ev)
642 set_position_from_event (ev);
646 Timeline::right_down (wxMouseEvent& ev)
648 shared_ptr<View> view = event_to_view (ev);
649 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
654 if (!cv->selected ()) {
656 cv->set_selected (true);
659 _menu.popup (_film, selected_content (), ev.GetPosition ());
663 Timeline::set_position_from_event (wxMouseEvent& ev)
665 if (!_pixels_per_second) {
669 double const pps = _pixels_per_second.get ();
671 wxPoint const p = ev.GetPosition();
674 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
675 before the drag is considered to have started.
677 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
688 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
693 DCPTime nearest_distance = DCPTime::max ();
694 DCPTime nearest_new_position = DCPTime::max ();
696 /* Find the nearest content edge; this is inefficient */
697 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
698 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
699 if (!cv || cv == _down_view) {
704 /* Snap starts to ends */
705 DCPTime const d = DCPTime (cv->content()->end() - new_position).abs ();
706 if (first || d < nearest_distance) {
707 nearest_distance = d;
708 nearest_new_position = cv->content()->end();
713 /* Snap ends to starts */
714 DCPTime const d = DCPTime (
715 cv->content()->position() - (new_position + _down_view->content()->length_after_trim())
718 if (d < nearest_distance) {
719 nearest_distance = d;
720 nearest_new_position = cv->content()->position() - _down_view->content()->length_after_trim ();
728 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
729 if (nearest_distance < DCPTime::from_seconds ((width() / pps) / 32)) {
730 new_position = nearest_new_position;
735 if (new_position < DCPTime ()) {
736 new_position = DCPTime ();
739 _down_view->content()->set_position (new_position);
741 shared_ptr<Film> film = _film.lock ();
743 film->set_sequence_video (false);
747 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
749 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
752 shared_ptr<const Film>
753 Timeline::film () const
755 return _film.lock ();
761 setup_pixels_per_second ();
765 Timeline::clear_selection ()
767 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
768 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
770 cv->set_selected (false);
775 Timeline::ContentViewList
776 Timeline::selected_views () const
780 for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
781 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
782 if (cv && cv->selected()) {
791 Timeline::selected_content () const
794 ContentViewList views = selected_views ();
796 for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
797 sel.push_back ((*i)->content ());