-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.cpp
353 lines (310 loc) · 10.8 KB
/
items.cpp
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
#include "items.h"
#include <QMessageBox>
#include <QFile>
#include <QDataStream>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QLayout>
#include <QGridLayout>
#include <QDebug>
//money
money::money(){
cu=0;
}
money::money(int mcu, int mag, int mau, int mpt){
cu = mcu+mag*10+mau*100+mpt*1000;
}
bool money::operator <(money x){
return cu<x.cu;
}
money money::operator +(money x){
cu = cu+x.cu;
return *this;
}
money money::operator -(money x){
cu = cu-x.cu;
return *this;
}
money& money::operator =(money x){
if(this!= &x){
cu = x.cu;
}
return *this;
}
money& money::operator =(int x){
cu = x;
return *this;
}
QString money::value(){
QString toreturn="";
QString gp = QObject::tr("gp"), sp = QObject::tr("sp"), cc = QObject::tr("cc");
int Mcu, Mag, Mau;
Mau = cu/100.;
Mag = (cu-Mau*100)/10.;
Mcu = (cu-Mau*100-Mag*10);
QString mau = QString::number(Mau),mag = QString::number(Mag),mcu = QString::number(Mcu);
if(Mcu!=0){
if(Mau!=0)
toreturn += mau + " "+ gp +" " + mag + " " + sp + " ";
else if (Mag!=0)
toreturn += mag + " " + sp + " ";
toreturn += mcu + " "+cc;
}else if (Mag!=0){
if(Mau!=0)
toreturn += mau + " " + gp + " ";
toreturn += mag + " " + sp;
}else{
toreturn += mau + " "+ gp;
}
return toreturn;
}
//item
CQTs_item::CQTs_item() :
price(){
code = type = name = "";
weight = 0;
}
CQTs_item::CQTs_item(QString mycode,QString mytype,QString myname,double myweight, money myprice):
price(myprice){
code = mycode;
type = mytype;
name = myname;
weight = myweight;
}
CQTs_item::CQTs_item(QString mycode,QString mytype,QString myname,double myweight, int mcu, int mag, int mau, int mpt):
price(mcu, mag, mau, mpt){
code = mycode;
type = mytype;
name = myname;
weight = myweight;
}
CQTs_item::CQTs_item(const CQTs_item& copyFrom):
price(copyFrom.price){
code = copyFrom.code;
name = copyFrom.name;
type = copyFrom.type;
weight = copyFrom.weight;
}
money CQTs_item::cost(){return price;}
double CQTs_item::myWeigh(){return weight;}
QString CQTs_item::myID(){return code;}
QString CQTs_item::myName(){return name;}
QString CQTs_item::myType(){return type;}
void CQTs_item::set_cost(money newPrice){price = newPrice;}
void CQTs_item::set_myWeigh(double newWeight){weight = newWeight;}
void CQTs_item::set_myID(QString newCode){code = newCode;}
void CQTs_item::set_myName(QString newName){name = newName;}
void CQTs_item::set_myType(QString newType){type = newType;}
CQTs_item& CQTs_item::operator = (CQTs_item x){
if(this!= &x){
code = x.code;
name = x.name;
type = x.type;
weight = x.weight;
price = x.price;
}
return *this;
}
bool CQTs_item::operator ==(CQTs_item otherItem){
return code == otherItem.code;
}
bool CQTs_item::operator ==(QString otherCode){
return code == otherCode;
}
bool CQTs_item::operator <(CQTs_item otherItem){//alphabetical order per name
return name<otherItem.name;
}
CQTs_item CQTs_item::read_Item(QXmlStreamReader& xmlstream){
QString code="", type="";
double weight=0;
money prize;
if(xmlstream.name()=="item"&&xmlstream.isStartElement()){
code = xmlstream.attributes().value("code").toString();
type = xmlstream.attributes().value("type").toString();
while(!(xmlstream.name()=="item"&&xmlstream.isEndElement())){
xmlstream.readNext();
if(xmlstream.name()=="weight"&&xmlstream.isStartElement()){
while(!xmlstream.readNext()==6);
weight= (xmlstream.text().toDouble());
}
if(xmlstream.name()=="price"&&xmlstream.isStartElement()){
QString change = xmlstream.attributes().value("money").toString();
//if is "sylver" or "copper" change the thing
int mult = 100;
if(change == "sp")
mult = 10;
else if(change == "cp")
mult = 1;
while(!xmlstream.readNext()==6);
prize = mult * (xmlstream.text().toInt());
}//type should be like "maintypesecondarytypespecifications" likw "weapon2h" or "armorweaponmagic"
/*if(type.contains("weapon")){//i will use "weapon1h", "weapon2h", "weapon2hrng", or "weaponarmor" for spiked things
if(xmlstream.name()=="weapon"&&xmlstream.isStartElement()){
//QString wptype = xmlstream.attributes().value("wptype").toString();
QString damage = xmlstream.attributes().value("damage").toString();
QString crit = xmlstream.attributes().value("critical").toString();
QString damageType = xmlstream.attributes().value("damagetype").toString();
if(type.contains("rng"))
int range = xmlstream.attributes().value("rangeinc").toInt();;//number of squares (translation friendly! :) )
}
}
if(type.contains("armor")){
if(xmlstream.name()=="armor"&&xmlstream.isStartElement()){
int AC = xmlstream.attributes().value("AC").toInt();
int dex = xmlstream.attributes().value("dexmax").toInt();
int arcanefail = xmlstream.attributes().value("arcane").toInt();
int armorcheck = xmlstream.attributes().value("checkpenalty").toInt();
bool limitspeed = xmlstream.attributes().value("speed").toInt();
}
}
if(type.contains("shield")){
if(xmlstream.name()=="shield"&&xmlstream.isStartElement()){
int AC = xmlstream.attributes().value("AC").toInt();
int dex = xmlstream.attributes().value("dexmax").toInt();
int arcanefail = xmlstream.attributes().value("arcane").toInt();
int armorcheck = xmlstream.attributes().value("checkpenalty").toInt();
}
}*/
}
}
return CQTs_item(code,type,code,weight,prize);
}
//Handler
CQTs_itemsHandler::CQTs_itemsHandler(QStringList filesData, QStringList filesNames, QObject *parent) :
QObject(parent)
{
loadFromFile(filesData);
loadNamesFromFiles(filesNames);
}
void CQTs_itemsHandler::loadFromFile(QStringList filesData){
for (int i = 0; i < filesData.size(); ++i) {
QFile file(filesData[i]);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
QMessageBox::information(0, QString("Information"), (QString(tr("Failed to load items from "))+filesData[i]), QMessageBox::Ok);
}else{
QXmlStreamReader xml(&file);
while(!(xml.name()=="items"&&xml.isStartElement())){
xml.readNext();
}
while(!(xml.name()=="items"&&xml.isEndElement())){
CQTs_item tItem = CQTs_item::read_Item(xml);
if(!(tItem == ""))
items.append(tItem);
xml.readNext();
}
if(xml.hasError()){
qDebug() <<xml.errorString();
qDebug() << xml.lineNumber() << xml.columnNumber();
break;
}
xml.readNext();
//
}
}
}
void CQTs_itemsHandler::loadNamesFromFiles(QStringList filesNames){
for (int i = 0; i < filesNames.size(); ++i) {
QFile file(filesNames[i]);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
QMessageBox::information(0, QString("Information"), (QString(tr("Failed to load item names from "))+filesNames[i]), QMessageBox::Ok);
}else{
QXmlStreamReader xml(&file);
}
}
}
int CQTs_itemsHandler::itemsNum(){return items.size();}
QString CQTs_itemsHandler::getName(int i){
if(i> items.size())
return "error";
return items[i].myName();}
QString CQTs_itemsHandler::getCost(int i){
if(i> items.size())
return "-1";
return items[i].cost().value();}
double CQTs_itemsHandler::getWeight(int i){
if(i> items.size())
return 0.;
return items[i].myWeigh();}
CQTs_item CQTs_itemsHandler::getItem(int i){
if(i> items.size())
return CQTs_item("error");
return items[i];
}
//bag thinked to be used by a pg
CQTs_bag::CQTs_bag(CQTs_item myID, QWidget *parent):
QWidget(parent),bagID(myID)
{
setWindowTitle(bagID.myName());
QVBoxLayout *mainLay = new QVBoxLayout();
setLayout(mainLay);
viewItems = new QListWidget();
//viewItems->setMovement(QListView::Free);
mainLay->addWidget(viewItems);
QLayout *tLay = new QHBoxLayout();
tLay ->addWidget(new QLabel("Weight inside:"));
tLay ->addWidget(labTotalWeight = new QLabel("0"));
mainLay->addLayout(tLay);
}
void CQTs_bag::put_inside(CQTs_item newItem){
if(!(newItem =="error"||newItem =="")){
int from = inside.size();
while(inside.contains(newItem)){//"protection" from copies
newItem.set_myName(newItem.myName()+"*"); //the * is temporary
newItem.set_myID(newItem.myID()+"*");
}
inside.append(newItem);
update(from);
}
}
void CQTs_bag::put_inside(QList<CQTs_item> newItems){
int from = inside.size();
inside.append(newItems);
update(from);
}
void CQTs_bag::update(int from){
for (int i = from; i < inside.size(); ++i) {
viewItems->insertItem(i,inside[i].myName());
}
uptateWeight();
}
double CQTs_bag::totalWeight(){
double newWeight = 0;
for (int i = 0; i < inside.size(); ++i) {
newWeight+=inside[i].myWeigh();
}
return newWeight;
}
void CQTs_bag::uptateWeight(){
labTotalWeight->setNum(totalWeight());
}
//viewer thinked to show ALL the items in a itemhandler
CQTs_ItemViewer::CQTs_ItemViewer(QWidget *parent):
QWidget(parent)
{
QScrollArea *Scroll = new QScrollArea();
QLayout *tlay = new QHBoxLayout();
tlay->addWidget(Scroll);
setLayout(tlay);
QStringList Items, Names;
#ifdef __WIN32
Items.push_back("goods.xml");
#else
Items.push_back("/home/iximiel/ProgettiQt/CharaQTersheet/xml/goods.xml");
#endif
itemsHandler = new CQTs_itemsHandler(Items,Names);
QWidget *toScroll = new QWidget();
QGridLayout *grid = new QGridLayout();
int row = 0;//number of row for legend
for (int i = 0; i < itemsHandler->itemsNum(); ++i) {
QLabel* tLab = new QLabel(itemsHandler->getName(i));
tLab->setMaximumHeight(15);
grid->addWidget(tLab,i+row,0);
tLab = new QLabel(itemsHandler->getCost(i));
grid->addWidget(tLab,i+row,1);
tLab = new QLabel(QString::number(itemsHandler->getWeight(i)));
grid->addWidget(tLab,i+row,2);
}
toScroll->setLayout(grid);
Scroll->setWidget(toScroll);
Scroll->setWidgetResizable(true);
}