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

8
    addCustomMode("640x480");
64

8
    addCustomMode("800x600");
65

8
    addCustomMode("1024x768");
66

8
    addCustomMode("1280x1024");
67

8
    addCustomMode("1400x900");
68

8
    addCustomMode("1500x990");
69

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

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