-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathInventoryWidget.cpp
More file actions
364 lines (306 loc) · 9.76 KB
/
InventoryWidget.cpp
File metadata and controls
364 lines (306 loc) · 9.76 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "InventoryWidget.h"
#include <iostream>
#include <fstream>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include "EditWidget.h"
InventoryWidget::InventoryWidget()
: QWidget()
{
//Setup widgets.
inventoryTableView = new QTableView();
inventoryTableView->verticalHeader()->hide();
inventoryTableView->horizontalHeader()->setStretchLastSection(true);
inventoryTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
inventoryTableView->setSelectionMode(QAbstractItemView::SingleSelection);
inventoryTableView->setEditTriggers(QTableView::NoEditTriggers);
inventoryTableView->setModel(&model);
inventoryTableView->setTabKeyNavigation(false);
connect(inventoryTableView->selectionModel(),
SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
SLOT(SelectedItem(const QItemSelection&,const QItemSelection&)));
connect(inventoryTableView,SIGNAL(doubleClicked(const QModelIndex&)),SLOT(NewItem()));
editWidget = new EditWidget();
QHBoxLayout* topLayout = new QHBoxLayout();
topLayout->addWidget(inventoryTableView,1);
topLayout->addWidget(editWidget);
setLayout(topLayout);
setEnabled(false);
//Setup events.
connect(editWidget,SIGNAL(NewItem()),SLOT(NewItem()));
connect(editWidget,SIGNAL(NewStoneItem()),SLOT(NewStoneItem()));
connect(editWidget,SIGNAL(NewWoodItem()),SLOT(NewWoodItem()));
connect(editWidget,SIGNAL(NewGlassItem()),SLOT(NewGlassItem()));
connect(editWidget,SIGNAL(NewTorchesItem()),SLOT(NewTorchesItem()));
connect(editWidget,SIGNAL(DeleteItem()),SLOT(DeleteItem()));
connect(editWidget,SIGNAL(UpdateItem()),SLOT(UpdateItem()));
}
void InventoryWidget::SetInventoryTag(NBT::Tag* toInventoryTag)
{
inventoryTag = toInventoryTag;
itemMap.clear();
setEnabled(inventoryTag != NULL);
InventoryTagToItemMap(inventoryTag,itemMap);
ReloadTableModel();
}
void InventoryWidget::ImportInventory(const char* filePath)
{
std::ifstream inFile(filePath);
if(!inFile.good())
{
QMessageBox::critical(NULL,"Unable to import inventory","Unable to read file. Do you have proper permissions?");
return;
}
//Clear existing items.
itemMap.clear();
//Read each line of csv file and split column groups of 4 into items. Ordered by slot, id, damage, and count.
while(inFile.good())
{
//Read a line.
string line;
std::getline(inFile,line);
//Split line by commas into 4 fields that make up the item.
vector<string> columns;
boost::split(columns,line,boost::algorithm::is_any_of(","),boost::token_compress_on);
if(columns.size() != 4)
continue;
//Try to turn text columns into item.
try
{
Item newItem;
newItem.slot = boost::numeric_cast<unsigned char>(boost::lexical_cast<short>(columns[0]));
newItem.id = boost::lexical_cast<short>(columns[1]);
newItem.damage = boost::lexical_cast<short>(columns[2]);
newItem.count = boost::numeric_cast<unsigned char>(boost::lexical_cast<short>(columns[3]));
itemMap[newItem.slot] = newItem;
}
catch(...)
{
//Ignore invalid item.
}
}
//Update table.
ReloadTableModel();
}
void InventoryWidget::ExportInventory(const char* filePath)
{
std::ofstream outFile(filePath);
if(!outFile.good())
{
QMessageBox::critical(NULL,"Unable to export inventory","Unable to write file. Do you have proper permissions?");
return;
}
BOOST_FOREACH(const ItemMap::value_type& itemIter,itemMap)
{
const Item& item = itemIter.second;
outFile << (int)item.slot << "," << (int)item.id << "," << (int)item.damage << "," << (int)item.count << std::endl;
}
}
void InventoryWidget::SaveChangesToInventoryTag()
{
if(inventoryTag == NULL)
return;
ItemMapToInventoryTag(itemMap,inventoryTag);
}
void InventoryWidget::ReloadFromInventoryTag()
{
SetInventoryTag(inventoryTag);
}
void InventoryWidget::NewItem()
{
CreateNewItem(1,0,1); //Stone, x1
}
void InventoryWidget::NewStoneItem()
{
CreateNewItem(1,0,64); //Stone, x64
}
void InventoryWidget::NewWoodItem()
{
CreateNewItem(5,0,64); //Wood, x64
}
void InventoryWidget::NewGlassItem()
{
CreateNewItem(20,0,64); //Glass, x64
}
void InventoryWidget::NewTorchesItem()
{
CreateNewItem(50,0,64); //Torch, x64
}
void InventoryWidget::DeleteItem()
{
//Find the selected row.
int selectedRow = 0;
unsigned char slot = 0;
if(!GetSelectedItem(selectedRow,slot))
return;
//Find the item that belongs on that row.
ItemMap::iterator itemIter = itemMap.find(slot);
if(itemIter == itemMap.end())
return;
//Delete item.
itemMap.erase(itemIter);
//Update table.
delete model.takeItem(selectedRow,1);
delete model.takeItem(selectedRow,2);
delete model.takeItem(selectedRow,3);
//Keep the row selected even after deleting the items.
inventoryTableView->selectRow(selectedRow);
}
void InventoryWidget::UpdateItem()
{
//Find the selected row.
int selectedRow = 0;
unsigned char slot = 0;
if(!GetSelectedItem(selectedRow,slot))
return;
//Find the item that belongs on that row.
ItemMap::iterator itemIter = itemMap.find(slot);
if(itemIter == itemMap.end())
return;
Item& item = itemIter->second;
//Update item.
if(!editWidget->GetItemInfo(item))
return;
//Update table.
QStandardItem* typeItem = model.item(selectedRow,1);
typeItem->setText(QString(ItemTypeName(item.id).c_str()));
QStandardItem* damageItem = model.item(selectedRow,2);
damageItem->setText(QString::number(item.damage));
QStandardItem* countItem = model.item(selectedRow,3);
countItem->setText(QString::number(item.count));
}
void InventoryWidget::CopyItem()
{
//Find the selected row.
int selectedRow = 0;
unsigned char slot = 0;
if(!GetSelectedItem(selectedRow,slot))
return;
//Find the item that belongs on that row.
ItemMap::iterator itemIter = itemMap.find(slot);
if(itemIter == itemMap.end())
return;
copiedItem = boost::shared_ptr<Item>(new Item(itemIter->second));
}
void InventoryWidget::PasteItem()
{
//An item must have been previously copied.
if(!copiedItem)
return;
//Find the selected row.
int selectedRow = 0;
unsigned char slot = 0;
if(!GetSelectedItem(selectedRow,slot))
return;
//Update item. If it doesn't exist, create it.
Item newItem = *copiedItem;
newItem.slot = slot;
itemMap[slot] = newItem;
//Update table.
model.setItem(selectedRow,1,new QStandardItem(ItemTypeName(newItem.id).c_str()));
model.setItem(selectedRow,2,new QStandardItem(QString::number(newItem.damage)));
model.setItem(selectedRow,3,new QStandardItem(QString::number(newItem.count)));
//Prevent row from deselecting after model change.
inventoryTableView->selectRow(selectedRow);
//Update edit widget to show the copied item.
editWidget->SetItem(&newItem);
}
void InventoryWidget::SelectedItem(const QItemSelection& selected,const QItemSelection&)
{
QList<QModelIndex> indexes = selected.indexes();
if(indexes.isEmpty())
{
SelectItem(NULL);
return;
}
QStandardItem* selectedSlotItem = model.item(indexes.first().row());
if(selectedSlotItem == NULL)
SelectItem(NULL);
else
{
const unsigned int slot = selectedSlotItem->data().toUInt();
SelectItem(&slot);
}
}
void InventoryWidget::ReloadTableModel()
{
//Remove any existing rows.
model.removeRows(0,model.rowCount());
//Insert each item slot into table and fill out columns for row if item exists.
SlotNameBimap slotNameBimap = CreateSlotNameBimap();
model.setHorizontalHeaderLabels(
QStringList() << "Slot" << "Type" << "Damage" << "Count");
for(SlotNameBimap::const_iterator slotIter = slotNameBimap.begin();
slotIter != slotNameBimap.end();
++slotIter)
{
QList<QStandardItem*> rowItems;
QStandardItem* slotItem = new QStandardItem(slotIter->right.c_str());
slotItem->setData(QVariant((unsigned int)slotIter->left));
rowItems.append(slotItem);
//If the item exists, fill out the remaining cells.
map<unsigned char,Item>::const_iterator itemIter = itemMap.find(slotIter->left);
if(itemIter != itemMap.end())
{
const Item& item = (*itemIter).second;
rowItems.append(new QStandardItem(ItemTypeName(item.id).c_str()));
rowItems.append(new QStandardItem(QString::number(item.damage)));
rowItems.append(new QStandardItem(QString::number(item.count)));
}
model.appendRow(rowItems);
}
}
void InventoryWidget::SelectItem(const unsigned int* slot)
{
if(slot == NULL)
editWidget->SetItem(NULL);
else
{
ItemMap::const_iterator itemIter = itemMap.find(*slot);
if(itemIter != itemMap.end())
editWidget->SetItem(&(*itemIter).second);
else
editWidget->SetItem(NULL);
}
}
bool InventoryWidget::GetSelectedItem(int& selectedRow,unsigned char& slot)
{
//Find the selected row in table.
QModelIndexList rowsList = inventoryTableView->selectionModel()->selectedRows();
if(rowsList.empty())
return false;
selectedRow = rowsList[0].row();
//Find slot of item that belongs on that row.
QStandardItem* slotItem = model.item(selectedRow,0);
slot = slotItem->data().toUInt();
return true;
}
void InventoryWidget::CreateNewItem(const short type,const short damage,const unsigned char count)
{
//Find the selected row.
QModelIndexList rowsList = inventoryTableView->selectionModel()->selectedRows();
if(rowsList.empty())
return;
const int selectedRow = rowsList[0].row();
//Make sure item doesn't already exist.
QStandardItem* slotItem = model.item(selectedRow,0);
const unsigned char slot = slotItem->data().toUInt();
ItemMap::iterator itemIter = itemMap.find(slot);
if(itemIter != itemMap.end())
return;
//Create item.
Item newItem;
newItem.slot = slot;
newItem.id = type;
newItem.damage = damage;
newItem.count = count;
itemMap[slot] = newItem;
//Update table.
model.setItem(selectedRow,1,new QStandardItem(ItemTypeName(newItem.id).c_str()));
model.setItem(selectedRow,2,new QStandardItem(QString::number(newItem.damage)));
model.setItem(selectedRow,3,new QStandardItem(QString::number(newItem.count)));
//Switch from "new" to "edit" mode.
editWidget->SetItem(&newItem);
}