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