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"
36 using std::shared_ptr;
37 using boost::optional;
38 using namespace dcpomatic;
40 ScreensPanel::ScreensPanel (wxWindow* parent)
41 : wxPanel (parent, wxID_ANY)
42 , _ignore_selection_change (false)
44 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
47 int const height = 30;
49 int const height = -1;
52 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, height));
54 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
55 _search->ShowCancelButton (true);
57 sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
59 wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
60 _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
61 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
65 wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
67 _add_cinema = new Button (this, _("Add Cinema..."));
68 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
69 _edit_cinema = new Button (this, _("Edit Cinema..."));
70 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
71 _remove_cinema = new Button (this, _("Remove Cinema"));
72 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
73 _add_screen = new Button (this, _("Add Screen..."));
74 target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
75 _edit_screen = new Button (this, _("Edit Screen..."));
76 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
77 _remove_screen = new Button (this, _("Remove Screen"));
78 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
80 targets->Add (target_buttons, 0, 0);
82 sizer->Add (targets, 1, wxEXPAND);
84 _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
85 _targets->Bind (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
87 _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
88 _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
89 _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
91 _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
92 _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
93 _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
98 ScreensPanel::~ScreensPanel ()
100 _targets->Unbind (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
104 ScreensPanel::setup_sensitivity ()
106 bool const sc = _selected_cinemas.size() == 1;
107 bool const ss = _selected_screens.size() == 1;
109 _edit_cinema->Enable (sc);
110 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
112 _add_screen->Enable (sc);
113 _edit_screen->Enable (ss);
114 _remove_screen->Enable (_selected_screens.size() >= 1);
118 optional<wxTreeItemId>
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) {
128 return optional<wxTreeItemId>();
132 wxTreeItemId id = _targets->AppendItem(_root, std_to_wx(c->name));
136 for (auto i: c->screens()) {
140 _targets->SortChildren (_root);
146 optional<wxTreeItemId>
147 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
149 CinemaMap::const_iterator i = _cinemas.begin();
150 while (i != _cinemas.end() && i->second != c) {
154 if (i == _cinemas.end()) {
155 return optional<wxTreeItemId> ();
158 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
163 ScreensPanel::add_cinema_clicked ()
165 CinemaDialog* d = new CinemaDialog (GetParent(), _("Add Cinema"));
166 if (d->ShowModal () == wxID_OK) {
167 shared_ptr<Cinema> c (new Cinema (d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute()));
168 Config::instance()->add_cinema (c);
169 optional<wxTreeItemId> id = add_cinema (c);
171 _targets->Unselect ();
172 _targets->SelectItem (*id);
180 ScreensPanel::edit_cinema_clicked ()
182 if (_selected_cinemas.size() != 1) {
186 pair<wxTreeItemId, shared_ptr<Cinema> > c = *_selected_cinemas.begin();
188 CinemaDialog* d = new CinemaDialog (
189 GetParent(), _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
192 if (d->ShowModal () == wxID_OK) {
193 c.second->name = d->name ();
194 c.second->emails = d->emails ();
195 c.second->notes = d->notes ();
196 c.second->set_utc_offset_hour (d->utc_offset_hour ());
197 c.second->set_utc_offset_minute (d->utc_offset_minute ());
198 _targets->SetItemText (c.first, std_to_wx (d->name()));
199 Config::instance()->changed (Config::CINEMAS);
206 ScreensPanel::remove_cinema_clicked ()
208 if (_selected_cinemas.size() == 1) {
209 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas.begin()->second->name)))) {
213 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
218 for (CinemaMap::iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
219 Config::instance()->remove_cinema (i->second);
220 _targets->Delete (i->first);
223 selection_changed ();
227 ScreensPanel::add_screen_clicked ()
229 if (_selected_cinemas.size() != 1) {
233 shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
235 ScreenDialog* d = new ScreenDialog (GetParent(), _("Add Screen"));
236 if (d->ShowModal () != wxID_OK) {
241 for (auto i: c->screens ()) {
242 if (i->name == d->name()) {
246 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
247 std_to_wx(d->name()).data()
254 shared_ptr<Screen> s (new Screen (d->name(), d->notes(), d->recipient(), d->trusted_devices()));
256 optional<wxTreeItemId> id = add_screen (c, s);
258 _targets->Expand (id.get ());
261 Config::instance()->changed (Config::CINEMAS);
267 ScreensPanel::edit_screen_clicked ()
269 if (_selected_screens.size() != 1) {
273 pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
275 ScreenDialog* d = new ScreenDialog (GetParent(), _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->trusted_devices);
276 if (d->ShowModal () != wxID_OK) {
281 shared_ptr<Cinema> c = s.second->cinema;
282 for (auto i: c->screens ()) {
283 if (i != s.second && i->name == d->name()) {
287 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
288 std_to_wx(d->name()).data()
295 s.second->name = d->name ();
296 s.second->notes = d->notes ();
297 s.second->recipient = d->recipient ();
298 s.second->trusted_devices = d->trusted_devices ();
299 _targets->SetItemText (s.first, std_to_wx (d->name()));
300 Config::instance()->changed (Config::CINEMAS);
306 ScreensPanel::remove_screen_clicked ()
308 if (_selected_screens.size() == 1) {
309 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens.begin()->second->name)))) {
313 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
318 for (ScreenMap::iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
319 CinemaMap::iterator j = _cinemas.begin ();
320 while (j != _cinemas.end ()) {
321 list<shared_ptr<Screen> > sc = j->second->screens ();
322 if (find (sc.begin(), sc.end(), i->second) != sc.end ()) {
329 if (j == _cinemas.end()) {
333 j->second->remove_screen (i->second);
334 _targets->Delete (i->first);
337 Config::instance()->changed (Config::CINEMAS);
340 list<shared_ptr<Screen> >
341 ScreensPanel::screens () const
343 list<shared_ptr<Screen> > s;
345 for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
346 list<shared_ptr<Screen> > sc = i->second->screens ();
347 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
352 for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
353 s.push_back (i->second);
363 ScreensPanel::selection_changed_shim (wxTreeEvent &)
365 selection_changed ();
369 ScreensPanel::selection_changed ()
371 if (_ignore_selection_change) {
375 wxArrayTreeItemIds s;
376 _targets->GetSelections (s);
378 _selected_cinemas.clear ();
379 _selected_screens.clear ();
381 for (size_t i = 0; i < s.GetCount(); ++i) {
382 CinemaMap::const_iterator j = _cinemas.find (s[i]);
383 if (j != _cinemas.end ()) {
384 _selected_cinemas[j->first] = j->second;
386 ScreenMap::const_iterator k = _screens.find (s[i]);
387 if (k != _screens.end ()) {
388 _selected_screens[k->first] = k->second;
392 setup_sensitivity ();
397 ScreensPanel::add_cinemas ()
399 _root = _targets->AddRoot ("Foo");
401 for (auto i: Config::instance()->cinemas()) {
407 ScreensPanel::search_changed ()
409 _targets->DeleteAllItems ();
415 _ignore_selection_change = true;
417 for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
418 /* The wxTreeItemIds will now be different, so we must search by cinema */
419 CinemaMap::const_iterator j = _cinemas.begin ();
420 while (j != _cinemas.end() && j->second != i->second) {
424 if (j != _cinemas.end ()) {
425 _targets->SelectItem (j->first);
429 for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
430 ScreenMap::const_iterator j = _screens.begin ();
431 while (j != _screens.end() && j->second != i->second) {
435 if (j != _screens.end ()) {
436 _targets->SelectItem (j->first);
440 _ignore_selection_change = false;