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