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