From ca03d2741ee3c5976b548870f3a844c7fe62e85f Mon Sep 17 00:00:00 2001 From: Fedja Beader Date: Sun, 11 Feb 2024 03:54:47 +0000 Subject: Total weight sorting --- src/gui/models/sortlistmodelbuy.h | 8 ++++-- src/gui/models/sortlistmodelinv.h | 8 ++++-- src/gui/widgets/itemcontainer.cpp | 54 ++++++++++++++++++++++++++++++++------- src/gui/windows/buydialog.cpp | 26 ++++++++++++++++++- 4 files changed, 82 insertions(+), 14 deletions(-) diff --git a/src/gui/models/sortlistmodelbuy.h b/src/gui/models/sortlistmodelbuy.h index 2b2f56d12..19ef70cc6 100644 --- a/src/gui/models/sortlistmodelbuy.h +++ b/src/gui/models/sortlistmodelbuy.h @@ -28,7 +28,9 @@ #include "utils/gettext.h" -static const char *const SORT_NAME_BUY[7] = +#define NUM_ELEMENTS(a) sizeof(a) / sizeof(a[0]) + +static const char *const SORT_NAME_BUY[8] = { // TRANSLATORS: buy dialog sort type. N_("unsorted"), @@ -40,6 +42,8 @@ static const char *const SORT_NAME_BUY[7] = N_("by id"), // TRANSLATORS: buy dialog sort type. N_("by weight"), + // TRANSLATORS: inventory sort mode + N_("by total weight"), // TRANSLATORS: buy dialog sort type. N_("by amount"), // TRANSLATORS: buy dialog sort type. @@ -56,7 +60,7 @@ class SortListModelBuy final : public ListModel A_DELETE_COPY(SortListModelBuy) int getNumberOfElements() override final - { return 7; } + { return NUM_ELEMENTS(SORT_NAME_BUY); } std::string getElementAt(int i) override final { diff --git a/src/gui/models/sortlistmodelinv.h b/src/gui/models/sortlistmodelinv.h index e3cd7e9ad..f89e21a8d 100644 --- a/src/gui/models/sortlistmodelinv.h +++ b/src/gui/models/sortlistmodelinv.h @@ -27,7 +27,9 @@ #include "utils/gettext.h" -static const char *const SORT_NAME_INVENTORY[6] = +#define NUM_ELEMENTS(a) sizeof(a) / sizeof(a[0]) + +static const char *const SORT_NAME_INVENTORY[7] = { // TRANSLATORS: inventory sort mode N_("default"), @@ -38,6 +40,8 @@ static const char *const SORT_NAME_INVENTORY[6] = // TRANSLATORS: inventory sort mode N_("by weight"), // TRANSLATORS: inventory sort mode + N_("by total weight"), + // TRANSLATORS: inventory sort mode N_("by amount"), // TRANSLATORS: inventory sort mode N_("by type") @@ -53,7 +57,7 @@ class SortListModelInv final : public ListModel A_DELETE_COPY(SortListModelInv) int getNumberOfElements() override final - { return 6; } + { return NUM_ELEMENTS(SORT_NAME_INVENTORY); } std::string getElementAt(int i) override final { diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp index a88c7798c..af1d24397 100644 --- a/src/gui/widgets/itemcontainer.cpp +++ b/src/gui/widgets/itemcontainer.cpp @@ -96,9 +96,9 @@ namespace if ((item1 == nullptr) || (item2 == nullptr)) return false; - const std::string name1 = item1->getInfo().getName( + const std::string& name1 = item1->getInfo().getName( item1->getColor()); - const std::string name2 = item2->getInfo().getName( + const std::string& name2 = item2->getInfo().getName( item2->getColor()); if (name1 == name2) { @@ -146,9 +146,9 @@ namespace const int w2 = pair2->mItem->getInfo().getWeight(); if (w1 == w2) { - const std::string name1 = + const std::string& name1 = pair1->mItem->getInfo().getName(); - const std::string name2 = + const std::string& name2 = pair2->mItem->getInfo().getName(); if (name1 == name2) { @@ -161,6 +161,38 @@ namespace } } itemWeightInvSorter; + class SortItemWeightTimesAmountFunctor final + { + public: + A_DEFAULT_COPY(SortItemWeightTimesAmountFunctor) + + bool operator() (const ItemIdPair *const pair1, + const ItemIdPair *const pair2) const + { + if ((pair1->mItem == nullptr) || (pair2->mItem == nullptr)) + return false; + + const int w1 = pair1->mItem->getInfo().getWeight() + * pair1->mItem->getQuantity(); + const int w2 = pair2->mItem->getInfo().getWeight() + * pair2->mItem->getQuantity(); + if (w1 == w2) + { + const std::string& name1 = + pair1->mItem->getInfo().getName(); + const std::string& name2 = + pair2->mItem->getInfo().getName(); + if (name1 == name2) + { + return pair1->mItem->getInvIndex() < + pair2->mItem->getInvIndex(); + } + return name1 < name2; + } + return w1 < w2; + } + } itemWeightTimesAmountInvSorter; + class SortItemAmountFunctor final { public: @@ -176,9 +208,9 @@ namespace const int c2 = pair2->mItem->getQuantity(); if (c1 == c2) { - const std::string name1 = + const std::string& name1 = pair1->mItem->getInfo().getName(); - const std::string name2 = + const std::string& name2 = pair2->mItem->getInfo().getName(); if (name1 == name2) { @@ -208,8 +240,8 @@ namespace const ItemDbTypeT t2 = info2.getType(); if (t1 == t2) { - const std::string &name1 = info1.getName(); - const std::string &name2 = info2.getName(); + const std::string& name1 = info1.getName(); + const std::string& name2 = info2.getName(); if (name1 == name2) { return pair1->mItem->getInvIndex() < @@ -1229,9 +1261,13 @@ int ItemContainer::updateMatrix() break; case 4: std::sort(sortedItems.begin(), sortedItems.end(), - itemAmountInvSorter); + itemWeightTimesAmountInvSorter); break; case 5: + std::sort(sortedItems.begin(), sortedItems.end(), + itemAmountInvSorter); + break; + case 6: std::sort(sortedItems.begin(), sortedItems.end(), itemTypeInvSorter); break; diff --git a/src/gui/windows/buydialog.cpp b/src/gui/windows/buydialog.cpp index ac795eb10..c1d6ba958 100644 --- a/src/gui/windows/buydialog.cpp +++ b/src/gui/windows/buydialog.cpp @@ -146,6 +146,27 @@ namespace } } itemWeightBuySorter; + class SortItemWeightTimesAmountFunctor final + { + public: + A_DEFAULT_COPY(SortItemWeightTimesAmountFunctor) + + bool operator() (const ShopItem *const item1, + const ShopItem *const item2) const + { + if ((item1 == nullptr) || (item2 == nullptr)) + return false; + + const int weight1 = item1->getInfo().getWeight() + * item1->getQuantity(); + const int weight2 = item2->getInfo().getWeight() + * item2->getQuantity(); + if (weight1 == weight2) + return item1->getPrice() < item2->getPrice(); + return weight1 < weight2; + } + } itemWeightTimesAmountBuySorter; + class SortItemAmountFunctor final { public: @@ -489,9 +510,12 @@ void BuyDialog::sort() std::sort(items.begin(), items.end(), itemWeightBuySorter); break; case 5: - std::sort(items.begin(), items.end(), itemAmountBuySorter); + std::sort(items.begin(), items.end(), itemWeightTimesAmountBuySorter); break; case 6: + std::sort(items.begin(), items.end(), itemAmountBuySorter); + break; + case 7: std::sort(items.begin(), items.end(), itemTypeBuySorter); break; case 0: -- cgit v1.2.3-60-g2f50