forked from krojew/evernus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetModel.cpp
More file actions
544 lines (451 loc) · 16.8 KB
/
Copy pathAssetModel.cpp
File metadata and controls
544 lines (451 loc) · 16.8 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/**
* 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 <QSettings>
#include <QLocale>
#include <QColor>
#include <QFont>
#include <QIcon>
#include "EveDataProvider.h"
#include "AssetProvider.h"
#include "PriceSettings.h"
#include "ExternalOrder.h"
#include "AssetList.h"
#include "IconUtils.h"
#include "TextUtils.h"
#include "AssetModel.h"
namespace Evernus
{
void AssetModel::TreeItem::appendChild(std::unique_ptr<TreeItem> child)
{
child->mParentItem = this;
mChildItems.emplace_back(std::move(child));
}
void AssetModel::TreeItem::clearChildren() noexcept
{
mChildItems.clear();
}
AssetModel::TreeItem *AssetModel::TreeItem::child(int row) const noexcept
{
return (row >= static_cast<int>(mChildItems.size())) ? (nullptr) : (mChildItems[row].get());
}
int AssetModel::TreeItem::childCount() const noexcept
{
return static_cast<int>(mChildItems.size());
}
int AssetModel::TreeItem::columnCount() const
{
return mItemData.count();
}
QVariant AssetModel::TreeItem::data(int column) const
{
return mItemData.value(column);
}
QVariantList AssetModel::TreeItem::data() const
{
return mItemData;
}
void AssetModel::TreeItem::setData(const QVariantList &data)
{
mItemData = data;
}
void AssetModel::TreeItem::addData(const QVariant &data)
{
mItemData << data;
}
QDateTime AssetModel::TreeItem::priceTimestamp() const
{
return mPriceTimestamp;
}
void AssetModel::TreeItem::setPriceTimestamp(const QDateTime &dt)
{
mPriceTimestamp = dt;
}
AssetModel::LocationId AssetModel::TreeItem::locationId() const noexcept
{
return mLocationId;
}
void AssetModel::TreeItem::setLocationId(LocationId id) noexcept
{
mLocationId = id;
}
ItemData::TypeIdType AssetModel::TreeItem::typeId() const noexcept
{
return mTypeId;
}
void AssetModel::TreeItem::setTypeId(ItemData::TypeIdType id) noexcept
{
mTypeId = id;
}
Character::IdType AssetModel::TreeItem::ownerId() const noexcept
{
return mOwnerId;
}
void AssetModel::TreeItem::setOwnerId(Character::IdType id) noexcept
{
mOwnerId = id;
}
QVariant AssetModel::TreeItem::decoration() const
{
return mDecoration;
}
void AssetModel::TreeItem::setDecoration(const QVariant &data)
{
mDecoration = data;
}
AssetModel::CustomValueType AssetModel::TreeItem::customValue() const
{
return mCustomValue;
}
void AssetModel::TreeItem::setCustomValue(CustomValueType value)
{
mCustomValue = std::move(value);
}
Item::IdType AssetModel::TreeItem::id() const noexcept
{
return mId;
}
void AssetModel::TreeItem::setId(Item::IdType id) noexcept
{
mId = id;
}
int AssetModel::TreeItem::row() const noexcept
{
if (mParentItem != nullptr)
{
auto row = 0;
for (const auto &child : mParentItem->mChildItems)
{
if (child.get() == this)
return row;
++row;
}
}
return 0;
}
AssetModel::TreeItem *AssetModel::TreeItem::parent() const noexcept
{
return mParentItem;
}
AssetModel::AssetModel(const AssetProvider &assetProvider,
const EveDataProvider &nameProvider,
bool showOwner,
QObject *parent)
: QAbstractItemModel(parent)
, mAssetProvider(assetProvider)
, mDataProvider(nameProvider)
{
mRootItem.setData(QVariantList{}
<< tr("Name")
<< tr("Quantity")
<< tr("Unit volume")
<< tr("Total volume")
<< tr("Local unit sell price")
<< tr("Custom value")
<< tr("Local total sell price"));
if (showOwner)
mRootItem.addData(tr("Owner"));
connect(&mDataProvider, &EveDataProvider::namesChanged, this, &AssetModel::updateNames);
}
int AssetModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<const TreeItem *>(parent.internalPointer())->columnCount();
else
return mRootItem.columnCount();
}
QVariant AssetModel::data(const QModelIndex &index, int role) const
{
if (Q_UNLIKELY(!index.isValid()))
return QVariant{};
auto item = static_cast<const TreeItem *>(index.internalPointer());
const auto column = index.column();
QLocale locale;
switch (role) {
case Qt::DecorationRole:
if (column == typeColumn)
return item->decoration();
break;
case Qt::UserRole:
if (column == ownerColumn)
{
const auto id = item->data(ownerColumn).value<Character::IdType>();
return (id == Character::invalidId) ? (QVariant{}) : (mDataProvider.getGenericName(id));
}
return item->data(column);
case Qt::DisplayRole:
switch (column) {
case quantityColumn:
return locale.toString(item->data(quantityColumn).toUInt());
case unitVolumeColumn:
if (item->parent() != &mRootItem)
return QString{"%1m³"}.arg(locale.toString(item->data(unitVolumeColumn).toDouble(), 'f', 2));
break;
case totalVolumeColumn:
return QString{"%1m³"}.arg(locale.toString(item->data(totalVolumeColumn).toDouble(), 'f', 2));
case unitPriceColumn:
if (item->parent() != &mRootItem)
return TextUtils::currencyToString(item->data(unitPriceColumn).toDouble(), locale);
break;
case customValueColumn:
{
const auto value = item->data(customValueColumn);
if (!value.isNull())
return TextUtils::currencyToString(value.toDouble(), locale);
}
break;
case totalPriceColumn:
return TextUtils::currencyToString(item->data(totalPriceColumn).toDouble(), locale);
case ownerColumn:
{
const auto id = item->data(ownerColumn).value<Character::IdType>();
return (id == Character::invalidId) ? (QVariant{}) : (mDataProvider.getGenericName(id));
}
}
return item->data(column);
case Qt::FontRole:
if (item->parent() == &mRootItem)
{
QFont font;
font.setBold(true);
return font;
}
return QFont{};
case Qt::BackgroundRole:
if ((column == unitPriceColumn || column == totalPriceColumn) && item->parent() != &mRootItem)
{
QSettings settings;
const auto maxPriceAge = settings.value(PriceSettings::priceMaxAgeKey, PriceSettings::priceMaxAgeDefault).toInt();
if (item->priceTimestamp() < QDateTime::currentDateTimeUtc().addSecs(-3600 * maxPriceAge))
return QColor{255, 255, 192};
}
break;
case Qt::ToolTipRole:
if ((column == unitPriceColumn || column == totalPriceColumn) && item->parent() != &mRootItem)
return tr("Price update time: %1").arg(TextUtils::dateTimeToString(item->priceTimestamp().toLocalTime(), locale));
}
return QVariant{};
}
QVariant AssetModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return mRootItem.data(section);
return QVariant{};
}
QModelIndex AssetModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex{};
const TreeItem *parentItem = nullptr;
if (!parent.isValid())
parentItem = &mRootItem;
else
parentItem = static_cast<const TreeItem *>(parent.internalPointer());
auto childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
return QModelIndex{};
}
QModelIndex AssetModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex{};
auto childItem = static_cast<const TreeItem *>(index.internalPointer());
auto parentItem = childItem->parent();
if (parentItem == &mRootItem)
return QModelIndex{};
return createIndex(parentItem->row(), 0, parentItem);
}
int AssetModel::rowCount(const QModelIndex &parent) const
{
const TreeItem *parentItem = nullptr;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = &mRootItem;
else
parentItem = static_cast<const TreeItem *>(parent.internalPointer());
return parentItem->childCount();
}
void AssetModel::setCharacter(Character::IdType id)
{
mCharacterId = id;
}
void AssetModel::setCustomStation(quint64 id)
{
mCustomStationId = id;
}
void AssetModel::setCombineCharacters(bool flag)
{
mCombineCharacters = flag;
}
bool AssetModel::isCombiningCharacters() const
{
return mCombineCharacters;
}
void AssetModel::reset()
{
beginResetModel();
mRootItem.clearChildren();
mLocationItems.clear();
mTotalAssets = 0;
mTotalVolume = mTotalSellPrice = 0.;
if (mCombineCharacters)
{
const auto assets = mAssetProvider.fetchAllAssets();
for (const auto &list : assets)
fillAssets(list);
}
else if (Q_LIKELY(mCharacterId != Character::invalidId))
{
fillAssets(mAssetProvider.fetchAssetsForCharacter(mCharacterId));
}
endResetModel();
}
uint AssetModel::getTotalAssets() const noexcept
{
return mTotalAssets;
}
double AssetModel::getTotalVolume() const noexcept
{
return mTotalVolume;
}
double AssetModel::getTotalSellPrice() const noexcept
{
return mTotalSellPrice;
}
AssetModel::LocationId AssetModel::getAssetLocationId(const QModelIndex &index) const
{
const auto item = static_cast<const TreeItem *>(index.internalPointer());
return (item != nullptr) ? (item->locationId()) : (LocationId{});
}
ItemData::TypeIdType AssetModel::getAssetTypeId(const QModelIndex &index) const
{
const auto item = static_cast<const TreeItem *>(index.internalPointer());
return (item != nullptr) ? (item->typeId()) : (ItemData::TypeIdType{});
}
Character::IdType AssetModel::getAssetOwnerId(const QModelIndex &index) const
{
const auto item = static_cast<const TreeItem *>(index.internalPointer());
return (item != nullptr) ? (item->ownerId()) : (Character::IdType{});
}
AssetModel::CustomValueType AssetModel::getAssetCustomValue(const QModelIndex &index) const
{
const auto item = static_cast<const TreeItem *>(index.internalPointer());
return (item != nullptr) ? (item->customValue()) : (CustomValueType{});
}
Item::IdType AssetModel::getAssetId(const QModelIndex &index) const
{
const auto item = static_cast<const TreeItem *>(index.internalPointer());
return (item != nullptr) ? (item->id()) : (Item::invalidId);
}
void AssetModel::updateNames()
{
std::function<void(TreeItem &, const QModelIndex &, int)> updateOwner = [=, &updateOwner](TreeItem &item, const QModelIndex &parent, int row) {
const auto thisIndex = index(row, ownerColumn, parent);
emit dataChanged(thisIndex, thisIndex, QVector<int>{} << Qt::UserRole << Qt::DisplayRole);
const auto childCount = item.childCount();
for (auto i = 0; i < childCount; ++i)
updateOwner(*item.child(i), index(row, 0, parent), i);
};
updateOwner(mRootItem, QModelIndex{}, 0);
}
void AssetModel::buildItemMap(const Item &item, TreeItem &treeItem, LocationId locationId, Character::IdType ownerId)
{
for (const auto &child : item)
{
auto childItem = createTreeItemForItem(*child, locationId, ownerId);
buildItemMap(*child, *childItem, locationId, ownerId);
treeItem.appendChild(std::move(childItem));
}
}
std::unique_ptr<AssetModel::TreeItem> AssetModel
::createTreeItemForItem(const Item &item, LocationId locationId, Character::IdType ownerId)
{
const auto typeId = item.getTypeId();
const auto volume = mDataProvider.getTypeVolume(typeId);
const auto quantity = item.getQuantity();
const auto sellPrice = (item.isBPC()) ? (ExternalOrder::nullOrder()) : (mDataProvider.getTypeStationSellPrice(typeId, locationId));
const auto metaIcon = IconUtils::getIconForMetaGroup(mDataProvider.getTypeMetaGroupName(typeId));
const auto customValue = item.getCustomValue();
const auto price = (customValue) ? (*customValue) : (sellPrice->getPrice());
mTotalAssets += quantity;
mTotalVolume += volume * quantity;
mTotalSellPrice += price * quantity;
auto treeItem = std::make_unique<TreeItem>();
treeItem->setData(QVariantList{}
<< mDataProvider.getTypeName(typeId)
<< quantity
<< volume
<< (volume * quantity)
<< sellPrice->getPrice()
<< ((customValue) ? (*customValue) : (QVariant{}))
<< (price * quantity)
<< ownerId
);
treeItem->setPriceTimestamp(sellPrice->getUpdateTime());
treeItem->setLocationId(locationId);
treeItem->setTypeId(typeId);
treeItem->setOwnerId(ownerId);
treeItem->setCustomValue(customValue);
treeItem->setId(item.getId());
if (!metaIcon.isNull())
treeItem->setDecoration(metaIcon);
return treeItem;
}
void AssetModel::fillAssets(const std::shared_ptr<AssetList> &assets)
{
for (const auto &item : *assets)
{
auto id = item->getLocationId();
if (!id)
id = LocationId{};
TreeItem *locationItem = nullptr;
const auto it = mLocationItems.find(*id);
if (it == std::end(mLocationItems))
{
auto treeItem = std::make_unique<TreeItem>();
treeItem->setData(QVariantList{}
<< mDataProvider.getLocationName(*id)
<< 0
<< QString{}
<< 0.
<< QString{}
<< QVariant{}
<< 0.
<< Character::invalidId);
treeItem->setLocationId(*id);
locationItem = treeItem.get();
mLocationItems[*id] = locationItem;
mRootItem.appendChild(std::move(treeItem));
}
else
{
locationItem = mLocationItems[*id];
}
const auto locationId = (mCustomStationId == 0) ? (*id) : (mCustomStationId);
const auto curAssets = mTotalAssets;
const auto curVolume = mTotalVolume;
const auto curSellPrice = mTotalSellPrice;
auto treeItem = createTreeItemForItem(*item, locationId, assets->getCharacterId());
buildItemMap(*item, *treeItem, locationId, assets->getCharacterId());
locationItem->appendChild(std::move(treeItem));
auto data = locationItem->data();
data[quantityColumn] = data[quantityColumn].toUInt() + mTotalAssets - curAssets;
data[totalVolumeColumn] = data[totalVolumeColumn].toDouble() + mTotalVolume - curVolume;
data[totalPriceColumn] = data[totalPriceColumn].toDouble() + mTotalSellPrice - curSellPrice;
locationItem->setData(data);
}
}
}