diff options
author | Stijn Buys <ingar@osirion.org> | 2010-11-08 23:33:49 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2010-11-08 23:33:49 +0000 |
commit | baf6ad1f48ef08187f50247115c09a3612ebeec3 (patch) | |
tree | c3c81f530c09b027f9880c8434df82a033a33323 /src | |
parent | 106d0cb0cf884dd7a2920564852c001e13af1568 (diff) |
added sorting of listview items
Diffstat (limited to 'src')
-rw-r--r-- | src/client/inventory.cc | 9 | ||||
-rw-r--r-- | src/client/inventorylistview.cc | 18 | ||||
-rw-r--r-- | src/client/inventorylistview.h | 15 | ||||
-rw-r--r-- | src/client/trademenu.cc | 15 | ||||
-rw-r--r-- | src/ui/listitem.h | 9 | ||||
-rw-r--r-- | src/ui/listview.cc | 18 | ||||
-rw-r--r-- | src/ui/listview.h | 3 |
7 files changed, 77 insertions, 10 deletions
diff --git a/src/client/inventory.cc b/src/client/inventory.cc index c6e86da..4894d3d 100644 --- a/src/client/inventory.cc +++ b/src/client/inventory.cc @@ -100,12 +100,18 @@ void Inventory::update_inventory() str << item->info()->name().c_str(); if (item->amount() > 0) { str << " (" << item->amount() << ")"; - } + } + listitem = new ui::ListItem(menu_listview, str.str().c_str()); listitem->set_height(listitem->font()->height() * 2.0f); listitem->set_item(item); listitem->set_info(item->info()); + std::string sortkey(item->info()->type()->label()); + sortkey += '.'; + sortkey += item->info()->label(); + listitem->set_sortkey(sortkey); + // preserve previous selection during update if (item == selecteditem) { menu_listview->select(listitem); @@ -128,6 +134,7 @@ void Inventory::update_inventory() set_info(0, 0); } + menu_listview->sort(); menu_listview->event_resize(); } diff --git a/src/client/inventorylistview.cc b/src/client/inventorylistview.cc index a8d98f3..e41aea2 100644 --- a/src/client/inventorylistview.cc +++ b/src/client/inventorylistview.cc @@ -17,6 +17,7 @@ InventoryListView::InventoryListView(ui::Widget *parent) : ui::ListView (parent) listview_infotype = 0; listview_timestamp = 0; listview_infotimestamp = 0; + listview_showempty = false; set_inventory(0, 0); } @@ -54,19 +55,30 @@ void InventoryListView::set_inventory(core::Inventory *inventory, core::InfoType listview_infotimestamp = item->info()->timestamp(); } - if ((item->info()->type() == infotype) && (item->amount() != 0)) { + if ((item->info()->type() == infotype) && (listview_showempty || (item->amount() != 0))) { ui::ListItem *listitem = 0; + std::string sortkey; std::ostringstream str; str << item->info()->name().c_str(); - if (item->amount() > 0) { + if (item->amount() >= 0) { str << " (" << item->amount() << ")"; } + + if (item->amount() == 0) { + sortkey.assign("-"); + } else { + sortkey.assign("+"); + } + + sortkey.append(item->info()->label()); + listitem = new ui::ListItem(this, str.str().c_str()); listitem->set_height(listitem->font()->height() * 2.0f); listitem->set_item(item); listitem->set_info(item->info()); + listitem->set_sortkey(sortkey); // preserve previous selection during update if (item == selecteditem) { ui::ListView::select(listitem); @@ -75,6 +87,8 @@ void InventoryListView::set_inventory(core::Inventory *inventory, core::InfoType } } + sort(); + event_resize(); emit(EventListViewChanged); diff --git a/src/client/inventorylistview.h b/src/client/inventorylistview.h index 1688560..b937e0e 100644 --- a/src/client/inventorylistview.h +++ b/src/client/inventorylistview.h @@ -13,7 +13,10 @@ namespace client { - + +/** + * @brief a listview displaying the items in an inventory belonging to a specified infotype + */ class InventoryListView : public ui::ListView { public: InventoryListView(ui::Widget *parent = 0); @@ -21,6 +24,15 @@ public: void set_inventory(core::Inventory *inventory, core::InfoType *infotype); + /** + * @brief enable or disable the listing of empty items + * Items are empty if their amount is 0 + */ + inline void set_showempty(const bool showempty) + { + listview_showempty = showempty; + } + inline const core::Inventory *inventory() const { return listview_inventory; } @@ -34,6 +46,7 @@ private: unsigned long listview_timestamp; unsigned long listview_infotimestamp; + bool listview_showempty; core::Inventory *listview_inventory; core::InfoType *listview_infotype; }; diff --git a/src/client/trademenu.cc b/src/client/trademenu.cc index 3c0847a..3bd4814 100644 --- a/src/client/trademenu.cc +++ b/src/client/trademenu.cc @@ -73,6 +73,7 @@ TradeMenu::TradeMenu(ui::Widget *parent, const char * label) : ui::Widget(parent menu_traderlistview->set_label("traderlistview"); menu_traderlistview->set_background(false); menu_traderlistview->set_border(true); + menu_traderlistview->set_showempty(true); menu_tradertext = new ui::PlainText(menu_tradewindow); menu_tradertext->set_label("tradertext"); @@ -147,7 +148,7 @@ void TradeMenu::set_item(ui::ListItem *item) return; } - long amount = 0; // reserved + long amount = 0; for (core::Info::Text::const_iterator it = item->info()->text().begin(); it != item->info()->text().end(); it++) { menu_infotext.push_back((*it)); @@ -181,8 +182,8 @@ void TradeMenu::set_item(ui::ListItem *item) menu_buybutton->show(); } - if (amount < 1) { - menu_msgtext->set_text("^1Can not sell"); + if (amount < 1) { + menu_msgtext->set_text("^1Can not sell here"); menu_msgtext->show(); } @@ -230,14 +231,16 @@ void TradeMenu::set_item(ui::ListItem *item) menu_buybutton->enable(); menu_buybutton->show(); } - + if (amount < 1) { - if (item_unit_price > core::localplayer()->credits()) { + if (item->item()->amount() == 0 ) { + menu_msgtext->set_text("^1Not available"); + } else if (item_unit_price > core::localplayer()->credits()) { menu_msgtext->set_text("^1Not enough credits"); } else if (item_unit_volume > menu_inventorylistview->inventory()->capacity_available()) { menu_msgtext->set_text("^1Not enough cargo space"); } else { - menu_msgtext->set_text("^1Can not buy"); + menu_msgtext->set_text("^1Can not buy here"); } menu_msgtext->show(); } diff --git a/src/ui/listitem.h b/src/ui/listitem.h index 3c9efea..5795917 100644 --- a/src/ui/listitem.h +++ b/src/ui/listitem.h @@ -35,6 +35,10 @@ public: return listitem_item; } + inline const std::string & sortkey() const { + return listitem_sortkey; + } + inline void set_info(const core::Info *info) { listitem_info = info; } @@ -42,6 +46,10 @@ public: inline void set_item(const core::Item *item) { listitem_item = item; } + + inline void set_sortkey(const std::string sortkey) { + listitem_sortkey.assign(sortkey); + } void select(); @@ -59,6 +67,7 @@ protected: private: const core::Info *listitem_info; const core::Item *listitem_item; + std::string listitem_sortkey; }; } // namespace ui diff --git a/src/ui/listview.cc b/src/ui/listview.cc index fc75806..70ecf8f 100644 --- a/src/ui/listview.cc +++ b/src/ui/listview.cc @@ -97,4 +97,22 @@ bool ListView::on_emit(Widget *sender, const Event event, void *data) return false; } +void ListView::sort() +{ + // bubble sort - there's a reason for using it here + for (Children::iterator low = children().begin(); low != children().end(); low++) { + Children::iterator high = low; + for (high++; high != children().end(); high++) { + ListItem *lowitem = dynamic_cast<ListItem *>(*low); + ListItem *highitem = dynamic_cast<ListItem *>(*high); + assert(lowitem && highitem); + if (highitem->sortkey() < lowitem->sortkey()) { + Widget *t = (*low); + (*low) = (*high); + (*high) = t; + } + } + } +} + } diff --git a/src/ui/listview.h b/src/ui/listview.h index a0c185b..f752ac2 100644 --- a/src/ui/listview.h +++ b/src/ui/listview.h @@ -59,6 +59,9 @@ public: /// set selection to nothing void deselect(); + /// sort child ListItems according to their sortkey + void sort(); + protected: virtual void resize(); |