GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/playertablemodel.cpp Lines: 34 53 64.2 %
Date: 2021-03-17 Branches: 11 48 22.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008-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 "gui/models/playertablemodel.h"
25
26
#include "being/playerrelations.h"
27
28
#include "gui/widgets/dropdown.h"
29
#include "gui/widgets/label.h"
30
31
#include "gui/models/playerrelationlistmodel.h"
32
33
#include "utils/delete2.h"
34
#include "utils/dtor.h"
35
36
#include "debug.h"
37
38
static const int COLUMNS_NR = 2;  // name plus listbox
39
static const int NAME_COLUMN = 0;
40
static const unsigned int RELATION_CHOICE_COLUMN = 1;
41
42
static const unsigned int ROW_HEIGHT = 12;
43
// The following column widths really shouldn't be hardcoded
44
// but should scale with the size of the widget... except
45
// that, right now, the widget doesn't exactly scale either.
46
static const unsigned int NAME_COLUMN_WIDTH = 230;
47
static const unsigned int RELATION_CHOICE_COLUMN_WIDTH = 80;
48
49
#define WIDGET_AT(row, column) (((row) * COLUMNS_NR) + (column))
50
51
2
PlayerTableModel::PlayerTableModel(const Widget2 *const widget) :
52
    Widget2(widget),
53
    TableModel(),
54
    mPlayers(nullptr),
55
    mWidgets(),
56
10
    mListModel(new PlayerRelationListModel)
57
{
58
2
    playerRelationsUpdated();
59
2
}
60
61
10
PlayerTableModel::~PlayerTableModel()
62
{
63
2
    freeWidgets();
64
4
    delete2(mListModel)
65
2
    delete2(mPlayers)
66
4
}
67
68
4
int PlayerTableModel::getRows() const
69
{
70
4
    if (mPlayers != nullptr)
71
8
        return CAST_S32(mPlayers->size());
72
    return 0;
73
}
74
75
4
int PlayerTableModel::getColumns() const
76
{
77
4
    return COLUMNS_NR;
78
}
79
80
2
int PlayerTableModel::getRowHeight() const
81
{
82
2
    return ROW_HEIGHT;
83
}
84
85
4
int PlayerTableModel::getColumnWidth(const int index) const
86
{
87
4
    if (index == NAME_COLUMN)
88
        return NAME_COLUMN_WIDTH;
89
2
    return RELATION_CHOICE_COLUMN_WIDTH;
90
}
91
92
2
void PlayerTableModel::playerRelationsUpdated()
93
{
94
2
    signalBeforeUpdate();
95
96
2
    freeWidgets();
97
2
    StringVect *const player_names = playerRelations.getPlayers();
98
2
    delete mPlayers;
99
2
    mPlayers = player_names;
100
101
    // set up widgets
102
2
    for (unsigned int r = 0, fsz = CAST_U32(
103
4
         player_names->size()); r < fsz; ++r)
104
    {
105
        const std::string name = (*player_names)[r];
106
        Widget *const widget = new Label(this, name);
107
        mWidgets.push_back(widget);
108
109
        DropDown *const choicebox = new DropDown(this,
110
            mListModel,
111
            false,
112
            Modal_false,
113
            nullptr,
114
            std::string());
115
        choicebox->setSelected(CAST_S32(
116
            playerRelations.getRelation(name)));
117
        mWidgets.push_back(choicebox);
118
    }
119
120
2
    signalAfterUpdate();
121
2
}
122
123
void PlayerTableModel::updateModelInRow(const int row) const
124
{
125
    const DropDown *const choicebox = static_cast<DropDown *>(
126
        getElementAt(row, RELATION_CHOICE_COLUMN));
127
    if (choicebox == nullptr)
128
        return;
129
    playerRelations.setRelation(getPlayerAt(row),
130
        static_cast<RelationT>(
131
        choicebox->getSelected()));
132
}
133
134
Widget *PlayerTableModel::getElementAt(int row, int column) const
135
{
136
    return mWidgets[WIDGET_AT(row, column)];
137
}
138
139
4
void PlayerTableModel::freeWidgets()
140
{
141
4
    delete2(mPlayers)
142
8
    delete_all(mWidgets);
143
8
    mWidgets.clear();
144
4
}
145
146
std::string PlayerTableModel::getPlayerAt(const int index) const
147
{
148
    if ((mPlayers == nullptr) || index < 0
149
        || index >= CAST_S32(mPlayers->size()))
150
    {
151
        return std::string();
152
    }
153
    return (*mPlayers)[index];
154
}