-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTradeTableModel.cpp
171 lines (155 loc) · 5.36 KB
/
TradeTableModel.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
#include "TradeTableModel.h"
#include "HierarchicalHeaderView.h"
TradeTableModel::TradeTableModel(const Galaxy *galaxy, QObject *parent) :
QAbstractTableModel(parent),_galaxy(galaxy)
{
fillHeaderModel();
}
int TradeTableModel::rowCount(const QModelIndex &parent) const
{
return _galaxy->marketsCount();
}
int TradeTableModel::columnCount(const QModelIndex &parent) const
{
return 31;//3+8*3+4
}
QVariant TradeTableModel::data(const QModelIndex &index, int role) const
{
if(role==HierarchicalHeaderView::HorizontalHeaderDataRole)
{
QVariant v;
v.setValue((QObject*)&_horizontalHeaderModel);
return v;
}
if (role == Qt::DisplayRole)
{
int col=index.column();
if(col==0)
{
return _galaxy->marketName(index.row());
}
else if(col==1)
{
return _galaxy->marketStarName(index.row());
}
else if(col==2)
{
return std::round(_galaxy->marketDistFromPlayer(index.row())*10.0)/10.0;
}
else if(col==27)
{
return _galaxy->marketPlanetSize(index.row());
}
else if(col==28)
{
return _galaxy->marketPlanetTechLevel(index.row());
}
else if(col==29)
{
return _galaxy->marketPlanetEconomy(index.row());
}
else if(col==30)
{
return _galaxy->marketPlanetOwner(index.row());
}
else
{
int quantSellBuy=(col-3)%3;
int goodNum=(col-3)/3;
switch(quantSellBuy)
{
case 0://quantity is requested
return _galaxy->marketQuantity(index.row())[goodNum];
break;
case 1://sale price is requested
return _galaxy->marketSale(index.row())[goodNum];
break;
case 2://buy price is requested
return _galaxy->marketBuy(index.row())[goodNum];
break;
}
}
return index.row()+index.column();
}
if (role == Qt::BackgroundColorRole)
{
int col=index.column();
if(col>2 && col<27)
{
int quantSellBuy=(col-3)%3;
if(quantSellBuy==0){
return QVariant();
}
int goodNum=(col-3)/3;
int maxBuyPrice=_galaxy->maxBuyPrice()[goodNum];
int minSellPrice=_galaxy->minSellPrice()[goodNum];
int quantity=_galaxy->marketQuantity(index.row())[goodNum];
int scale=-1;
if(quantSellBuy==1) {
int price=_galaxy->marketSale(index.row())[goodNum];
if(price>maxBuyPrice || quantity==0) {
return QVariant();
}
scale=255+255.0*(minSellPrice-price)/(maxBuyPrice-minSellPrice);
return QColor(0, 163, 22,std::min(scale,255));
}
else if(quantSellBuy==2) {
int price=_galaxy->marketBuy(index.row())[goodNum];
if(price<minSellPrice) {
return QVariant();
}
scale=255.0*(price-minSellPrice)/(maxBuyPrice-minSellPrice);
return QColor(0, 116, 224,std::min(scale,255));
}
}
return QVariant();
}
return QVariant();
}
QVariant TradeTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
static const QVector<QString> header={tr("Name"),tr("Star"),tr("Dist."),
tr("Fq"), tr("Fs"), tr("Fb"),
tr("Mq"), tr("Ms"), tr("Mb"),
tr("Aq"), tr("As"), tr("Ab"),
tr("Sq"), tr("Ss"), tr("Sb"),
tr("Lq"), tr("Ls"), tr("Lb"),
tr("Tq"), tr("Ts"), tr("Tb"),
tr("Wq"), tr("Ws"), tr("Wb"),
tr("Dq"), tr("Ds"), tr("Db"),
tr("Size"),tr("Tech"),tr("Type"),tr("Owner")};
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Vertical)
{
return _galaxy->marketId(section);
}
else if (orientation == Qt::Horizontal) {
return header.at(section);
}
}
return QVariant();
}
void TradeTableModel::fillHeaderModel()
{
_horizontalHeaderModel.setItem(0, 0, new QStandardItem(tr("Name")));
_horizontalHeaderModel.setItem(0, 1, new QStandardItem(tr("Star")));
_horizontalHeaderModel.setItem(0, 2, new QStandardItem(tr("Dist.")));
static const QVector<QString> products={tr("Food"),tr("Meds"),tr("Alcohol"),tr("Minerals"),
tr("Luxury"),tr("Technics"),tr("Weapons"),tr("Drugs")};
for(int prod=0; prod<8; prod++)
{
QStandardItem* rootItem = new QStandardItem(products.at(prod));
QList<QStandardItem*> l;
l.push_back(new QStandardItem("#"));
rootItem->appendColumn(l);
l.clear();
l.push_back(new QStandardItem("S"));
rootItem->appendColumn(l);
l.clear();
l.push_back(new QStandardItem("B"));
rootItem->appendColumn(l);
l.clear();
_horizontalHeaderModel.setItem(0, prod+3, rootItem);
}
}