GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/tablemodel.cpp Lines: 50 68 73.5 %
Date: 2017-11-29 Branches: 22 46 47.8 %

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/tablemodel.h"
24
25
#include "utils/dtor.h"
26
27
#include "gui/widgets/widget.h"
28
29
#include "listeners/tablemodellistener.h"
30
31
#include "debug.h"
32
33
8
void TableModel::installListener(TableModelListener *const listener)
34
{
35
8
    if (listener != nullptr)
36
8
        listeners.insert(listener);
37
8
}
38
39
void TableModel::removeListener(TableModelListener *const listener)
40
{
41
    if (listener != nullptr)
42
        listeners.erase(listener);
43
}
44
45
12
void TableModel::signalBeforeUpdate()
46
{
47
24
    for (std::set<TableModelListener *>::const_iterator it = listeners.begin();
48
40
         it != listeners.end(); ++it)
49
    {
50
8
        (*it)->modelUpdated(false);
51
    }
52
12
}
53
54
12
void TableModel::signalAfterUpdate()
55
{
56
24
    for (std::set<TableModelListener *>::const_iterator it = listeners.begin();
57
40
         it != listeners.end(); ++it)
58
    {
59
8
        if (*it != nullptr)
60
8
            (*it)->modelUpdated(true);
61
    }
62
12
}
63
64
65
#define WIDGET_AT(row, column) (((row) * mColumns) + (column))
66
#define DYN_SIZE(h) ((h) >= 0)
67
68
4
StaticTableModel::StaticTableModel(const int row, const int column) :
69
    TableModel(),
70
    mRows(row),
71
    mColumns(column),
72
    mHeight(1),
73
    mTableModel(),
74
16
    mWidths()
75
{
76
4
    mTableModel.resize(row * column, nullptr);
77
4
    mWidths.resize(column, 1);
78
4
}
79
80
24
StaticTableModel::~StaticTableModel()
81
{
82
8
    delete_all(mTableModel);
83
8
    mTableModel.clear();
84
8
}
85
86
void StaticTableModel::resize()
87
{
88
    mRows = getRows();
89
    mColumns = getColumns();
90
    mTableModel.resize(mRows * mColumns, nullptr);
91
}
92
93
8
void StaticTableModel::set(const int row, const int column,
94
                           Widget *const widget)
95
{
96

8
    if ((widget == nullptr) || row >= mRows || row < 0
97

8
        || column >= mColumns || column < 0)
98
    {
99
        // raise exn?
100
        return;
101
    }
102
103
8
    if (DYN_SIZE(mHeight)
104

16
        && widget->getHeight() > mHeight)
105
    {
106
4
        mHeight = widget->getHeight();
107
    }
108
109
16
    if (DYN_SIZE(mWidths[column])
110

8
        && widget->getWidth() > mWidths[column])
111
    {
112
        mWidths[column] = widget->getWidth();
113
    }
114
115
8
    signalBeforeUpdate();
116
117
16
    delete mTableModel[WIDGET_AT(row, column)];
118
119
16
    mTableModel[WIDGET_AT(row, column)] = widget;
120
121
8
    signalAfterUpdate();
122
}
123
124
24
Widget *StaticTableModel::getElementAt(const int row,
125
                                       const int column) const
126
{
127
48
    return mTableModel[WIDGET_AT(row, column)];
128
}
129
130
8
void StaticTableModel::fixColumnWidth(const int column, const int width)
131
{
132

8
    if (width < 0 || column < 0 || column >= mColumns)
133
        return;
134
135
16
    mWidths[column] = -width;  // Negate to tag as fixed
136
}
137
138
void StaticTableModel::fixRowHeight(const int height)
139
{
140
    if (height < 0)
141
        return;
142
143
    mHeight = -height;
144
}
145
146
12
int StaticTableModel::getRowHeight() const
147
{
148
12
    return abs(mHeight);
149
}
150
151
24
int StaticTableModel::getColumnWidth(const int column) const
152
{
153

24
    if (column < 0 || column >= mColumns)
154
        return 0;
155
156
48
    return abs(mWidths[column]);
157
}
158
159
24
int StaticTableModel::getRows() const
160
{
161
24
    return mRows;
162
}
163
164
24
int StaticTableModel::getColumns() const
165
{
166
24
    return mColumns;
167
}
168
169
int StaticTableModel::getWidth() const
170
{
171
    int width = 0;
172
173
    for (size_t i = 0, sz = mWidths.size(); i < sz; i++)
174
        width += mWidths[i];
175
176
    return width;
177
}
178
179
int StaticTableModel::getHeight() const
180
{
181
    return mColumns * mHeight;
182
}