GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/shopitems.h Lines: 4 6 66.7 %
Date: 2017-11-29 Branches: 0 0 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
#ifndef GUI_MODELS_SHOPITEMS_H
24
#define GUI_MODELS_SHOPITEMS_H
25
26
#include "enums/resources/item/itemtype.h"
27
28
#include "enums/simpletypes/itemcolor.h"
29
30
#include "gui/models/listmodel.h"
31
32
#include "utils/cast.h"
33
#include "utils/vector.h"
34
35
#include "localconsts.h"
36
37
class ShopItem;
38
39
/**
40
 * This class handles the list of items available in a shop.
41
 *
42
 * The addItem routine can automatically check, if an item already exists and
43
 * only adds duplicates to the old item, if one is found. The original
44
 * distribution of the duplicates can be retrieved from the item.
45
 *
46
 * This functionality can be enabled in the constructor.
47
 */
48
class ShopItems final : public ListModel
49
{
50
    public:
51
        /**
52
         * Constructor.
53
         *
54
         * @param mergeDuplicates lets the Shop look for duplicate entries and
55
         *                        merges them to one item.
56
         */
57
        ShopItems(const bool mergeDuplicates,
58
                  const std::string &currency);
59
60
        A_DELETE_COPY(ShopItems)
61
62
        ~ShopItems();
63
64
        /**
65
         * Adds an item to the list.
66
         */
67
        ShopItem *addItem(const int id,
68
                          const ItemTypeT type,
69
                          const ItemColor color,
70
                          const int amount,
71
                          const int price);
72
73
        /**
74
         * Adds an item to the list (used by sell dialog). Looks for
75
         * duplicate entries, if mergeDuplicates was turned on.
76
         *
77
         * @param inventoryIndex the inventory index of the item
78
         * @param id the id of the item
79
         * @param amount number of available copies of the item
80
         * @param price price of the item
81
         */
82
        ShopItem *addItem2(const int inventoryIndex,
83
                           const int id,
84
                           const ItemTypeT type,
85
                           const ItemColor color,
86
                           const int amount,
87
                           const int price);
88
89
        ShopItem *addItemNoDup(const int id,
90
                               const ItemTypeT type,
91
                               const ItemColor color,
92
                               const int amount,
93
                               const int price);
94
95
        /**
96
         * Returns the number of items in the shop.
97
         */
98
36
        int getNumberOfElements() override final A_WARN_UNUSED
99
76
        { return CAST_S32(mShopItems.size()); }
100
101
        bool empty() const noexcept2 A_WARN_UNUSED
102
        { return mShopItems.empty(); }
103
104
        /**
105
         * Returns the name of item number i in the shop.
106
         *
107
         * @param i the index to retrieve
108
         */
109
        std::string getElementAt(int i) override final A_WARN_UNUSED;
110
111
        /**
112
         * Returns the item number i in the shop.
113
         */
114
        ShopItem *at(const size_t i) const A_WARN_UNUSED;
115
116
        /**
117
         * Removes an element from the shop.
118
         *
119
         * @param i index to remove
120
         */
121
        void erase(const unsigned int i);
122
123
        /**
124
         * Removes an element from the shop and destroy it.
125
         *
126
         * @param i index to remove
127
         */
128
        void del(const unsigned int i);
129
130
        /**
131
         * Clears the list of items in the shop.
132
         */
133
        void clear();
134
135
        STD_VECTOR<ShopItem*> &items() A_WARN_UNUSED
136
4
        { return mShopItems; }
137
138
        STD_VECTOR<ShopItem*> &allItems() A_WARN_UNUSED
139
        { return mAllShopItems; }
140
141
        void setMergeDuplicates(const bool b)
142
6
        { mMergeDuplicates = b; }
143
144
        void updateList();
145
146
    private:
147
        /**
148
         * Searches the current items in the shop for the specified
149
         * id and returns the item if found, or 0 else.
150
         *
151
         * @return the item found or 0
152
         */
153
        ShopItem *findItem(const int id,
154
                           const ItemColor color) const A_WARN_UNUSED;
155
156
        bool findInAllItems(STD_VECTOR<ShopItem*>::iterator &it,
157
                            const ShopItem *const item);
158
159
        /** The list of items in the shop. */
160
        STD_VECTOR<ShopItem*> mAllShopItems;
161
162
        STD_VECTOR<ShopItem*> mShopItems;
163
164
        std::string mCurrency;
165
166
        /** Look for duplicate entries on addition. */
167
        bool mMergeDuplicates;
168
};
169
170
#endif  // GUI_MODELS_SHOPITEMS_H