Sort cinemas in screens panel (#726).
[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
28 using std::list;
29 using std::pair;
30 using std::cout;
31 using std::map;
32 using std::make_pair;
33 using boost::shared_ptr;
34
35 ScreensPanel::ScreensPanel (wxWindow* parent)
36         : wxPanel (parent, wxID_ANY)
37 {
38         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
39
40         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
41         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
42         targets->Add (_targets, 1, wxEXPAND | wxTOP | wxRIGHT, DCPOMATIC_SIZER_GAP);
43
44         _root = _targets->AddRoot ("Foo");
45
46         list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
47         for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
48                 add_cinema (*i);
49         }
50
51         _targets->ExpandAll ();
52
53         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
54
55         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
56         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
57         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
58         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
59         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
60         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
61
62         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
63         target_buttons->Add (_add_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
64         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
65         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
66         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
67         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
68
69         targets->Add (target_buttons, 0, 0);
70
71         sizer->Add (targets, 1, wxEXPAND);
72
73         _targets->Bind       (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed, this);
74
75         _add_cinema->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_cinema_clicked, this));
76         _edit_cinema->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
77         _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
78
79         _add_screen->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_screen_clicked, this));
80         _edit_screen->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_screen_clicked, this));
81         _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_screen_clicked, this));
82
83         SetSizer (sizer);
84 }
85
86 ScreensPanel::~ScreensPanel ()
87 {
88         _targets->Unbind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed, this);
89 }
90
91 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
92 ScreensPanel::selected_cinemas () const
93 {
94         wxArrayTreeItemIds s;
95         _targets->GetSelections (s);
96
97         list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
98         for (size_t i = 0; i < s.GetCount(); ++i) {
99                 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
100                 if (j != _cinemas.end ()) {
101                         c.push_back (make_pair (j->first, j->second));
102                 }
103         }
104
105         return c;
106 }
107
108 list<pair<wxTreeItemId, shared_ptr<Screen> > >
109 ScreensPanel::selected_screens () const
110 {
111         wxArrayTreeItemIds s;
112         _targets->GetSelections (s);
113
114         list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
115         for (size_t i = 0; i < s.GetCount(); ++i) {
116                 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
117                 if (j != _screens.end ()) {
118                         c.push_back (make_pair (j->first, j->second));
119                 }
120         }
121
122         return c;
123 }
124
125 void
126 ScreensPanel::setup_sensitivity ()
127 {
128         bool const sc = selected_cinemas().size() == 1;
129         bool const ss = selected_screens().size() == 1;
130
131         _edit_cinema->Enable (sc);
132         _remove_cinema->Enable (sc);
133
134         _add_screen->Enable (sc);
135         _edit_screen->Enable (ss);
136         _remove_screen->Enable (ss);
137 }
138
139
140 void
141 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
142 {
143         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
144
145         list<shared_ptr<Screen> > sc = c->screens ();
146         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
147                 add_screen (c, *i);
148         }
149
150         _targets->SortChildren (_root);
151 }
152
153 void
154 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
155 {
156         map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
157         while (i != _cinemas.end() && i->second != c) {
158                 ++i;
159         }
160
161         if (i == _cinemas.end()) {
162                 return;
163         }
164
165         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
166         _targets->Expand (i->first);
167 }
168
169 void
170 ScreensPanel::add_cinema_clicked ()
171 {
172         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
173         if (d->ShowModal () == wxID_OK) {
174                 shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
175                 Config::instance()->add_cinema (c);
176                 add_cinema (c);
177         }
178
179         d->Destroy ();
180 }
181
182 void
183 ScreensPanel::edit_cinema_clicked ()
184 {
185         if (selected_cinemas().size() != 1) {
186                 return;
187         }
188
189         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
190
191         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
192         if (d->ShowModal () == wxID_OK) {
193                 c.second->name = d->name ();
194                 c.second->email = d->email ();
195                 _targets->SetItemText (c.first, std_to_wx (d->name()));
196                 Config::instance()->changed ();
197         }
198
199         d->Destroy ();
200 }
201
202 void
203 ScreensPanel::remove_cinema_clicked ()
204 {
205         if (selected_cinemas().size() != 1) {
206                 return;
207         }
208
209         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
210
211         Config::instance()->remove_cinema (c.second);
212         _targets->Delete (c.first);
213 }
214
215 void
216 ScreensPanel::add_screen_clicked ()
217 {
218         if (selected_cinemas().size() != 1) {
219                 return;
220         }
221
222         shared_ptr<Cinema> c = selected_cinemas().front().second;
223
224         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
225         if (d->ShowModal () != wxID_OK) {
226                 return;
227         }
228
229         shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
230         c->add_screen (s);
231         add_screen (c, s);
232
233         Config::instance()->changed ();
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().front();
246
247         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
248         if (d->ShowModal () == wxID_OK) {
249                 s.second->name = d->name ();
250                 s.second->certificate = d->certificate ();
251                 _targets->SetItemText (s.first, std_to_wx (d->name()));
252                 Config::instance()->changed ();
253         }
254
255         d->Destroy ();
256 }
257
258 void
259 ScreensPanel::remove_screen_clicked ()
260 {
261         if (selected_screens().size() != 1) {
262                 return;
263         }
264
265         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
266
267         map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
268         while (i != _cinemas.end ()) {
269                 list<shared_ptr<Screen> > sc = i->second->screens ();
270                 if (find (sc.begin(), sc.end(), s.second) != sc.end ()) {
271                         break;
272                 }
273         }
274
275         if (i == _cinemas.end()) {
276                 return;
277         }
278
279         i->second->remove_screen (s.second);
280         _targets->Delete (s.first);
281
282         Config::instance()->changed ();
283 }
284
285 list<shared_ptr<Screen> >
286 ScreensPanel::screens () const
287 {
288         list<shared_ptr<Screen> > s;
289
290         list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
291         for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
292                 list<shared_ptr<Screen> > sc = i->second->screens ();
293                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
294                         s.push_back (*j);
295                 }
296         }
297
298         list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
299         for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
300                 s.push_back (i->second);
301         }
302
303         s.sort ();
304         s.unique ();
305
306         return s;
307 }
308
309 void
310 ScreensPanel::selection_changed (wxTreeEvent &)
311 {
312         setup_sensitivity ();
313         ScreensChanged ();
314 }