GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setuptabscroll.cpp Lines: 40 75 53.3 %
Date: 2021-03-17 Branches: 16 60 26.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/widgets/tabs/setuptabscroll.h"
23
24
#include "gui/widgets/scrollarea.h"
25
#include "gui/widgets/setupitem.h"
26
#include "gui/widgets/vertcontainer.h"
27
28
#include "utils/delete2.h"
29
30
#include "debug.h"
31
32
14
SetupTabScroll::SetupTabScroll(const Widget2 *const widget) :
33
    SetupTab(widget),
34

14
    mContainer(new VertContainer(this, 25, false, 8)),
35

28
    mScroll(new ScrollArea(this, mContainer, Opaque_false, std::string())),
36
    mItems(),
37
    mAllItems(),
38
84
    mPreferredFirstItemSize(200)
39
{
40
14
    mScroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
41
14
    mScroll->setVerticalScrollPolicy(ScrollArea::SHOW_AUTO);
42
28
    mScroll->setSelectable(false);
43
28
    mContainer->setSelectable(false);
44
14
}
45
46
56
SetupTabScroll::~SetupTabScroll()
47
{
48
14
    mScroll = nullptr;
49
28
    delete2(mContainer)
50
14
    removeItems();
51
14
}
52
53
14
void SetupTabScroll::removeItems()
54
{
55
28
    std::set<SetupItem*>::iterator it = mAllItems.begin();
56
28
    const std::set<SetupItem*>::iterator it_end = mAllItems.end();
57
502
    while (it != it_end)
58
    {
59
488
        delete *it;
60
        ++ it;
61
    }
62
28
    mAllItems.clear();
63
64
28
    mItems.clear();
65
14
}
66
67
void SetupTabScroll::clear()
68
{
69
    removeItems();
70
    mContainer->removeControls();
71
    mContainer->clear();
72
}
73
74
488
void SetupTabScroll::addControl(SetupItem *const widget)
75
{
76
488
    if (widget == nullptr)
77
        return;
78
976
    const std::string actionId = widget->getActionEventId();
79
488
    if (!actionId.empty())
80
    {
81
        const std::map<std::string, SetupItem*>::iterator iter
82
824
            = mItems.find(actionId);
83
824
        if (iter != mItems.end())
84
        {
85
            delete (*iter).second;
86
            mItems.erase(iter);
87
        }
88
412
        mItems[actionId] = widget;
89
    }
90
976
    mAllItems.insert(widget);
91
}
92
93
80
void SetupTabScroll::addControl(SetupItem *const widget,
94
                                const std::string &event)
95
{
96
    const std::map<std::string, SetupItem*>::iterator iter
97
160
        = mItems.find(event);
98
160
    if (iter != mItems.end())
99
    {
100
        delete (*iter).second;
101
        mItems.erase(iter);
102
    }
103
80
    mItems[event] = widget;
104
160
    mAllItems.insert(widget);
105
80
}
106
107
void SetupTabScroll::apply()
108
{
109
    for (std::map<std::string, SetupItem*>::const_iterator
110
         iter = mItems.begin(), iter_end = mItems.end();
111
         iter != iter_end; ++ iter)
112
    {
113
        if ((*iter).second != nullptr)
114
            (*iter).second->apply((*iter).first);
115
    }
116
}
117
118
void SetupTabScroll::cancel()
119
{
120
    for (std::map<std::string, SetupItem*>::const_iterator
121
         iter = mItems.begin(), iter_end = mItems.end();
122
         iter != iter_end; ++ iter)
123
    {
124
        if ((*iter).second != nullptr)
125
            (*iter).second->cancel((*iter).first);
126
    }
127
}
128
129
void SetupTabScroll::externalUpdated()
130
{
131
    for (std::map<std::string, SetupItem*>::const_iterator
132
         iter = mItems.begin(), iter_end = mItems.end();
133
         iter != iter_end; ++ iter)
134
    {
135
        SetupItem *const widget = (*iter).second;
136
        if ((widget != nullptr) && widget->isMainConfig() == MainConfig_false)
137
            widget->externalUpdated((*iter).first);
138
    }
139
}
140
141
void SetupTabScroll::externalUnloaded()
142
{
143
    for (std::map<std::string, SetupItem*>::const_iterator
144
         iter = mItems.begin(), iter_end = mItems.end();
145
         iter != iter_end; ++ iter)
146
    {
147
        SetupItem *const widget = (*iter).second;
148
        if ((widget != nullptr) && widget->isMainConfig() == MainConfig_false)
149
            widget->externalUnloaded((*iter).first);
150
    }
151
}
152
153
28
void SetupTabScroll::widgetResized(const Event &event A_UNUSED)
154
{
155
56
    mScroll->setWidth(getWidth() - 12);
156
56
    mScroll->setHeight(getHeight() - 12 - 12);
157
28
}
158
159
void SetupTabScroll::reread(const std::string &name)
160
{
161
    SetupItem *const item = mItems[name + "Event"];
162
    if (item != nullptr)
163
        item->rereadValue();
164
}