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