Tidy up ScreensPanel code a bit.
[dcpomatic.git] / src / wx / screens_panel.cc
1 /*
2     Copyright (C) 2015 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
37 ScreensPanel::ScreensPanel (wxWindow* parent)
38         : wxPanel (parent, wxID_ANY)
39         , _ignore_selection_change (false)
40 {
41         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
42
43         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (200, -1));
44         _search->ShowCancelButton (true);
45         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
46
47         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
48         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
49         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
50
51         add_cinemas ();
52
53         _targets->ExpandAll ();
54
55         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
56
57         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
58         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
59         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
60         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
61         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
62         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
63
64         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
65         target_buttons->Add (_add_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
66         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
67         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
68         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
69         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
70
71         targets->Add (target_buttons, 0, 0);
72
73         sizer->Add (targets, 1, wxEXPAND);
74
75         _search->Bind        (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ScreensPanel::search_changed, this));
76         _targets->Bind       (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed, this);
77
78         _add_cinema->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_cinema_clicked, this));
79         _edit_cinema->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
80         _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
81
82         _add_screen->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_screen_clicked, this));
83         _edit_screen->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_screen_clicked, this));
84         _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_screen_clicked, this));
85
86         SetSizer (sizer);
87 }
88
89 ScreensPanel::~ScreensPanel ()
90 {
91         _targets->Unbind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed, 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 (sc);
102
103         _add_screen->Enable (sc);
104         _edit_screen->Enable (ss);
105         _remove_screen->Enable (ss);
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 void
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;
142         }
143
144         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
145         _targets->Expand (i->first);
146 }
147
148 void
149 ScreensPanel::add_cinema_clicked ()
150 {
151         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
152         if (d->ShowModal () == wxID_OK) {
153                 shared_ptr<Cinema> c (new Cinema (d->name(), d->emails()));
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 (this, "Edit cinema", c.second->name, c.second->emails);
171         if (d->ShowModal () == wxID_OK) {
172                 c.second->name = d->name ();
173                 c.second->emails = d->emails ();
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         if (_selected_cinemas.size() != 1) {
185                 return;
186         }
187
188         pair<wxTreeItemId, shared_ptr<Cinema> > c = *_selected_cinemas.begin();
189
190         Config::instance()->remove_cinema (c.second);
191         _targets->Delete (c.first);
192 }
193
194 void
195 ScreensPanel::add_screen_clicked ()
196 {
197         if (_selected_cinemas.size() != 1) {
198                 return;
199         }
200
201         shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
202
203         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
204         if (d->ShowModal () != wxID_OK) {
205                 return;
206         }
207
208         shared_ptr<Screen> s (new Screen (d->name(), d->recipient(), d->trusted_devices()));
209         c->add_screen (s);
210         add_screen (c, s);
211
212         Config::instance()->changed ();
213
214         d->Destroy ();
215 }
216
217 void
218 ScreensPanel::edit_screen_clicked ()
219 {
220         if (_selected_screens.size() != 1) {
221                 return;
222         }
223
224         pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
225
226         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->recipient, s.second->trusted_devices);
227         if (d->ShowModal () == wxID_OK) {
228                 s.second->name = d->name ();
229                 s.second->recipient = d->recipient ();
230                 s.second->trusted_devices = d->trusted_devices ();
231                 _targets->SetItemText (s.first, std_to_wx (d->name()));
232                 Config::instance()->changed ();
233         }
234
235         d->Destroy ();
236 }
237
238 void
239 ScreensPanel::remove_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         CinemaMap::iterator i = _cinemas.begin ();
248         while (i != _cinemas.end ()) {
249                 list<shared_ptr<Screen> > sc = i->second->screens ();
250                 if (find (sc.begin(), sc.end(), s.second) != sc.end ()) {
251                         break;
252                 }
253
254                 ++i;
255         }
256
257         if (i == _cinemas.end()) {
258                 return;
259         }
260
261         i->second->remove_screen (s.second);
262         _targets->Delete (s.first);
263
264         Config::instance()->changed ();
265 }
266
267 list<shared_ptr<Screen> >
268 ScreensPanel::screens () const
269 {
270         list<shared_ptr<Screen> > s;
271
272         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
273                 list<shared_ptr<Screen> > sc = i->second->screens ();
274                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
275                         s.push_back (*j);
276                 }
277         }
278
279         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
280                 s.push_back (i->second);
281         }
282
283         s.sort ();
284         s.unique ();
285
286         return s;
287 }
288
289 void
290 ScreensPanel::selection_changed (wxTreeEvent &)
291 {
292         if (_ignore_selection_change) {
293                 return;
294         }
295
296         wxArrayTreeItemIds s;
297         _targets->GetSelections (s);
298
299         _selected_cinemas.clear ();
300         _selected_screens.clear ();
301
302         for (size_t i = 0; i < s.GetCount(); ++i) {
303                 CinemaMap::const_iterator j = _cinemas.find (s[i]);
304                 if (j != _cinemas.end ()) {
305                         _selected_cinemas[j->first] = j->second;
306                 }
307                 ScreenMap::const_iterator k = _screens.find (s[i]);
308                 if (k != _screens.end ()) {
309                         _selected_screens[k->first] = k->second;
310                 }
311         }
312
313         setup_sensitivity ();
314         ScreensChanged ();
315 }
316
317 void
318 ScreensPanel::add_cinemas ()
319 {
320         _root = _targets->AddRoot ("Foo");
321
322         BOOST_FOREACH (shared_ptr<Cinema> i, Config::instance()->cinemas ()) {
323                 add_cinema (i);
324         }
325 }
326
327 void
328 ScreensPanel::search_changed ()
329 {
330         _targets->DeleteAllItems ();
331         _cinemas.clear ();
332         _screens.clear ();
333
334         add_cinemas ();
335
336         _ignore_selection_change = true;
337
338         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
339                 /* The wxTreeItemIds will now be different, so we must search by cinema */
340                 CinemaMap::const_iterator j = _cinemas.begin ();
341                 while (j != _cinemas.end() && j->second != i->second) {
342                         ++j;
343                 }
344
345                 if (j != _cinemas.end ()) {
346                         _targets->SelectItem (j->first);
347                 }
348         }
349
350         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
351                 ScreenMap::const_iterator j = _screens.begin ();
352                 while (j != _screens.end() && j->second != i->second) {
353                         ++j;
354                 }
355
356                 if (j != _screens.end ()) {
357                         _targets->SelectItem (j->first);
358                 }
359         }
360
361         _ignore_selection_change = false;
362 }