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