GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/colorpage.cpp Lines: 20 52 38.5 %
Date: 2017-11-29 Branches: 10 34 29.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-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/colorpage.h"
22
23
#include "gui/skin.h"
24
25
#include "gui/fonts/font.h"
26
27
#include "gui/models/colormodel.h"
28
29
#include "render/graphics.h"
30
31
#include "debug.h"
32
33
2
ColorPage::ColorPage(const Widget2 *const widget,
34
                     ListModel *const listModel,
35
2
                     const std::string &skin) :
36
2
    ListBox(widget, listModel, skin)
37
{
38


6
    mItemPadding = mSkin != nullptr ? mSkin->getOption("itemPadding") : 1;
39
2
    const Font *const font = getFont();
40
4
    mRowHeight = CAST_U32(font->getHeight() +
41
2
        2 * mItemPadding);
42
2
    if (mListModel != nullptr)
43
    {
44
6
        setHeight(CAST_S32(getRowHeight()) *
45
2
            mListModel->getNumberOfElements()
46
2
            + 2 * mPadding + 20);
47
    }
48
2
}
49
50
2
ColorPage::~ColorPage()
51
{
52
2
}
53
54
void ColorPage::draw(Graphics *const graphics)
55
{
56
    BLOCK_START("ColorPage::draw")
57
58
    const ColorModel *const model = static_cast<const ColorModel*>(
59
        mListModel);
60
61
    mHighlightColor.a = CAST_U32(mAlpha * 255.0F);
62
    updateAlpha();
63
    Font *const font = getFont();
64
65
    const int rowHeight = CAST_S32(getRowHeight());
66
    const int width = mDimension.width;
67
68
    if (mSelected >= 0)
69
    {
70
        graphics->setColor(mHighlightColor);
71
        graphics->fillRectangle(Rect(mPadding,
72
            rowHeight * mSelected + mPadding,
73
            mDimension.width - 2 * mPadding, rowHeight));
74
75
        const ColorPair *const colors = model->getColorAt(mSelected);
76
        const std::string str = mListModel->getElementAt(mSelected);
77
        font->drawString(graphics,
78
            *colors->color1,
79
            *colors->color2,
80
            str,
81
            (width - font->getWidth(str)) / 2,
82
            mSelected * rowHeight + mPadding);
83
    }
84
85
    const int sz = mListModel->getNumberOfElements();
86
    for (int i = 0, y = mPadding; i < sz; ++i, y += rowHeight)
87
    {
88
        if (i != mSelected)
89
        {
90
            const ColorPair *const colors = model->getColorAt(i);
91
            const std::string str = mListModel->getElementAt(i);
92
            font->drawString(graphics,
93
                *colors->color1,
94
                *colors->color2,
95
                str,
96
                (width - font->getWidth(str)) / 2,
97
                y);
98
        }
99
    }
100
101
    BLOCK_END("ColorPage::draw")
102
}
103
104
void ColorPage::safeDraw(Graphics *const graphics)
105
{
106
    ColorPage::draw(graphics);
107
}
108
109
void ColorPage::resetAction()
110
{
111
    setSelected(-1);
112
}
113
114
2
void ColorPage::adjustSize()
115
{
116
    BLOCK_START("ColorPage::adjustSize")
117
2
    if (mListModel != nullptr)
118
    {
119
6
        setHeight(CAST_S32(getRowHeight()) *
120
4
            mListModel->getNumberOfElements() +
121
2
            2 * mPadding + 20);
122
    }
123
    BLOCK_END("ColorPage::adjustSize")
124
2
}