From baf6ad1f48ef08187f50247115c09a3612ebeec3 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 8 Nov 2010 23:33:49 +0000 Subject: added sorting of listview items --- src/client/inventory.cc | 9 ++++++++- src/client/inventorylistview.cc | 18 ++++++++++++++++-- src/client/inventorylistview.h | 15 ++++++++++++++- src/client/trademenu.cc | 15 +++++++++------ 4 files changed, 47 insertions(+), 10 deletions(-) (limited to 'src/client') 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(); } -- cgit v1.2.3