From cfe738b2f780d8adfdff80cdb38618bcb78faf20 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Thu, 13 Sep 2012 18:24:57 +0200 Subject: [PATCH] add interface HasColumns and its appropriate implementations --- src/actions.cpp | 34 ++-------- src/helpers.h | 5 ++ src/interfaces.h | 9 +++ src/media_library.cpp | 84 +++++++++++++------------ src/media_library.h | 21 ++++--- src/playlist.h | 2 +- src/playlist_editor.cpp | 99 +++++++++++++++-------------- src/playlist_editor.h | 23 +++---- src/tag_editor.cpp | 134 +++++++++++++++++++--------------------- src/tag_editor.h | 61 +++++++++--------- 10 files changed, 230 insertions(+), 242 deletions(-) diff --git a/src/actions.cpp b/src/actions.cpp index f9b55a0a..17b8c2c8 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -668,46 +668,24 @@ void PressSpace::Run() bool PreviousColumn::canBeRun() const { - return (myScreen == myLibrary && myLibrary->isPrevColumnAvailable()) - || (myScreen == myPlaylistEditor && myPlaylistEditor->isPrevColumnAvailable()) -# ifdef HAVE_TAGLIB_H - || (myScreen == myTagEditor && myTagEditor->isPrevColumnAvailable()) -# endif // HAVE_TAGLIB_H - ; + auto hc = hasColumns(myScreen); + return hc && hc->previousColumnAvailable(); } void PreviousColumn::Run() { - if (myScreen == myLibrary) - myLibrary->PrevColumn(); - else if (myScreen == myPlaylistEditor) - myPlaylistEditor->PrevColumn(); -# ifdef HAVE_TAGLIB_H - else if (myScreen == myTagEditor) - myTagEditor->PrevColumn(); -# endif // HAVE_TAGLIB_H + hasColumns(myScreen)->previousColumn(); } bool NextColumn::canBeRun() const { - return (myScreen == myLibrary && myLibrary->isNextColumnAvailable()) - || (myScreen == myPlaylistEditor && myPlaylistEditor->isNextColumnAvailable()) -# ifdef HAVE_TAGLIB_H - || (myScreen == myTagEditor && myTagEditor->isNextColumnAvailable()) -# endif // HAVE_TAGLIB_H - ; + auto hc = hasColumns(myScreen); + return hc && hc->nextColumnAvailable(); } void NextColumn::Run() { - if (myScreen == myLibrary) - myLibrary->NextColumn(); - else if (myScreen == myPlaylistEditor) - myPlaylistEditor->NextColumn(); -# ifdef HAVE_TAGLIB_H - else if (myScreen == myTagEditor) - myTagEditor->NextColumn(); -# endif // HAVE_TAGLIB_H + hasColumns(myScreen)->nextColumn(); } bool MasterScreen::canBeRun() const diff --git a/src/helpers.h b/src/helpers.h index 3df02b5b..13a6a365 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -28,6 +28,11 @@ #include "status.h" #include "utility/wide_string.h" +inline HasColumns *hasColumns(BasicScreen *screen) +{ + return dynamic_cast(screen); +} + inline HasSongs *hasSongs(BasicScreen *screen) { return dynamic_cast(screen); diff --git a/src/interfaces.h b/src/interfaces.h index bcecae28..a8a989e6 100644 --- a/src/interfaces.h +++ b/src/interfaces.h @@ -50,4 +50,13 @@ struct HasSongs virtual MPD::SongList getSelectedSongs() = 0; }; +struct HasColumns +{ + virtual bool previousColumnAvailable() = 0; + virtual void previousColumn() = 0; + + virtual bool nextColumnAvailable() = 0; + virtual void nextColumn() = 0; +}; + #endif // _INTERFACES_H diff --git a/src/media_library.cpp b/src/media_library.cpp index e5924942..23095de1 100644 --- a/src/media_library.cpp +++ b/src/media_library.cpp @@ -414,8 +414,8 @@ void MediaLibrary::MouseButtonPressed(MEVENT me) bool result = true; if (w != Songs) { - if (isNextColumnAvailable()) - NextColumn(); + if (nextColumnAvailable()) + nextColumn(); else result = false; } @@ -425,8 +425,8 @@ void MediaLibrary::MouseButtonPressed(MEVENT me) bool result = true; if (w != Tags) { - if (isPrevColumnAvailable()) - PrevColumn(); + if (previousColumnAvailable()) + previousColumn(); else result = false; } @@ -683,15 +683,41 @@ MPD::SongList MediaLibrary::getSelectedSongs() /***********************************************************************/ -int MediaLibrary::Columns() +bool MediaLibrary::previousColumnAvailable() { - if (hasTwoColumns) - return 2; - else - return 3; + assert(!hasTwoColumns || w != Tags); + if (w == Songs) + { + if (!Albums->reallyEmpty() && (hasTwoColumns || !Tags->reallyEmpty())) + return true; + } + else if (w == Albums) + { + if (!hasTwoColumns && !Tags->reallyEmpty()) + return true; + } + return false; } -bool MediaLibrary::isNextColumnAvailable() +void MediaLibrary::previousColumn() +{ + if (w == Songs) + { + Songs->setHighlightColor(Config.main_highlight_color); + w->refresh(); + w = Albums; + Albums->setHighlightColor(Config.active_column_color); + } + else if (w == Albums && !hasTwoColumns) + { + Albums->setHighlightColor(Config.main_highlight_color); + w->refresh(); + w = Tags; + Tags->setHighlightColor(Config.active_column_color); + } +} + +bool MediaLibrary::nextColumnAvailable() { assert(!hasTwoColumns || w != Tags); if (w == Tags) @@ -707,7 +733,7 @@ bool MediaLibrary::isNextColumnAvailable() return false; } -void MediaLibrary::NextColumn() +void MediaLibrary::nextColumn() { if (w == Tags) { @@ -725,38 +751,14 @@ void MediaLibrary::NextColumn() } } -bool MediaLibrary::isPrevColumnAvailable() -{ - assert(!hasTwoColumns || w != Tags); - if (w == Songs) - { - if (!Albums->reallyEmpty() && (hasTwoColumns || !Tags->reallyEmpty())) - return true; - } - else if (w == Albums) - { - if (!hasTwoColumns && !Tags->reallyEmpty()) - return true; - } - return false; -} +/***********************************************************************/ -void MediaLibrary::PrevColumn() +int MediaLibrary::Columns() { - if (w == Songs) - { - Songs->setHighlightColor(Config.main_highlight_color); - w->refresh(); - w = Albums; - Albums->setHighlightColor(Config.active_column_color); - } - else if (w == Albums && !hasTwoColumns) - { - Albums->setHighlightColor(Config.main_highlight_color); - w->refresh(); - w = Tags; - Tags->setHighlightColor(Config.active_column_color); - } + if (hasTwoColumns) + return 2; + else + return 3; } std::shared_ptr MediaLibrary::songsProxyList() diff --git a/src/media_library.h b/src/media_library.h index e4f2720c..b15ca5c1 100644 --- a/src/media_library.h +++ b/src/media_library.h @@ -24,7 +24,7 @@ #include "interfaces.h" #include "screen.h" -class MediaLibrary : public Screen, public Filterable, public HasSongs, public Searchable +class MediaLibrary : public Screen, public Filterable, public HasColumns, public HasSongs, public Searchable { public: virtual void SwitchTo() OVERRIDE; @@ -42,33 +42,34 @@ class MediaLibrary : public Screen, public Filterable, public HasSon virtual bool isTabbable() OVERRIDE { return true; } virtual bool isMergable() OVERRIDE { return true; } - /// Filterable implementation + // Filterable implementation virtual bool allowsFiltering() OVERRIDE; virtual std::string currentFilter() OVERRIDE; virtual void applyFilter(const std::string &filter) OVERRIDE; - /// Searchable implementation + // Searchable implementation virtual bool allowsSearching() OVERRIDE; virtual bool search(const std::string &constraint) OVERRIDE; virtual void nextFound(bool wrap) OVERRIDE; virtual void prevFound(bool wrap) OVERRIDE; - /// HasSongs implementation + // HasSongs implementation virtual std::shared_ptr getProxySongList() OVERRIDE; virtual bool allowsSelection() OVERRIDE; virtual void reverseSelection() OVERRIDE; virtual MPD::SongList getSelectedSongs() OVERRIDE; + // HasColumns implementation + virtual bool previousColumnAvailable() OVERRIDE; + virtual void previousColumn() OVERRIDE; + + virtual bool nextColumnAvailable() OVERRIDE; + virtual void nextColumn() OVERRIDE; + // private members int Columns(); - bool isNextColumnAvailable(); - void NextColumn(); - bool isPrevColumnAvailable(); - void PrevColumn(); - void LocateSong(const MPD::Song &); - std::shared_ptr songsProxyList(); struct SearchConstraints diff --git a/src/playlist.h b/src/playlist.h index a63fc2dd..922c8fb0 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -93,7 +93,7 @@ class Playlist : public Screen, public Filterable, public HasSongs, void registerHash(size_t hash); void unregisterHash(size_t hash); - NC::Menu< MPD::Song > *Items; + NC::Menu *Items; static bool ReloadTotalLength; static bool ReloadRemaining; diff --git a/src/playlist_editor.cpp b/src/playlist_editor.cpp index effc3601..82ee63b7 100644 --- a/src/playlist_editor.cpp +++ b/src/playlist_editor.cpp @@ -219,52 +219,6 @@ bool PlaylistEditor::isContentFiltered() return false; } -bool PlaylistEditor::isNextColumnAvailable() -{ - if (w == Playlists) - { - if (!Content->reallyEmpty()) - return true; - } - return false; -} - -bool PlaylistEditor::NextColumn() -{ - if (w == Playlists) - { - Playlists->setHighlightColor(Config.main_highlight_color); - w->refresh(); - w = Content; - Content->setHighlightColor(Config.active_column_color); - return true; - } - return false; -} - -bool PlaylistEditor::isPrevColumnAvailable() -{ - if (w == Content) - { - if (!Playlists->reallyEmpty()) - return true; - } - return false; -} - -bool PlaylistEditor::PrevColumn() -{ - if (w == Content) - { - Content->setHighlightColor(Config.main_highlight_color); - w->refresh(); - w = Playlists; - Playlists->setHighlightColor(Config.active_column_color); - return true; - } - return false; -} - std::shared_ptr PlaylistEditor::contentProxyList() { return mkProxySongList(*Content, [](NC::Menu::Item &item) { @@ -328,8 +282,8 @@ void PlaylistEditor::MouseButtonPressed(MEVENT me) { if (w != Playlists) { - if (isPrevColumnAvailable()) - PrevColumn(); + if (previousColumnAvailable()) + previousColumn(); else return; } @@ -352,8 +306,8 @@ void PlaylistEditor::MouseButtonPressed(MEVENT me) { if (w != Content) { - if (isNextColumnAvailable()) - NextColumn(); + if (nextColumnAvailable()) + nextColumn(); else return; } @@ -508,6 +462,51 @@ MPD::SongList PlaylistEditor::getSelectedSongs() /***********************************************************************/ +bool PlaylistEditor::previousColumnAvailable() +{ + if (w == Content) + { + if (!Playlists->reallyEmpty()) + return true; + } + return false; +} + +void PlaylistEditor::previousColumn() +{ + if (w == Content) + { + Content->setHighlightColor(Config.main_highlight_color); + w->refresh(); + w = Playlists; + Playlists->setHighlightColor(Config.active_column_color); + } +} + +bool PlaylistEditor::nextColumnAvailable() +{ + if (w == Playlists) + { + if (!Content->reallyEmpty()) + return true; + } + return false; +} + +void PlaylistEditor::nextColumn() +{ + if (w == Playlists) + { + Playlists->setHighlightColor(Config.main_highlight_color); + w->refresh(); + w = Content; + Content->setHighlightColor(Config.active_column_color); + } +} + +/***********************************************************************/ + + void PlaylistEditor::Locate(const std::string &name) { if (!isInitialized) diff --git a/src/playlist_editor.h b/src/playlist_editor.h index 70b15760..0214d729 100644 --- a/src/playlist_editor.h +++ b/src/playlist_editor.h @@ -24,7 +24,7 @@ #include "interfaces.h" #include "screen.h" -class PlaylistEditor : public Screen, public Filterable, public HasSongs, public Searchable +class PlaylistEditor : public Screen, public Filterable, public HasColumns, public HasSongs, public Searchable { public: virtual void SwitchTo() OVERRIDE; @@ -42,36 +42,37 @@ class PlaylistEditor : public Screen, public Filterable, public HasS virtual bool isTabbable() OVERRIDE { return true; } virtual bool isMergable() OVERRIDE { return true; } - /// Filterable implementation + // Filterable implementation virtual bool allowsFiltering() OVERRIDE; virtual std::string currentFilter() OVERRIDE; virtual void applyFilter(const std::string &filter) OVERRIDE; - /// Searchable implementation + // Searchable implementation virtual bool allowsSearching() OVERRIDE; virtual bool search(const std::string &constraint) OVERRIDE; virtual void nextFound(bool wrap) OVERRIDE; virtual void prevFound(bool wrap) OVERRIDE; - /// HasSongs implementation + // HasSongs implementation virtual std::shared_ptr getProxySongList() OVERRIDE; virtual bool allowsSelection() OVERRIDE; virtual void reverseSelection() OVERRIDE; virtual MPD::SongList getSelectedSongs() OVERRIDE; - // private members - virtual void Locate(const std::string &); + // HasColumns implementation + virtual bool previousColumnAvailable() OVERRIDE; + virtual void previousColumn() OVERRIDE; + virtual bool nextColumnAvailable() OVERRIDE; + virtual void nextColumn() OVERRIDE; + + // private members void requestPlaylistsUpdate() { playlistsUpdateRequested = true; } void requestContentsUpdate() { contentUpdateRequested = true; } + virtual void Locate(const std::string &); bool isContentFiltered(); - bool isNextColumnAvailable(); - bool NextColumn(); - bool isPrevColumnAvailable(); - bool PrevColumn(); - std::shared_ptr contentProxyList(); NC::Menu *Playlists; diff --git a/src/tag_editor.cpp b/src/tag_editor.cpp index c230f910..3d775dd5 100644 --- a/src/tag_editor.cpp +++ b/src/tag_editor.cpp @@ -646,8 +646,8 @@ void TagEditor::MouseButtonPressed(MEVENT me) bool result = true; if (w != Dirs) { - if (isPrevColumnAvailable()) - PrevColumn(); + if (previousColumnAvailable()) + previousColumn(); else result = false; } @@ -657,8 +657,8 @@ void TagEditor::MouseButtonPressed(MEVENT me) bool result = true; if (w != Tags) { - if (isNextColumnAvailable()) - NextColumn(); + if (nextColumnAvailable()) + nextColumn(); else result = false; } @@ -684,8 +684,8 @@ void TagEditor::MouseButtonPressed(MEVENT me) { if (w != FParser) { - if (isPrevColumnAvailable()) - PrevColumn(); + if (previousColumnAvailable()) + previousColumn(); else return; } @@ -702,8 +702,8 @@ void TagEditor::MouseButtonPressed(MEVENT me) { if (w != FParserHelper) { - if (isNextColumnAvailable()) - NextColumn(); + if (nextColumnAvailable()) + nextColumn(); else return; } @@ -880,63 +880,7 @@ MPD::SongList TagEditor::getSelectedSongs() /***********************************************************************/ -bool TagEditor::ifAnyModifiedAskForDiscarding() -{ - bool result = true; - if (isAnyModified(*Tags)) - result = Action::AskYesNoQuestion("There are pending changes, are you sure?", Status::trace); - return result; -} - -bool TagEditor::isNextColumnAvailable() -{ - bool result = false; - if (w == Dirs) - { - if (!TagTypes->reallyEmpty() && !Tags->reallyEmpty()) - result = true; - } - else if (w == TagTypes) - { - if (!Tags->reallyEmpty()) - result = true; - } - else if (w == FParser) - result = true; - return result; -} - -bool TagEditor::NextColumn() -{ - if (w == Dirs) - { - Dirs->setHighlightColor(Config.main_highlight_color); - w->refresh(); - w = TagTypes; - TagTypes->setHighlightColor(Config.active_column_color); - return true; - } - else if (w == TagTypes && TagTypes->choice() < 13 && !Tags->reallyEmpty()) - { - TagTypes->setHighlightColor(Config.main_highlight_color); - w->refresh(); - w = Tags; - Tags->setHighlightColor(Config.active_column_color); - return true; - } - else if (w == FParser) - { - FParser->setBorder(Config.window_border); - FParser->display(); - w = FParserHelper; - FParserHelper->setBorder(Config.active_window_border); - FParserHelper->display(); - return true; - } - return false; -} - -bool TagEditor::isPrevColumnAvailable() +bool TagEditor::previousColumnAvailable() { bool result = false; if (w == Tags) @@ -954,7 +898,7 @@ bool TagEditor::isPrevColumnAvailable() return result; } -bool TagEditor::PrevColumn() +void TagEditor::previousColumn() { if (w == Tags) { @@ -962,7 +906,6 @@ bool TagEditor::PrevColumn() w->refresh(); w = TagTypes; TagTypes->setHighlightColor(Config.active_column_color); - return true; } else if (w == TagTypes) { @@ -970,7 +913,6 @@ bool TagEditor::PrevColumn() w->refresh(); w = Dirs; Dirs->setHighlightColor(Config.active_column_color); - return true; } else if (w == FParserHelper) { @@ -979,9 +921,61 @@ bool TagEditor::PrevColumn() w = FParser; FParser->setBorder(Config.active_window_border); FParser->display(); - return true; } - return false; +} + +bool TagEditor::nextColumnAvailable() +{ + bool result = false; + if (w == Dirs) + { + if (!TagTypes->reallyEmpty() && !Tags->reallyEmpty()) + result = true; + } + else if (w == TagTypes) + { + if (!Tags->reallyEmpty()) + result = true; + } + else if (w == FParser) + result = true; + return result; +} + +void TagEditor::nextColumn() +{ + if (w == Dirs) + { + Dirs->setHighlightColor(Config.main_highlight_color); + w->refresh(); + w = TagTypes; + TagTypes->setHighlightColor(Config.active_column_color); + } + else if (w == TagTypes && TagTypes->choice() < 13 && !Tags->reallyEmpty()) + { + TagTypes->setHighlightColor(Config.main_highlight_color); + w->refresh(); + w = Tags; + Tags->setHighlightColor(Config.active_column_color); + } + else if (w == FParser) + { + FParser->setBorder(Config.window_border); + FParser->display(); + w = FParserHelper; + FParserHelper->setBorder(Config.active_window_border); + FParserHelper->display(); + } +} + +/***********************************************************************/ + +bool TagEditor::ifAnyModifiedAskForDiscarding() +{ + bool result = true; + if (isAnyModified(*Tags)) + result = Action::AskYesNoQuestion("There are pending changes, are you sure?", Status::trace); + return result; } void TagEditor::LocateSong(const MPD::Song &s) diff --git a/src/tag_editor.h b/src/tag_editor.h index b6e7b38b..8b0a3f15 100644 --- a/src/tag_editor.h +++ b/src/tag_editor.h @@ -33,61 +33,60 @@ #include "regex_filter.h" #include "screen.h" -class TagEditor : public Screen, public Filterable, public HasSongs, public Searchable +class TagEditor : public Screen, public Filterable, public HasColumns, public HasSongs, public Searchable { public: TagEditor() : FParser(0), FParserHelper(0), FParserLegend(0), FParserPreview(0), itsBrowsedDir("/") { } - virtual void Resize(); - virtual void SwitchTo(); + virtual void Resize() OVERRIDE; + virtual void SwitchTo() OVERRIDE; - virtual std::wstring Title(); + virtual std::wstring Title() OVERRIDE; - virtual void Refresh(); - virtual void Update(); + virtual void Refresh() OVERRIDE; + virtual void Update() OVERRIDE; - virtual void EnterPressed(); - virtual void SpacePressed(); - virtual void MouseButtonPressed(MEVENT); - virtual bool isTabbable() { return true; } + virtual void EnterPressed() OVERRIDE; + virtual void SpacePressed() OVERRIDE; + virtual void MouseButtonPressed(MEVENT) OVERRIDE; + + virtual bool isTabbable() OVERRIDE { return true; } + virtual bool isMergable() OVERRIDE { return true; } // Filterable implementation - virtual bool allowsFiltering(); - virtual std::string currentFilter(); - virtual void applyFilter(const std::string &filter); + virtual bool allowsFiltering() OVERRIDE; + virtual std::string currentFilter() OVERRIDE; + virtual void applyFilter(const std::string &filter) OVERRIDE; // Searchable implementation - virtual bool allowsSearching(); - virtual bool search(const std::string &constraint); - virtual void nextFound(bool wrap); - virtual void prevFound(bool wrap); + virtual bool allowsSearching() OVERRIDE; + virtual bool search(const std::string &constraint) OVERRIDE; + virtual void nextFound(bool wrap) OVERRIDE; + virtual void prevFound(bool wrap) OVERRIDE; // HasSongs implementation - virtual std::shared_ptr getProxySongList(); + virtual std::shared_ptr getProxySongList() OVERRIDE; - virtual bool allowsSelection(); - virtual void reverseSelection(); - virtual MPD::SongList getSelectedSongs(); + virtual bool allowsSelection() OVERRIDE; + virtual void reverseSelection() OVERRIDE; + virtual MPD::SongList getSelectedSongs() OVERRIDE; - virtual bool isMergable() { return true; } + // HasColumns implementation + virtual bool previousColumnAvailable() OVERRIDE; + virtual void previousColumn() OVERRIDE; + + virtual bool nextColumnAvailable() OVERRIDE; + virtual void nextColumn() OVERRIDE; // private members bool ifAnyModifiedAskForDiscarding(); - - bool isNextColumnAvailable(); - bool NextColumn(); - bool isPrevColumnAvailable(); - bool PrevColumn(); - void LocateSong(const MPD::Song &s); + const std::string &CurrentDir() { return itsBrowsedDir; } NC::Menu< std::pair > *Dirs; NC::Menu *TagTypes; NC::Menu *Tags; - /// NOTICE: this string is always in utf8, no need to convert it - const std::string &CurrentDir() { return itsBrowsedDir; } - static void ReadTags(MPD::MutableSong &); static bool WriteTags(MPD::MutableSong &);