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