provide generic interface for dealing with selected items

This commit is contained in:
Andrzej Rybczak
2009-02-16 18:20:45 +01:00
parent 2ec7748acd
commit 43a8e2284e
21 changed files with 172 additions and 106 deletions

View File

@@ -1,7 +1,7 @@
bin_PROGRAMS = ncmpcpp bin_PROGRAMS = ncmpcpp
ncmpcpp_SOURCES = browser.cpp charset.cpp clock.cpp display.cpp help.cpp \ ncmpcpp_SOURCES = browser.cpp charset.cpp clock.cpp display.cpp help.cpp \
helpers.cpp info.cpp libmpdclient.c lyrics.cpp media_library.cpp misc.cpp mpdpp.cpp \ helpers.cpp info.cpp libmpdclient.c lyrics.cpp media_library.cpp menu.cpp misc.cpp \
ncmpcpp.cpp playlist.cpp playlist_editor.cpp screen.cpp scrollpad.cpp \ mpdpp.cpp ncmpcpp.cpp playlist.cpp playlist_editor.cpp scrollpad.cpp \
search_engine.cpp settings.cpp song.cpp status.cpp str_pool.c tag_editor.cpp window.cpp search_engine.cpp settings.cpp song.cpp status.cpp str_pool.c tag_editor.cpp window.cpp
# set the include path found by configure # set the include path found by configure

View File

