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

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

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

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