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