@@ -146,7 +146,7 @@ void Browser::SpacePressed()
{ {
if (Config.space_selects && w->Choice() >= (itsBrowsedDir != "/" ? 1 : 0)) if (Config.space_selects && w->Choice() >= (itsBrowsedDir != "/" ? 1 : 0))
{ {
Select(w); w->SelectCurrent();
w->Scroll(wDown); w->Scroll(wDown);
return; return;
} }
@@ -238,6 +238,39 @@ MPD::Song *Browser::CurrentSong()
return !w->Empty() && w->Current().type == itSong ? w->Current().song : 0; return !w->Empty() && w->Current().type == itSong ? w->Current().song : 0;
} }
void Browser::ReverseSelection()
{
w->ReverseSelection(itsBrowsedDir == "/" ? 0 : 1);
}
void Browser::GetSelectedSongs(MPD::SongList &v)
{
std::vector<size_t> selected;
w->GetSelected(selected);
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); it++)
{
const Item &item = w->at(*it);
switch (item.type)
{
case itDirectory:
{
Mpd->GetDirectoryRecursive(locale_to_utf_cpy(item.name), v);
break;
}
case itSong:
{
v.push_back(new Song(*item.song));
break;
}
case itPlaylist:
{
Mpd->GetPlaylistContent(locale_to_utf_cpy(item.name), v);
break;
}
}
}
}
namespace namespace
{ {
const char *supported_extensions[] = { "wma", "asf", "rm", "mp1", "mp2", "mp3", "mp4", "m4a", "flac", "ogg", "wav", "au", "aiff", "aif", "ac3", "aac", "mpc", "it", "mod", "s3m", "xm", "wv", 0 }; const char *supported_extensions[] = { "wma", "asf", "rm", "mp1", "mp2", "mp3", "mp4", "m4a", "flac", "ogg", "wav", "au", "aiff", "aif", "ac3", "aac", "mpc", "it", "mod", "s3m", "xm", "wv", 0 };

View File

@@ -40,6 +40,11 @@ class Browser : public Screen< Menu<MPD::Item> >
virtual MPD::Song *CurrentSong(); virtual MPD::Song *CurrentSong();
virtual bool allowsSelection() { return true; }
virtual void ReverseSelection();
virtual bool Deselect() { return w->Deselect(); }
virtual void GetSelectedSongs(MPD::SongList &);
const std::string &CurrentDir() { return itsBrowsedDir; } const std::string &CurrentDir() { return itsBrowsedDir; }
void GetDirectory(std::string, std::string = "/"); void GetDirectory(std::string, std::string = "/");

View File

@@ -41,7 +41,9 @@ class Clock : public Screen<Window>
virtual void Update(); virtual void Update();
virtual void Scroll() { } virtual void Scroll() { }
virtual bool allowsSelection() { return false; }
protected: protected:
static void Prepare(); static void Prepare();
static void Set(int, int); static void Set(int, int);

View File

@@ -33,6 +33,8 @@ class Help : public Screen<Scrollpad>
virtual std::string Title(); virtual std::string Title();
virtual bool allowsSelection() { return false; }
protected: protected:
std::string DisplayKeys(int *, int = 2); std::string DisplayKeys(int *, int = 2);
void GetKeybindings(); void GetKeybindings();

View File

@@ -36,6 +36,8 @@ class Info : public Screen<Scrollpad>
virtual void Update(); virtual void Update();
virtual bool allowsSelection() { return false; }
void GetSong(); void GetSong();
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
void GetArtist(); void GetArtist();

View File

@@ -54,6 +54,8 @@ class Lyrics : public Screen<Scrollpad>
virtual void SpacePressed(); virtual void SpacePressed();
virtual bool allowsSelection() { return false; }
static bool Reload; static bool Reload;
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H

View File

@@ -246,7 +246,7 @@ void MediaLibrary::SpacePressed()
{ {
if (Config.space_selects && w == Songs) if (Config.space_selects && w == Songs)
{ {
Select(Songs); Songs->SelectCurrent();
w->Scroll(wDown); w->Scroll(wDown);
return; return;
} }
@@ -258,6 +258,16 @@ MPD::Song *MediaLibrary::CurrentSong()
return w == Songs && !Songs->Empty() ? &Songs->Current() : 0; return w == Songs && !Songs->Empty() ? &Songs->Current() : 0;
} }
void MediaLibrary::GetSelectedSongs(MPD::SongList &v)
{
std::vector<size_t> selected;
Songs->GetSelected(selected);
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); it++)
{
v.push_back(new MPD::Song(Songs->at(*it)));
}
}
void MediaLibrary::NextColumn() void MediaLibrary::NextColumn()
{ {
CLEAR_FIND_HISTORY; CLEAR_FIND_HISTORY;

View File

@@ -41,6 +41,11 @@ class MediaLibrary : public Screen<Window>
virtual MPD::Song *CurrentSong(); virtual MPD::Song *CurrentSong();
virtual bool allowsSelection() { return w == Songs; }
virtual void ReverseSelection() { Songs->ReverseSelection(); }
virtual bool Deselect() { return Songs->Deselect(); }
virtual void GetSelectedSongs(MPD::SongList &);
void NextColumn(); void NextColumn();
void PrevColumn(); void PrevColumn();

View File

@@ -18,13 +18,28 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#include "screen.h" #include "menu.h"
void BasicScreen::Select(List *l) void List::SelectCurrent()
{ {
if (l->Empty()) if (Empty())
return; return;
size_t i = l->Choice(); size_t i = Choice();
l->Select(i, !l->isSelected(i)); Select(i, !isSelected(i));
}
void List::ReverseSelection(size_t beginning)
{
for (size_t i = beginning; i < Size(); i++)
Select(i, !isSelected(i) && !isStatic(i));
}
bool List::Deselect()
{
if (!hasSelected())
return false;
for (size_t i = 0; i < Size(); i++)
Select(i, 0);
return true;
} }

View File

@@ -31,7 +31,8 @@ class List
class InvalidItem { }; class InvalidItem { };
List() { } List() { }
virtual ~List() { }; virtual ~List() { }
virtual void Select(int, bool) = 0; virtual void Select(int, bool) = 0;
virtual void Static(int, bool) = 0; virtual void Static(int, bool) = 0;
virtual bool Empty() const = 0; virtual bool Empty() const = 0;
@@ -43,6 +44,10 @@ class List
virtual size_t Size() const = 0; virtual size_t Size() const = 0;
virtual size_t Choice() const = 0; virtual size_t Choice() const = 0;
virtual size_t RealChoice() const = 0; virtual size_t RealChoice() const = 0;
void SelectCurrent();
void ReverseSelection(size_t = 0);
bool Deselect();
}; };
template <class T> class Menu : public Window, public List template <class T> class Menu : public Window, public List

