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"
28 #include "lib/cinema.h"
29 #include "lib/config.h"
30 #include "lib/scope_guard.h"
31 #include "lib/screen.h"
32 #include "lib/timer.h"
38 using std::make_shared;
41 using std::shared_ptr;
44 using boost::optional;
45 #if BOOST_VERSION >= 106100
46 using namespace boost::placeholders;
48 using namespace dcpomatic;
51 ScreensPanel::ScreensPanel (wxWindow* parent)
52 : wxPanel (parent, wxID_ANY)
54 auto sizer = new wxBoxSizer (wxVERTICAL);
56 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_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 wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
65 _targets->AppendColumn (wxT("foo"));
67 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
71 auto side_buttons = new wxBoxSizer (wxVERTICAL);
73 auto target_buttons = new wxBoxSizer (wxVERTICAL);
75 _add_cinema = new Button (this, _("Add Cinema..."));
76 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
77 _edit_cinema = new Button (this, _("Edit Cinema..."));
78 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
79 _remove_cinema = new Button (this, _("Remove Cinema"));
80 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
81 _add_screen = new Button (this, _("Add Screen..."));
82 target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
83 _edit_screen = new Button (this, _("Edit Screen..."));
84 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
85 _remove_screen = new Button (this, _("Remove Screen"));
86 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
88 side_buttons->Add (target_buttons, 0, 0);
90 auto check_buttons = new wxBoxSizer (wxVERTICAL);
92 _check_all = new Button (this, _("Check all"));
93 check_buttons->Add (_check_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
94 _uncheck_all = new Button (this, _("Uncheck all"));
95 check_buttons->Add (_uncheck_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
97 side_buttons->Add (check_buttons, 1, wxEXPAND | wxTOP, DCPOMATIC_BUTTON_STACK_GAP * 8);
99 targets->Add (side_buttons, 0, 0);
101 sizer->Add (targets, 1, wxEXPAND);
103 _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
104 _targets->Bind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
105 _targets->Bind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
107 _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
108 _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
109 _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
111 _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
112 _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
113 _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
115 _check_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
116 _uncheck_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
120 _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
124 ScreensPanel::~ScreensPanel ()
126 _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
127 _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
132 ScreensPanel::check_all ()
134 for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) {
135 _targets->CheckItem(cinema, wxCHK_CHECKED);
136 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
137 _targets->CheckItem(screen, wxCHK_CHECKED);
138 set_screen_checked(screen, true);
145 ScreensPanel::uncheck_all ()
147 for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) {
148 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
149 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
150 _targets->CheckItem(screen, wxCHK_UNCHECKED);
151 set_screen_checked(screen, false);
158 ScreensPanel::setup_sensitivity ()
160 bool const sc = _selected_cinemas.size() == 1;
161 bool const ss = _selected_screens.size() == 1;
163 _edit_cinema->Enable (sc || ss);
164 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
166 _add_screen->Enable (sc || ss);
167 _edit_screen->Enable (ss);
168 _remove_screen->Enable (_selected_screens.size() >= 1);
173 ScreensPanel::convert_to_lower(string& s)
175 transform(s.begin(), s.end(), s.begin(), ::tolower);
180 ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string lower_case_search)
182 if (lower_case_search.empty()) {
186 auto name = cinema->name;
187 convert_to_lower(name);
188 return name.find(lower_case_search) != string::npos;
192 optional<wxTreeListItem>
193 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
195 auto search = wx_to_std (_search->GetValue ());
196 convert_to_lower(search);
197 if (!matches_search(cinema, search)) {
201 auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
203 _item_to_cinema[id] = cinema;
204 _cinema_to_item[cinema] = id;
206 for (auto screen: cinema->screens()) {
207 add_screen (cinema, screen);
214 optional<wxTreeListItem>
215 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
217 auto item = cinema_to_item(cinema);
222 auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
224 _item_to_screen[id] = screen;
225 _screen_to_item[screen] = id;
232 ScreensPanel::add_cinema_clicked ()
234 auto dialog = make_wx<CinemaDialog>(GetParent(), _("Add Cinema"));
236 if (dialog->ShowModal() == wxID_OK) {
237 auto cinema = make_shared<Cinema>(dialog->name(), dialog->emails(), dialog->notes(), dialog->utc_offset_hour(), dialog->utc_offset_minute());
239 auto cinemas = sorted_cinemas();
242 _ignore_cinemas_changed = true;
243 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
244 Config::instance()->add_cinema(cinema);
245 } catch (FileError& e) {
246 error_dialog(GetParent(), _("Could not write cinema details to the cinemas.xml file. Check that the location of cinemas.xml is valid in DCP-o-matic's preferences."), std_to_wx(e.what()));
250 wxTreeListItem previous = wxTLI_FIRST;
252 auto search = wx_to_std(_search->GetValue());
253 convert_to_lower(search);
254 for (auto existing_cinema: cinemas) {
255 if (!matches_search(existing_cinema, search)) {
258 if (_collator.compare(dialog->name(), existing_cinema->name) < 0) {
259 /* existing_cinema should be after the one we're inserting */
263 auto item = cinema_to_item(existing_cinema);
264 DCPOMATIC_ASSERT(item);
268 auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
271 _targets->UnselectAll ();
272 _targets->Select (*item);
276 selection_changed ();
281 ScreensPanel::cinema_for_operation () const
283 if (_selected_cinemas.size() == 1) {
284 return _selected_cinemas[0];
285 } else if (_selected_screens.size() == 1) {
286 return _selected_screens[0]->cinema;
294 ScreensPanel::edit_cinema_clicked ()
296 auto cinema = cinema_for_operation ();
301 auto dialog = make_wx<CinemaDialog>(
302 GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
305 if (dialog->ShowModal() == wxID_OK) {
306 cinema->name = dialog->name();
307 cinema->emails = dialog->emails();
308 cinema->notes = dialog->notes();
309 cinema->set_utc_offset_hour(dialog->utc_offset_hour());
310 cinema->set_utc_offset_minute(dialog->utc_offset_minute());
311 notify_cinemas_changed();
312 auto item = cinema_to_item(cinema);
313 DCPOMATIC_ASSERT(item);
314 _targets->SetItemText (*item, std_to_wx(dialog->name()));
320 ScreensPanel::remove_cinema_clicked ()
322 if (_selected_cinemas.size() == 1) {
323 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
327 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
332 for (auto const& cinema: _selected_cinemas) {
333 _ignore_cinemas_changed = true;
334 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
335 Config::instance()->remove_cinema(cinema);
336 auto item = cinema_to_item(cinema);
337 DCPOMATIC_ASSERT(item);
338 _targets->DeleteItem(*item);
341 selection_changed ();
346 ScreensPanel::add_screen_clicked ()
348 auto cinema = cinema_for_operation ();
353 auto dialog = make_wx<ScreenDialog>(GetParent(), _("Add Screen"));
355 if (dialog->ShowModal () != wxID_OK) {
359 for (auto screen: cinema->screens()) {
360 if (screen->name == dialog->name()) {
364 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
365 std_to_wx(dialog->name()).data()
372 auto screen = std::make_shared<Screen>(dialog->name(), dialog->notes(), dialog->recipient(), dialog->recipient_file(), dialog->trusted_devices());
373 cinema->add_screen (screen);
374 notify_cinemas_changed();
376 auto id = add_screen (cinema, screen);
378 _targets->Expand (id.get ());
384 ScreensPanel::edit_screen_clicked ()
386 if (_selected_screens.size() != 1) {
390 auto edit_screen = _selected_screens[0];
392 auto dialog = make_wx<ScreenDialog>(
393 GetParent(), _("Edit screen"),
396 edit_screen->recipient,
397 edit_screen->recipient_file,
398 edit_screen->trusted_devices
401 if (dialog->ShowModal() != wxID_OK) {
405 auto cinema = edit_screen->cinema;
406 for (auto screen: cinema->screens()) {
407 if (screen != edit_screen && screen->name == dialog->name()) {
411 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
412 std_to_wx(dialog->name()).data()
419 edit_screen->name = dialog->name();
420 edit_screen->notes = dialog->notes();
421 edit_screen->recipient = dialog->recipient();
422 edit_screen->recipient_file = dialog->recipient_file();
423 edit_screen->trusted_devices = dialog->trusted_devices();
424 notify_cinemas_changed();
426 auto item = screen_to_item(edit_screen);
427 DCPOMATIC_ASSERT (item);
428 _targets->SetItemText (*item, std_to_wx(dialog->name()));
433 ScreensPanel::remove_screen_clicked ()
435 if (_selected_screens.size() == 1) {
436 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
440 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
445 for (auto const& screen: _selected_screens) {
446 screen->cinema->remove_screen(screen);
447 auto item = screen_to_item(screen);
448 DCPOMATIC_ASSERT(item);
449 _targets->DeleteItem(*item);
452 notify_cinemas_changed();
456 vector<shared_ptr<Screen>>
457 ScreensPanel::screens () const
459 vector<shared_ptr<Screen>> output;
460 std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
466 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
468 selection_changed ();
473 ScreensPanel::selection_changed ()
475 if (_ignore_selection_change) {
479 wxTreeListItems selection;
480 _targets->GetSelections (selection);
482 _selected_cinemas.clear ();
483 _selected_screens.clear ();
485 for (size_t i = 0; i < selection.size(); ++i) {
486 if (auto cinema = item_to_cinema(selection[i])) {
487 _selected_cinemas.push_back(cinema);
489 if (auto screen = item_to_screen(selection[i])) {
490 _selected_screens.push_back(screen);
494 setup_sensitivity ();
498 list<shared_ptr<Cinema>>
499 ScreensPanel::sorted_cinemas() const
501 auto cinemas = Config::instance()->cinemas();
504 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
512 ScreensPanel::add_cinemas ()
514 for (auto cinema: sorted_cinemas()) {
515 add_cinema (cinema, wxTLI_LAST);
521 ScreensPanel::clear_and_re_add()
523 _targets->DeleteAllItems ();
525 _item_to_cinema.clear ();
526 _cinema_to_item.clear ();
527 _item_to_screen.clear ();
528 _screen_to_item.clear ();
535 ScreensPanel::search_changed ()
539 _ignore_selection_change = true;
541 for (auto const& selection: _selected_cinemas) {
542 if (auto item = cinema_to_item(selection)) {
543 _targets->Select (*item);
547 for (auto const& selection: _selected_screens) {
548 if (auto item = screen_to_item(selection)) {
549 _targets->Select (*item);
553 _ignore_selection_change = false;
555 _ignore_check_change = true;
557 for (auto const& checked: _checked_screens) {
558 if (auto item = screen_to_item(checked)) {
559 _targets->CheckItem(*item, wxCHK_CHECKED);
560 setup_cinema_checked_state(*item);
564 _ignore_check_change = false;
569 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
571 auto screen = item_to_screen(item);
572 DCPOMATIC_ASSERT(screen);
574 _checked_screens.insert(screen);
576 _checked_screens.erase(screen);
582 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
584 auto cinema = _targets->GetItemParent(screen);
585 DCPOMATIC_ASSERT (cinema.IsOk());
588 for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
589 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
596 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
597 } else if (unchecked == 0) {
598 _targets->CheckItem(cinema, wxCHK_CHECKED);
600 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
606 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
608 if (_ignore_check_change) {
612 if (item_to_cinema(ev.GetItem())) {
613 /* Cinema: check/uncheck all children */
614 auto const checked = _targets->GetCheckedState(ev.GetItem());
615 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
616 _targets->CheckItem(child, checked);
617 set_screen_checked(child, checked);
620 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
621 setup_cinema_checked_state(ev.GetItem());
629 ScreensPanel::item_to_cinema (wxTreeListItem item) const
631 auto iter = _item_to_cinema.find (item);
632 if (iter == _item_to_cinema.end()) {
641 ScreensPanel::item_to_screen (wxTreeListItem item) const
643 auto iter = _item_to_screen.find (item);
644 if (iter == _item_to_screen.end()) {
652 optional<wxTreeListItem>
653 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
655 auto iter = _cinema_to_item.find (cinema);
656 if (iter == _cinema_to_item.end()) {
664 optional<wxTreeListItem>
665 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
667 auto iter = _screen_to_item.find (screen);
668 if (iter == _screen_to_item.end()) {
677 ScreensPanel::notify_cinemas_changed()
679 _ignore_cinemas_changed = true;
680 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
683 Config::instance()->changed(Config::CINEMAS);
684 } catch (FileError& e) {
685 error_dialog(GetParent(), _("Could not write cinema details to the cinemas.xml file. Check that the location of cinemas.xml is valid in DCP-o-matic's preferences."), std_to_wx(e.what()));
694 ScreensPanel::config_changed(Config::Property property)
696 if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {