Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: d87ed2bd9c93dce6cb1524469bd95f17b8ec52c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
   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 "client/inventorylistview.h"
#include "ui/listitem.h"

namespace client {

InventoryListView::InventoryListView(ui::Widget *parent) : ui::ListView (parent)
{
	listview_timestamp = 0;
	set_inventory(0, 0);
}

InventoryListView::~InventoryListView() {
}

void InventoryListView::set_inventory(core::Inventory *inventory, core::InfoType *infotype)
{
	remove_children();
	
	listview_inventory = inventory;
	listview_infotype = infotype;
	
	const core::Item *selecteditem = (selected() ? selected()->item() : 0);
	
	if (!inventory || !infotype) {
		return;
	}
	
	// TODO scan the inventories and request updated infos
	// update when necessary
	
	for (core::Inventory::Items::const_iterator it = inventory->items().begin(); it != inventory->items().end(); it++) {
		core::Item *item = (*it);
	
		if (item->info() && (item->info()->type() == infotype) && (item->amount() != 0) ) {
			ui::ListItem *listitem = 0;
			
			std::ostringstream str;
			str << item->info()->name().c_str();
			
			if (item->amount() > 0) {
				str << " (" << item->amount() << ")";
			}
			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());
			// preserve previous selection during update
			if (item == selecteditem) {
				ui::ListView::select(listitem);
			}
		}
	}
	
	listview_timestamp = inventory->timestamp();
	
	event_resize();
	
	emit(EventListViewChanged);
}

void InventoryListView::draw()
{
	if (listview_inventory && (listview_timestamp != listview_inventory->timestamp())) {
		set_inventory(listview_inventory, listview_infotype);
	}
	
	ListView::draw();
}

} // namespace client