GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/shopitems.cpp Lines: 12 75 16.0 %
Date: 2021-03-17 Branches: 0 76 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/models/shopitems.h"
25
26
#include "resources/item/shopitem.h"
27
28
#include "utils/dtor.h"
29
#include "utils/foreach.h"
30
31
#include "debug.h"
32
33
10
ShopItems::ShopItems(const bool mergeDuplicates,
34
10
                     const std::string &currency) :
35
    ListModel(),
36
    mAllShopItems(),
37
    mShopItems(),
38
    mCurrency(currency),
39
50
    mMergeDuplicates(mergeDuplicates)
40
{
41
10
}
42
43
60
ShopItems::~ShopItems()
44
{
45
10
    clear();
46
20
}
47
48
std::string ShopItems::getElementAt(int i)
49
{
50
    if (i < 0 || CAST_U32(i)
51
        >= CAST_U32(mShopItems.size()) || (mShopItems.at(i) == nullptr))
52
    {
53
        return "";
54
    }
55
56
    return mShopItems.at(i)->getDisplayName();
57
}
58
59
ShopItem *ShopItems::addItem(const int id,
60
                             const ItemTypeT type,
61
                             const ItemColor color,
62
                             const int amount,
63
                             const int price)
64
{
65
    ShopItem *const item = new ShopItem(-1,
66
        id,
67
        type,
68
        color,
69
        amount,
70
        price,
71
        mCurrency);
72
    mShopItems.push_back(item);
73
    mAllShopItems.push_back(item);
74
    return item;
75
}
76
77
ShopItem *ShopItems::addItemNoDup(const int id,
78
                                  const ItemTypeT type,
79
                                  const ItemColor color,
80
                                  const int amount,
81
                                  const int price)
82
{
83
    ShopItem *item = findItem(id, color);
84
    if (item == nullptr)
85
    {
86
        item = new ShopItem(-1,
87
            id,
88
            type,
89
            color,
90
            amount,
91
            price,
92
            mCurrency);
93
        mShopItems.push_back(item);
94
        mAllShopItems.push_back(item);
95
    }
96
    return item;
97
}
98
99
ShopItem *ShopItems::addItem2(const int inventoryIndex,
100
                              const int id,
101
                              const ItemTypeT type,
102
                              const ItemColor color,
103
                              const int quantity,
104
                              const int price)
105
{
106
    ShopItem *item = nullptr;
107
    if (mMergeDuplicates)
108
        item = findItem(id, color);
109
110
    if (item != nullptr)
111
    {
112
        item->addDuplicate(inventoryIndex, quantity);
113
    }
114
    else
115
    {
116
        item = new ShopItem(inventoryIndex,
117
            id,
118
            type,
119
            color,
120
            quantity,
121
            price,
122
            mCurrency);
123
        mShopItems.push_back(item);
124
        mAllShopItems.push_back(item);
125
    }
126
    return item;
127
}
128
129
ShopItem *ShopItems::at(const size_t i) const
130
{
131
    if (i >= mShopItems.size())
132
        return nullptr;
133
134
    return mShopItems.at(i);
135
}
136
137
bool ShopItems::findInAllItems(STD_VECTOR<ShopItem*>::iterator &it,
138
                               const ShopItem *const item)
139
{
140
    const STD_VECTOR<ShopItem*>::iterator it_end = mAllShopItems.end();
141
    for (it = mAllShopItems.begin(); it != it_end; ++ it)
142
    {
143
        if (*it == item)
144
            return true;
145
    }
146
    return false;
147
}
148
149
void ShopItems::erase(const unsigned int i)
150
{
151
    if (i >= CAST_U32(mShopItems.size()))
152
        return;
153
154
    const ShopItem *const item = *(mShopItems.begin() + i);
155
    STD_VECTOR<ShopItem*>::iterator it;
156
    if (findInAllItems(it, item))
157
        mAllShopItems.erase(it);
158
    mShopItems.erase(mShopItems.begin() + i);
159
}
160
161
void ShopItems::del(const unsigned int i)
162
{
163
    if (i >= CAST_U32(mShopItems.size()))
164
        return;
165
166
    ShopItem *item = *(mShopItems.begin() + i);
167
    STD_VECTOR<ShopItem*>::iterator it;
168
    if (findInAllItems(it, item))
169
        mAllShopItems.erase(it);
170
    mShopItems.erase(mShopItems.begin() + i);
171
    delete item;
172
}
173
174
12
void ShopItems::clear()
175
{
176
24
    delete_all(mAllShopItems);
177
24
    mAllShopItems.clear();
178
24
    mShopItems.clear();
179
12
}
180
181
ShopItem *ShopItems::findItem(const int id,
182
                              const ItemColor color) const
183
{
184
    STD_VECTOR<ShopItem*>::const_iterator it = mShopItems.begin();
185
    const STD_VECTOR<ShopItem*>::const_iterator e = mShopItems.end();
186
    while (it != e)
187
    {
188
        ShopItem *const item = *it;
189
        if (item->getId() == id && item->getColor() == color)
190
            return item;
191
192
        ++it;
193
    }
194
195
    return nullptr;
196
}
197
198
void ShopItems::updateList()
199
{
200
    mShopItems.clear();
201
    FOR_EACH (STD_VECTOR<ShopItem*>::iterator, it, mAllShopItems)
202
    {
203
        ShopItem *const item = *it;
204
        if ((item != nullptr) && item->isVisible())
205
            mShopItems.push_back(item);
206
    }
207
}