Move _state_timer into VideoView.
[dcpomatic.git] / src / wx / screens_panel.cc
1 /*
2     Copyright (C) 2015-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 "screens_panel.h"
22 #include "wx_util.h"
23 #include "cinema_dialog.h"
24 #include "screen_dialog.h"
25 #include "dcpomatic_button.h"
26 #include "lib/config.h"
27 #include "lib/cinema.h"
28 #include "lib/screen.h"
29 #include <boost/foreach.hpp>
30
31 using std::list;
32 using std::pair;
33 using std::cout;
34 using std::map;
35 using std::string;
36 using std::make_pair;
37 using boost::shared_ptr;
38 using boost::optional;
39 using namespace dcpomatic;
40
41 ScreensPanel::ScreensPanel (wxWindow* parent)
42         : wxPanel (parent, wxID_ANY)
43         , _ignore_selection_change (false)
44 {
45         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
46
47         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (200, -1));
48         _search->ShowCancelButton (true);
49         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
50
51         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
52         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
53         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
54
55         add_cinemas ();
56
57         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
58
59         _add_cinema = new Button (this, _("Add Cinema..."));
60         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
61         _edit_cinema = new Button (this, _("Edit Cinema..."));
62         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
63         _remove_cinema = new Button (this, _("Remove Cinema"));
64         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
65         _add_screen = new Button (this, _("Add Screen..."));
66         target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
67         _edit_screen = new Button (this, _("Edit Screen..."));
68         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
69         _remove_screen = new Button (this, _("Remove Screen"));
70         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
71
72         targets->Add (target_buttons, 0, 0);
73
74         sizer->Add (targets, 1, wxEXPAND);
75
76         _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
77         _targets->Bind       (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
78
79         _add_cinema->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
80         _edit_cinema->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
81         _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
82
83         _add_screen->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
84         _edit_screen->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
85         _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
86
87         SetSizer (sizer);
88 }
89
90 ScreensPanel::~ScreensPanel ()
91 {
92         _targets->Unbind (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
93 }
94
95 void
96 ScreensPanel::setup_sensitivity ()
97 {
98         bool const sc = _selected_cinemas.size() == 1;
99         bool const ss = _selected_screens.size() == 1;
100
101         _edit_cinema->Enable (sc);
102         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
103
104         _add_screen->Enable (sc);
105         _edit_screen->Enable (ss);
106         _remove_screen->Enable (_selected_screens.size() >= 1);
107 }
108
109 void
110 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
111 {
112         string search = wx_to_std (_search->GetValue ());
113         transform (search.begin(), search.end(), search.begin(), ::tolower);
114
115         if (!search.empty ()) {
116                 string name = c->name;
117                 transform (name.begin(), name.end(), name.begin(), ::tolower);
118                 if (name.find (search) == string::npos) {
119                         return;
120                 }
121         }
122
123         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
124
125         list<shared_ptr<Screen> > sc = c->screens ();
126         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
127                 add_screen (c, *i);
128         }
129
130         _targets->SortChildren (_root);
131 }
132
133 optional<wxTreeItemId>
134 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
135 {
136         CinemaMap::const_iterator i = _cinemas.begin();
137         while (i != _cinemas.end() && i->second != c) {
138                 ++i;
139         }
140
141         if (i == _cinemas.end()) {
142                 return optional<wxTreeItemId> ();
143         }
144
145         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
146         return i->first;
147 }
148
149 void
150 ScreensPanel::add_cinema_clicked ()
151 {
152         CinemaDialog* d = new CinemaDialog (GetParent(), _("Add Cinema"));
153         if (d->ShowModal () == wxID_OK) {
154                 shared_ptr<Cinema> c (new Cinema (d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute()));
155                 Config::instance()->add_cinema (c);
156                 add_cinema (c);
157         }
158
159         d->Destroy ();
160 }
161
162 void
163 ScreensPanel::edit_cinema_clicked ()
164 {
165         if (_selected_cinemas.size() != 1) {
166                 return;
167         }
168
169         pair<wxTreeItemId, shared_ptr<Cinema> > c = *_selected_cinemas.begin();
170
171         CinemaDialog* d = new CinemaDialog (
172                 GetParent(), _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
173                 );
174
175         if (d->ShowModal () == wxID_OK) {
176                 c.second->name = d->name ();
177                 c.second->emails = d->emails ();
178                 c.second->notes = d->notes ();
179                 c.second->set_utc_offset_hour (d->utc_offset_hour ());
180                 c.second->set_utc_offset_minute (d->utc_offset_minute ());
181                 _targets->SetItemText (c.first, std_to_wx (d->name()));
182                 Config::instance()->changed (Config::CINEMAS);
183         }
184
185         d->Destroy ();
186 }
187
188 void
189 ScreensPanel::remove_cinema_clicked ()
190 {
191         for (CinemaMap::iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
192                 Config::instance()->remove_cinema (i->second);
193                 _targets->Delete (i->first);
194         }
195
196         selection_changed ();
197 }
198
199 void
200 ScreensPanel::add_screen_clicked ()
201 {
202         if (_selected_cinemas.size() != 1) {
203                 return;
204         }
205
206         shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
207
208         ScreenDialog* d = new ScreenDialog (GetParent(), _("Add Screen"));
209         if (d->ShowModal () != wxID_OK) {
210                 d->Destroy ();
211                 return;
212         }
213
214         BOOST_FOREACH (shared_ptr<Screen> i, c->screens ()) {
215                 if (i->name == d->name()) {
216                         error_dialog (
217                                 GetParent(),
218                                 wxString::Format (
219                                         _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
220                                         std_to_wx(d->name()).data()
221                                         )
222                                 );
223                         return;
224                 }
225         }
226
227         shared_ptr<Screen> s (new Screen (d->name(), d->notes(), d->recipient(), d->trusted_devices()));
228         c->add_screen (s);
229         optional<wxTreeItemId> id = add_screen (c, s);
230         if (id) {
231                 _targets->Expand (id.get ());
232         }
233
234         Config::instance()->changed (Config::CINEMAS);
235
236         d->Destroy ();
237 }
238
239 void
240 ScreensPanel::edit_screen_clicked ()
241 {
242         if (_selected_screens.size() != 1) {
243                 return;
244         }
245
246         pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
247
248         ScreenDialog* d = new ScreenDialog (GetParent(), _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->trusted_devices);
249         if (d->ShowModal () != wxID_OK) {
250                 d->Destroy ();
251                 return;
252         }
253
254         shared_ptr<Cinema> c = s.second->cinema;
255         BOOST_FOREACH (shared_ptr<Screen> i, c->screens ()) {
256                 if (i != s.second && i->name == d->name()) {
257                         error_dialog (
258                                 GetParent(),
259                                 wxString::Format (
260                                         _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
261                                         std_to_wx(d->name()).data()
262                                         )
263                                 );
264                         return;
265                 }
266         }
267
268         s.second->name = d->name ();
269         s.second->notes = d->notes ();
270         s.second->recipient = d->recipient ();
271         s.second->trusted_devices = d->trusted_devices ();
272         _targets->SetItemText (s.first, std_to_wx (d->name()));
273         Config::instance()->changed (Config::CINEMAS);
274
275         d->Destroy ();
276 }
277
278 void
279 ScreensPanel::remove_screen_clicked ()
280 {
281         for (ScreenMap::iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
282                 CinemaMap::iterator j = _cinemas.begin ();
283                 while (j != _cinemas.end ()) {
284                         list<shared_ptr<Screen> > sc = j->second->screens ();
285                         if (find (sc.begin(), sc.end(), i->second) != sc.end ()) {
286                                 break;
287                         }
288
289                         ++j;
290                 }
291
292                 if (j == _cinemas.end()) {
293                         continue;
294                 }
295
296                 j->second->remove_screen (i->second);
297                 _targets->Delete (i->first);
298         }
299
300         Config::instance()->changed (Config::CINEMAS);
301 }
302
303 list<shared_ptr<Screen> >
304 ScreensPanel::screens () const
305 {
306         list<shared_ptr<Screen> > s;
307
308         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
309                 list<shared_ptr<Screen> > sc = i->second->screens ();
310                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
311                         s.push_back (*j);
312                 }
313         }
314
315         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
316                 s.push_back (i->second);
317         }
318
319         s.sort ();
320         s.unique ();
321
322         return s;
323 }
324
325 void
326 ScreensPanel::selection_changed_shim (wxTreeEvent &)
327 {
328         selection_changed ();
329 }
330
331 void
332 ScreensPanel::selection_changed ()
333 {
334         if (_ignore_selection_change) {
335                 return;
336         }
337
338         wxArrayTreeItemIds s;
339         _targets->GetSelections (s);
340
341         _selected_cinemas.clear ();
342         _selected_screens.clear ();
343
344         for (size_t i = 0; i < s.GetCount(); ++i) {
345                 CinemaMap::const_iterator j = _cinemas.find (s[i]);
346                 if (j != _cinemas.end ()) {
347                         _selected_cinemas[j->first] = j->second;
348                 }
349                 ScreenMap::const_iterator k = _screens.find (s[i]);
350                 if (k != _screens.end ()) {
351                         _selected_screens[k->first] = k->second;
352                 }
353         }
354
355         setup_sensitivity ();
356         ScreensChanged ();
357 }
358
359 void
360 ScreensPanel::add_cinemas ()
361 {
362         _root = _targets->AddRoot ("Foo");
363
364         BOOST_FOREACH (shared_ptr<Cinema> i, Config::instance()->cinemas ()) {
365                 add_cinema (i);
366         }
367 }
368
369 void
370 ScreensPanel::search_changed ()
371 {
372         _targets->DeleteAllItems ();
373         _cinemas.clear ();
374         _screens.clear ();
375
376         add_cinemas ();
377
378         _ignore_selection_change = true;
379
380         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
381                 /* The wxTreeItemIds will now be different, so we must search by cinema */
382                 CinemaMap::const_iterator j = _cinemas.begin ();
383                 while (j != _cinemas.end() && j->second != i->second) {
384                         ++j;
385                 }
386
387                 if (j != _cinemas.end ()) {
388                         _targets->SelectItem (j->first);
389                 }
390         }
391
392         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
393                 ScreenMap::const_iterator j = _screens.begin ();
394                 while (j != _screens.end() && j->second != i->second) {
395                         ++j;
396                 }
397
398                 if (j != _screens.end ()) {
399                         _targets->SelectItem (j->first);
400                 }
401         }
402
403         _ignore_selection_change = false;
404 }