GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/modelistmodel.cpp Lines: 34 36 94.4 %
Date: 2018-07-14 Branches: 27 52 51.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2018  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "render/graphics.h"
24
25
#include "utils/sdlhelper.h"
26
#include "utils/stringutils.h"
27
28
#include "gui/models/modelistmodel.h"
29
30
#include <algorithm>
31
32
#include "debug.h"
33
34
#ifndef ANDROID
35
20
static bool modeSorter(const std::string &mode1, const std::string &mode2)
36
{
37
80
    const int width1 = atoi(mode1.substr(0, mode1.find('x')).c_str());
38
80
    const int height1 = atoi(mode1.substr(mode1.find('x') + 1).c_str());
39
20
    if ((width1 == 0) || (height1 == 0))
40
        return false;
41
42
80
    const int width2 = atoi(mode2.substr(0, mode2.find('x')).c_str());
43
80
    const int height2 = atoi(mode2.substr(mode2.find('x') + 1).c_str());
44
20
    if ((width2 == 0) || (height2 == 0))
45
        return false;
46
20
    if (width1 != width2)
47
20
        return width1 < width2;
48
49
    if (height1 != height2)
50
        return height1 < height2;
51
52
    return false;
53
}
54
#endif  // ANDROID
55
56
2
ModeListModel::ModeListModel() :
57
    ListModel(),
58
6
    mVideoModes()
59
{
60
2
    SDL::getAllVideoModes(mVideoModes);
61
#ifndef ANDROID
62

8
    addCustomMode("640x480");
63

8
    addCustomMode("800x600");
64

8
    addCustomMode("1024x768");
65

8
    addCustomMode("1280x1024");
66

8
    addCustomMode("1400x900");
67

8
    addCustomMode("1500x990");
68

4
    addCustomMode(toString(mainGraphics->mActualWidth).append("x")
69

6
        .append(toString(mainGraphics->mActualHeight)));
70
71
8
    std::sort(mVideoModes.begin(), mVideoModes.end(), &modeSorter);
72
10
    mVideoModes.push_back("custom");
73
#endif  // ANDROID
74
2
}
75
76
#ifndef ANDROID
77
14
void ModeListModel::addCustomMode(const std::string &mode)
78
{
79
42
    StringVectCIter it = mVideoModes.begin();
80
42
    const StringVectCIter it_end = mVideoModes.end();
81
44
    while (it != it_end)
82
    {
83
32
        if (*it == mode)
84
            return;
85
        ++ it;
86
    }
87
12
    mVideoModes.push_back(mode);
88
}
89
#endif  // ANDROID
90
91
2
int ModeListModel::getIndexOf(const std::string &widthXHeightMode)
92
{
93
4
    std::string currentMode;
94
4
    for (int i = 0; i < getNumberOfElements(); i++)
95
    {
96
4
        currentMode = getElementAt(i);
97
2
        if (currentMode == widthXHeightMode)
98
            return i;
99
    }
100
    return -1;
101
}