From 57563d06e9947380a2cf4a7f97ca5835c4bd8b2b Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 10 Mar 2013 14:04:08 +0000 Subject: Improved ui::ListView::sort(). --- src/ui/listview.cc | 21 +++++++++++++-------- src/ui/listview.h | 20 ++++++++------------ 2 files changed, 21 insertions(+), 20 deletions(-) (limited to 'src/ui') diff --git a/src/ui/listview.cc b/src/ui/listview.cc index ac2a4e8..0c01596 100644 --- a/src/ui/listview.cc +++ b/src/ui/listview.cc @@ -132,6 +132,11 @@ void ListView::clear() listview_scrollbar->set_value(listview_scroll); } +ListItem *ListView::add_item(const std::string & text) +{ + return new ui::ListItem(this, text.c_str()); +} + bool ListView::on_emit(Widget *sender, const Event event, void *data) { if (sender == listview_scrollbar) { @@ -173,7 +178,7 @@ bool ListView::on_keypress(const int key, const unsigned int modifier) return false; } -bool compare_listitems(const Widget *first, const Widget *second) +bool compare_listitems_ascending(const Widget *first, const Widget *second) { const ListItem *firstitem = dynamic_cast(first); const ListItem *seconditem = dynamic_cast(second); @@ -192,12 +197,8 @@ bool compare_listitems(const Widget *first, const Widget *second) } } -void ListView::sort() -{ - children().sort(compare_listitems); -} -bool compare_listitems_reverse(const Widget *first, const Widget *second) +bool compare_listitems_descending(const Widget *first, const Widget *second) { const ListItem *firstitem = dynamic_cast(first); const ListItem *seconditem = dynamic_cast(second); @@ -216,9 +217,13 @@ bool compare_listitems_reverse(const Widget *first, const Widget *second) } } -void ListView::sort_reverse() +void ListView::sort(bool ascending) { - children().sort(compare_listitems_reverse); + if (ascending) { + children().sort(compare_listitems_ascending); + } else { + children().sort(compare_listitems_descending); + } } } diff --git a/src/ui/listview.h b/src/ui/listview.h index b4c00dc..7678b8b 100644 --- a/src/ui/listview.h +++ b/src/ui/listview.h @@ -41,6 +41,11 @@ public: } /* -- mutators --------------------------------------------- */ + + /** + * @brief add a new item + * */ + ListItem *add_item(const std::string & text); /// set scroll void set_scroll(float scroll); @@ -51,7 +56,7 @@ public: /// scroll up void dec_scroll(float scroll); - /// clear all listitems + /// delete all listitems void clear(); /// set selection to specified ListItem @@ -61,18 +66,9 @@ public: void deselect(); /** - * @brief sort child ListItems according to their sortkey - * Sort will fail with an assert error if the ListView - * contains any non-ListItem child widgets - * */ - void sort(); - - /** - * @brief reverse sort child ListItems according to their sortkey - * Sort will fail with an assert error if the ListView - * contains any non-ListItem child widgets + * @brief sort listitems according to their sortkey * */ - void sort_reverse(); + void sort(bool ascending = true); protected: virtual void resize(); -- cgit v1.2.3