GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setuptabscroll.cpp Lines: 40 75 53.3 %
Date: 2017-11-29 Branches: 17 58 29.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  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
28
SetupTabScroll::SetupTabScroll(const Widget2 *const widget) :
32
    SetupTab(widget),
33

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

28
    mScroll(new ScrollArea(this, mContainer, Opaque_false)),
35
    mItems(),
36
    mAllItems(),
37
224
    mPreferredFirstItemSize(200)
38
{
39
28
    mScroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
40
28
    mScroll->setVerticalScrollPolicy(ScrollArea::SHOW_AUTO);
41
56
    mScroll->setSelectable(false);
42
56
    mContainer->setSelectable(false);
43
28
}
44
45
112
SetupTabScroll::~SetupTabScroll()
46
{
47
28
    mScroll = nullptr;
48
56
    delete2(mContainer);
49
28
    removeItems();
50
28
}
51
52
28
void SetupTabScroll::removeItems()
53
{
54
56
    std::set<SetupItem*>::iterator it = mAllItems.begin();
55
56
    const std::set<SetupItem*>::iterator it_end = mAllItems.end();
56
988
    while (it != it_end)
57
    {
58
960
        delete *it;
59
        ++ it;
60
    }
61
56
    mAllItems.clear();
62
63
56
    mItems.clear();
64
28
}
65
66
void SetupTabScroll::clear()
67
{
68
    removeItems();
69
    mContainer->removeControls();
70
    mContainer->clear();
71
}
72
73
960
void SetupTabScroll::addControl(SetupItem *const widget)
74
{
75
960
    if (widget == nullptr)
76
        return;
77
1920
    const std::string actionId = widget->getActionEventId();
78
960
    if (!actionId.empty())
79
    {
80
        const std::map<std::string, SetupItem*>::iterator iter
81
1616
            = mItems.find(actionId);
82
1616
        if (iter != mItems.end())
83
        {
84
            delete (*iter).second;
85
            mItems.erase(iter);
86
        }
87
808
        mItems[actionId] = widget;
88
    }
89
1920
    mAllItems.insert(widget);
90
}
91
92
152
void SetupTabScroll::addControl(SetupItem *const widget,
93
                                const std::string &event)
94
{
95
    const std::map<std::string, SetupItem*>::iterator iter
96
304
        = mItems.find(event);
97
304
    if (iter != mItems.end())
98
    {
99
        delete (*iter).second;
100
        mItems.erase(iter);
101
    }
102
152
    mItems[event] = widget;
103
304
    mAllItems.insert(widget);
104
152
}
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
56
void SetupTabScroll::widgetResized(const Event &event A_UNUSED)
153
{
154
112
    mScroll->setWidth(getWidth() - 12);
155
112
    mScroll->setHeight(getHeight() - 12 - 12);
156
56
}
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
}