Tidying.
[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
22 #include "timeline.h"
23 #include "timeline_content_view.h"
24 #include "wx_util.h"
25 #include "lib/content.h"
26 #include <wx/graphics.h>
27
28
29 using std::list;
30 using std::shared_ptr;
31 using namespace dcpomatic;
32 #if BOOST_VERSION >= 106100
33 using namespace boost::placeholders;
34 #endif
35
36
37 TimelineContentView::TimelineContentView (Timeline& tl, shared_ptr<Content> c)
38         : TimelineView (tl)
39         , _content (c)
40 {
41         _content_connection = c->Change.connect (bind (&TimelineContentView::content_change, this, _1, _3));
42 }
43
44
45 dcpomatic::Rect<int>
46 TimelineContentView::bbox () const
47 {
48         DCPOMATIC_ASSERT (_track);
49
50         auto film = _timeline.film ();
51         auto content = _content.lock ();
52         if (!film || !content) {
53                 return {};
54         }
55
56         return dcpomatic::Rect<int> (
57                 time_x (content->position ()),
58                 y_pos (_track.get()),
59                 content->length_after_trim(film).seconds() * _timeline.pixels_per_second().get_value_or(0),
60                 _timeline.pixels_per_track()
61                 );
62 }
63
64
65 void
66 TimelineContentView::set_selected (bool s)
67 {
68         _selected = s;
69         force_redraw ();
70 }
71
72
73 bool
74 TimelineContentView::selected () const
75 {
76         return _selected;
77 }
78
79
80 shared_ptr<Content>
81 TimelineContentView::content () const
82 {
83         return _content.lock ();
84 }
85
86
87 void
88 TimelineContentView::set_track (int t)
89 {
90         _track = t;
91 }
92
93
94 void
95 TimelineContentView::unset_track ()
96 {
97         _track = boost::optional<int>();
98 }
99
100
101 boost::optional<int>
102 TimelineContentView::track () const
103 {
104         return _track;
105 }
106
107
108 void
109 TimelineContentView::do_paint (wxGraphicsContext* gc, list<dcpomatic::Rect<int>> overlaps)
110 {
111         DCPOMATIC_ASSERT (_track);
112
113         auto film = _timeline.film ();
114         auto cont = content ();
115         if (!film || !cont) {
116                 return;
117         }
118
119         auto const position = cont->position ();
120         auto const len = cont->length_after_trim (film);
121
122         wxColour selected (background_colour().Red() / 2, background_colour().Green() / 2, background_colour().Blue() / 2);
123
124         gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 4, wxPENSTYLE_SOLID));
125         if (_selected) {
126                 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
127         } else {
128                 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (background_colour(), wxBRUSHSTYLE_SOLID));
129         }
130
131         /* Outline */
132         auto path = gc->CreatePath ();
133         path.MoveToPoint    (time_x (position) + 2,           y_pos (_track.get()) + 4);
134         path.AddLineToPoint (time_x (position + len) - 1,     y_pos (_track.get()) + 4);
135         path.AddLineToPoint (time_x (position + len) - 1,     y_pos (_track.get() + 1) - 4);
136         path.AddLineToPoint (time_x (position) + 2,           y_pos (_track.get() + 1) - 4);
137         path.AddLineToPoint (time_x (position) + 2,           y_pos (_track.get()) + 4);
138         gc->StrokePath (path);
139         gc->FillPath (path);
140
141         /* Reel split points */
142         gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 1, wxPENSTYLE_DOT));
143         for (auto i: cont->reel_split_points(film)) {
144                 path = gc->CreatePath ();
145                 path.MoveToPoint (time_x (i), y_pos (_track.get()) + 4);
146                 path.AddLineToPoint (time_x (i), y_pos (_track.get() + 1) - 4);
147                 gc->StrokePath (path);
148         }
149
150         /* Overlaps */
151         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (foreground_colour(), wxBRUSHSTYLE_CROSSDIAG_HATCH));
152         for (auto const& i: overlaps) {
153                 gc->DrawRectangle (i.x, i.y + 4, i.width, i.height - 8);
154         }
155
156         /* Label text */
157         auto lab = label ();
158         wxDouble lab_width;
159         wxDouble lab_height;
160         wxDouble lab_descent;
161         wxDouble lab_leading;
162         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT, foreground_colour ()));
163         gc->GetTextExtent (lab, &lab_width, &lab_height, &lab_descent, &lab_leading);
164         gc->PushState ();
165         gc->Clip (wxRegion (time_x (position), y_pos (_track.get()), len.seconds() * _timeline.pixels_per_second().get_value_or(0), _timeline.pixels_per_track()));
166         gc->DrawText (lab, time_x (position) + 12, y_pos (_track.get() + 1) - lab_height - 4);
167         gc->PopState ();
168 }
169
170
171 int
172 TimelineContentView::y_pos (int t) const
173 {
174         return t * _timeline.pixels_per_track() + _timeline.tracks_y_offset();
175 }
176
177
178 void
179 TimelineContentView::content_change (ChangeType type, int p)
180 {
181         if (type != ChangeType::DONE) {
182                 return;
183         }
184
185         ensure_ui_thread ();
186
187         if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
188                 force_redraw ();
189         }
190 }
191
192
193 wxString
194 TimelineContentView::label () const
195 {
196         return std_to_wx(content()->summary());
197 }