2 Copyright (C) 2015-2022 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 "cinema_dialog.h"
23 #include "dcpomatic_button.h"
24 #include "screen_dialog.h"
25 #include "screens_panel.h"
27 #include "lib/cinema.h"
28 #include "lib/config.h"
29 #include "lib/screen.h"
34 using std::make_shared;
37 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 auto sizer = new wxBoxSizer (wxVERTICAL);
50 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
52 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
53 _search->ShowCancelButton (true);
55 sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
57 auto targets = new wxBoxSizer (wxHORIZONTAL);
58 _targets = new TreeListCtrl (this);
59 _targets->AppendColumn (wxT("foo"));
60 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
64 auto target_buttons = new wxBoxSizer (wxVERTICAL);
66 _add_cinema = new Button (this, _("Add Cinema..."));
67 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
68 _edit_cinema = new Button (this, _("Edit Cinema..."));
69 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
70 _remove_cinema = new Button (this, _("Remove Cinema"));
71 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
72 _add_screen = new Button (this, _("Add Screen..."));
73 target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
74 _edit_screen = new Button (this, _("Edit Screen..."));
75 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
76 _remove_screen = new Button (this, _("Remove Screen"));
77 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
79 targets->Add (target_buttons, 0, 0);
81 sizer->Add (targets, 1, wxEXPAND);
83 _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
84 _targets->Bind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
85 _targets->Bind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, 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));
99 ScreensPanel::~ScreensPanel ()
101 _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
102 _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
107 ScreensPanel::setup_sensitivity ()
109 bool const sc = _selected_cinemas.size() == 1;
110 bool const ss = _selected_screens.size() == 1;
112 _edit_cinema->Enable (sc);
113 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
115 _add_screen->Enable (sc);
116 _edit_screen->Enable (ss);
117 _remove_screen->Enable (_selected_screens.size() >= 1);
121 optional<wxTreeListItem>
122 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
124 auto search = wx_to_std (_search->GetValue ());
125 transform (search.begin(), search.end(), search.begin(), ::tolower);
127 if (!search.empty ()) {
129 transform (name.begin(), name.end(), name.begin(), ::tolower);
130 if (name.find (search) == string::npos) {
135 auto id = _targets->AppendItem(_targets->GetRootItem(), std_to_wx(c->name));
139 for (auto i: c->screens()) {
143 _targets->SetSortColumn (0);
149 optional<wxTreeListItem>
150 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
152 auto i = _cinemas.begin();
153 while (i != _cinemas.end() && i->second != c) {
157 if (i == _cinemas.end()) {
161 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
167 ScreensPanel::add_cinema_clicked ()
169 auto d = new CinemaDialog (GetParent(), _("Add Cinema"));
170 if (d->ShowModal () == wxID_OK) {
171 auto c = make_shared<Cinema>(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute());
172 Config::instance()->add_cinema (c);
173 auto id = add_cinema (c);
175 _targets->UnselectAll ();
176 _targets->Select (*id);
185 ScreensPanel::edit_cinema_clicked ()
187 if (_selected_cinemas.size() != 1) {
191 auto c = *_selected_cinemas.begin();
193 auto d = new CinemaDialog (
194 GetParent(), _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
197 if (d->ShowModal() == wxID_OK) {
198 c.second->name = d->name ();
199 c.second->emails = d->emails ();
200 c.second->notes = d->notes ();
201 c.second->set_utc_offset_hour (d->utc_offset_hour ());
202 c.second->set_utc_offset_minute (d->utc_offset_minute ());
203 _targets->SetItemText (c.first, std_to_wx (d->name()));
204 Config::instance()->changed (Config::CINEMAS);
212 ScreensPanel::remove_cinema_clicked ()
214 if (_selected_cinemas.size() == 1) {
215 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas.begin()->second->name)))) {
219 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
224 for (auto const& i: _selected_cinemas) {
225 Config::instance()->remove_cinema (i.second);
226 _targets->DeleteItem (i.first);
229 selection_changed ();
234 ScreensPanel::add_screen_clicked ()
236 if (_selected_cinemas.size() != 1) {
240 auto c = _selected_cinemas.begin()->second;
242 auto d = new ScreenDialog (GetParent(), _("Add Screen"));
243 if (d->ShowModal () != wxID_OK) {
248 for (auto i: c->screens ()) {
249 if (i->name == d->name()) {
253 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
254 std_to_wx(d->name()).data()
261 auto s = std::make_shared<Screen>(d->name(), d->notes(), d->recipient(), d->recipient_file(), d->trusted_devices());
263 auto id = add_screen (c, s);
265 _targets->Expand (id.get ());
268 Config::instance()->changed (Config::CINEMAS);
275 ScreensPanel::edit_screen_clicked ()
277 if (_selected_screens.size() != 1) {
281 auto s = *_selected_screens.begin();
283 auto d = new ScreenDialog (GetParent(), _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->recipient_file, s.second->trusted_devices);
284 if (d->ShowModal() != wxID_OK) {
289 auto c = s.second->cinema;
290 for (auto i: c->screens ()) {
291 if (i != s.second && i->name == d->name()) {
295 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
296 std_to_wx(d->name()).data()
303 s.second->name = d->name ();
304 s.second->notes = d->notes ();
305 s.second->recipient = d->recipient ();
306 s.second->recipient_file = d->recipient_file ();
307 s.second->trusted_devices = d->trusted_devices ();
308 _targets->SetItemText (s.first, std_to_wx (d->name()));
309 Config::instance()->changed (Config::CINEMAS);
316 ScreensPanel::remove_screen_clicked ()
318 if (_selected_screens.size() == 1) {
319 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens.begin()->second->name)))) {
323 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
328 for (auto const& i: _selected_screens) {
329 auto j = _cinemas.begin ();
330 while (j != _cinemas.end ()) {
331 auto sc = j->second->screens ();
332 if (find (sc.begin(), sc.end(), i.second) != sc.end ()) {
339 if (j == _cinemas.end()) {
343 j->second->remove_screen (i.second);
344 _targets->DeleteItem (i.first);
347 Config::instance()->changed (Config::CINEMAS);
351 vector<shared_ptr<Screen>>
352 ScreensPanel::screens () const
354 vector<shared_ptr<Screen>> output;
356 for (auto item = _targets->GetFirstItem(); item.IsOk(); item = _targets->GetNextItem(item)) {
357 if (_targets->GetCheckedState(item) == wxCHK_CHECKED) {
358 auto screen_iter = _screens.find(item);
359 if (screen_iter != _screens.end()) {
360 output.push_back (screen_iter->second);
370 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
372 selection_changed ();
377 ScreensPanel::selection_changed ()
379 if (_ignore_selection_change) {
384 _targets->GetSelections (s);
386 _selected_cinemas.clear ();
387 _selected_screens.clear ();
389 for (size_t i = 0; i < s.size(); ++i) {
390 auto j = _cinemas.find (s[i]);
391 if (j != _cinemas.end ()) {
392 _selected_cinemas[j->first] = j->second;
394 auto k = _screens.find (s[i]);
395 if (k != _screens.end ()) {
396 _selected_screens[k->first] = k->second;
400 setup_sensitivity ();
405 ScreensPanel::add_cinemas ()
407 for (auto i: Config::instance()->cinemas()) {
414 ScreensPanel::search_changed ()
416 _targets->DeleteAllItems ();
422 _ignore_selection_change = true;
424 for (auto const& i: _selected_cinemas) {
425 /* The wxTreeListItems will now be different, so we must search by cinema */
426 auto j = _cinemas.begin ();
427 while (j != _cinemas.end() && j->second != i.second) {
431 if (j != _cinemas.end()) {
432 _targets->Select (j->first);
436 for (auto const& i: _selected_screens) {
437 auto j = _screens.begin ();
438 while (j != _screens.end() && j->second != i.second) {
442 if (j != _screens.end()) {
443 _targets->Select (j->first);
447 _ignore_selection_change = false;
452 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
454 if (_cinemas.find(ev.GetItem()) != _cinemas.end()) {
455 /* Cinema: check/uncheck all children */
456 auto const checked = _targets->GetCheckedState(ev.GetItem());
457 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
458 _targets->CheckItem(child, checked);
461 /* Screen: set cinema to checked/unchecked/3state */
462 auto parent = _targets->GetItemParent(ev.GetItem());
463 DCPOMATIC_ASSERT (parent.IsOk());
466 for (auto child = _targets->GetFirstChild(parent); child.IsOk(); child = _targets->GetNextSibling(child)) {
467 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
474 _targets->CheckItem(parent, wxCHK_UNCHECKED);
475 } else if (unchecked == 0) {
476 _targets->CheckItem(parent, wxCHK_CHECKED);
478 _targets->CheckItem(parent, wxCHK_UNDETERMINED);
487 wxIMPLEMENT_DYNAMIC_CLASS (TreeListCtrl, wxTreeListCtrl);
491 TreeListCtrl::OnCompareItems (wxTreeListItem const& a, wxTreeListItem const& b)
493 return strcoll (wx_to_std(GetItemText(a)).c_str(), wx_to_std(GetItemText(b)).c_str());