Take Film pointer out of Content.
[dcpomatic.git] / src / wx / timeline_content_view.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "timeline_content_view.h"
22 #include "timeline.h"
23 #include "wx_util.h"
24 #include "lib/content.h"
25 #include <wx/graphics.h>
26 #include <boost/foreach.hpp>
27
28 using std::list;
29 using boost::shared_ptr;
30
31 TimelineContentView::TimelineContentView (Timeline& tl, shared_ptr<Content> c)
32         : TimelineView (tl)
33         , _content (c)
34         , _selected (false)
35 {
36         _content_connection = c->Change.connect (bind (&TimelineContentView::content_change, this, _1, _3));
37 }
38
39 dcpomatic::Rect<int>
40 TimelineContentView::bbox () const
41 {
42         DCPOMATIC_ASSERT (_track);
43
44         shared_ptr<const Film> film = _timeline.film ();
45         shared_ptr<const Content> content = _content.lock ();
46         if (!film || !content) {
47                 return dcpomatic::Rect<int> ();
48         }
49
50         return dcpomatic::Rect<int> (
51                 time_x (content->position ()),
52                 y_pos (_track.get()),
53                 content->length_after_trim(film).seconds() * _timeline.pixels_per_second().get_value_or(0),
54                 _timeline.pixels_per_track()
55                 );
56 }
57
58 void
59 TimelineContentView::set_selected (bool s)
60 {
61         _selected = s;
62         force_redraw ();
63 }
64
65 bool
66 TimelineContentView::selected () const
67 {
68         return _selected;
69 }
70
71 shared_ptr<Content>
72 TimelineContentView::content () const
73 {
74         return _content.lock ();
75 }
76
77 void
78 TimelineContentView::set_track (int t)
79 {
80         _track = t;
81 }
82
83 void
84 TimelineContentView::unset_track ()
85 {
86         _track = boost::optional<int> ();
87 }
88
89 boost::optional<int>
90 TimelineContentView::track () const
91 {
92         return _track;
93 }
94
95 void
96 TimelineContentView::do_paint (wxGraphicsContext* gc, list<dcpomatic::Rect<int> > overlaps)
97 {
98         DCPOMATIC_ASSERT (_track);
99
100         shared_ptr<const Film> film = _timeline.film ();
101         shared_ptr<const Content> cont = content ();
102         if (!film || !cont) {
103                 return;
104         }
105
106         DCPTime const position = cont->position ();
107         DCPTime const len = cont->length_after_trim (film);
108
109         wxColour selected (background_colour().Red() / 2, background_colour().Green() / 2, background_colour().Blue() / 2);
110
111         gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 4, wxPENSTYLE_SOLID));
112         if (_selected) {
113                 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
114         } else {
115                 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (background_colour(), wxBRUSHSTYLE_SOLID));
116         }
117
118         /* Outline */
119         wxGraphicsPath path = gc->CreatePath ();
120         path.MoveToPoint    (time_x (position) + 2,           y_pos (_track.get()) + 4);
121         path.AddLineToPoint (time_x (position + len) - 1,     y_pos (_track.get()) + 4);
122         path.AddLineToPoint (time_x (position + len) - 1,     y_pos (_track.get() + 1) - 4);
123         path.AddLineToPoint (time_x (position) + 2,           y_pos (_track.get() + 1) - 4);
124         path.AddLineToPoint (time_x (position) + 2,           y_pos (_track.get()) + 4);
125         gc->StrokePath (path);
126         gc->FillPath (path);
127
128         /* Reel split points */
129         gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 1, wxPENSTYLE_DOT));
130         BOOST_FOREACH (DCPTime i, cont->reel_split_points ()) {
131                 path = gc->CreatePath ();
132                 path.MoveToPoint (time_x (i), y_pos (_track.get()) + 4);
133                 path.AddLineToPoint (time_x (i), y_pos (_track.get() + 1) - 4);
134                 gc->StrokePath (path);
135         }
136
137         /* Overlaps */
138         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (foreground_colour(), wxBRUSHSTYLE_CROSSDIAG_HATCH));
139         for (list<dcpomatic::Rect<int> >::const_iterator i = overlaps.begin(); i != overlaps.end(); ++i) {
140                 gc->DrawRectangle (i->x, i->y + 4, i->width, i->height - 8);
141         }
142
143         /* Label text */
144         wxString lab = label ();
145         wxDouble lab_width;
146         wxDouble lab_height;
147         wxDouble lab_descent;
148         wxDouble lab_leading;
149         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT, foreground_colour ()));
150         gc->GetTextExtent (lab, &lab_width, &lab_height, &lab_descent, &lab_leading);
151         gc->Clip (wxRegion (time_x (position), y_pos (_track.get()), len.seconds() * _timeline.pixels_per_second().get_value_or(0), _timeline.pixels_per_track()));
152         gc->DrawText (lab, time_x (position) + 12, y_pos (_track.get() + 1) - lab_height - 4);
153         gc->ResetClip ();
154 }
155
156 int
157 TimelineContentView::y_pos (int t) const
158 {
159         return t * _timeline.pixels_per_track() + _timeline.tracks_y_offset();
160 }
161
162 void
163 TimelineContentView::content_change (ChangeType type, int p)
164 {
165         if (type != CHANGE_TYPE_DONE) {
166                 return;
167         }
168
169         ensure_ui_thread ();
170
171         if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
172                 force_redraw ();
173         }
174 }
175
176 wxString
177 TimelineContentView::label () const
178 {
179         return std_to_wx(content()->summary());
180 }