Add search button to screens panel.
[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 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
95 ScreensPanel::selected_cinemas () const
96 {
97         wxArrayTreeItemIds s;
98         _targets->GetSelections (s);
99
100         list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
101         for (size_t i = 0; i < s.GetCount(); ++i) {
102                 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
103                 if (j != _cinemas.end ()) {
104                         c.push_back (make_pair (j->first, j->second));
105                 }
106         }
107
108         return c;
109 }
110
111 list<pair<wxTreeItemId, shared_ptr<Screen> > >
112 ScreensPanel::selected_screens () const
113 {
114         wxArrayTreeItemIds s;
115         _targets->GetSelections (s);
116
117         list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
118         for (size_t i = 0; i < s.GetCount(); ++i) {
119                 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
120                 if (j != _screens.end ()) {
121                         c.push_back (make_pair (j->first, j->second));
122                 }
123         }
124
125         return c;
126 }
127
128 void
129 ScreensPanel::setup_sensitivity ()
130 {
131         bool const sc = selected_cinemas().size() == 1;
132         bool const ss = selected_screens().size() == 1;
133
134         _edit_cinema->Enable (sc);
135         _remove_cinema->Enable (sc);
136
137         _add_screen->Enable (sc);
138         _edit_screen->Enable (ss);
139         _remove_screen->Enable (ss);
140 }
141
142 void
143 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
144 {
145         string search = wx_to_std (_search->GetValue ());
146         transform (search.begin(), search.end(), search.begin(), ::tolower);
147
148         if (!search.empty ()) {
149                 string name = c->name;
150                 transform (name.begin(), name.end(), name.begin(), ::tolower);
151                 if (name.find (search) == string::npos) {
152                         return;
153                 }
154         }
155
156         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
157
158         list<shared_ptr<Screen> > sc = c->screens ();
159         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
160                 add_screen (c, *i);
161         }
162
163         _targets->SortChildren (_root);
164 }
165
166 void
167 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
168 {
169         map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
170         while (i != _cinemas.end() && i->second != c) {
171                 ++i;
172         }
173
174         if (i == _cinemas.end()) {
175                 return;
176         }
177
178         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
179         _targets->Expand (i->first);
180 }
181
182 void
183 ScreensPanel::add_cinema_clicked ()
184 {
185         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
186         if (d->ShowModal () == wxID_OK) {
187                 shared_ptr<Cinema> c (new Cinema (d->name(), d->emails()));
188                 Config::instance()->add_cinema (c);
189                 add_cinema (c);
190         }
191
192         d->Destroy ();
193 }
194
195 void
196 ScreensPanel::edit_cinema_clicked ()
197 {
198         if (selected_cinemas().size() != 1) {
199                 return;
200         }
201
202         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
203
204         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->emails);
205         if (d->ShowModal () == wxID_OK) {
206                 c.second->name = d->name ();
207                 c.second->emails = d->emails ();
208                 _targets->SetItemText (c.first, std_to_wx (d->name()));
209                 Config::instance()->changed ();
210         }
211
212         d->Destroy ();
213 }
214
215 void
216 ScreensPanel::remove_cinema_clicked ()
217 {
218         if (selected_cinemas().size() != 1) {
219                 return;
220         }
221
222         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
223
224         Config::instance()->remove_cinema (c.second);
225         _targets->Delete (c.first);
226 }
227
228 void
229 ScreensPanel::add_screen_clicked ()
230 {
231         if (selected_cinemas().size() != 1) {
232                 return;
233         }
234
235         shared_ptr<Cinema> c = selected_cinemas().front().second;
236
237         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
238         if (d->ShowModal () != wxID_OK) {
239                 return;
240         }
241
242         shared_ptr<Screen> s (new Screen (d->name(), d->recipient(), d->trusted_devices()));
243         c->add_screen (s);
244         add_screen (c, s);
245
246         Config::instance()->changed ();
247
248         d->Destroy ();
249 }
250
251 void
252 ScreensPanel::edit_screen_clicked ()
253 {
254         if (selected_screens().size() != 1) {
255                 return;
256         }
257
258         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
259
260         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->recipient, s.second->trusted_devices);
261         if (d->ShowModal () == wxID_OK) {
262                 s.second->name = d->name ();
263                 s.second->recipient = d->recipient ();
264                 s.second->trusted_devices = d->trusted_devices ();
265                 _targets->SetItemText (s.first, std_to_wx (d->name()));
266                 Config::instance()->changed ();
267         }
268
269         d->Destroy ();
270 }
271
272 void
273 ScreensPanel::remove_screen_clicked ()
274 {
275         if (selected_screens().size() != 1) {
276                 return;
277         }
278
279         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
280
281         map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
282         while (i != _cinemas.end ()) {
283                 list<shared_ptr<Screen> > sc = i->second->screens ();
284                 if (find (sc.begin(), sc.end(), s.second) != sc.end ()) {
285                         break;
286                 }
287
288                 ++i;
289         }
290
291         if (i == _cinemas.end()) {
292                 return;
293         }
294
295         i->second->remove_screen (s.second);
296         _targets->Delete (s.first);
297
298         Config::instance()->changed ();
299 }
300
301 list<shared_ptr<Screen> >
302 ScreensPanel::screens () const
303 {
304         list<shared_ptr<Screen> > s;
305
306         list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
307         for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != 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         list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
315         for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
316                 s.push_back (i->second);
317         }
318
319         s.sort ();
320         s.unique ();
321
322         return s;
323 }
324
325 void
326 ScreensPanel::selection_changed (wxTreeEvent &)
327 {
328         if (_ignore_selection_change) {
329                 return;
330         }
331
332         wxArrayTreeItemIds s;
333         _targets->GetSelections (s);
334
335         _selected_cinemas.clear ();
336         _selected_screens.clear ();
337
338         for (size_t i = 0; i < s.GetCount(); ++i) {
339                 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
340                 if (j != _cinemas.end ()) {
341                         _selected_cinemas.push_back (j->second);
342                 }
343                 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator k = _screens.find (s[i]);
344                 if (k != _screens.end ()) {
345                         _selected_screens.push_back (k->second);
346                 }
347         }
348
349         setup_sensitivity ();
350         ScreensChanged ();
351 }
352
353 void
354 ScreensPanel::add_cinemas ()
355 {
356         _root = _targets->AddRoot ("Foo");
357
358         BOOST_FOREACH (shared_ptr<Cinema> i, Config::instance()->cinemas ()) {
359                 add_cinema (i);
360         }
361 }
362
363 void
364 ScreensPanel::search_changed ()
365 {
366         _targets->DeleteAllItems ();
367         _cinemas.clear ();
368         _screens.clear ();
369
370         add_cinemas ();
371
372         _ignore_selection_change = true;
373
374         BOOST_FOREACH (shared_ptr<Cinema> i, _selected_cinemas) {
375                 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.begin ();
376                 while (j != _cinemas.end() && j->second != i) {
377                         ++j;
378                 }
379
380                 if (j != _cinemas.end ()) {
381                         _targets->SelectItem (j->first);
382                 }
383         }
384
385         BOOST_FOREACH (shared_ptr<Screen> i, _selected_screens) {
386                 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.begin ();
387                 while (j != _screens.end() && j->second != i) {
388                         ++j;
389                 }
390
391                 if (j != _screens.end ()) {
392                         _targets->SelectItem (j->first);
393                 }
394         }
395
396         _ignore_selection_change = false;
397 }