/* client/inventorylistview.cc This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include "core/application.h" #include "client/inventorylistview.h" #include "ui/listitem.h" #include "ui/paint.h" namespace client { InventoryListView::InventoryListView(ui::Widget *parent) : ui::ListView (parent) { listview_inventory = 0; listview_infotype = 0; listview_timestamp = 0; listview_infotimestamp = 0; listview_showempty = false; set_inventory(0, 0); } InventoryListView::~InventoryListView() { } void InventoryListView::set_inventory(core::Inventory *inventory, core::InfoType *infotype) { remove_children(); deselect(); listview_timestamp = 0; listview_infotimestamp = 0; listview_inventory = inventory; listview_infotype = infotype; if (!listview_inventory || !listview_infotype) { return; } const core::Item *selecteditem = (selected() ? selected()->item() : 0); listview_timestamp = inventory->timestamp(); for (core::Inventory::Items::const_iterator it = inventory->items().begin(); it != inventory->items().end(); it++) { core::Item *item = (*it); if (item->info()) { // this makes sure inventory info gets requested from the server core::game()->request_info(item->info()->id()); if (item->info()->timestamp() > listview_infotimestamp) { listview_infotimestamp = item->info()->timestamp(); } 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) { 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); } } } } sort(); event_resize(); emit(EventListViewChanged); } bool InventoryListView::verify() const { if (!listview_inventory || !listview_infotype) { return true; } if (listview_timestamp != listview_inventory->timestamp()) { return false; } for (core::Inventory::Items::const_iterator it = listview_inventory->items().begin(); it != listview_inventory->items().end(); it++) { core::Item *item = (*it); if (item->info() && (item->info()->timestamp() > listview_infotimestamp)) { return false; } } return true; } void InventoryListView::draw() { if (!verify()) { set_inventory(listview_inventory, listview_infotype); } ListView::draw(); } } // namespace client