Add free-text notes field to cinemas and screens.
[dcpomatic.git] / src / wx / screens_panel.cc
1 /*
2     Copyright (C) 2015-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/config.h"
21 #include "lib/cinema.h"
22 #include "lib/screen.h"
23 #include "screens_panel.h"
24 #include "wx_util.h"
25 #include "cinema_dialog.h"
26 #include "screen_dialog.h"
27 #include <boost/foreach.hpp>
28
29 using std::list;
30 using std::pair;
31 using std::cout;
32 using std::map;
33 using std::string;
34 using std::make_pair;
35 using boost::shared_ptr;
36 using boost::optional;
37
38 ScreensPanel::ScreensPanel (wxWindow* parent)
39         : wxPanel (parent, wxID_ANY)
40         , _ignore_selection_change (false)
41 {
42         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
43
44         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (200, -1));
45         _search->ShowCancelButton (true);
46         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
47
48         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
49         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
50         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
51
52         add_cinemas ();
53
54         wxGridSizer* target_buttons = new wxGridSizer (2, DCPOMATIC_BUTTON_STACK_GAP * 2, DCPOMATIC_SIZER_Y_GAP);
55
56         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
57         target_buttons->Add (_add_cinema, 1, wxEXPAND);
58         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
59         target_buttons->Add (_add_screen, 1, wxEXPAND);
60         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
61         target_buttons->Add (_edit_cinema, 1, wxEXPAND);
62         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
63         target_buttons->Add (_edit_screen, 1, wxEXPAND);
64         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
65         target_buttons->Add (_remove_cinema, 1, wxEXPAND);
66         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
67         target_buttons->Add (_remove_screen, 1, wxEXPAND);
68
69         targets->Add (target_buttons, 0, 0);
70
71         sizer->Add (targets, 1, wxEXPAND);
72
73         _search->Bind        (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ScreensPanel::search_changed, this));
74         _targets->Bind       (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
75
76         _add_cinema->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_cinema_clicked, this));
77         _edit_cinema->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
78         _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
79
80         _add_screen->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_screen_clicked, this));
81         _edit_screen->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_screen_clicked, this));
82         _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_screen_clicked, this));
83
84         SetSizer (sizer);
85 }
86
87 ScreensPanel::~ScreensPanel ()
88 {
89         _targets->Unbind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
90 }
91
92 void
93 ScreensPanel::setup_sensitivity ()
94 {
95         bool const sc = _selected_cinemas.size() == 1;
96         bool const ss = _selected_screens.size() == 1;
97
98         _edit_cinema->Enable (sc);
99         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
100
101         _add_screen->Enable (sc);
102         _edit_screen->Enable (ss);
103         _remove_screen->Enable (_selected_screens.size() >= 1);
104 }
105
106 void
107 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
108 {
109         string search = wx_to_std (_search->GetValue ());
110         transform (search.begin(), search.end(), search.begin(), ::tolower);
111
112         if (!search.empty ()) {
113                 string name = c->name;
114                 transform (name.begin(), name.end(), name.begin(), ::tolower);
115                 if (name.find (search) == string::npos) {
116                         return;
117                 }
118         }
119
120         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
121
122         list<shared_ptr<Screen> > sc = c->screens ();
123         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
124                 add_screen (c, *i);
125         }
126
127         _targets->SortChildren (_root);
128 }
129
130 optional<wxTreeItemId>
131 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
132 {
133         CinemaMap::const_iterator i = _cinemas.begin();
134         while (i != _cinemas.end() && i->second != c) {
135                 ++i;
136         }
137
138         if (i == _cinemas.end()) {
139                 return optional<wxTreeItemId> ();
140         }
141
142         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
143         return i->first;
144 }
145
146 void
147 ScreensPanel::add_cinema_clicked ()
148 {
149         CinemaDialog* d = new CinemaDialog (this, _("Add Cinema"));
150         if (d->ShowModal () == wxID_OK) {
151                 shared_ptr<Cinema> c (new Cinema (d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute()));
152                 Config::instance()->add_cinema (c);
153                 add_cinema (c);
154         }
155
156         d->Destroy ();
157 }
158
159 void
160 ScreensPanel::edit_cinema_clicked ()
161 {
162         if (_selected_cinemas.size() != 1) {
163                 return;
164         }
165
166         pair<wxTreeItemId, shared_ptr<Cinema> > c = *_selected_cinemas.begin();
167
168         CinemaDialog* d = new CinemaDialog (
169                 this, _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
170                 );
171
172         if (d->ShowModal () == wxID_OK) {
173                 c.second->name = d->name ();
174                 c.second->emails = d->emails ();
175                 c.second->notes = d->notes ();
176                 c.second->set_utc_offset_hour (d->utc_offset_hour ());
177                 c.second->set_utc_offset_minute (d->utc_offset_minute ());
178                 _targets->SetItemText (c.first, std_to_wx (d->name()));
179                 Config::instance()->changed ();
180         }
181
182         d->Destroy ();
183 }
184
185 void
186 ScreensPanel::remove_cinema_clicked ()
187 {
188         for (CinemaMap::iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
189                 Config::instance()->remove_cinema (i->second);
190                 _targets->Delete (i->first);
191         }
192
193         selection_changed ();
194 }
195
196 void
197 ScreensPanel::add_screen_clicked ()
198 {
199         if (_selected_cinemas.size() != 1) {
200                 return;
201         }
202
203         shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
204
205         ScreenDialog* d = new ScreenDialog (this, _("Add Screen"));
206         if (d->ShowModal () != wxID_OK) {
207                 return;
208         }
209
210         shared_ptr<Screen> s (new Screen (d->name(), d->recipient(), d->trusted_devices()));
211         c->add_screen (s);
212         optional<wxTreeItemId> id = add_screen (c, s);
213         if (id) {
214                 _targets->Expand (id.get ());
215         }
216
217         Config::instance()->changed ();
218
219         d->Destroy ();
220 }
221
222 void
223 ScreensPanel::edit_screen_clicked ()
224 {
225         if (_selected_screens.size() != 1) {
226                 return;
227         }
228
229         pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
230
231         ScreenDialog* d = new ScreenDialog (this, _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->trusted_devices);
232         if (d->ShowModal () == wxID_OK) {
233                 s.second->name = d->name ();
234                 s.second->notes = d->notes ();
235                 s.second->recipient = d->recipient ();
236                 s.second->trusted_devices = d->trusted_devices ();
237                 _targets->SetItemText (s.first, std_to_wx (d->name()));
238                 Config::instance()->changed ();
239         }
240
241         d->Destroy ();
242 }
243
244 void
245 ScreensPanel::remove_screen_clicked ()
246 {
247         for (ScreenMap::iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
248                 CinemaMap::iterator j = _cinemas.begin ();
249                 while (j != _cinemas.end ()) {
250                         list<shared_ptr<Screen> > sc = j->second->screens ();
251                         if (find (sc.begin(), sc.end(), i->second) != sc.end ()) {
252                                 break;
253                         }
254
255                         ++j;
256                 }
257
258                 if (j == _cinemas.end()) {
259                         continue;
260                 }
261
262                 j->second->remove_screen (i->second);
263                 _targets->Delete (i->first);
264         }
265
266         Config::instance()->changed ();
267 }
268
269 list<shared_ptr<Screen> >
270 ScreensPanel::screens () const
271 {
272         list<shared_ptr<Screen> > s;
273
274         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
275                 list<shared_ptr<Screen> > sc = i->second->screens ();
276                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
277                         s.push_back (*j);
278                 }
279         }
280
281         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
282                 s.push_back (i->second);
283         }
284
285         s.sort ();
286         s.unique ();
287
288         return s;
289 }
290
291 void
292 ScreensPanel::selection_changed_shim (wxTreeEvent &)
293 {
294         selection_changed ();
295 }
296
297 void
298 ScreensPanel::selection_changed ()
299 {
300         if (_ignore_selection_change) {
301                 return;
302         }
303
304         wxArrayTreeItemIds s;
305         _targets->GetSelections (s);
306
307         _selected_cinemas.clear ();
308         _selected_screens.clear ();
309
310         for (size_t i = 0; i < s.GetCount(); ++i) {
311                 CinemaMap::const_iterator j = _cinemas.find (s[i]);
312                 if (j != _cinemas.end ()) {
313                         _selected_cinemas[j->first] = j->second;
314                 }
315                 ScreenMap::const_iterator k = _screens.find (s[i]);
316                 if (k != _screens.end ()) {
317                         _selected_screens[k->first] = k->second;
318                 }
319         }
320
321         setup_sensitivity ();
322         ScreensChanged ();
323 }
324
325 void
326 ScreensPanel::add_cinemas ()
327 {
328         _root = _targets->AddRoot ("Foo");
329
330         BOOST_FOREACH (shared_ptr<Cinema> i, Config::instance()->cinemas ()) {
331                 add_cinema (i);
332         }
333 }
334
335 void
336 ScreensPanel::search_changed ()
337 {
338         _targets->DeleteAllItems ();
339         _cinemas.clear ();
340         _screens.clear ();
341
342         add_cinemas ();
343
344         _ignore_selection_change = true;
345
346         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
347                 /* The wxTreeItemIds will now be different, so we must search by cinema */
348                 CinemaMap::const_iterator j = _cinemas.begin ();
349                 while (j != _cinemas.end() && j->second != i->second) {
350                         ++j;
351                 }
352
353                 if (j != _cinemas.end ()) {
354                         _targets->SelectItem (j->first);
355                 }
356         }
357
358         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
359                 ScreenMap::const_iterator j = _screens.begin ();
360                 while (j != _screens.end() && j->second != i->second) {
361                         ++j;
362                 }
363
364                 if (j != _screens.end ()) {
365                         _targets->SelectItem (j->first);
366                 }
367         }
368
369         _ignore_selection_change = false;
370 }