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