clean NCurse::List class a bit and remove a few redundant functions

This commit is contained in:
Andrzej Rybczak
2009-09-19 01:18:41 +02:00
parent 2f971147b1
commit 3bdb798852
9 changed files with 62 additions and 107 deletions

View File

@@ -125,14 +125,17 @@ void Browser::EnterPressed()
void Browser::SpacePressed() void Browser::SpacePressed()
{ {
if (w->Empty())
return;
if (Config.space_selects && w->Choice() >= (itsBrowsedDir != "/" ? 1 : 0)) if (Config.space_selects && w->Choice() >= (itsBrowsedDir != "/" ? 1 : 0))
{ {
w->SelectCurrent(); w->Select(w->Choice(), !w->isSelected());
w->Scroll(wDown); w->Scroll(wDown);
return; return;
} }
if (w->Empty() || (itsBrowsedDir != "/" && w->Choice() == 0 /* parent dir */)) if (itsBrowsedDir != "/" && w->Choice() == 0 /* parent dir */)
return; return;
const Item &item = w->Current(); const Item &item = w->Current();

View File

@@ -324,10 +324,10 @@ void MediaLibrary::SpacePressed()
{ {
if (Config.space_selects && w == Songs) if (Config.space_selects && w == Songs)
{ {
Songs->SelectCurrent(); Songs->Select(Songs->Choice(), !Songs->isSelected());
w->Scroll(wDown); w->Scroll(wDown);
return;
} }
else
AddToPlaylist(0); AddToPlaylist(0);
} }

View File

@@ -22,29 +22,6 @@
using namespace NCurses; using namespace NCurses;
void List::SelectCurrent()
{
if (Empty())
return;
size_t i = Choice();
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;
}
template <> std::string Menu<std::string>::GetOption(size_t pos) template <> std::string Menu<std::string>::GetOption(size_t pos)
{ {
if (itsOptionsPtr->at(pos)) if (itsOptionsPtr->at(pos))

View File

@@ -34,7 +34,6 @@ namespace NCurses
class List class List
{ {
public: public:
/// Exception class, thrown by various functions /// Exception class, thrown by various functions
/// that return references to items on the list /// that return references to items on the list
/// if requested item is separator /// if requested item is separator
@@ -49,22 +48,10 @@ namespace NCurses
/// ///
virtual void Select(int pos, bool state) = 0; virtual void Select(int pos, bool state) = 0;
/// @see Menu::Static()
///
virtual void Static(int pos, bool state) = 0;
/// @see Menu::Empty()
///
virtual bool Empty() const = 0;
/// @see Menu::isSelected() /// @see Menu::isSelected()
/// ///
virtual bool isSelected(int pos = -1) const = 0; virtual bool isSelected(int pos = -1) const = 0;
/// @see Menu::isStatic()
///
virtual bool isStatic(int pos = -1) const = 0;
/// @see Menu::hasSelected() /// @see Menu::hasSelected()
/// ///
virtual bool hasSelected() const = 0; virtual bool hasSelected() const = 0;
@@ -73,36 +60,14 @@ namespace NCurses
/// ///
virtual void GetSelected(std::vector<size_t> &v) const = 0; virtual void GetSelected(std::vector<size_t> &v) const = 0;
/// @see Menu::Highlight() /// @see Menu::Empty()
/// ///
virtual void Highlight(size_t pos) = 0; virtual bool Empty() const = 0;
/// @see Menu::Size() /// @see Menu::Size()
/// ///
virtual size_t Size() const = 0; virtual size_t Size() const = 0;
/// @see Menu::Choice()
///
virtual size_t Choice() const = 0;
/// @see Menu::RealChoice()
///
virtual size_t RealChoice() const = 0;
/// Selects current position
///
void SelectCurrent();
/// Reverses selection of all items in list
/// @param beginning beginning of range that has to be reversed
///
void ReverseSelection(size_t beginning = 0);
/// Deselects all items in list
/// @return true if there was at least one selected items, false otherwise
///
bool Deselect();
/// @see Menu::Search() /// @see Menu::Search()
/// ///
virtual bool Search(const std::string &constraint, size_t beginning = 0, int flags = 0) = 0; virtual bool Search(const std::string &constraint, size_t beginning = 0, int flags = 0) = 0;
@@ -127,10 +92,6 @@ namespace NCurses
/// ///
virtual const std::string &GetFilter() = 0; virtual const std::string &GetFilter() = 0;
/// @see Menu::GetOption()
///
virtual std::string GetOption(size_t pos) = 0;
/// @see Menu::isFiltered() /// @see Menu::isFiltered()
/// ///
virtual bool isFiltered() = 0; virtual bool isFiltered() = 0;
@@ -304,31 +265,31 @@ namespace NCurses
/// ///
void BoldOption(int pos, bool state); void BoldOption(int pos, bool state);
/// Makes given position static/active.
/// Static positions cannot be highlighted.
/// @param pos position in list
/// @param state state of activity
///
void Static(int pos, bool state);
/// Checks whether given position is static or active
/// @param pos position to be checked, -1 checks currently highlighted position
/// @return true if position is static, false otherwise
///
bool isStatic(int pos = -1) const;
/// Selects/deselects given position /// Selects/deselects given position
/// @param pos position in list /// @param pos position in list
/// @param state state of selection /// @param state state of selection
/// ///
virtual void Select(int pos, bool state); virtual void Select(int pos, bool state);
/// Makes given position static/active.
/// Static positions cannot be highlighted.
/// @param pos position in list
/// @param state state of activity
///
virtual void Static(int pos, bool state);
/// Checks if given position is selected /// Checks if given position is selected
/// @param pos position to be checked, -1 checks currently highlighted position /// @param pos position to be checked, -1 checks currently highlighted position
/// @return true if position is selected, false otherwise /// @return true if position is selected, false otherwise
/// ///
virtual bool isSelected(int pos = -1) const; virtual bool isSelected(int pos = -1) const;
/// Checks whether given position is static or active
/// @param pos position to be checked, -1 checks currently highlighted position
/// @return true if position is static, false otherwise
///
virtual bool isStatic(int pos = -1) const;
/// Checks whether list contains selected positions /// Checks whether list contains selected positions
/// @return true if it contains them, false otherwise /// @return true if it contains them, false otherwise
/// ///
@@ -339,23 +300,24 @@ namespace NCurses
/// ///
virtual void GetSelected(std::vector<size_t> &v) const; virtual void GetSelected(std::vector<size_t> &v) const;
/// Reverses selection of all items in list
/// @param beginning beginning of range that has to be reversed
///
void ReverseSelection(size_t beginning = 0);
/// Highlights given position /// Highlights given position
/// @param pos position to be highlighted /// @param pos position to be highlighted
/// ///
virtual void Highlight(size_t pos); void Highlight(size_t pos);
/// @return size of the list
///
virtual size_t Size() const;
/// @return currently highlighted position /// @return currently highlighted position
/// ///
virtual size_t Choice() const; size_t Choice() const;
/// @return real current positions, i.e it doesn't /// @return real current positions, i.e it doesn't
/// count positions that are static or separators /// count positions that are static or separators
/// ///
virtual size_t RealChoice() const; size_t RealChoice() const;
/// Searches the list for a given contraint. It uses GetStringFunction to convert stored items /// Searches the list for a given contraint. It uses GetStringFunction to convert stored items
/// into strings and then performs pattern matching. Note that this supports regular expressions. /// into strings and then performs pattern matching. Note that this supports regular expressions.
@@ -395,14 +357,6 @@ namespace NCurses
/// ///
virtual const std::string &GetFilter(); virtual const std::string &GetFilter();
/// Converts given position in list to string using GetStringFunction
/// if specified and an empty string otherwise
/// @param pos position to be converted
/// @return item converted to string
/// @see SetItemDisplayer()
///
virtual std::string GetOption(size_t pos);
/// @return true if list is currently filtered, false otherwise /// @return true if list is currently filtered, false otherwise
/// ///
virtual bool isFiltered() { return itsOptionsPtr == &itsFilteredOptions; } virtual bool isFiltered() { return itsOptionsPtr == &itsFilteredOptions; }
@@ -415,6 +369,14 @@ namespace NCurses
/// ///
void ShowFiltered() { itsOptionsPtr = &itsFilteredOptions; } void ShowFiltered() { itsOptionsPtr = &itsFilteredOptions; }
/// Converts given position in list to string using GetStringFunction
/// if specified and an empty string otherwise
/// @param pos position to be converted
/// @return item converted to string
/// @see SetItemDisplayer()
///
std::string GetOption(size_t pos);
/// Refreshes the menu window /// Refreshes the menu window
/// @see Window::Refresh() /// @see Window::Refresh()
/// ///
@@ -485,6 +447,10 @@ namespace NCurses
/// ///
virtual bool Empty() const { return itsOptionsPtr->empty(); } virtual bool Empty() const { return itsOptionsPtr->empty(); }
/// @return size of the list
///
virtual size_t Size() const;
/// @return reference to last item on the list /// @return reference to last item on the list
/// @throw List::InvalidItem if requested item is separator /// @throw List::InvalidItem if requested item is separator
/// ///
@@ -1004,6 +970,13 @@ template <typename T> size_t NCurses::Menu<T>::RealChoice() const
return result; return result;
} }
template <typename T> void NCurses::Menu<T>::ReverseSelection(size_t beginning)
{
option_iterator it = itsOptionsPtr->begin()+beginning;
for (size_t i = beginning; i < Size(); ++i, ++it)
(*it)->isSelected = !(*it)->isSelected && !(*it)->isStatic;
}
template <typename T> bool NCurses::Menu<T>::Search(const std::string &constraint, size_t beginning, int flags) template <typename T> bool NCurses::Menu<T>::Search(const std::string &constraint, size_t beginning, int flags)
{ {
itsFound.clear(); itsFound.clear();

View File

@@ -1549,12 +1549,14 @@ int main(int argc, char *argv[])
{ {
if (myScreen->allowsSelection()) if (myScreen->allowsSelection())
{ {
if (myScreen->GetList()->Deselect()) List *mList = myScreen->GetList();
{ if (!mList->hasSelected())
continue;
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))
{ {
mySelectedItemsAdder->SwitchTo(); mySelectedItemsAdder->SwitchTo();

View File

@@ -220,7 +220,7 @@ void Playlist::SpacePressed()
{ {
if (w == Items) if (w == Items)
{ {
Items->SelectCurrent(); Items->Select(Items->Choice(), !Items->isSelected());
Items->Scroll(wDown); Items->Scroll(wDown);
} }
} }

View File

@@ -213,10 +213,10 @@ void PlaylistEditor::SpacePressed()
{ {
if (Config.space_selects && w == Content) if (Config.space_selects && w == Content)
{ {
Content->SelectCurrent(); Content->Select(Content->Choice(), !Content->isSelected());
w->Scroll(wDown); w->Scroll(wDown);
return;
} }
else
AddToPlaylist(0); AddToPlaylist(0);
} }

View File

@@ -251,7 +251,7 @@ void SearchEngine::SpacePressed()
if (Config.space_selects) if (Config.space_selects)
{ {
w->SelectCurrent(); w->Select(w->Choice(), !w->isSelected());
w->Scroll(wDown); w->Scroll(wDown);
return; return;
} }

View File

@@ -682,7 +682,7 @@ void TagEditor::SpacePressed()
{ {
if (w == Tags) if (w == Tags)
{ {
Tags->SelectCurrent(); Tags->Select(Tags->Choice(), !Tags->isSelected());
w->Scroll(wDown); w->Scroll(wDown);
return; return;
} }