2 Copyright (C) 2015-2016 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "lib/config.h"
21 #include "lib/cinema.h"
22 #include "lib/screen.h"
23 #include "screens_panel.h"
25 #include "cinema_dialog.h"
26 #include "screen_dialog.h"
27 #include <boost/foreach.hpp>
35 using boost::shared_ptr;
36 using boost::optional;
38 ScreensPanel::ScreensPanel (wxWindow* parent)
39 : wxPanel (parent, wxID_ANY)
40 , _ignore_selection_change (false)
42 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
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);
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);
54 wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
56 _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
57 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
58 _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
59 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
60 _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
61 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
63 _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
64 target_buttons->Add (_add_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
65 _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
66 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
67 _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
68 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
70 targets->Add (target_buttons, 0, 0);
72 sizer->Add (targets, 1, wxEXPAND);
74 _search->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ScreensPanel::search_changed, this));
75 _targets->Bind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
77 _add_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_cinema_clicked, this));
78 _edit_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
79 _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
81 _add_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_screen_clicked, this));
82 _edit_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_screen_clicked, this));
83 _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_screen_clicked, this));
88 ScreensPanel::~ScreensPanel ()
90 _targets->Unbind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
94 ScreensPanel::setup_sensitivity ()
96 bool const sc = _selected_cinemas.size() == 1;
97 bool const ss = _selected_screens.size() == 1;
99 _edit_cinema->Enable (sc);
100 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
102 _add_screen->Enable (sc);
103 _edit_screen->Enable (ss);
104 _remove_screen->Enable (_selected_screens.size() >= 1);
108 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
110 string search = wx_to_std (_search->GetValue ());
111 transform (search.begin(), search.end(), search.begin(), ::tolower);
113 if (!search.empty ()) {
114 string name = c->name;
115 transform (name.begin(), name.end(), name.begin(), ::tolower);
116 if (name.find (search) == string::npos) {
121 _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
123 list<shared_ptr<Screen> > sc = c->screens ();
124 for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
128 _targets->SortChildren (_root);
131 optional<wxTreeItemId>
132 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
134 CinemaMap::const_iterator i = _cinemas.begin();
135 while (i != _cinemas.end() && i->second != c) {
139 if (i == _cinemas.end()) {
140 return optional<wxTreeItemId> ();
143 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
148 ScreensPanel::add_cinema_clicked ()
150 CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
151 if (d->ShowModal () == wxID_OK) {
152 shared_ptr<Cinema> c (new Cinema (d->name(), d->emails()));
153 Config::instance()->add_cinema (c);
161 ScreensPanel::edit_cinema_clicked ()
163 if (_selected_cinemas.size() != 1) {
167 pair<wxTreeItemId, shared_ptr<Cinema> > c = *_selected_cinemas.begin();
169 CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->emails);
170 if (d->ShowModal () == wxID_OK) {
171 c.second->name = d->name ();
172 c.second->emails = d->emails ();
173 _targets->SetItemText (c.first, std_to_wx (d->name()));
174 Config::instance()->changed ();
181 ScreensPanel::remove_cinema_clicked ()
183 for (CinemaMap::iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
184 Config::instance()->remove_cinema (i->second);
185 _targets->Delete (i->first);
188 selection_changed ();
192 ScreensPanel::add_screen_clicked ()
194 if (_selected_cinemas.size() != 1) {
198 shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
200 ScreenDialog* d = new ScreenDialog (this, "Add Screen");
201 if (d->ShowModal () != wxID_OK) {
205 shared_ptr<Screen> s (new Screen (d->name(), d->recipient(), d->trusted_devices()));
207 optional<wxTreeItemId> id = add_screen (c, s);
209 _targets->Expand (id.get ());
212 Config::instance()->changed ();
218 ScreensPanel::edit_screen_clicked ()
220 if (_selected_screens.size() != 1) {
224 pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
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 ();
239 ScreensPanel::remove_screen_clicked ()
241 for (ScreenMap::iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
242 CinemaMap::iterator j = _cinemas.begin ();
243 while (j != _cinemas.end ()) {
244 list<shared_ptr<Screen> > sc = j->second->screens ();
245 if (find (sc.begin(), sc.end(), i->second) != sc.end ()) {
252 if (j == _cinemas.end()) {
256 j->second->remove_screen (i->second);
257 _targets->Delete (i->first);
260 Config::instance()->changed ();
263 list<shared_ptr<Screen> >
264 ScreensPanel::screens () const
266 list<shared_ptr<Screen> > s;
268 for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
269 list<shared_ptr<Screen> > sc = i->second->screens ();
270 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
275 for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
276 s.push_back (i->second);
286 ScreensPanel::selection_changed_shim (wxTreeEvent &)
288 selection_changed ();
292 ScreensPanel::selection_changed ()
294 if (_ignore_selection_change) {
298 wxArrayTreeItemIds s;
299 _targets->GetSelections (s);
301 _selected_cinemas.clear ();
302 _selected_screens.clear ();
304 for (size_t i = 0; i < s.GetCount(); ++i) {
305 CinemaMap::const_iterator j = _cinemas.find (s[i]);
306 if (j != _cinemas.end ()) {
307 _selected_cinemas[j->first] = j->second;
309 ScreenMap::const_iterator k = _screens.find (s[i]);
310 if (k != _screens.end ()) {
311 _selected_screens[k->first] = k->second;
315 setup_sensitivity ();
320 ScreensPanel::add_cinemas ()
322 _root = _targets->AddRoot ("Foo");
324 BOOST_FOREACH (shared_ptr<Cinema> i, Config::instance()->cinemas ()) {
330 ScreensPanel::search_changed ()
332 _targets->DeleteAllItems ();
338 _ignore_selection_change = true;
340 for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
341 /* The wxTreeItemIds will now be different, so we must search by cinema */
342 CinemaMap::const_iterator j = _cinemas.begin ();
343 while (j != _cinemas.end() && j->second != i->second) {
347 if (j != _cinemas.end ()) {
348 _targets->SelectItem (j->first);
352 for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
353 ScreenMap::const_iterator j = _screens.begin ();
354 while (j != _screens.end() && j->second != i->second) {
358 if (j != _screens.end ()) {
359 _targets->SelectItem (j->first);
363 _ignore_selection_change = false;