2 Copyright (C) 2015-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "screens_panel.h"
23 #include "cinema_dialog.h"
24 #include "screen_dialog.h"
25 #include "dcpomatic_button.h"
26 #include "lib/config.h"
27 #include "lib/cinema.h"
28 #include "lib/screen.h"
29 #include <boost/foreach.hpp>
37 using boost::shared_ptr;
38 using boost::optional;
39 using namespace dcpomatic;
41 ScreensPanel::ScreensPanel (wxWindow* parent)
42 : wxPanel (parent, wxID_ANY)
43 , _ignore_selection_change (false)
45 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
48 int const height = 30;
50 int const height = -1;
53 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, height));
55 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
56 _search->ShowCancelButton (true);
58 sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
60 wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
61 _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
62 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
66 wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
68 _add_cinema = new Button (this, _("Add Cinema..."));
69 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
70 _edit_cinema = new Button (this, _("Edit Cinema..."));
71 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
72 _remove_cinema = new Button (this, _("Remove Cinema"));
73 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
74 _add_screen = new Button (this, _("Add Screen..."));
75 target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
76 _edit_screen = new Button (this, _("Edit Screen..."));
77 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
78 _remove_screen = new Button (this, _("Remove Screen"));
79 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
81 targets->Add (target_buttons, 0, 0);
83 sizer->Add (targets, 1, wxEXPAND);
85 _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
86 _targets->Bind (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
88 _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
89 _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
90 _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
92 _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
93 _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
94 _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
99 ScreensPanel::~ScreensPanel ()
101 _targets->Unbind (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
105 ScreensPanel::setup_sensitivity ()
107 bool const sc = _selected_cinemas.size() == 1;
108 bool const ss = _selected_screens.size() == 1;
110 _edit_cinema->Enable (sc);
111 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
113 _add_screen->Enable (sc);
114 _edit_screen->Enable (ss);
115 _remove_screen->Enable (_selected_screens.size() >= 1);
119 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
121 string search = wx_to_std (_search->GetValue ());
122 transform (search.begin(), search.end(), search.begin(), ::tolower);
124 if (!search.empty ()) {
125 string name = c->name;
126 transform (name.begin(), name.end(), name.begin(), ::tolower);
127 if (name.find (search) == string::npos) {
132 _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
134 list<shared_ptr<Screen> > sc = c->screens ();
135 for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
139 _targets->SortChildren (_root);
142 optional<wxTreeItemId>
143 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
145 CinemaMap::const_iterator i = _cinemas.begin();
146 while (i != _cinemas.end() && i->second != c) {
150 if (i == _cinemas.end()) {
151 return optional<wxTreeItemId> ();
154 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
159 ScreensPanel::add_cinema_clicked ()
161 CinemaDialog* d = new CinemaDialog (GetParent(), _("Add Cinema"));
162 if (d->ShowModal () == wxID_OK) {
163 shared_ptr<Cinema> c (new Cinema (d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute()));
164 Config::instance()->add_cinema (c);
172 ScreensPanel::edit_cinema_clicked ()
174 if (_selected_cinemas.size() != 1) {
178 pair<wxTreeItemId, shared_ptr<Cinema> > c = *_selected_cinemas.begin();
180 CinemaDialog* d = new CinemaDialog (
181 GetParent(), _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
184 if (d->ShowModal () == wxID_OK) {
185 c.second->name = d->name ();
186 c.second->emails = d->emails ();
187 c.second->notes = d->notes ();
188 c.second->set_utc_offset_hour (d->utc_offset_hour ());
189 c.second->set_utc_offset_minute (d->utc_offset_minute ());
190 _targets->SetItemText (c.first, std_to_wx (d->name()));
191 Config::instance()->changed (Config::CINEMAS);
198 ScreensPanel::remove_cinema_clicked ()
200 for (CinemaMap::iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
201 Config::instance()->remove_cinema (i->second);
202 _targets->Delete (i->first);
205 selection_changed ();
209 ScreensPanel::add_screen_clicked ()
211 if (_selected_cinemas.size() != 1) {
215 shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
217 ScreenDialog* d = new ScreenDialog (GetParent(), _("Add Screen"));
218 if (d->ShowModal () != wxID_OK) {
223 BOOST_FOREACH (shared_ptr<Screen> i, c->screens ()) {
224 if (i->name == d->name()) {
228 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
229 std_to_wx(d->name()).data()
236 shared_ptr<Screen> s (new Screen (d->name(), d->notes(), d->recipient(), d->trusted_devices()));
238 optional<wxTreeItemId> id = add_screen (c, s);
240 _targets->Expand (id.get ());
243 Config::instance()->changed (Config::CINEMAS);
249 ScreensPanel::edit_screen_clicked ()
251 if (_selected_screens.size() != 1) {
255 pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
257 ScreenDialog* d = new ScreenDialog (GetParent(), _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->trusted_devices);
258 if (d->ShowModal () != wxID_OK) {
263 shared_ptr<Cinema> c = s.second->cinema;
264 BOOST_FOREACH (shared_ptr<Screen> i, c->screens ()) {
265 if (i != s.second && i->name == d->name()) {
269 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
270 std_to_wx(d->name()).data()
277 s.second->name = d->name ();
278 s.second->notes = d->notes ();
279 s.second->recipient = d->recipient ();
280 s.second->trusted_devices = d->trusted_devices ();
281 _targets->SetItemText (s.first, std_to_wx (d->name()));
282 Config::instance()->changed (Config::CINEMAS);
288 ScreensPanel::remove_screen_clicked ()
290 for (ScreenMap::iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
291 CinemaMap::iterator j = _cinemas.begin ();
292 while (j != _cinemas.end ()) {
293 list<shared_ptr<Screen> > sc = j->second->screens ();
294 if (find (sc.begin(), sc.end(), i->second) != sc.end ()) {
301 if (j == _cinemas.end()) {
305 j->second->remove_screen (i->second);
306 _targets->Delete (i->first);
309 Config::instance()->changed (Config::CINEMAS);
312 list<shared_ptr<Screen> >
313 ScreensPanel::screens () const
315 list<shared_ptr<Screen> > s;
317 for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
318 list<shared_ptr<Screen> > sc = i->second->screens ();
319 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
324 for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
325 s.push_back (i->second);
335 ScreensPanel::selection_changed_shim (wxTreeEvent &)
337 selection_changed ();
341 ScreensPanel::selection_changed ()
343 if (_ignore_selection_change) {
347 wxArrayTreeItemIds s;
348 _targets->GetSelections (s);
350 _selected_cinemas.clear ();
351 _selected_screens.clear ();
353 for (size_t i = 0; i < s.GetCount(); ++i) {
354 CinemaMap::const_iterator j = _cinemas.find (s[i]);
355 if (j != _cinemas.end ()) {
356 _selected_cinemas[j->first] = j->second;
358 ScreenMap::const_iterator k = _screens.find (s[i]);
359 if (k != _screens.end ()) {
360 _selected_screens[k->first] = k->second;
364 setup_sensitivity ();
369 ScreensPanel::add_cinemas ()
371 _root = _targets->AddRoot ("Foo");
373 BOOST_FOREACH (shared_ptr<Cinema> i, Config::instance()->cinemas ()) {
379 ScreensPanel::search_changed ()
381 _targets->DeleteAllItems ();
387 _ignore_selection_change = true;
389 for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
390 /* The wxTreeItemIds will now be different, so we must search by cinema */
391 CinemaMap::const_iterator j = _cinemas.begin ();
392 while (j != _cinemas.end() && j->second != i->second) {
396 if (j != _cinemas.end ()) {
397 _targets->SelectItem (j->first);
401 for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
402 ScreenMap::const_iterator j = _screens.begin ();
403 while (j != _screens.end() && j->second != i->second) {
407 if (j != _screens.end ()) {
408 _targets->SelectItem (j->first);
412 _ignore_selection_change = false;