From a26b1f62f969296e26adcfcf7a0be584f3cd2d39 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sat, 8 Sep 2012 00:28:02 +0200 Subject: [PATCH] actions: implement filtering playlist on priorities --- src/actions.cpp | 28 ++++++++++++++++++++++++++-- src/actions.h | 13 ++++++++++++- src/bindings.cpp | 1 - src/help.cpp | 1 + src/utility/string.cpp | 4 ++-- src/utility/string.h | 2 +- 6 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/actions.cpp b/src/actions.cpp index 1917567f..a7323e7f 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -2204,9 +2204,9 @@ void SetSelectedItemsPriority::Run() Statusbar() << "Set priority [0-255]: "; std::string strprio = wFooter->getString(); UnlockStatusbar(); - if (!isInteger(strprio.c_str())) + if (!isInteger(strprio.c_str(), true)) return; - int prio = atoi(strprio.c_str()); + int prio = stringToInt(strprio); if (prio < 0 || prio > 255) { ShowMessage("Number is out of range"); @@ -2215,6 +2215,29 @@ void SetSelectedItemsPriority::Run() myPlaylist->SetSelectedItemsPriority(prio); } +bool FilterPlaylistOnPriorities::canBeRun() const +{ + return myScreen->ActiveWindow() == myPlaylist->Items; +} + +void FilterPlaylistOnPriorities::Run() +{ + using Global::wFooter; + + LockStatusbar(); + Statusbar() << "Show songs with priority higher than: "; + std::string strprio = wFooter->getString(); + UnlockStatusbar(); + if (!isInteger(strprio.c_str(), false)) + return; + unsigned prio = stringToInt(strprio); + myPlaylist->Items->filter(myPlaylist->Items->begin(), myPlaylist->Items->end(), + [prio](const NC::Menu::Item &s) { + return s.value().getPrio() > prio; + }); + ShowMessage("Playlist filtered (songs with priority higher than %u)", prio); +} + void ShowSongInfo::Run() { mySongInfo->SwitchTo(); @@ -2578,6 +2601,7 @@ void populateActions() insertAction(new RefetchLyrics()); insertAction(new RefetchArtistInfo()); insertAction(new SetSelectedItemsPriority()); + insertAction(new FilterPlaylistOnPriorities()); insertAction(new ShowSongInfo()); insertAction(new ShowArtistInfo()); insertAction(new ShowLyrics()); diff --git a/src/actions.h b/src/actions.h index adb98725..9ad2613b 100644 --- a/src/actions.h +++ b/src/actions.h @@ -44,7 +44,8 @@ enum ActionType aApplyFilter, aFind, aFindItemForward, aFindItemBackward, aNextFoundItem, aPreviousFoundItem, aToggleFindMode, aToggleReplayGainMode, aToggleSpaceMode, aToggleAddMode, aToggleMouse, aToggleBitrateVisibility, aAddRandomItems, aToggleBrowserSortMode, aToggleLibraryTagType, - aRefetchLyrics, aRefetchArtistInfo, aSetSelectedItemsPriority, aShowSongInfo, aShowArtistInfo, + aRefetchLyrics, aRefetchArtistInfo, aSetSelectedItemsPriority, aFilterPlaylistOnPriorities, + aShowSongInfo, aShowArtistInfo, aShowLyrics, aQuit, aNextScreen, aPreviousScreen, aShowHelp, aShowPlaylist, aShowBrowser, aShowSearchEngine, aShowMediaLibrary, aShowPlaylistEditor, aShowTagEditor, aShowOutputs, aShowVisualizer, aShowClock, aShowServerInfo @@ -915,6 +916,16 @@ protected: virtual void Run(); }; +struct FilterPlaylistOnPriorities : public Action +{ + FilterPlaylistOnPriorities() + : Action(aFilterPlaylistOnPriorities, "filter_playlist_on_priorities") { } + +protected: + virtual bool canBeRun() const; + virtual void Run(); +}; + struct ShowSongInfo : public Action { ShowSongInfo() : Action(aShowSongInfo, "show_song_info") { } diff --git a/src/bindings.cpp b/src/bindings.cpp index 7a3276fc..5de8c232 100644 --- a/src/bindings.cpp +++ b/src/bindings.cpp @@ -493,7 +493,6 @@ void BindingsConfiguration::generateDefaults() if (notBound(k = stringToKey("q"))) bind(k, aQuit); - if (notBound(k = stringToKey("-"))) bind(k, aVolumeDown); } diff --git a/src/help.cpp b/src/help.cpp index 65f9035f..b340891e 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -297,6 +297,7 @@ void Help::GetKeybindings() KeyDesc(aSavePlaylist, "Save playlist"); KeyDesc(aSortPlaylist, "Sort playlist"); KeyDesc(aReversePlaylist, "Reverse playlist"); + KeyDesc(aFilterPlaylistOnPriorities, "Filter playlist on priorities"); KeyDesc(aJumpToPlayingSong, "Jump to playing song"); KeyDesc(aTogglePlayingSongCentering, "Toggle playing song centering"); diff --git a/src/utility/string.cpp b/src/utility/string.cpp index 527a694d..d75806b5 100644 --- a/src/utility/string.cpp +++ b/src/utility/string.cpp @@ -33,13 +33,13 @@ long stringToLongInt(const std::string &s) return atol(s.c_str()); } -bool isInteger(const char *s) +bool isInteger(const char *s, bool accept_signed) { assert(s); if (*s == '\0') return false; for (const char *it = s; *it != '\0'; ++it) - if (!isdigit(*it) && (it != s || *it != '-')) + if (!(isdigit(*it) || (accept_signed && it == s && *it == '-'))) return false; return true; } diff --git a/src/utility/string.h b/src/utility/string.h index 94a7a783..902d120f 100644 --- a/src/utility/string.h +++ b/src/utility/string.h @@ -54,7 +54,7 @@ template struct print { int stringToInt(const std::string &s); long stringToLongInt(const std::string &s); -bool isInteger(const char *s); +bool isInteger(const char *s, bool accept_signed); std::string ToString(const std::wstring &ws); std::wstring ToWString(const std::string &s);