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));
86 dcpomatic::Rect<int> bbox () const
88 shared_ptr<const Film> film = _timeline.film ();
90 return dcpomatic::Rect<int> ();
93 return dcpomatic::Rect<int> (
94 time_x (_content->start ()) - 8,
96 _content->length () * _timeline.pixels_per_time_unit() + 16,
97 _timeline.track_height() + 16
101 void set_selected (bool s) {
106 bool selected () const {
110 shared_ptr<Content> content () const {
114 void set_track (int t) {
122 virtual wxString type () const = 0;
123 virtual wxColour colour () const = 0;
127 void do_paint (wxGraphicsContext* gc)
129 shared_ptr<const Film> film = _timeline.film ();
134 Time const start = _content->start ();
135 Time const len = _content->length ();
137 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
139 gc->SetPen (*wxBLACK_PEN);
141 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
143 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
145 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
148 wxGraphicsPath path = gc->CreatePath ();
149 path.MoveToPoint (time_x (start), y_pos (_track) + 4);
150 path.AddLineToPoint (time_x (start + len), y_pos (_track) + 4);
151 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1) - 4);
152 path.AddLineToPoint (time_x (start), y_pos (_track + 1) - 4);
153 path.AddLineToPoint (time_x (start), y_pos (_track) + 4);
154 gc->StrokePath (path);
157 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (_content->file().filename().string()).data(), type().data());
159 wxDouble name_height;
160 wxDouble name_descent;
161 wxDouble name_leading;
162 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
164 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
165 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
169 int y_pos (int t) const
171 return _timeline.tracks_position().y + t * _timeline.track_height();
174 void content_changed (int p)
176 if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
181 /* This must be a shared_ptr, not a weak_ptr, as in the looped case this
182 will be the only remaining pointer to the looped content that we get
185 boost::shared_ptr<Content> _content;
189 boost::signals2::scoped_connection _content_connection;
192 class AudioContentView : public ContentView
195 AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
196 : ContentView (tl, c, t)
200 wxString type () const
205 wxColour colour () const
207 return wxColour (149, 121, 232, 255);
211 class VideoContentView : public ContentView
214 VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
215 : ContentView (tl, c, t)
220 wxString type () const
222 if (dynamic_pointer_cast<FFmpegContent> (content ())) {
229 wxColour colour () const
231 return wxColour (242, 92, 120, 255);
235 class TimeAxisView : public View
238 TimeAxisView (Timeline& tl, int y)
243 dcpomatic::Rect<int> bbox () const
245 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
256 void do_paint (wxGraphicsContext* gc)
258 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
260 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
261 if (mark_interval > 5) {
262 mark_interval -= mark_interval % 5;
264 if (mark_interval > 10) {
265 mark_interval -= mark_interval % 10;
267 if (mark_interval > 60) {
268 mark_interval -= mark_interval % 60;
270 if (mark_interval > 3600) {
271 mark_interval -= mark_interval % 3600;
274 if (mark_interval < 1) {
278 wxGraphicsPath path = gc->CreatePath ();
279 path.MoveToPoint (_timeline.x_offset(), _y);
280 path.AddLineToPoint (_timeline.width(), _y);
281 gc->StrokePath (path);
284 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
285 wxGraphicsPath path = gc->CreatePath ();
286 path.MoveToPoint (time_x (t), _y - 4);
287 path.AddLineToPoint (time_x (t), _y + 4);
288 gc->StrokePath (path);
290 int tc = t / TIME_HZ;
291 int const h = tc / 3600;
293 int const m = tc / 60;
297 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
300 wxDouble str_descent;
301 wxDouble str_leading;
302 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
304 int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
305 if ((tx + str_width) < _timeline.width()) {
306 gc->DrawText (str, time_x (t), _y + 16);
309 t += mark_interval * TIME_HZ;
317 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
318 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
321 , _time_axis_view (new TimeAxisView (*this, 32))
323 , _pixels_per_time_unit (0)
325 , _down_view_start (0)
326 , _first_move (false)
329 SetDoubleBuffered (true);
332 setup_pixels_per_time_unit ();
334 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
335 Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
336 Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
337 Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
338 Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
342 SetMinSize (wxSize (640, tracks() * track_height() + 96));
344 _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
346 _views.push_back (_time_axis_view);
350 Timeline::paint (wxPaintEvent &)
354 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
359 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
361 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
369 Timeline::playlist_changed ()
371 shared_ptr<const Film> fl = _film.lock ();
378 Playlist::ContentList content = fl->playlist()->content_with_loop ();
380 for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
381 if (dynamic_pointer_cast<VideoContent> (*i)) {
382 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
384 if (dynamic_pointer_cast<AudioContent> (*i)) {
385 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 0)));
394 Timeline::assign_tracks ()
396 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
397 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
404 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
405 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
410 shared_ptr<Content> acv_content = acv->content();
414 list<shared_ptr<View> >::iterator j = _views.begin();
415 while (j != _views.end()) {
416 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
422 shared_ptr<Content> test_content = test->content();
424 if (test && test->track() == t) {
425 if ((acv_content->start() < test_content->start() && test_content->start() < acv_content->end()) ||
426 (acv_content->start() < test_content->end() && test_content->end() < acv_content->end())) {
427 /* we have an overlap on track `t' */
436 if (j == _views.end ()) {
437 /* no overlap on `t' */
443 _tracks = max (_tracks, t + 1);
446 _time_axis_view->set_y (tracks() * track_height() + 32);
450 Timeline::tracks () const
456 Timeline::setup_pixels_per_time_unit ()
458 shared_ptr<const Film> film = _film.lock ();
463 _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length_with_loop();
467 Timeline::left_down (wxMouseEvent& ev)
469 list<shared_ptr<View> >::iterator i = _views.begin();
470 Position<int> const p (ev.GetX(), ev.GetY());
471 while (i != _views.end() && !(*i)->bbox().contains (p)) {
477 if (i != _views.end ()) {
478 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
481 _down_view_start = cv->content()->start ();
485 for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
486 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
488 cv->set_selected (i == j);
490 _film_editor->set_selection (cv->content ());
496 _down_point = ev.GetPosition ();
500 _down_view->content()->set_change_signals_frequent (true);
505 Timeline::left_up (wxMouseEvent& ev)
510 _down_view->content()->set_change_signals_frequent (false);
513 set_start_from_event (ev);
517 Timeline::mouse_moved (wxMouseEvent& ev)
523 set_start_from_event (ev);
527 Timeline::set_start_from_event (wxMouseEvent& ev)
529 wxPoint const p = ev.GetPosition();
532 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
539 Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
541 _down_view->content()->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
543 shared_ptr<Film> film = _film.lock ();
545 film->set_sequence_video (false);
550 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
552 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
555 shared_ptr<const Film>
556 Timeline::film () const
558 return _film.lock ();
562 Timeline::resized (wxSizeEvent &)
564 setup_pixels_per_time_unit ();