2 Copyright (C) 2015-2021 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/>.
22 #include "screens_panel.h"
24 #include "cinema_dialog.h"
25 #include "screen_dialog.h"
26 #include "dcpomatic_button.h"
27 #include "lib/config.h"
28 #include "lib/cinema.h"
29 #include "lib/screen.h"
38 using std::make_shared;
39 using std::shared_ptr;
40 using boost::optional;
41 using namespace dcpomatic;
44 ScreensPanel::ScreensPanel (wxWindow* parent)
45 : wxPanel (parent, wxID_ANY)
46 , _ignore_selection_change (false)
48 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
51 int const height = 30;
53 int const height = -1;
56 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, height));
58 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
59 _search->ShowCancelButton (true);
61 sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
63 auto targets = new wxBoxSizer (wxHORIZONTAL);
64 _targets = new TreeCtrl (this);
65 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
69 auto target_buttons = new wxBoxSizer (wxVERTICAL);
71 _add_cinema = new Button (this, _("Add Cinema..."));
72 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
73 _edit_cinema = new Button (this, _("Edit Cinema..."));
74 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
75 _remove_cinema = new Button (this, _("Remove Cinema"));
76 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
77 _add_screen = new Button (this, _("Add Screen..."));
78 target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
79 _edit_screen = new Button (this, _("Edit Screen..."));
80 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
81 _remove_screen = new Button (this, _("Remove Screen"));
82 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
84 targets->Add (target_buttons, 0, 0);
86 sizer->Add (targets, 1, wxEXPAND);
88 _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
89 _targets->Bind (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
91 _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
92 _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
93 _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
95 _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
96 _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
97 _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
103 ScreensPanel::~ScreensPanel ()
105 _targets->Unbind (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
110 ScreensPanel::setup_sensitivity ()
112 bool const sc = _selected_cinemas.size() == 1;
113 bool const ss = _selected_screens.size() == 1;
115 _edit_cinema->Enable (sc);
116 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
118 _add_screen->Enable (sc);
119 _edit_screen->Enable (ss);
120 _remove_screen->Enable (_selected_screens.size() >= 1);
124 optional<wxTreeItemId>
125 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
127 auto search = wx_to_std (_search->GetValue ());
128 transform (search.begin(), search.end(), search.begin(), ::tolower);
130 if (!search.empty ()) {
132 transform (name.begin(), name.end(), name.begin(), ::tolower);
133 if (name.find (search) == string::npos) {
134 return optional<wxTreeItemId>();
138 auto id = _targets->AppendItem(_root, std_to_wx(c->name));
142 for (auto i: c->screens()) {
146 _targets->SortChildren (_root);
152 optional<wxTreeItemId>
153 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
155 auto i = _cinemas.begin();
156 while (i != _cinemas.end() && i->second != c) {
160 if (i == _cinemas.end()) {
164 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
170 ScreensPanel::add_cinema_clicked ()
172 auto d = new CinemaDialog (GetParent(), _("Add Cinema"));
173 if (d->ShowModal () == wxID_OK) {
174 auto c = make_shared<Cinema>(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute());
175 Config::instance()->add_cinema (c);
176 auto id = add_cinema (c);
178 _targets->Unselect ();
179 _targets->SelectItem (*id);
188 ScreensPanel::edit_cinema_clicked ()
190 if (_selected_cinemas.size() != 1) {
194 auto c = *_selected_cinemas.begin();
196 auto d = new CinemaDialog (
197 GetParent(), _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
200 if (d->ShowModal() == wxID_OK) {
201 c.second->name = d->name ();
202 c.second->emails = d->emails ();
203 c.second->notes = d->notes ();
204 c.second->set_utc_offset_hour (d->utc_offset_hour ());
205 c.second->set_utc_offset_minute (d->utc_offset_minute ());
206 _targets->SetItemText (c.first, std_to_wx (d->name()));
207 Config::instance()->changed (Config::CINEMAS);
215 ScreensPanel::remove_cinema_clicked ()
217 if (_selected_cinemas.size() == 1) {
218 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas.begin()->second->name)))) {
222 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
227 for (auto const& i: _selected_cinemas) {
228 Config::instance()->remove_cinema (i.second);
229 _targets->Delete (i.first);
232 selection_changed ();
237 ScreensPanel::add_screen_clicked ()
239 if (_selected_cinemas.size() != 1) {
243 auto c = _selected_cinemas.begin()->second;
245 auto d = new ScreenDialog (GetParent(), _("Add Screen"));
246 if (d->ShowModal () != wxID_OK) {
251 for (auto i: c->screens ()) {
252 if (i->name == d->name()) {
256 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
257 std_to_wx(d->name()).data()
264 auto s = std::make_shared<Screen>(d->name(), d->notes(), d->recipient(), d->trusted_devices());
266 auto id = add_screen (c, s);
268 _targets->Expand (id.get ());
271 Config::instance()->changed (Config::CINEMAS);
278 ScreensPanel::edit_screen_clicked ()
280 if (_selected_screens.size() != 1) {
284 auto s = *_selected_screens.begin();
286 auto d = new ScreenDialog (GetParent(), _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->trusted_devices);
287 if (d->ShowModal() != wxID_OK) {
292 auto c = s.second->cinema;
293 for (auto i: c->screens ()) {
294 if (i != s.second && i->name == d->name()) {
298 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
299 std_to_wx(d->name()).data()
306 s.second->name = d->name ();
307 s.second->notes = d->notes ();
308 s.second->recipient = d->recipient ();
309 s.second->trusted_devices = d->trusted_devices ();
310 _targets->SetItemText (s.first, std_to_wx (d->name()));
311 Config::instance()->changed (Config::CINEMAS);
318 ScreensPanel::remove_screen_clicked ()
320 if (_selected_screens.size() == 1) {
321 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens.begin()->second->name)))) {
325 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
330 for (auto const& i: _selected_screens) {
331 auto j = _cinemas.begin ();
332 while (j != _cinemas.end ()) {
333 auto sc = j->second->screens ();
334 if (find (sc.begin(), sc.end(), i.second) != sc.end ()) {
341 if (j == _cinemas.end()) {
345 j->second->remove_screen (i.second);
346 _targets->Delete (i.first);
349 Config::instance()->changed (Config::CINEMAS);
353 list<shared_ptr<Screen>>
354 ScreensPanel::screens () const
356 list<shared_ptr<Screen>> s;
358 for (auto const& i: _selected_cinemas) {
359 for (auto j: i.second->screens()) {
364 for (auto const& i: _selected_screens) {
365 s.push_back (i.second);
376 ScreensPanel::selection_changed_shim (wxTreeEvent &)
378 selection_changed ();
383 ScreensPanel::selection_changed ()
385 if (_ignore_selection_change) {
389 wxArrayTreeItemIds s;
390 _targets->GetSelections (s);
392 _selected_cinemas.clear ();
393 _selected_screens.clear ();
395 for (size_t i = 0; i < s.GetCount(); ++i) {
396 auto j = _cinemas.find (s[i]);
397 if (j != _cinemas.end ()) {
398 _selected_cinemas[j->first] = j->second;
400 auto k = _screens.find (s[i]);
401 if (k != _screens.end ()) {
402 _selected_screens[k->first] = k->second;
406 setup_sensitivity ();
412 ScreensPanel::add_cinemas ()
414 _root = _targets->AddRoot ("Foo");
416 for (auto i: Config::instance()->cinemas()) {
423 ScreensPanel::search_changed ()
425 _targets->DeleteAllItems ();
431 _ignore_selection_change = true;
433 for (auto const& i: _selected_cinemas) {
434 /* The wxTreeItemIds will now be different, so we must search by cinema */
435 auto j = _cinemas.begin ();
436 while (j != _cinemas.end() && j->second != i.second) {
440 if (j != _cinemas.end()) {
441 _targets->SelectItem (j->first);
445 for (auto const& i: _selected_screens) {
446 auto j = _screens.begin ();
447 while (j != _screens.end() && j->second != i.second) {
451 if (j != _screens.end()) {
452 _targets->SelectItem (j->first);
456 _ignore_selection_change = false;
460 wxIMPLEMENT_DYNAMIC_CLASS (TreeCtrl, wxTreeCtrl);
464 TreeCtrl::OnCompareItems (wxTreeItemId const& a, wxTreeItemId const& b)
466 return strcoll (wx_to_std(GetItemText(a)).c_str(), wx_to_std(GetItemText(b)).c_str());