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