Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 77daba2253feb39dbbd1debaa0048678cbcc8b9f (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
   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"
#include "ui/ui.h"

#include <iomanip>

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;
	
	listview_timestamp = 0;
	listview_infotimestamp = 0;
	listview_inventory = 0;
}

InventoryListView::~InventoryListView()
{
}

void InventoryListView::set_inventory(core::Inventory *inventory, core::InfoType *infotype)
{
	ListView::clear();
	
	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;
				
				if (inventory == core::localcontrol()->inventory()) {
					
					// special rules for own ship inventory
					// retreive price from the trader
					const core::Item *trader_item = (core::localplayer()->view() && core::localplayer()->view()->inventory() ? core::localplayer()->view()->inventory()->find(item->info()) : 0);
					if (!trader_item) {
						// not sold here
						str << "^N";
					}
					
					str << item->info()->name();
					
					std::ostringstream str_amount;
					str_amount << item->amount();
					str << '\n' << aux::pad_right(str_amount.str(), 10);
					
					if (trader_item) {
						std::ostringstream std_price;
						std_price << trader_item->price() << " credits";
						str << aux::pad_left(std_price.str(), 12);
					}
						
					if (trader_item) {
						sortkey.assign("+");
					} else {
						sortkey.assign("-");
					}
					
					
				} else {
				
					if (item->amount() == 0) {
						str << "^N";
					}
					
					str << item->info()->name();

					if (item->amount() > 0) {
						std::ostringstream str_amount;
						str_amount << item->amount();
						str << '\n' << aux::pad_right(str_amount.str(), 10);
						
						std::ostringstream std_price;
						std_price << item->price() << " credits";
						str << aux::pad_left(std_price.str(), 12);
						
					} else {
						std::ostringstream std_price;
						std_price << item->price() << " credits";
						str << '\n' << aux::pad_left(std_price.str(), 22);
					}
				
				
					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_font(ui::root()->font_tiny());
				listitem->set_height(listitem->font()->height() * 3.0f);
				listitem->set_item(item);
				listitem->set_info(item->info());
				listitem->set_sortkey(sortkey);
				
				//if (item->amount() == 0) {
				//	listitem->disable();
				//}
				// 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