View File

@@ -1184,114 +1184,36 @@ int main(int argc, char *argv[])
} }
else if (Keypressed(input, Key.ReverseSelection)) else if (Keypressed(input, Key.ReverseSelection))
{ {
if (myScreen == myPlaylist if (myScreen->allowsSelection())
|| myScreen == myBrowser
|| (myScreen == mySearcher && !mySearcher->Main()->Current().first)
|| myScreen->Cmp() == myLibrary->Songs
|| myScreen->Cmp() == myPlaylistEditor->Content
# ifdef HAVE_TAGLIB_H
|| myScreen->Cmp() == myTagEditor->Tags
# endif // HAVE_TAGLIB_H
)
{ {
List *mList = reinterpret_cast<Menu<Song> *>(myWindow->Main()); myScreen->ReverseSelection();
for (size_t i = 0; i < mList->Size(); i++)
mList->Select(i, !mList->isSelected(i) && !mList->isStatic(i));
// hackish shit begins
if (myScreen == myBrowser && myBrowser->CurrentDir() != "/")
mList->Select(0, 0); // [..] cannot be selected, uhm.
if (myScreen == mySearcher)
mList->Select(SearchEngine::ResetButton, 0); // 'Reset' cannot be selected, omgplz.
// hacking shit ends.
ShowMessage("Selection reversed!"); ShowMessage("Selection reversed!");
} }
} }
else if (Keypressed(input, Key.DeselectAll)) else if (Keypressed(input, Key.DeselectAll))
{ {
if (myScreen == myPlaylist if (myScreen->allowsSelection())
|| myScreen == myBrowser
|| myScreen == mySearcher
|| myScreen->Cmp() == myLibrary->Songs
|| myScreen->Cmp() == myPlaylistEditor->Content
# ifdef HAVE_TAGLIB_H
|| myScreen->Cmp() == myTagEditor->Tags
# endif // HAVE_TAGLIB_H
)
{ {
List *mList = reinterpret_cast<Menu<Song> *>(myWindow->Main()); if (myScreen->Deselect())
if (mList->hasSelected())
{ {
for (size_t i = 0; i < mList->Size(); i++)
mList->Select(i, 0);
ShowMessage("Items deselected!"); ShowMessage("Items deselected!");
} }
} }
} }
else if (Keypressed(input, Key.AddSelected)) else if (Keypressed(input, Key.AddSelected))
{ {
if (myScreen != myPlaylist if (!myScreen->allowsSelection())
&& myScreen != myBrowser
&& myScreen != mySearcher
&& myScreen->Cmp() != myLibrary->Songs
&& myScreen->Cmp() != myPlaylistEditor->Content)
continue; continue;
List *mList = reinterpret_cast<Menu<Song> *>(myWindow->Main()); SongList result;
if (!mList->hasSelected()) myScreen->GetSelectedSongs(result);
if (result.empty())
{ {
ShowMessage("No selected items!"); ShowMessage("No selected items!");
continue; continue;
} }
vector<size_t> list;
mList->GetSelected(list);
SongList result;
for (vector<size_t>::const_iterator it = list.begin(); it != list.end(); it++)
{
if (myScreen == myPlaylist)
{
Song *s = new Song(myPlaylist->Main()->at(*it));
result.push_back(s);
}
else if (myScreen == myBrowser)
{
const Item &item = myBrowser->Main()->at(*it);
switch (item.type)
{
case itDirectory:
{
Mpd->GetDirectoryRecursive(locale_to_utf_cpy(item.name), result);
break;
}
case itSong:
{
Song *s = new Song(*item.song);
result.push_back(s);
break;
}
case itPlaylist:
{
Mpd->GetPlaylistContent(locale_to_utf_cpy(item.name), result);
break;
}
}
}
else if (myScreen == mySearcher)
{
Song *s = new Song(*mySearcher->Main()->at(*it).second);
result.push_back(s);
}
else if (myScreen == myLibrary)
{
Song *s = new Song(myLibrary->Songs->at(*it));
result.push_back(s);
}
else if (myScreen == myPlaylistEditor)
{
Song *s = new Song(myPlaylistEditor->Content->at(*it));
result.push_back(s);
}
}
size_t dialog_width = COLS*0.8; size_t dialog_width = COLS*0.8;
size_t dialog_height = LINES*0.6; size_t dialog_height = LINES*0.6;
Menu<string> *mDialog = new Menu<string>((COLS-dialog_width)/2, (LINES-dialog_height)/2, dialog_width, dialog_height, "Add selected items to...", Config.main_color, Config.window_border); Menu<string> *mDialog = new Menu<string>((COLS-dialog_width)/2, (LINES-dialog_height)/2, dialog_width, dialog_height, "Add selected items to...", Config.main_color, Config.window_border);

View File

@@ -81,7 +81,7 @@ void Playlist::EnterPressed()
void Playlist::SpacePressed() void Playlist::SpacePressed()
{ {
Select(w); w->SelectCurrent();
w->Scroll(wDown); w->Scroll(wDown);
} }
@@ -90,6 +90,16 @@ MPD::Song *Playlist::CurrentSong()
return !w->Empty() ? &w->Current() : 0; return !w->Empty() ? &w->Current() : 0;
} }
void Playlist::GetSelectedSongs(MPD::SongList &v)
{
vector<size_t> selected;
w->GetSelected(selected);
for (vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); it++)
{
v.push_back(new MPD::Song(w->at(*it)));
}
}
std::string Playlist::TotalLength() std::string Playlist::TotalLength()
{ {
std::ostringstream result; std::ostringstream result;

View File

@@ -42,6 +42,11 @@ class Playlist : public Screen< Menu<MPD::Song> >
virtual MPD::Song *CurrentSong(); virtual MPD::Song *CurrentSong();
virtual bool allowsSelection() { return true; }
virtual void ReverseSelection() { w->ReverseSelection(); }
virtual bool Deselect() { return w->Deselect(); }
virtual void GetSelectedSongs(MPD::SongList &);
bool isPlaying() { return NowPlaying >= 0 && !w->Empty(); } bool isPlaying() { return NowPlaying >= 0 && !w->Empty(); }
const MPD::Song &NowPlayingSong(); const MPD::Song &NowPlayingSong();

View File

@@ -266,7 +266,7 @@ void PlaylistEditor::SpacePressed()
{ {
if (Config.space_selects && w == Content) if (Config.space_selects && w == Content)
{ {
Select(Content); Content->SelectCurrent();
w->Scroll(wDown); w->Scroll(wDown);
return; return;
} }
@@ -278,3 +278,13 @@ MPD::Song *PlaylistEditor::CurrentSong()
return w == Content && !Content->Empty() ? &Content->Current() : 0; return w == Content && !Content->Empty() ? &Content->Current() : 0;
} }
void PlaylistEditor::GetSelectedSongs(MPD::SongList &v)
{
std::vector<size_t> selected;
Content->GetSelected(selected);
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); it++)
{
v.push_back(new MPD::Song(Content->at(*it)));
}
}

