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"
28 #include "content_panel.h"
34 using boost::shared_ptr;
35 using boost::weak_ptr;
36 using boost::dynamic_pointer_cast;
38 using boost::optional;
41 * @brief Parent class for components of the timeline (e.g. a piece of content or an axis).
43 class View : public boost::noncopyable
54 void paint (wxGraphicsContext* g)
56 _last_paint_bbox = bbox ();
62 _timeline.force_redraw (_last_paint_bbox);
63 _timeline.force_redraw (bbox ());
66 virtual dcpomatic::Rect<int> bbox () const = 0;
69 virtual void do_paint (wxGraphicsContext *) = 0;
71 int time_x (DCPTime t) const
73 return _timeline.tracks_position().x + t.seconds() * _timeline.pixels_per_second().get_value_or (0);
79 dcpomatic::Rect<int> _last_paint_bbox;
83 /** @class ContentView
84 * @brief Parent class for views of pieces of content.
86 class ContentView : public View
89 ContentView (Timeline& tl, shared_ptr<Content> c)
94 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3));
97 dcpomatic::Rect<int> bbox () const
101 shared_ptr<const Film> film = _timeline.film ();
102 shared_ptr<const Content> content = _content.lock ();
103 if (!film || !content) {
104 return dcpomatic::Rect<int> ();
107 return dcpomatic::Rect<int> (
108 time_x (content->position ()) - 8,
109 y_pos (_track.get()) - 8,
110 content->length_after_trim().seconds() * _timeline.pixels_per_second().get_value_or(0) + 16,
111 _timeline.track_height() + 16
115 void set_selected (bool s) {
120 bool selected () const {
124 shared_ptr<Content> content () const {
125 return _content.lock ();
128 void set_track (int t) {
132 void unset_track () {
133 _track = boost::optional<int> ();
136 optional<int> track () const {
140 virtual wxString type () const = 0;
141 virtual wxColour background_colour () const = 0;
142 virtual wxColour foreground_colour () const = 0;
146 void do_paint (wxGraphicsContext* gc)
150 shared_ptr<const Film> film = _timeline.film ();
151 shared_ptr<const Content> cont = content ();
152 if (!film || !cont) {
156 DCPTime const position = cont->position ();
157 DCPTime const len = cont->length_after_trim ();
159 wxColour selected (background_colour().Red() / 2, background_colour().Green() / 2, background_colour().Blue() / 2);
161 gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 4, wxPENSTYLE_SOLID));
163 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
165 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (background_colour(), wxBRUSHSTYLE_SOLID));
168 wxGraphicsPath path = gc->CreatePath ();
169 path.MoveToPoint (time_x (position), y_pos (_track.get()) + 4);
170 path.AddLineToPoint (time_x (position + len), y_pos (_track.get()) + 4);
171 path.AddLineToPoint (time_x (position + len), y_pos (_track.get() + 1) - 4);
172 path.AddLineToPoint (time_x (position), y_pos (_track.get() + 1) - 4);
173 path.AddLineToPoint (time_x (position), y_pos (_track.get()) + 4);
174 gc->StrokePath (path);
177 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->summary()).data(), type().data());
179 wxDouble name_height;
180 wxDouble name_descent;
181 wxDouble name_leading;
182 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
184 gc->Clip (wxRegion (time_x (position), y_pos (_track.get()), len.seconds() * _timeline.pixels_per_second().get_value_or(0), _timeline.track_height()));
185 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT, foreground_colour ()));
186 gc->DrawText (name, time_x (position) + 12, y_pos (_track.get() + 1) - name_height - 4);
190 int y_pos (int t) const
192 return _timeline.tracks_position().y + t * _timeline.track_height();
195 void content_changed (int p, bool frequent)
199 if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
204 _timeline.setup_pixels_per_second ();
205 _timeline.Refresh ();
209 boost::weak_ptr<Content> _content;
210 optional<int> _track;
213 boost::signals2::scoped_connection _content_connection;
216 class AudioContentView : public ContentView
219 AudioContentView (Timeline& tl, shared_ptr<Content> c)
220 : ContentView (tl, c)
224 wxString type () const
229 wxColour background_colour () const
231 return wxColour (149, 121, 232, 255);
234 wxColour foreground_colour () const
236 return wxColour (0, 0, 0, 255);
240 class VideoContentView : public ContentView
243 VideoContentView (Timeline& tl, shared_ptr<Content> c)
244 : ContentView (tl, c)
249 wxString type () const
251 if (dynamic_pointer_cast<ImageContent> (content ()) && content()->number_of_paths() == 1) {
258 wxColour background_colour () const
260 return wxColour (242, 92, 120, 255);
263 wxColour foreground_colour () const
265 return wxColour (0, 0, 0, 255);
269 class SubtitleContentView : public ContentView
272 SubtitleContentView (Timeline& tl, shared_ptr<SubtitleContent> c)
273 : ContentView (tl, c)
274 , _subtitle_content (c)
278 wxString type () const
280 return _("subtitles");
283 wxColour background_colour () const
285 shared_ptr<SubtitleContent> sc = _subtitle_content.lock ();
286 if (!sc || !sc->use_subtitles ()) {
287 return wxColour (210, 210, 210, 128);
290 return wxColour (163, 255, 154, 255);
293 wxColour foreground_colour () const
295 shared_ptr<SubtitleContent> sc = _subtitle_content.lock ();
296 if (!sc || !sc->use_subtitles ()) {
297 return wxColour (180, 180, 180, 128);
300 return wxColour (0, 0, 0, 255);
303 boost::weak_ptr<SubtitleContent> _subtitle_content;
306 class TimeAxisView : public View
309 TimeAxisView (Timeline& tl, int y)
314 dcpomatic::Rect<int> bbox () const
316 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
327 void do_paint (wxGraphicsContext* gc)
329 if (!_timeline.pixels_per_second()) {
333 double const pps = _timeline.pixels_per_second().get ();
335 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
337 double mark_interval = rint (128 / pps);
338 if (mark_interval > 5) {
339 mark_interval -= int (rint (mark_interval)) % 5;
341 if (mark_interval > 10) {
342 mark_interval -= int (rint (mark_interval)) % 10;
344 if (mark_interval > 60) {
345 mark_interval -= int (rint (mark_interval)) % 60;
347 if (mark_interval > 3600) {
348 mark_interval -= int (rint (mark_interval)) % 3600;
351 if (mark_interval < 1) {
355 wxGraphicsPath path = gc->CreatePath ();
356 path.MoveToPoint (_timeline.x_offset(), _y);
357 path.AddLineToPoint (_timeline.width(), _y);
358 gc->StrokePath (path);
360 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
362 /* Time in seconds */
364 while ((t.seconds() * pps) < _timeline.width()) {
365 wxGraphicsPath path = gc->CreatePath ();
366 path.MoveToPoint (time_x (t), _y - 4);
367 path.AddLineToPoint (time_x (t), _y + 4);
368 gc->StrokePath (path);
370 double tc = t.seconds ();
371 int const h = tc / 3600;
373 int const m = tc / 60;
377 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
380 wxDouble str_descent;
381 wxDouble str_leading;
382 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
384 int const tx = _timeline.x_offset() + t.seconds() * pps;
385 if ((tx + str_width) < _timeline.width()) {
386 gc->DrawText (str, time_x (t), _y + 16);
389 t += DCPTime::from_seconds (mark_interval);
398 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
399 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
400 , _content_panel (cp)
402 , _time_axis_view (new TimeAxisView (*this, 32))
405 , _down_view_position (0)
406 , _first_move (false)
411 SetDoubleBuffered (true);
414 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
415 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
416 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
417 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
418 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
419 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
423 SetMinSize (wxSize (640, tracks() * track_height() + 96));
425 _playlist_changed_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
426 _playlist_content_changed_connection = film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_content_changed, this, _2));
434 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
439 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
447 Timeline::playlist_changed ()
451 shared_ptr<const Film> fl = _film.lock ();
457 _views.push_back (_time_axis_view);
459 ContentList content = fl->playlist()->content ();
461 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
462 if (dynamic_pointer_cast<VideoContent> (*i)) {
463 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i)));
465 if (dynamic_pointer_cast<AudioContent> (*i)) {
466 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i)));
469 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (*i);
470 if (sc && sc->has_subtitles ()) {
471 _views.push_back (shared_ptr<View> (new SubtitleContentView (*this, sc)));
476 setup_pixels_per_second ();
481 Timeline::playlist_content_changed (int property)
485 if (property == ContentProperty::POSITION) {
487 setup_pixels_per_second ();
493 Timeline::assign_tracks ()
495 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
496 shared_ptr<ContentView> c = dynamic_pointer_cast<ContentView> (*i);
502 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
503 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
508 shared_ptr<Content> content = cv->content();
512 ViewList::iterator j = _views.begin();
513 while (j != _views.end()) {
514 shared_ptr<ContentView> test = dynamic_pointer_cast<ContentView> (*j);
520 shared_ptr<Content> test_content = test->content();
522 if (test && test->track() && test->track().get() == t) {
523 bool const no_overlap =
524 (content->position() < test_content->position() && content->end() < test_content->position()) ||
525 (content->position() > test_content->end() && content->end() > test_content->end());
528 /* we have an overlap on track `t' */
537 if (j == _views.end ()) {
538 /* no overlap on `t' */
544 _tracks = max (_tracks, t + 1);
547 _time_axis_view->set_y (tracks() * track_height() + 32);
551 Timeline::tracks () const
557 Timeline::setup_pixels_per_second ()
559 shared_ptr<const Film> film = _film.lock ();
560 if (!film || film->length() == DCPTime ()) {
564 _pixels_per_second = static_cast<double>(width() - x_offset() * 2) / film->length().seconds ();
568 Timeline::event_to_view (wxMouseEvent& ev)
570 ViewList::iterator i = _views.begin();
571 Position<int> const p (ev.GetX(), ev.GetY());
572 while (i != _views.end() && !(*i)->bbox().contains (p)) {
576 if (i == _views.end ()) {
577 return shared_ptr<View> ();
584 Timeline::left_down (wxMouseEvent& ev)
586 shared_ptr<View> view = event_to_view (ev);
587 shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
592 _down_view = content_view;
593 _down_view_position = content_view->content()->position ();
596 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
597 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
602 if (!ev.ShiftDown ()) {
603 cv->set_selected (view == *i);
607 _content_panel->set_selection (cv->content ());
611 if (content_view && ev.ShiftDown ()) {
612 content_view->set_selected (!content_view->selected ());
616 _down_point = ev.GetPosition ();
620 _down_view->content()->set_change_signals_frequent (true);
625 Timeline::left_up (wxMouseEvent& ev)
630 _down_view->content()->set_change_signals_frequent (false);
633 set_position_from_event (ev);
637 Timeline::mouse_moved (wxMouseEvent& ev)
643 set_position_from_event (ev);
647 Timeline::right_down (wxMouseEvent& ev)
649 shared_ptr<View> view = event_to_view (ev);
650 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
655 if (!cv->selected ()) {
657 cv->set_selected (true);
660 _menu.popup (_film, selected_content (), ev.GetPosition ());
664 Timeline::set_position_from_event (wxMouseEvent& ev)
666 if (!_pixels_per_second) {
670 double const pps = _pixels_per_second.get ();
672 wxPoint const p = ev.GetPosition();
675 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
676 before the drag is considered to have started.
678 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
689 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
694 DCPTime nearest_distance = DCPTime::max ();
695 DCPTime nearest_new_position = DCPTime::max ();
697 /* Find the nearest content edge; this is inefficient */
698 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
699 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
700 if (!cv || cv == _down_view) {
705 /* Snap starts to ends */
706 DCPTime const d = DCPTime (cv->content()->end() - new_position).abs ();
707 if (first || d < nearest_distance) {
708 nearest_distance = d;
709 nearest_new_position = cv->content()->end();
714 /* Snap ends to starts */
715 DCPTime const d = DCPTime (
716 cv->content()->position() - (new_position + _down_view->content()->length_after_trim())
719 if (d < nearest_distance) {
720 nearest_distance = d;
721 nearest_new_position = cv->content()->position() - _down_view->content()->length_after_trim ();
729 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
730 if (nearest_distance < DCPTime::from_seconds ((width() / pps) / 32)) {
731 new_position = nearest_new_position;
736 if (new_position < DCPTime ()) {
737 new_position = DCPTime ();
740 _down_view->content()->set_position (new_position);
742 shared_ptr<Film> film = _film.lock ();
744 film->set_sequence_video (false);
748 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
750 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
753 shared_ptr<const Film>
754 Timeline::film () const
756 return _film.lock ();
762 setup_pixels_per_second ();
766 Timeline::clear_selection ()
768 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
769 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
771 cv->set_selected (false);
776 Timeline::ContentViewList
777 Timeline::selected_views () const
781 for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
782 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
783 if (cv && cv->selected()) {
792 Timeline::selected_content () const
795 ContentViewList views = selected_views ();
797 for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
798 sel.push_back ((*i)->content ());