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 "film_editor.h"
27 #include "lib/playlist.h"
32 using boost::shared_ptr;
33 using boost::weak_ptr;
34 using boost::dynamic_pointer_cast;
46 void paint (wxGraphicsContext* g)
48 _last_paint_bbox = bbox ();
54 _timeline.force_redraw (_last_paint_bbox);
55 _timeline.force_redraw (bbox ());
58 virtual Rect 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 Rect _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));
88 shared_ptr<const Film> film = _timeline.film ();
89 shared_ptr<const Content> content = _content.lock ();
90 if (!film || !content) {
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 weak_ptr<Content> content () const {
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> content = _content.lock ();
132 if (!film || !content) {
136 Time const start = content->start ();
137 Time const len = content->length ();
139 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
141 gc->SetPen (*wxBLACK_PEN);
143 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
144 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
146 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
148 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
151 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
153 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxSOLID));
155 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
159 wxGraphicsPath path = gc->CreatePath ();
160 path.MoveToPoint (time_x (start), y_pos (_track) + 4);
161 path.AddLineToPoint (time_x (start + len), y_pos (_track) + 4);
162 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1) - 4);
163 path.AddLineToPoint (time_x (start), y_pos (_track + 1) - 4);
164 path.AddLineToPoint (time_x (start), y_pos (_track) + 4);
165 gc->StrokePath (path);
168 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).data(), type().data());
170 wxDouble name_height;
171 wxDouble name_descent;
172 wxDouble name_leading;
173 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
175 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
176 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
180 int y_pos (int t) const
182 return _timeline.tracks_position().y + t * _timeline.track_height();
185 void content_changed (int p)
187 if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
192 boost::weak_ptr<Content> _content;
196 boost::signals2::scoped_connection _content_connection;
199 class AudioContentView : public ContentView
202 AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
203 : ContentView (tl, c, t)
207 wxString type () const
212 wxColour colour () const
214 return wxColour (149, 121, 232, 255);
218 class VideoContentView : public ContentView
221 VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
222 : ContentView (tl, c, t)
227 wxString type () const
232 wxColour colour () const
234 return wxColour (242, 92, 120, 255);
238 class TimeAxisView : public View
241 TimeAxisView (Timeline& tl, int y)
248 return Rect (0, _y - 4, _timeline.width(), 24);
259 void do_paint (wxGraphicsContext* gc)
261 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
262 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
264 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
267 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
268 if (mark_interval > 5) {
269 mark_interval -= mark_interval % 5;
271 if (mark_interval > 10) {
272 mark_interval -= mark_interval % 10;
274 if (mark_interval > 60) {
275 mark_interval -= mark_interval % 60;
277 if (mark_interval > 3600) {
278 mark_interval -= mark_interval % 3600;
281 if (mark_interval < 1) {
285 wxGraphicsPath path = gc->CreatePath ();
286 path.MoveToPoint (_timeline.x_offset(), _y);
287 path.AddLineToPoint (_timeline.width(), _y);
288 gc->StrokePath (path);
291 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
292 wxGraphicsPath path = gc->CreatePath ();
293 path.MoveToPoint (time_x (t), _y - 4);
294 path.AddLineToPoint (time_x (t), _y + 4);
295 gc->StrokePath (path);
297 int tc = t / TIME_HZ;
298 int const h = tc / 3600;
300 int const m = tc / 60;
304 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
307 wxDouble str_descent;
308 wxDouble str_leading;
309 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
311 int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
312 if ((tx + str_width) < _timeline.width()) {
313 gc->DrawText (str, time_x (t), _y + 16);
316 t += mark_interval * TIME_HZ;
324 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
325 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
328 , _time_axis_view (new TimeAxisView (*this, 32))
330 , _pixels_per_time_unit (0)
332 , _down_view_start (0)
333 , _first_move (false)
335 SetDoubleBuffered (true);
337 setup_pixels_per_time_unit ();
339 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
340 Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
341 Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 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));
351 _views.push_back (_time_axis_view);
355 Timeline::paint (wxPaintEvent &)
359 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
364 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
366 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
374 Timeline::playlist_changed ()
376 shared_ptr<const Film> fl = _film.lock ();
383 Playlist::ContentList content = fl->playlist()->content ();
385 for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
386 if (dynamic_pointer_cast<VideoContent> (*i)) {
387 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
389 if (dynamic_pointer_cast<AudioContent> (*i)) {
390 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 0)));
399 Timeline::assign_tracks ()
401 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
402 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
409 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
410 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
415 shared_ptr<Content> acv_content = acv->content().lock ();
416 assert (acv_content);
420 list<shared_ptr<View> >::iterator j = _views.begin();
421 while (j != _views.end()) {
422 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
428 shared_ptr<Content> test_content = test->content().lock ();
429 assert (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::left_down (wxMouseEvent& ev)
476 list<shared_ptr<View> >::iterator i = _views.begin();
477 Position const p (ev.GetX(), ev.GetY());
478 while (i != _views.end() && !(*i)->bbox().contains (p)) {
484 if (i != _views.end ()) {
485 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
488 shared_ptr<Content> c = cv->content().lock();
490 _down_view_start = c->start ();
494 for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
495 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
497 cv->set_selected (i == j);
499 _film_editor->set_selection (cv->content ());
505 _down_point = ev.GetPosition ();
510 Timeline::left_up (wxMouseEvent &)
516 Timeline::mouse_moved (wxMouseEvent& ev)
522 wxPoint const p = ev.GetPosition();
525 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
532 Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
534 shared_ptr<Content> c = _down_view->content().lock();
536 c->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
538 shared_ptr<Film> film = _film.lock ();
540 film->set_sequence_video (false);
546 Timeline::force_redraw (Rect const & r)
548 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
551 shared_ptr<const Film>
552 Timeline::film () const
554 return _film.lock ();
558 Timeline::resized (wxSizeEvent &)
560 setup_pixels_per_time_unit ();