View File

@@ -40,6 +40,11 @@ class PlaylistEditor : public Screen<Window>
virtual MPD::Song *CurrentSong(); virtual MPD::Song *CurrentSong();
virtual bool allowsSelection() { return w == Content; }
virtual void ReverseSelection() { Content->ReverseSelection(); }
virtual bool Deselect() { return Content->Deselect(); }
virtual void GetSelectedSongs(MPD::SongList &);
void NextColumn(); void NextColumn();
void PrevColumn(); void PrevColumn();

View File

@@ -50,10 +50,12 @@ class BasicScreen
virtual MPD::Song *CurrentSong() { return 0; } virtual MPD::Song *CurrentSong() { return 0; }
bool hasToBeResized; virtual bool allowsSelection() = 0;
virtual void ReverseSelection() { }
virtual bool Deselect() { return false; }
virtual void GetSelectedSongs(MPD::SongList &) { }
protected: bool hasToBeResized;
void Select(List *);
}; };
template <class WindowType> class Screen : public BasicScreen template <class WindowType> class Screen : public BasicScreen
@@ -78,7 +80,6 @@ template <class WindowType> void *&Screen<WindowType>::Cmp()
return *(void **)(void *)&w; return *(void **)(void *)&w;
} }
template <class WindowType> WindowType *&Screen<WindowType>::Main() template <class WindowType> WindowType *&Screen<WindowType>::Main()
{ {
return w; return w;

View File

@@ -254,7 +254,7 @@ void SearchEngine::SpacePressed()
if (Config.space_selects) if (Config.space_selects)
{ {
Select(w); w->SelectCurrent();
w->Scroll(wDown); w->Scroll(wDown);
return; return;
} }
@@ -293,6 +293,16 @@ MPD::Song *SearchEngine::CurrentSong()
return !w->Empty() ? w->Current().second : 0; return !w->Empty() ? w->Current().second : 0;
} }
void SearchEngine::GetSelectedSongs(MPD::SongList &v)
{
std::vector<size_t> selected;
w->GetSelected(selected);
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); it++)
{
v.push_back(new MPD::Song(*w->at(*it).second));
}
}
void SearchEngine::UpdateFoundList() void SearchEngine::UpdateFoundList()
{ {
bool bold = 0; bool bold = 0;

View File

@@ -51,6 +51,11 @@ class SearchEngine : public Screen< Menu< std::pair<Buffer *, MPD::Song *> > >
virtual MPD::Song *CurrentSong(); virtual MPD::Song *CurrentSong();
virtual bool allowsSelection() { return w->Choice() >= StaticOptions; }
virtual void ReverseSelection() { w->ReverseSelection(StaticOptions); }
virtual bool Deselect() { return w->Deselect(); }
virtual void GetSelectedSongs(MPD::SongList &);
void UpdateFoundList(); void UpdateFoundList();
static size_t StaticOptions; static size_t StaticOptions;

View File

@@ -704,7 +704,7 @@ void TagEditor::SpacePressed()
{ {
if (w == Tags) if (w == Tags)
{ {
Select(Tags); Tags->SelectCurrent();
w->Scroll(wDown); w->Scroll(wDown);
return; return;
} }
@@ -723,6 +723,16 @@ MPD::Song *TagEditor::CurrentSong()
return w == Tags && !Tags->Empty() ? &Tags->Current() : 0; return w == Tags && !Tags->Empty() ? &Tags->Current() : 0;
} }
void TagEditor::GetSelectedSongs(MPD::SongList &v)
{
std::vector<size_t> selected;
Tags->GetSelected(selected);
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); it++)
{
v.push_back(new MPD::Song(Tags->at(*it)));
}
}
void TagEditor::NextColumn() void TagEditor::NextColumn()
{ {
CLEAR_FIND_HISTORY; CLEAR_FIND_HISTORY;

View File

@@ -44,6 +44,8 @@ class TinyTagEditor : public Screen< Menu<Buffer> >
virtual void EnterPressed(); virtual void EnterPressed();
virtual bool allowsSelection() { return false; }
bool SetEdited(MPD::Song *); bool SetEdited(MPD::Song *);
protected: protected:
@@ -70,6 +72,11 @@ class TagEditor : public Screen<Window>
virtual MPD::Song *CurrentSong(); virtual MPD::Song *CurrentSong();
virtual bool allowsSelection() { return w == Tags; }
virtual void ReverseSelection() { Tags->ReverseSelection(); }
virtual bool Deselect() { return Tags->Deselect(); }
virtual void GetSelectedSongs(MPD::SongList &);
void NextColumn(); void NextColumn();
void PrevColumn(); void PrevColumn();