Use a vector<pair<...>> to store screens and cinemas instead of a map.
[dcpomatic.git] / src / wx / screens_panel.cc
1 /*
2     Copyright (C) 2015-2022 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
22 #include "cinema_dialog.h"
23 #include "dcpomatic_button.h"
24 #include "screen_dialog.h"
25 #include "screens_panel.h"
26 #include "wx_util.h"
27 #include "lib/cinema.h"
28 #include "lib/config.h"
29 #include "lib/screen.h"
30
31
32 using std::cout;
33 using std::make_pair;
34 using std::make_shared;
35 using std::map;
36 using std::pair;
37 using std::shared_ptr;
38 using std::string;
39 using std::vector;
40 using boost::optional;
41 using namespace dcpomatic;
42
43
44 ScreensPanel::ScreensPanel (wxWindow* parent)
45         : wxPanel (parent, wxID_ANY)
46         , _ignore_selection_change (false)
47 {
48         auto sizer = new wxBoxSizer (wxVERTICAL);
49
50         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
51 #ifndef __WXGTK3__
52         /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
53         _search->ShowCancelButton (true);
54 #endif
55         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
56
57         auto targets = new wxBoxSizer (wxHORIZONTAL);
58         _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
59         _targets->AppendColumn (wxT("foo"));
60         _targets->SetSortColumn (0);
61         _targets->SetItemComparator (&_comparator);
62
63         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
64
65         add_cinemas ();
66
67         auto target_buttons = new wxBoxSizer (wxVERTICAL);
68
69         _add_cinema = new Button (this, _("Add Cinema..."));
70         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
71         _edit_cinema = new Button (this, _("Edit Cinema..."));
72         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
73         _remove_cinema = new Button (this, _("Remove Cinema"));
74         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
75         _add_screen = new Button (this, _("Add Screen..."));
76         target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
77         _edit_screen = new Button (this, _("Edit Screen..."));
78         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
79         _remove_screen = new Button (this, _("Remove Screen"));
80         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
81
82         targets->Add (target_buttons, 0, 0);
83
84         sizer->Add (targets, 1, wxEXPAND);
85
86         _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
87         _targets->Bind       (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
88         _targets->Bind       (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
89
90         _add_cinema->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
91         _edit_cinema->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
92         _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
93
94         _add_screen->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
95         _edit_screen->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
96         _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
97
98         SetSizer (sizer);
99 }
100
101
102 ScreensPanel::~ScreensPanel ()
103 {
104         _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
105         _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
106 }
107
108
109 void
110 ScreensPanel::setup_sensitivity ()
111 {
112         bool const sc = _selected_cinemas.size() == 1;
113         bool const ss = _selected_screens.size() == 1;
114
115         _edit_cinema->Enable (sc || ss);
116         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
117
118         _add_screen->Enable (sc || ss);
119         _edit_screen->Enable (ss);
120         _remove_screen->Enable (_selected_screens.size() >= 1);
121 }
122
123
124 optional<wxTreeListItem>
125 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema)
126 {
127         auto search = wx_to_std (_search->GetValue ());
128         transform (search.begin(), search.end(), search.begin(), ::tolower);
129
130         if (!search.empty ()) {
131                 auto name = cinema->name;
132                 transform (name.begin(), name.end(), name.begin(), ::tolower);
133                 if (name.find (search) == string::npos) {
134                         return {};
135                 }
136         }
137
138         auto id = _targets->AppendItem(_targets->GetRootItem(), std_to_wx(cinema->name));
139
140         _cinemas.push_back(make_pair(id, cinema));
141
142         for (auto screen: cinema->screens()) {
143                 add_screen (cinema, screen);
144         }
145
146         return id;
147 }
148
149
150 optional<wxTreeListItem>
151 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
152 {
153         auto cinema_iter = _cinemas.begin();
154         while (cinema_iter != _cinemas.end() && cinema_iter->second != cinema) {
155                 ++cinema_iter;
156         }
157
158         if (cinema_iter == _cinemas.end()) {
159                 return {};
160         }
161
162         _screens.push_back(make_pair(_targets->AppendItem(cinema_iter->first, std_to_wx(screen->name)), screen));
163         return cinema_iter->first;
164 }
165
166
167 void
168 ScreensPanel::add_cinema_clicked ()
169 {
170         auto d = new CinemaDialog (GetParent(), _("Add Cinema"));
171         if (d->ShowModal () == wxID_OK) {
172                 auto cinema = make_shared<Cinema>(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute());
173                 Config::instance()->add_cinema (cinema);
174                 auto id = add_cinema (cinema);
175                 if (id) {
176                         _targets->UnselectAll ();
177                         _targets->Select (*id);
178                 }
179         }
180
181         d->Destroy ();
182 }
183
184
185 optional<pair<wxTreeListItem, shared_ptr<Cinema>>>
186 ScreensPanel::cinema_for_operation () const
187 {
188         if (_selected_cinemas.size() == 1) {
189                 return make_pair(_selected_cinemas.begin()->first, _selected_cinemas.begin()->second);
190         } else if (_selected_screens.size() == 1) {
191                 return make_pair(_targets->GetItemParent(_selected_screens.begin()->first), _selected_screens.begin()->second->cinema);
192         }
193
194         return {};
195 }
196
197
198 void
199 ScreensPanel::edit_cinema_clicked ()
200 {
201         auto cinema = cinema_for_operation ();
202         if (!cinema) {
203                 return;
204         }
205
206         auto d = new CinemaDialog (
207                 GetParent(), _("Edit cinema"), cinema->second->name, cinema->second->emails, cinema->second->notes, cinema->second->utc_offset_hour(), cinema->second->utc_offset_minute()
208                 );
209
210         if (d->ShowModal() == wxID_OK) {
211                 cinema->second->name = d->name ();
212                 cinema->second->emails = d->emails ();
213                 cinema->second->notes = d->notes ();
214                 cinema->second->set_utc_offset_hour (d->utc_offset_hour ());
215                 cinema->second->set_utc_offset_minute (d->utc_offset_minute ());
216                 _targets->SetItemText (cinema->first, std_to_wx(d->name()));
217                 Config::instance()->changed (Config::CINEMAS);
218         }
219
220         d->Destroy ();
221 }
222
223
224 void
225 ScreensPanel::remove_cinema_clicked ()
226 {
227         if (_selected_cinemas.size() == 1) {
228                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas.begin()->second->name)))) {
229                         return;
230                 }
231         } else {
232                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
233                         return;
234                 }
235         }
236
237         for (auto const& i: _selected_cinemas) {
238                 Config::instance()->remove_cinema (i.second);
239                 _targets->DeleteItem (i.first);
240         }
241
242         selection_changed ();
243 }
244
245
246 void
247 ScreensPanel::add_screen_clicked ()
248 {
249         auto cinema = cinema_for_operation ();
250         if (!cinema) {
251                 return;
252         }
253
254         auto d = new ScreenDialog (GetParent(), _("Add Screen"));
255         if (d->ShowModal () != wxID_OK) {
256                 d->Destroy ();
257                 return;
258         }
259
260         for (auto screen: cinema->second->screens()) {
261                 if (screen->name == d->name()) {
262                         error_dialog (
263                                 GetParent(),
264                                 wxString::Format (
265                                         _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
266                                         std_to_wx(d->name()).data()
267                                         )
268                                 );
269                         return;
270                 }
271         }
272
273         auto screen = std::make_shared<Screen>(d->name(), d->notes(), d->recipient(), d->recipient_file(), d->trusted_devices());
274         cinema->second->add_screen (screen);
275         auto id = add_screen (cinema->second, screen);
276         if (id) {
277                 _targets->Expand (id.get ());
278         }
279
280         Config::instance()->changed (Config::CINEMAS);
281
282         d->Destroy ();
283 }
284
285
286 void
287 ScreensPanel::edit_screen_clicked ()
288 {
289         if (_selected_screens.size() != 1) {
290                 return;
291         }
292
293         auto edit_screen = *_selected_screens.begin();
294
295         auto d = new ScreenDialog (
296                 GetParent(), _("Edit screen"),
297                 edit_screen.second->name,
298                 edit_screen.second->notes,
299                 edit_screen.second->recipient,
300                 edit_screen.second->recipient_file,
301                 edit_screen.second->trusted_devices
302                 );
303
304         if (d->ShowModal() != wxID_OK) {
305                 d->Destroy ();
306                 return;
307         }
308
309         auto cinema = edit_screen.second->cinema;
310         for (auto screen: cinema->screens()) {
311                 if (screen != edit_screen.second && screen->name == d->name()) {
312                         error_dialog (
313                                 GetParent(),
314                                 wxString::Format (
315                                         _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
316                                         std_to_wx(d->name()).data()
317                                         )
318                                 );
319                         return;
320                 }
321         }
322
323         edit_screen.second->name = d->name ();
324         edit_screen.second->notes = d->notes ();
325         edit_screen.second->recipient = d->recipient ();
326         edit_screen.second->recipient_file = d->recipient_file ();
327         edit_screen.second->trusted_devices = d->trusted_devices ();
328         _targets->SetItemText (edit_screen.first, std_to_wx(d->name()));
329         Config::instance()->changed (Config::CINEMAS);
330
331         d->Destroy ();
332 }
333
334
335 void
336 ScreensPanel::remove_screen_clicked ()
337 {
338         if (_selected_screens.size() == 1) {
339                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens.begin()->second->name)))) {
340                         return;
341                 }
342         } else {
343                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
344                         return;
345                 }
346         }
347
348         for (auto const& i: _selected_screens) {
349                 auto j = _cinemas.begin ();
350                 while (j != _cinemas.end ()) {
351                         auto sc = j->second->screens ();
352                         if (find (sc.begin(), sc.end(), i.second) != sc.end ()) {
353                                 break;
354                         }
355
356                         ++j;
357                 }
358
359                 if (j == _cinemas.end()) {
360                         continue;
361                 }
362
363                 j->second->remove_screen (i.second);
364                 _targets->DeleteItem (i.first);
365         }
366
367         Config::instance()->changed (Config::CINEMAS);
368 }
369
370
371 vector<shared_ptr<Screen>>
372 ScreensPanel::screens () const
373 {
374         vector<shared_ptr<Screen>> output;
375
376         for (auto item = _targets->GetFirstItem(); item.IsOk(); item = _targets->GetNextItem(item)) {
377                 if (_targets->GetCheckedState(item) == wxCHK_CHECKED) {
378                         auto screen_iter = screen_by_tree_list_item(item);
379                         if (screen_iter != _screens.end()) {
380                                 output.push_back (screen_iter->second);
381                         }
382                 }
383         }
384
385         return output;
386 }
387
388
389 void
390 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
391 {
392         selection_changed ();
393 }
394
395
396 void
397 ScreensPanel::selection_changed ()
398 {
399         if (_ignore_selection_change) {
400                 return;
401         }
402
403         wxTreeListItems selection;
404         _targets->GetSelections (selection);
405
406         _selected_cinemas.clear ();
407         _selected_screens.clear ();
408
409         for (size_t i = 0; i < selection.size(); ++i) {
410                 auto cinema = cinema_by_tree_list_item(selection[i]);
411                 if (cinema != _cinemas.end ()) {
412                         _selected_cinemas.push_back(*cinema);
413                 }
414                 auto screen = screen_by_tree_list_item(selection[i]);
415                 if (screen != _screens.end ()) {
416                         _selected_screens.push_back(*screen);
417                 }
418         }
419
420         setup_sensitivity ();
421 }
422
423
424 void
425 ScreensPanel::add_cinemas ()
426 {
427         for (auto cinema: Config::instance()->cinemas()) {
428                 add_cinema (cinema);
429         }
430 }
431
432
433 void
434 ScreensPanel::search_changed ()
435 {
436         _targets->DeleteAllItems ();
437         _cinemas.clear ();
438         _screens.clear ();
439
440         add_cinemas ();
441
442         _ignore_selection_change = true;
443
444         for (auto const& selection: _selected_cinemas) {
445                 /* The wxTreeListItems will now be different, so we must search by cinema */
446                 auto cinema = _cinemas.begin ();
447                 while (cinema != _cinemas.end() && cinema->second != selection.second) {
448                         ++cinema;
449                 }
450
451                 if (cinema != _cinemas.end()) {
452                         _targets->Select (cinema->first);
453                 }
454         }
455
456         for (auto const& selection: _selected_screens) {
457                 auto screen = _screens.begin ();
458                 while (screen != _screens.end() && screen->second != selection.second) {
459                         ++screen;
460                 }
461
462                 if (screen != _screens.end()) {
463                         _targets->Select (screen->first);
464                 }
465         }
466
467         _ignore_selection_change = false;
468 }
469
470
471 void
472 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
473 {
474         auto cinema_iter = cinema_by_tree_list_item(ev.GetItem());
475         if (cinema_iter != _cinemas.end()) {
476                 /* Cinema: check/uncheck all children */
477                 auto const checked = _targets->GetCheckedState(ev.GetItem());
478                 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
479                         _targets->CheckItem(child, checked);
480                 }
481         } else {
482                 /* Screen: set cinema to checked/unchecked/3state */
483                 auto parent = _targets->GetItemParent(ev.GetItem());
484                 DCPOMATIC_ASSERT (parent.IsOk());
485                 int checked = 0;
486                 int unchecked = 0;
487                 for (auto child = _targets->GetFirstChild(parent); child.IsOk(); child = _targets->GetNextSibling(child)) {
488                         if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
489                             ++checked;
490                         } else {
491                             ++unchecked;
492                         }
493                 }
494                 if (checked == 0) {
495                         _targets->CheckItem(parent, wxCHK_UNCHECKED);
496                 } else if (unchecked == 0) {
497                         _targets->CheckItem(parent, wxCHK_CHECKED);
498                 } else {
499                         _targets->CheckItem(parent, wxCHK_UNDETERMINED);
500                 }
501         }
502
503         ScreensChanged ();
504 }
505
506
507 ScreensPanel::Cinemas::iterator
508 ScreensPanel::cinema_by_tree_list_item (wxTreeListItem item)
509 {
510         return std::find_if(
511                 _cinemas.begin(), _cinemas.end(),
512                 [item](pair<wxTreeListItem, shared_ptr<Cinema>> const& s) { return s.first == item; }
513                 );
514 }
515
516
517 ScreensPanel::Screens::const_iterator
518 ScreensPanel::screen_by_tree_list_item (wxTreeListItem item) const
519 {
520         return std::find_if(
521                 _screens.begin(), _screens.end(),
522                 [item](pair<wxTreeListItem, shared_ptr<Screen>> const& s) { return s.first == item; }
523                 );
524 }
525