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