Use a vector<pair<...>> to store screens and cinemas instead of a map.
[dcpomatic.git] / src / wx / screens_panel.cc
index c5aea4223e5c62f4c381d16f1f7a1df28785c468..2075515f353fe46edae7eb3b02ec13ae0064508f 100644 (file)
@@ -55,8 +55,11 @@ ScreensPanel::ScreensPanel (wxWindow* parent)
        sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
 
        auto targets = new wxBoxSizer (wxHORIZONTAL);
-       _targets = new TreeListCtrl (this);
+       _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
        _targets->AppendColumn (wxT("foo"));
+       _targets->SetSortColumn (0);
+       _targets->SetItemComparator (&_comparator);
+
        targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
 
        add_cinemas ();
@@ -109,57 +112,55 @@ ScreensPanel::setup_sensitivity ()
        bool const sc = _selected_cinemas.size() == 1;
        bool const ss = _selected_screens.size() == 1;
 
-       _edit_cinema->Enable (sc);
+       _edit_cinema->Enable (sc || ss);
        _remove_cinema->Enable (_selected_cinemas.size() >= 1);
 
-       _add_screen->Enable (sc);
+       _add_screen->Enable (sc || ss);
        _edit_screen->Enable (ss);
        _remove_screen->Enable (_selected_screens.size() >= 1);
 }
 
 
 optional<wxTreeListItem>
