forked from krojew/evernus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggregatedAssetModel.cpp
More file actions
187 lines (160 loc) · 5.56 KB
/
Copy pathAggregatedAssetModel.cpp
File metadata and controls
187 lines (160 loc) · 5.56 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
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QLocale>
#include "EveDataProvider.h"
#include "AssetProvider.h"
#include "ExternalOrder.h"
#include "TextUtils.h"
#include "IconUtils.h"
#include "AssetList.h"
#include "Item.h"
#include "AggregatedAssetModel.h"
namespace Evernus
{
AggregatedAssetModel::AggregatedAssetModel(const AssetProvider &assetProvider,
const EveDataProvider &dataProvider,
QObject *parent)
: QAbstractTableModel{parent}
, mAssetProvider{assetProvider}
, mDataProvider{dataProvider}
{
}
int AggregatedAssetModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return numColumns;
}
QVariant AggregatedAssetModel::data(const QModelIndex &index, int role) const
{
if (Q_UNLIKELY(!index.isValid()))
return {};
const auto &row = mData[index.row()];
const auto column = index.column();
switch (role) {
case Qt::DisplayRole:
switch (column) {
case nameColumn:
return mDataProvider.getTypeName(row.mId);
case quantityColumn:
{
QLocale locale;
return locale.toString(row.mQuantity);
}
case totalValue:
{
QLocale locale;
return TextUtils::currencyToString(row.mTotalValue, locale);
}
}
break;
case Qt::UserRole:
switch (column) {
case nameColumn:
return mDataProvider.getTypeName(row.mId);
case quantityColumn:
return row.mQuantity;
case totalValue:
return row.mTotalValue;
}
break;
case Qt::DecorationRole:
if (column == nameColumn)
return row.mDecoration;
}
return {};
}
QVariant AggregatedAssetModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch (section) {
case nameColumn:
return tr("Name");
case quantityColumn:
return tr("Quantity");
case totalValue:
return tr("Total value");
}
}
return {};
}
int AggregatedAssetModel::rowCount(const QModelIndex &parent) const
{
return (parent.isValid()) ? (0) : (static_cast<int>(mData.size()));
}
void AggregatedAssetModel::setCharacter(Character::IdType id)
{
mCharacterId = id;
}
void AggregatedAssetModel::setCustomStation(quint64 id)
{
mCustomStationId = id;
}
void AggregatedAssetModel::setCombineCharacters(bool flag)
{
mCombineCharacters = flag;
}
bool AggregatedAssetModel::isCombiningCharacters() const
{
return mCombineCharacters;
}
void AggregatedAssetModel::reset()
{
beginResetModel();
mData.clear();
ItemMap items;
if (mCombineCharacters)
{
const auto assets = mAssetProvider.fetchAllAssets();
for (const auto &list : assets)
fillAssets(list, items);
}
else if (Q_LIKELY(mCharacterId != Character::invalidId))
{
fillAssets(mAssetProvider.fetchAssetsForCharacter(mCharacterId), items);
}
mData.reserve(items.size());
for (auto &item : items)
{
item.second.mId = item.first;
mData.emplace_back(std::move(item.second));
}
endResetModel();
}
void AggregatedAssetModel::fillAssets(const std::shared_ptr<AssetList> &assets, ItemMap &map) const
{
for (const auto &item : *assets)
{
auto itemLocationId = item->getLocationId();
if (!itemLocationId)
itemLocationId = 0;
buildItemMap(*item, map, (mCustomStationId == 0) ? (*itemLocationId) : (mCustomStationId));
}
}
void AggregatedAssetModel::buildItemMap(const Item &item, ItemMap &map, quint64 locationId) const
{
const auto typeId = item.getTypeId();
const auto sellPrice = (item.isBPC()) ? (ExternalOrder::nullOrder()) : (mDataProvider.getTypeStationSellPrice(typeId, locationId));
const auto quantity = item.getQuantity();
const auto customValue = item.getCustomValue();
const auto price = (customValue) ? (*customValue) : (sellPrice->getPrice());
auto &mappedItem = map[typeId];
mappedItem.mQuantity += quantity;
mappedItem.mTotalValue += quantity * price;
mappedItem.mDecoration = IconUtils::getIconForMetaGroup(mDataProvider.getTypeMetaGroupName(typeId));
for (const auto &child : item)
buildItemMap(*child, map, locationId);
}
}