GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/setuptouchitem.cpp Lines: 35 48 72.9 %
Date: 2021-03-17 Branches: 9 28 32.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-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/setuptouchitem.h"
23
24
#include "gui/models/touchactionmodel.h"
25
26
#include "gui/widgets/dropdown.h"
27
#include "gui/widgets/horizontcontainer.h"
28
#include "gui/widgets/label.h"
29
#include "gui/widgets/vertcontainer.h"
30
31
#include "gui/widgets/tabs/setuptabscroll.h"
32
33
#include "utils/stringutils.h"
34
35
#include "debug.h"
36
37
26
SetupActionDropDown::SetupActionDropDown(const std::string &restrict text,
38
                                         const std::string &restrict
39
                                         description,
40
                                         const std::string &restrict keyName,
41
                                         SetupTabScroll *restrict const parent,
42
                                         const std::string &restrict eventName,
43
                                         TouchActionsModel *restrict
44
                                         const model,
45
                                         const int width,
46
26
                                         const MainConfig mainConfig) :
47
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
48
    mHorizont(nullptr),
49
    mLabel(nullptr),
50
    mModel(model),
51
    mDropDown(nullptr),
52
26
    mWidth(width)
53
{
54
26
    mValueType = VSTR;
55
26
    createControls();
56
26
}
57
58
SetupActionDropDown::SetupActionDropDown(const std::string &restrict text,
59
                                         const std::string &restrict
60
                                         description,
61
                                         const std::string &restrict keyName,
62
                                         SetupTabScroll *restrict const parent,
63
                                         const std::string &restrict eventName,
64
                                         TouchActionsModel *restrict
65
                                         const model,
66
                                         const int width,
67
                                         const std::string &restrict def,
68
                                         const MainConfig mainConfig) :
69
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
70
    mHorizont(nullptr),
71
    mLabel(nullptr),
72
    mModel(model),
73
    mDropDown(nullptr),
74
    mWidth(width)
75
{
76
    mValueType = VSTR;
77
    createControls();
78
}
79
80
52
SetupActionDropDown::~SetupActionDropDown()
81
{
82
26
    mHorizont = nullptr;
83
26
    mWidget = nullptr;
84
26
    mModel = nullptr;
85
26
    mDropDown = nullptr;
86
26
    mLabel = nullptr;
87
26
}
88
89
26
void SetupActionDropDown::createControls()
90
{
91
26
    if (mModel == nullptr)
92
        return;
93
94
26
    load();
95
26
    mHorizont = new HorizontContainer(this, 32, 2);
96
97
26
    mLabel = new Label(this, mText);
98
52
    mLabel->setToolTip(mDescription);
99
26
    mDropDown = new DropDown(this,
100
26
        mModel,
101
        false,
102
        Modal_false,
103
        nullptr,
104

78
        std::string());
105
52
    mDropDown->setActionEventId(mEventName);
106
26
    mDropDown->addActionListener(mParent);
107
26
    mDropDown->setWidth(mWidth);
108
26
    mDropDown->setSelected(mModel->getSelectionFromAction(
109
104
        static_cast<InputActionT>(atoi(mValue.c_str()))));
110
111
26
    mWidget = mDropDown;
112
26
    fixFirstItemSize(mLabel);
113
26
    mHorizont->add(mLabel);
114
26
    mHorizont->add(mDropDown);
115
116
52
    mParent->getContainer()->add2(mHorizont, true, 4);
117
26
    mParent->addControl(this);
118
26
    mParent->addActionListener(this);
119
26
    mWidget->addActionListener(this);
120
}
121
122
void SetupActionDropDown::fromWidget()
123
{
124
    if ((mDropDown == nullptr) || (mModel == nullptr))
125
        return;
126
127
    mValue = toString(CAST_S32(mModel->getActionFromSelection(
128
        mDropDown->getSelected())));
129
}
130
131
void SetupActionDropDown::toWidget()
132
{
133
    if ((mDropDown == nullptr) || (mModel == nullptr))
134
        return;
135
136
    mDropDown->setSelected(mModel->getSelectionFromAction(
137
        static_cast<InputActionT>(atoi(mValue.c_str()))));
138
}