-ScreensPanel::add_cinema (shared_ptr<Cinema> c)
+ScreensPanel::add_cinema (shared_ptr<Cinema> cinema)
 {
        auto search = wx_to_std (_search->GetValue ());
        transform (search.begin(), search.end(), search.begin(), ::tolower);
 
        if (!search.empty ()) {
-               auto name = c->name;
+               auto name = cinema->name;
                transform (name.begin(), name.end(), name.begin(), ::tolower);
                if (name.find (search) == string::npos) {
                        return {};
                }
        }
 
-       auto id = _targets->AppendItem(_targets->GetRootItem(), std_to_wx(c->name));
+       auto id = _targets->AppendItem(_targets->GetRootItem(), std_to_wx(cinema->name));
 
-       _cinemas[id] = c;
+       _cinemas.push_back(make_pair(id, cinema));
 
-       for (auto i: c->screens()) {
-               add_screen (c, i);
+       for (auto screen: cinema->screens()) {
+               add_screen (cinema, screen);
        }
 
-       _targets->SetSortColumn (0);
-
        return id;
 }
 
 
 optional<wxTreeListItem>
-ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
+ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
 {
-       auto i = _cinemas.begin();
-       while (i != _cinemas.end() && i->second != c) {
-               ++i;
+       auto cinema_iter = _cinemas.begin();
+       while (cinema_iter != _cinemas.end() && cinema_iter->second != cinema) {
+               ++cinema_iter;
        }
 
-       if (i == _cinemas.end()) {
+       if (cinema_iter == _cinemas.end()) {
                return {};
        }
 
-       _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
-       return i->first;
+       _screens.push_back(make_pair(_targets->AppendItem(cinema_iter->first, std_to_wx(screen->name)), screen));
+       return cinema_iter->first;
 }
 
 
@@ -168,9 +169,9 @@ ScreensPanel::add_cinema_clicked ()
 {
        auto d = new CinemaDialog (GetParent(), _("Add Cinema"));
        if (d->ShowModal () == wxID_OK) {
-               auto c = make_shared<Cinema>(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute());
-               Config::instance()->add_cinema (c);
-               auto id = add_cinema (c);
+               auto cinema = make_shared<Cinema>(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute());
+               Config::instance()->add_cinema (cinema);
+               auto id = add_cinema (cinema);
                if (id) {
                        _targets->UnselectAll ();
                        _targets->Select (*id);
@@ -181,26 +182,38 @@ ScreensPanel::add_cinema_clicked ()
 }
 
 
+optional<pair<wxTreeListItem, shared_ptr<Cinema>>>
+ScreensPanel::cinema_for_operation () const
+{
+       if (_selected_cinemas.size() == 1) {
+               return make_pair(_selected_cinemas.begin()->first, _selected_cinemas.begin()->second);
+       } else if (_selected_screens.size() == 1) {
+               return make_pair(_targets->GetItemParent(_selected_screens.begin()->first), _selected_screens.begin()->second->cinema);
+       }
+
+       return {};
+}
+
+
 void
 ScreensPanel::edit_cinema_clicked ()
 {
-       if (_selected_cinemas.size() != 1) {
+       auto cinema = cinema_for_operation ();
+       if (!cinema) {
                return;
        }
 
-       auto c = *_selected_cinemas.begin();
-
        auto d = new CinemaDialog (
-               GetParent(), _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
+               GetParent(), _("Edit cinema"), cinema->second->name, cinema->second->emails, cinema->second->notes, cinema->second->utc_offset_hour(), cinema->second->utc_offset_minute()
                );
 
        if (d->ShowModal() == wxID_OK) {
-               c.second->name = d->name ();
-               c.second->emails = d->emails ();
-               c.second->notes = d->notes ();
-               c.second->set_utc_offset_hour (d->utc_offset_hour ());
-               c.second->set_utc_offset_minute (d->utc_offset_minute ());
-               _targets->SetItemText (c.first, std_to_wx (d->name()));
+               cinema->second->name = d->name ();
+               cinema->second->emails = d->emails ();
+               cinema->second->notes = d->notes ();
+               cinema->second->set_utc_offset_hour (d->utc_offset_hour ());
+               cinema->second->set_utc_offset_minute (d->utc_offset_minute ());
+               _targets->SetItemText (cinema->first, std_to_wx(d->name()));
                Config::instance()->changed (Config::CINEMAS);
        }
 
@@ -233,20 +246,19 @@ ScreensPanel::remove_cinema_clicked ()
 void
 ScreensPanel::add_screen_clicked ()
 {
-       if (_selected_cinemas.size() != 1) {
+       auto cinema = cinema_for_operation ();
+       if (!cinema) {
                return;
        }
 
-       auto c = _selected_cinemas.begin()->second;
-
        auto d = new ScreenDialog (GetParent(), _("Add Screen"));
        if (d->ShowModal () != wxID_OK) {
                d->Destroy ();
                return;
        }
 
-       for (auto i: c->screens ()) {
-               if (i->name == d->name()) {
+       for (auto screen: cinema->second->screens()) {
+               if (screen->name == d->name()) {
                        error_dialog (
                                GetParent(),
                                wxString::Format (
@@ -258,9 +270,9 @@ ScreensPanel::add_screen_clicked ()
                }
        }
 
-       auto s = std::make_shared<Screen>(d->name(), d->notes(), d->recipient(), d->recipient_file(), d->trusted_devices());
-       c->add_screen (s);
-       auto id = add_screen (c, s);
+       auto screen = std::make_shared<Screen>(d->name(), d->notes(), d->recipient(), d->recipient_file(), d->trusted_devices());
+       cinema->second->add_screen (screen);
+       auto id = add_screen (cinema->second, screen);
        if (id) {
                _targets->Expand (id.get ());
        }
@@ -278,17 +290,25 @@ ScreensPanel::edit_screen_clicked ()
                return;
        }
 
-       auto s = *_selected_screens.begin();
+       auto edit_screen = *_selected_screens.begin();
+
+       auto d = new ScreenDialog (
+               GetParent(), _("Edit screen"),
+               edit_screen.second->name,
+               edit_screen.second->notes,
+               edit_screen.second->recipient,
+               edit_screen.second->recipient_file,
+               edit_screen.second->trusted_devices
+               );
 
-       auto d = new ScreenDialog (GetParent(), _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->recipient_file, s.second->trusted_devices);
        if (d->ShowModal() != wxID_OK) {
                d->Destroy ();
                return;
        }
 
-       auto c = s.second->cinema;
-       for (auto i: c->screens ()) {
-               if (i != s.second && i->name == d->name()) {
+       auto cinema = edit_screen.second->cinema;
+       for (auto screen: cinema->screens()) {
+               if (screen != edit_screen.second && screen->name == d->name()) {
                        error_dialog (
                                GetParent(),
                                wxString::Format (
@@ -300,12 +320,12 @@ ScreensPanel::edit_screen_clicked ()
                }
        }
 
-       s.second->name = d->name ();
-       s.second->notes = d->notes ();
-       s.second->recipient = d->recipient ();
-       s.second->recipient_file = d->recipient_file ();
-       s.second->trusted_devices = d->trusted_devices ();
-       _targets->SetItemText (s.first, std_to_wx (d->name()));
+       edit_screen.second->name = d->name ();
+       edit_screen.second->notes = d->notes ();
+       edit_screen.second->recipient = d->recipient ();
+       edit_screen.second->recipient_file = d->recipient_file ();
+       edit_screen.second->trusted_devices = d->trusted_devices ();
+       _targets->SetItemText (edit_screen.first, std_to_wx(d->name()));
        Config::instance()->changed (Config::CINEMAS);
 
        d->Destroy ();
@@ -355,7 +375,7 @@ ScreensPanel::screens () const
 
        for (auto item = _targets->GetFirstItem(); item.IsOk(); item = _targets->GetNextItem(item)) {
                if (_targets->GetCheckedState(item) == wxCHK_CHECKED) {
-                       auto screen_iter = _screens.find(item);
+                       auto screen_iter = screen_by_tree_list_item(item);
                        if (screen_iter != _screens.end()) {
                                output.push_back (screen_iter->second);
                        }
@@ -380,20 +400,20 @@ ScreensPanel::selection_changed ()
                return;
        }
 
-       wxTreeListItems s;
-       _targets->GetSelections (s);
+       wxTreeListItems selection;
+       _targets->GetSelections (selection);
 
        _selected_cinemas.clear ();
        _selected_screens.clear ();
 
-       for (size_t i = 0; i < s.size(); ++i) {
-               auto j = _cinemas.find (s[i]);
-               if (j != _cinemas.end ()) {
-                       _selected_cinemas[j->first] = j->second;
+       for (size_t i = 0; i < selection.size(); ++i) {
+               auto cinema = cinema_by_tree_list_item(selection[i]);
+               if (cinema != _cinemas.end ()) {
+                       _selected_cinemas.push_back(*cinema);
                }
-               auto k = _screens.find (s[i]);
-               if (k != _screens.end ()) {
-                       _selected_screens[k->first] = k->second;
+               auto screen = screen_by_tree_list_item(selection[i]);
+               if (screen != _screens.end ()) {
+                       _selected_screens.push_back(*screen);
                }
        }
 
@@ -404,8 +424,8 @@ ScreensPanel::selection_changed ()
 void
 ScreensPanel::add_cinemas ()
 {
-       for (auto i: Config::instance()->cinemas()) {
-               add_cinema (i);
+       for (auto cinema: Config::instance()->cinemas()) {
+               add_cinema (cinema);
        }
 }
 
@@ -421,26 +441,26 @@ ScreensPanel::search_changed ()
 
        _ignore_selection_change = true;
 
-       for (auto const& i: _selected_cinemas) {
+       for (auto const& selection: _selected_cinemas) {
                /* The wxTreeListItems will now be different, so we must search by cinema */
-               auto j = _cinemas.begin ();
-               while (j != _cinemas.end() && j->second != i.second) {
-                       ++j;
+               auto cinema = _cinemas.begin ();
+               while (cinema != _cinemas.end() && cinema->second != selection.second) {
+                       ++cinema;
                }
 
-               if (j != _cinemas.end()) {
-                       _targets->Select (j->first);
+               if (cinema != _cinemas.end()) {
+                       _targets->Select (cinema->first);
                }
        }
 
-       for (auto const& i: _selected_screens) {
-               auto j = _screens.begin ();
-               while (j != _screens.end() && j->second != i.second) {
-                       ++j;
+       for (auto const& selection: _selected_screens) {
+               auto screen = _screens.begin ();
+               while (screen != _screens.end() && screen->second != selection.second) {
+                       ++screen;
                }
 
-               if (j != _screens.end()) {
-                       _targets->Select (j->first);
+               if (screen != _screens.end()) {
+                       _targets->Select (screen->first);
                }
        }
 
@@ -451,7 +471,8 @@ ScreensPanel::search_changed ()
 void
 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
 {
-       if (_cinemas.find(ev.GetItem()) != _cinemas.end()) {
+       auto cinema_iter = cinema_by_tree_list_item(ev.GetItem());
+       if (cinema_iter != _cinemas.end()) {
                /* Cinema: check/uncheck all children */
                auto const checked = _targets->GetCheckedState(ev.GetItem());
                for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
@@ -483,14 +504,22 @@ ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
 }
 
 
-
-wxIMPLEMENT_DYNAMIC_CLASS (TreeListCtrl, wxTreeListCtrl);
+ScreensPanel::Cinemas::iterator
+ScreensPanel::cinema_by_tree_list_item (wxTreeListItem item)
+{
+       return std::find_if(
+               _cinemas.begin(), _cinemas.end(),
+               [item](pair<wxTreeListItem, shared_ptr<Cinema>> const& s) { return s.first == item; }
+               );
+}
 
 
-int
-TreeListCtrl::OnCompareItems (wxTreeListItem const& a, wxTreeListItem const& b)
+ScreensPanel::Screens::const_iterator
+ScreensPanel::screen_by_tree_list_item (wxTreeListItem item) const
 {
-       return strcoll (wx_to_std(GetItemText(a)).c_str(), wx_to_std(GetItemText(b)).c_str());
+       return std::find_if(
+               _screens.begin(), _screens.end(),
+               [item](pair<wxTreeListItem, shared_ptr<Screen>> const& s) { return s.first == item; }
+               );
 }
 
-