swaroop: make disable-timeline work.
[dcpomatic.git] / src / wx / swaroop_controls.cc
1 /*
2     Copyright (C) 2018 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 "swaroop_controls.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "content_view.h"
25 #include "lib/player_video.h"
26 #include "lib/dcp_content.h"
27 #include <wx/listctrl.h>
28
29 using std::string;
30 using std::cout;
31 using std::exception;
32 using boost::shared_ptr;
33 using boost::dynamic_pointer_cast;
34 using boost::optional;
35
36 SwaroopControls::SwaroopControls (wxWindow* parent, shared_ptr<FilmViewer> viewer)
37         : Controls (parent, viewer, false)
38         , _play_button (new wxButton(this, wxID_ANY, _("Play")))
39         , _pause_button (new wxButton(this, wxID_ANY, _("Pause")))
40         , _stop_button (new wxButton(this, wxID_ANY, _("Stop")))
41         , _current_disable_timeline (false)
42 {
43         _button_sizer->Add (_play_button, 0, wxEXPAND);
44         _button_sizer->Add (_pause_button, 0, wxEXPAND);
45         _button_sizer->Add (_stop_button, 0, wxEXPAND);
46
47         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
48         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
49
50         wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
51         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
52
53         left_sizer->Add (_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
54
55         _content_view = new ContentView (this);
56         left_sizer->Add (_content_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
57
58         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
59         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
60         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
61         e_sizer->Add (left_sizer, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
62         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
63
64         _v_sizer->Add (e_sizer, 1, wxEXPAND);
65
66         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
67         _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
68
69         _play_button->Bind  (wxEVT_BUTTON, boost::bind(&SwaroopControls::play_clicked,  this));
70         _pause_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::pause_clicked, this));
71         _stop_button->Bind  (wxEVT_BUTTON, boost::bind(&SwaroopControls::stop_clicked,  this));
72         _spl_view->Bind     (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&SwaroopControls::spl_selection_changed, this));
73         _spl_view->Bind     (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&SwaroopControls::spl_selection_changed, this));
74         _viewer->ImageChanged.connect (boost::bind(&SwaroopControls::image_changed, this, _1));
75
76         _content_view->update ();
77         update_playlist_directory ();
78 }
79
80 void
81 SwaroopControls::started ()
82 {
83         Controls::started ();
84         _play_button->Enable (false);
85         _pause_button->Enable (true);
86 }
87
88 void
89 SwaroopControls::stopped ()
90 {
91         Controls::stopped ();
92         _play_button->Enable (true);
93         _pause_button->Enable (false);
94 }
95
96 void
97 SwaroopControls::play_clicked ()
98 {
99         _viewer->start ();
100 }
101
102 void
103 SwaroopControls::setup_sensitivity ()
104 {
105         Controls::setup_sensitivity ();
106         bool const active_job = _active_job && *_active_job != "examine_content";
107         bool const c = _film && !_film->content().empty() && !active_job;
108         _play_button->Enable (c && !_viewer->playing());
109         _pause_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT) && _viewer->playing());
110         _stop_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
111         _slider->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT) && !_current_disable_timeline);
112 }
113
114 void
115 SwaroopControls::pause_clicked ()
116 {
117         _viewer->stop ();
118 }
119
120 void
121 SwaroopControls::stop_clicked ()
122 {
123         _viewer->stop ();
124         _viewer->seek (DCPTime(), true);
125 }
126
127 void
128 SwaroopControls::log (wxString s)
129 {
130         struct timeval time;
131         gettimeofday (&time, 0);
132         char buffer[64];
133         time_t const sec = time.tv_sec;
134         struct tm* t = localtime (&sec);
135         strftime (buffer, 64, "%c", t);
136         wxString ts = std_to_wx(string(buffer)) + N_(": ");
137         _log->SetValue(_log->GetValue() + ts + s + "\n");
138 }
139
140 void
141 SwaroopControls::image_changed (boost::weak_ptr<PlayerVideo> weak_pv)
142 {
143         shared_ptr<PlayerVideo> pv = weak_pv.lock ();
144         if (!pv) {
145                 return;
146         }
147
148         shared_ptr<Content> c = pv->content().lock();
149         if (!c) {
150                 return;
151         }
152
153         if (c == _current_content.lock()) {
154                 return;
155         }
156
157         _current_content = c;
158
159         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
160         if (_selected_playlist) {
161                 BOOST_FOREACH (SPLEntry i, _playlists[*_selected_playlist].get()) {
162                         if (i.content == c) {
163                                 _current_disable_timeline = i.disable_timeline;
164                                 setup_sensitivity ();
165                         }
166                 }
167         }
168
169         shared_ptr<DCPContent> dc = dynamic_pointer_cast<DCPContent> (c);
170         if (!dc) {
171                 return;
172         }
173
174         if (!_current_kind || *_current_kind != dc->content_kind()) {
175                 _current_kind = dc->content_kind ();
176                 setup_sensitivity ();
177         }
178 }
179
180 void
181 SwaroopControls::add_playlist_to_list (SPL spl)
182 {
183         int const N = _spl_view->GetItemCount();
184
185         wxListItem it;
186         it.SetId(N);
187         it.SetColumn(0);
188         it.SetText (std_to_wx(spl.name()));
189         _spl_view->InsertItem (it);
190 }
191
192 void
193 SwaroopControls::update_playlist_directory ()
194 {
195         using namespace boost::filesystem;
196
197         _spl_view->DeleteAllItems ();
198         optional<path> dir = Config::instance()->player_playlist_directory();
199         if (!dir) {
200                 return;
201         }
202
203         _playlists.clear ();
204
205         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
206                 try {
207                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
208                                 SPL spl;
209                                 spl.read (i->path(), _content_view);
210                                 _playlists.push_back (spl);
211                                 add_playlist_to_list (spl);
212                         }
213                 } catch (exception& e) {
214                         /* Never mind */
215                 }
216         }
217 }
218
219 void
220 SwaroopControls::spl_selection_changed ()
221 {
222         _current_spl_view->DeleteAllItems ();
223
224         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
225         if (selected == -1) {
226                 _selected_playlist = boost::none;
227                 return;
228         }
229
230         _selected_playlist = selected;
231
232         shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
233
234         int N = 0;
235         BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
236                 wxListItem it;
237                 it.SetId (N);
238                 it.SetColumn (0);
239                 it.SetText (std_to_wx(i.name));
240                 _current_spl_view->InsertItem (it);
241                 film->add_content (i.content);
242                 ++N;
243         }
244
245         ResetFilm (film);
246 }
247
248 void
249 SwaroopControls::config_changed (int property)
250 {
251         Controls::config_changed (property);
252
253         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
254                 _content_view->update ();
255         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
256                 update_playlist_directory ();
257         }
258 }
259
260 void
261 SwaroopControls::set_film (shared_ptr<Film> film)
262 {
263         Controls::set_film (film);
264         update_playlist_directory ();
265         setup_sensitivity ();
266 }