Set Delete button sensitivity correctly.
[dcpomatic.git] / src / tools / dcpomatic_playlist.cc
index bf958b9f79e3fb80aec5f5c9e459d9c652eae98c..3e3bd02669cc8e359c855126608b66a69e551d08 100644 (file)
@@ -136,6 +136,8 @@ public:
                _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistList::selection_changed, this));
                _new->Bind (wxEVT_BUTTON, bind(&PlaylistList::new_playlist, this));
                _delete->Bind (wxEVT_BUTTON, bind(&PlaylistList::delete_playlist, this));
+
+               setup_sensitivity();
        }
 
        wxSizer* sizer ()
@@ -155,6 +157,11 @@ public:
        boost::signals2::signal<void (shared_ptr<SignalSPL>)> Edit;
 
 private:
+       void setup_sensitivity()
+       {
+               _delete->Enable(static_cast<bool>(selected()));
+       }
+
        void add_playlist_to_view (shared_ptr<const SignalSPL> playlist)
        {
                wxListItem item;
@@ -166,22 +173,33 @@ private:
        void add_playlist_to_model (shared_ptr<SignalSPL> playlist)
        {
                _playlists.push_back (playlist);
-               playlist->NameChanged.connect (bind(&PlaylistList::name_changed, this, weak_ptr<SignalSPL>(playlist)));
+               playlist->Changed.connect(bind(&PlaylistList::changed, this, weak_ptr<SignalSPL>(playlist), _1));
        }
 
-       void name_changed (weak_ptr<SignalSPL> wp)
+       void changed(weak_ptr<SignalSPL> wp, SignalSPL::Change change)
        {
                auto playlist = wp.lock ();
                if (!playlist) {
                        return;
                }
 
-               int N = 0;
-               for (auto i: _playlists) {
-                       if (i == playlist) {
-                               _list->SetItem (N, 0, std_to_wx(i->name()));
+               switch (change) {
+               case SignalSPL::Change::NAME:
+               {
+                       int N = 0;
+                       for (auto i: _playlists) {
+                               if (i == playlist) {
+                                       _list->SetItem (N, 0, std_to_wx(i->name()));
+                               }
+                               ++N;
                        }
-                       ++N;
+                       break;
+               }
+               case SignalSPL::Change::CONTENT:
+                       if (auto dir = Config::instance()->player_playlist_directory()) {
+                               playlist->write(*dir / (playlist->id() + ".xml"));
+                       }
+                       break;
                }
        }
 
@@ -215,10 +233,20 @@ private:
                _list->SetItemState (_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
        }
 
-       void delete_playlist ()
+       boost::optional<int> selected() const
        {
-               long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+               long int selected = _list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
                if (selected < 0 || selected >= int(_playlists.size())) {
+                       return {};
+               }
+
+               return selected;
+       }
+
+       void delete_playlist ()
+       {
+               auto index = selected();
+               if (!index) {
                        return;
                }
 
@@ -227,11 +255,11 @@ private:
                        return;
                }
 
-               boost::filesystem::remove (*dir / (_playlists[selected]->id() + ".xml"));
-               _list->DeleteItem (selected);
-               _playlists.erase (_playlists.begin() + selected);
+               boost::filesystem::remove(*dir / (_playlists[*index]->id() + ".xml"));
+               _list->DeleteItem(*index);
+               _playlists.erase(_playlists.begin() + *index);
 
-               Edit (shared_ptr<SignalSPL>());
+               Edit(shared_ptr<SignalSPL>());
        }
 
        void selection_changed ()
@@ -242,6 +270,8 @@ private:
                } else {
                        Edit (_playlists[selected]);
                }
+
+               setup_sensitivity();
        }
 
        wxBoxSizer* _sizer;
@@ -282,8 +312,8 @@ public:
                auto images = new wxImageList (16, 16);
                wxIcon tick_icon;
                wxIcon no_tick_icon;
-               tick_icon.LoadFile (bitmap_path("tick"), wxBITMAP_TYPE_PNG);
-               no_tick_icon.LoadFile (bitmap_path("no_tick"), wxBITMAP_TYPE_PNG);
+               tick_icon.LoadFile (bitmap_path("tick.png"), wxBITMAP_TYPE_PNG);
+               no_tick_icon.LoadFile (bitmap_path("no_tick.png"), wxBITMAP_TYPE_PNG);
                images->Add (tick_icon);
                images->Add (no_tick_icon);
 
@@ -312,6 +342,8 @@ public:
                _down->Bind (wxEVT_BUTTON, bind(&PlaylistContent::down_clicked, this));
                _add->Bind (wxEVT_BUTTON, bind(&PlaylistContent::add_clicked, this));
                _remove->Bind (wxEVT_BUTTON, bind(&PlaylistContent::remove_clicked, this));
+
+               setup_sensitivity();
        }
 
        wxSizer* sizer ()
@@ -360,7 +392,7 @@ private:
        {
                _list->SetItem (N, 0, std_to_wx(e.name));
                _list->SetItem (N, 1, std_to_wx(e.id));
-               _list->SetItem (N, 2, std_to_wx(dcp::content_kind_to_string(e.kind)));
+               _list->SetItem (N, 2, std_to_wx(e.kind->name()));
                _list->SetItem (N, 3, e.encrypted ? S_("Question|Y") : S_("Question|N"));
        }
 
@@ -400,9 +432,7 @@ private:
 
                DCPOMATIC_ASSERT (_playlist);
 
-               auto tmp = (*_playlist)[s];
-               (*_playlist)[s] = (*_playlist)[s-1];
-               (*_playlist)[s-1] = tmp;
+               _playlist->swap(s, s - 1);
 
                set_item (s - 1, (*_playlist)[s-1]);
                set_item (s, (*_playlist)[s]);
@@ -417,9 +447,7 @@ private:
 
                DCPOMATIC_ASSERT (_playlist);
 
-               auto tmp = (*_playlist)[s];
-               (*_playlist)[s] = (*_playlist)[s+1];
-               (*_playlist)[s+1] = tmp;
+               _playlist->swap(s, s + 1);
 
                set_item (s + 1, (*_playlist)[s+1]);
                set_item (s, (*_playlist)[s]);
@@ -477,8 +505,6 @@ public:
 
                _playlist_list->Edit.connect (bind(&DOMFrame::change_playlist, this, _1));
 
-               _playlist_content->set (_playlist_list->first_playlist());
-
                Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
                Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
                Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);