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