Tweak vertical layout in the KDM creator (#833).
[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->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 (this, "Edit cinema", c.second->name, c.second->emails, c.second->utc_offset_hour(), c.second->utc_offset_minute());
169         if (d->ShowModal () == wxID_OK) {
170                 c.second->name = d->name ();
171                 c.second->emails = d->emails ();
172                 c.second->set_utc_offset_hour (d->utc_offset_hour ());
173                 c.second->set_utc_offset_minute (d->utc_offset_minute ());
174                 _targets->SetItemText (c.first, std_to_wx (d->name()));
175                 Config::instance()->changed ();
176         }
177
178         d->Destroy ();
179 }
180
181 void
182 ScreensPanel::remove_cinema_clicked ()
183 {
184         for (CinemaMap::iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
185                 Config::instance()->remove_cinema (i->second);
186                 _targets->Delete (i->first);
187         }
188
189         selection_changed ();
190 }
191
192 void
193 ScreensPanel::add_screen_clicked ()
194 {
195         if (_selected_cinemas.size() != 1) {
196                 return;
197         }
198
199         shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
200
201         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
202         if (d->ShowModal () != wxID_OK) {
203                 return;
204         }
205
206         shared_ptr<Screen> s (new Screen (d->name(), d->recipient(), d->trusted_devices()));
207         c->add_screen (s);
208         optional<wxTreeItemId> id = add_screen (c, s);
209         if (id) {
210                 _targets->Expand (id.get ());
211         }
212
213         Config::instance()->changed ();
214
215         d->Destroy ();
216 }
217
218 void
219 ScreensPanel::edit_screen_clicked ()
220 {
221         if (_selected_screens.size() != 1) {
222                 return;
223         }
224
225         pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
226
227         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->recipient, s.second->trusted_devices);
228         if (d->ShowModal () == wxID_OK) {
229                 s.second->name = d->name ();
230                 s.second->recipient = d->recipient ();
231                 s.second->trusted_devices = d->trusted_devices ();
232                 _targets->SetItemText (s.first, std_to_wx (d->name()));
233                 Config::instance()->changed ();
234         }
235
236         d->Destroy ();
237 }
238
239 void
240 ScreensPanel::remove_screen_clicked ()
241 {
242         for (ScreenMap::iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
243                 CinemaMap::iterator j = _cinemas.begin ();
244                 while (j != _cinemas.end ()) {
245                         list<shared_ptr<Screen> > sc = j->second->screens ();
246                         if (find (sc.begin(), sc.end(), i->second) != sc.end ()) {
247                                 break;
248                         }
249
250                         ++j;
251                 }
252
253                 if (j == _cinemas.end()) {
254                         continue;
255                 }
256
257                 j->second->remove_screen (i->second);
258                 _targets->Delete (i->first);
259         }
260
261         Config::instance()->changed ();
262 }
263
264 list<shared_ptr<Screen> >
265 ScreensPanel::screens () const
266 {
267         list<shared_ptr<Screen> > s;
268
269         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
270                 list<shared_ptr<Screen> > sc = i->second->screens ();
271                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
272                         s.push_back (*j);
273                 }
274         }
275
276         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
277                 s.push_back (i->second);
278         }
279
280         s.sort ();
281         s.unique ();
282
283         return s;
284 }
285
286 void
287 ScreensPanel::selection_changed_shim (wxTreeEvent &)
288 {
289         selection_changed ();
290 }
291
292 void
293 ScreensPanel::selection_changed ()
294 {
295         if (_ignore_selection_change) {
296                 return;
297         }
298
299         wxArrayTreeItemIds s;
300         _targets->GetSelections (s);
301
302         _selected_cinemas.clear ();
303         _selected_screens.clear ();
304
305         for (size_t i = 0; i < s.GetCount(); ++i) {
306                 CinemaMap::const_iterator j = _cinemas.find (s[i]);
307                 if (j != _cinemas.end ()) {
308                         _selected_cinemas[j->first] = j->second;
309                 }
310                 ScreenMap::const_iterator k = _screens.find (s[i]);
311                 if (k != _screens.end ()) {
312                         _selected_screens[k->first] = k->second;
313                 }
314         }
315
316         setup_sensitivity ();
317         ScreensChanged ();
318 }
319
320 void
321 ScreensPanel::add_cinemas ()
322 {
323         _root = _targets->AddRoot ("Foo");
324
325         BOOST_FOREACH (shared_ptr<Cinema> i, Config::instance()->cinemas ()) {
326                 add_cinema (i);
327         }
328 }
329
330 void
331 ScreensPanel::search_changed ()
332 {
333         _targets->DeleteAllItems ();
334         _cinemas.clear ();
335         _screens.clear ();
336
337         add_cinemas ();
338
339         _ignore_selection_change = true;
340
341         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
342                 /* The wxTreeItemIds will now be different, so we must search by cinema */
343                 CinemaMap::const_iterator j = _cinemas.begin ();
344                 while (j != _cinemas.end() && j->second != i->second) {
345                         ++j;
346                 }
347
348                 if (j != _cinemas.end ()) {
349                         _targets->SelectItem (j->first);
350                 }
351         }
352
353         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
354                 ScreenMap::const_iterator j = _screens.begin ();
355                 while (j != _screens.end() && j->second != i->second) {
356                         ++j;
357                 }
358
359                 if (j != _screens.end ()) {
360                         _targets->SelectItem (j->first);
361                 }
362         }
363
364         _ignore_selection_change = false;
365 }