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;
37 class View : public boost::noncopyable
46 void paint (wxGraphicsContext* g)
48 _last_paint_bbox = bbox ();
54 _timeline.force_redraw (_last_paint_bbox);
55 _timeline.force_redraw (bbox ());
58 virtual dcpomatic::Rect<int> bbox () const = 0;
61 virtual void do_paint (wxGraphicsContext *) = 0;
63 int time_x (Time t) const
65 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
71 dcpomatic::Rect<int> _last_paint_bbox;
74 class ContentView : public View
77 ContentView (Timeline& tl, shared_ptr<Content> c, int t)
83 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3));
86 dcpomatic::Rect<int> bbox () const
88 shared_ptr<const Film> film = _timeline.film ();
89 shared_ptr<const Content> content = _content.lock ();
90 if (!film || !content) {
91 return dcpomatic::Rect<int> ();
94 return dcpomatic::Rect<int> (
95 time_x (content->start ()) - 8,
97 content->length () * _timeline.pixels_per_time_unit() + 16,
98 _timeline.track_height() + 16
102 void set_selected (bool s) {
107 bool selected () const {
111 shared_ptr<Content> content () const {
112 return _content.lock ();
115 void set_track (int t) {
123 virtual wxString type () const = 0;
124 virtual wxColour colour () const = 0;
128 void do_paint (wxGraphicsContext* gc)
130 shared_ptr<const Film> film = _timeline.film ();
131 shared_ptr<const Content> cont = content ();
132 if (!film || !cont) {
136 Time const start = cont->start ();
137 Time const len = cont->length ();
139 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
141 gc->SetPen (*wxBLACK_PEN);
143 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
145 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
147 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
150 wxGraphicsPath path = gc->CreatePath ();
151 path.MoveToPoint (time_x (start), y_pos (_track) + 4);
152 path.AddLineToPoint (time_x (start + len), y_pos (_track) + 4);
153 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1) - 4);
154 path.AddLineToPoint (time_x (start), y_pos (_track + 1) - 4);
155 path.AddLineToPoint (time_x (start), y_pos (_track) + 4);
156 gc->StrokePath (path);
159 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->file().filename().string()).data(), type().data());
161 wxDouble name_height;
162 wxDouble name_descent;
163 wxDouble name_leading;
164 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
166 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
167 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
171 int y_pos (int t) const
173 return _timeline.tracks_position().y + t * _timeline.track_height();
176 void content_changed (int p, bool frequent)
180 if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
185 _timeline.setup_pixels_per_time_unit ();
186 _timeline.Refresh ();
190 boost::weak_ptr<Content> _content;
194 boost::signals2::scoped_connection _content_connection;
197 class AudioContentView : public ContentView
200 AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
201 : ContentView (tl, c, t)
205 wxString type () const
210 wxColour colour () const
212 return wxColour (149, 121, 232, 255);
216 class VideoContentView : public ContentView
219 VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
220 : ContentView (tl, c, t)
225 wxString type () const
227 if (dynamic_pointer_cast<FFmpegContent> (content ())) {
234 wxColour colour () const
236 return wxColour (242, 92, 120, 255);
240 class TimeAxisView : public View
243 TimeAxisView (Timeline& tl, int y)
248 dcpomatic::Rect<int> bbox () const
250 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
261 void do_paint (wxGraphicsContext* gc)
263 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
265 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
266 if (mark_interval > 5) {
267 mark_interval -= mark_interval % 5;
269 if (mark_interval > 10) {
270 mark_interval -= mark_interval % 10;
272 if (mark_interval > 60) {
273 mark_interval -= mark_interval % 60;
275 if (mark_interval > 3600) {
276 mark_interval -= mark_interval % 3600;
279 if (mark_interval < 1) {
283 wxGraphicsPath path = gc->CreatePath ();
284 path.MoveToPoint (_timeline.x_offset(), _y);
285 path.AddLineToPoint (_timeline.width(), _y);
286 gc->StrokePath (path);
289 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
290 wxGraphicsPath path = gc->CreatePath ();
291 path.MoveToPoint (time_x (t), _y - 4);
292 path.AddLineToPoint (time_x (t), _y + 4);
293 gc->StrokePath (path);
295 int tc = t / TIME_HZ;
296 int const h = tc / 3600;
298 int const m = tc / 60;
302 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
305 wxDouble str_descent;
306 wxDouble str_leading;
307 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
309 int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
310 if ((tx + str_width) < _timeline.width()) {
311 gc->DrawText (str, time_x (t), _y + 16);
314 t += mark_interval * TIME_HZ;
322 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
323 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
326 , _time_axis_view (new TimeAxisView (*this, 32))
328 , _pixels_per_time_unit (0)
330 , _down_view_start (0)
331 , _first_move (false)
335 SetDoubleBuffered (true);
338 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
339 Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
340 Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
341 Connect (wxID_ANY, wxEVT_RIGHT_DOWN, wxMouseEventHandler (Timeline::right_down), 0, this);
342 Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
343 Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
347 SetMinSize (wxSize (640, tracks() * track_height() + 96));
349 _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
353 Timeline::paint (wxPaintEvent &)
357 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
362 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
364 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
372 Timeline::playlist_changed ()
376 shared_ptr<const Film> fl = _film.lock ();
382 _views.push_back (_time_axis_view);
384 ContentList content = fl->playlist()->content ();
386 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
387 if (dynamic_pointer_cast<VideoContent> (*i)) {
388 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
390 if (dynamic_pointer_cast<AudioContent> (*i)) {
391 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 0)));
396 setup_pixels_per_time_unit ();
401 Timeline::assign_tracks ()
403 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
404 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
411 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
412 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
417 shared_ptr<Content> acv_content = acv->content();
421 ViewList::iterator j = _views.begin();
422 while (j != _views.end()) {
423 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
429 shared_ptr<Content> test_content = test->content();
431 if (test && test->track() == t) {
432 if ((acv_content->start() < test_content->start() && test_content->start() < acv_content->end()) ||
433 (acv_content->start() < test_content->end() && test_content->end() < acv_content->end())) {
434 /* we have an overlap on track `t' */
443 if (j == _views.end ()) {
444 /* no overlap on `t' */
450 _tracks = max (_tracks, t + 1);
453 _time_axis_view->set_y (tracks() * track_height() + 32);
457 Timeline::tracks () const
463 Timeline::setup_pixels_per_time_unit ()
465 shared_ptr<const Film> film = _film.lock ();
470 _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length ();
474 Timeline::event_to_view (wxMouseEvent& ev)
476 ViewList::iterator i = _views.begin();
477 Position<int> const p (ev.GetX(), ev.GetY());
478 while (i != _views.end() && !(*i)->bbox().contains (p)) {
482 if (i == _views.end ()) {
483 return shared_ptr<View> ();
490 Timeline::left_down (wxMouseEvent& ev)
492 shared_ptr<View> view = event_to_view (ev);
493 shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
498 _down_view = content_view;
499 _down_view_start = content_view->content()->start ();
502 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
503 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
508 if (!ev.ShiftDown ()) {
509 cv->set_selected (view == *i);
513 _film_editor->set_selection (cv->content ());
517 if (content_view && ev.ShiftDown ()) {
518 content_view->set_selected (!content_view->selected ());
522 _down_point = ev.GetPosition ();
526 _down_view->content()->set_change_signals_frequent (true);
531 Timeline::left_up (wxMouseEvent& ev)
536 _down_view->content()->set_change_signals_frequent (false);
539 set_start_from_event (ev);
543 Timeline::mouse_moved (wxMouseEvent& ev)
549 set_start_from_event (ev);
553 Timeline::right_down (wxMouseEvent& ev)
555 shared_ptr<View> view = event_to_view (ev);
556 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
561 if (!cv->selected ()) {
563 cv->set_selected (true);
566 _menu.popup (selected_content (), ev.GetPosition ());
570 Timeline::set_start_from_event (wxMouseEvent& ev)
572 wxPoint const p = ev.GetPosition();
575 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
582 Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
584 _down_view->content()->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
586 shared_ptr<Film> film = _film.lock ();
588 film->set_sequence_video (false);
593 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
595 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
598 shared_ptr<const Film>
599 Timeline::film () const
601 return _film.lock ();
605 Timeline::resized (wxSizeEvent &)
607 setup_pixels_per_time_unit ();
611 Timeline::clear_selection ()
613 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
614 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
616 cv->set_selected (false);
621 Timeline::ContentViewList
622 Timeline::selected_views () const
626 for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
627 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
628 if (cv && cv->selected()) {
637 Timeline::selected_content () const
640 ContentViewList views = selected_views ();
642 for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
643 sel.push_back ((*i)->content ());