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/tablemodel.h" |
25 |
|
|
|
26 |
|
|
#include "utils/dtor.h" |
27 |
|
|
|
28 |
|
|
#include "gui/widgets/widget.h" |
29 |
|
|
|
30 |
|
|
#include "listeners/tablemodellistener.h" |
31 |
|
|
|
32 |
|
|
#include "debug.h" |
33 |
|
|
|
34 |
|
4 |
void TableModel::installListener(TableModelListener *const listener) |
35 |
|
|
{ |
36 |
✓✗ |
4 |
if (listener != nullptr) |
37 |
|
4 |
listeners.insert(listener); |
38 |
|
4 |
} |
39 |
|
|
|
40 |
|
|
void TableModel::removeListener(TableModelListener *const listener) |
41 |
|
|
{ |
42 |
|
|
if (listener != nullptr) |
43 |
|
|
listeners.erase(listener); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
6 |
void TableModel::signalBeforeUpdate() |
47 |
|
|
{ |
48 |
✓✓ |
22 |
for (std::set<TableModelListener *>::const_iterator it = listeners.begin(); |
49 |
|
20 |
it != listeners.end(); ++it) |
50 |
|
|
{ |
51 |
|
4 |
(*it)->modelUpdated(false); |
52 |
|
|
} |
53 |
|
6 |
} |
54 |
|
|
|
55 |
|
6 |
void TableModel::signalAfterUpdate() |
56 |
|
|
{ |
57 |
✓✓ |
22 |
for (std::set<TableModelListener *>::const_iterator it = listeners.begin(); |
58 |
|
20 |
it != listeners.end(); ++it) |
59 |
|
|
{ |
60 |
✓✗ |
4 |
if (*it != nullptr) |
61 |
|
4 |
(*it)->modelUpdated(true); |
62 |
|
|
} |
63 |
|
6 |
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
#define WIDGET_AT(row, column) (((row) * mColumns) + (column)) |
67 |
|
|
#define DYN_SIZE(h) ((h) >= 0) |
68 |
|
|
|
69 |
|
2 |
StaticTableModel::StaticTableModel(const int row, const int column) : |
70 |
|
|
TableModel(), |
71 |
|
|
mRows(row), |
72 |
|
|
mColumns(column), |
73 |
|
|
mHeight(1), |
74 |
|
|
mTableModel(), |
75 |
|
8 |
mWidths() |
76 |
|
|
{ |
77 |
✓✗ |
2 |
mTableModel.resize(row * column, nullptr); |
78 |
✓✗ |
2 |
mWidths.resize(column, 1); |
79 |
|
2 |
} |
80 |
|
|
|
81 |
|
12 |
StaticTableModel::~StaticTableModel() |
82 |
|
|
{ |
83 |
|
4 |
delete_all(mTableModel); |
84 |
|
4 |
mTableModel.clear(); |
85 |
|
4 |
} |
86 |
|
|
|
87 |
|
|
void StaticTableModel::resize() |
88 |
|
|
{ |
89 |
|
|
mRows = getRows(); |
90 |
|
|
mColumns = getColumns(); |
91 |
|
|
mTableModel.resize(mRows * mColumns, nullptr); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
4 |
void StaticTableModel::set(const int row, const int column, |
95 |
|
|
Widget *const widget) |
96 |
|
|
{ |
97 |
✓✗✓✗ ✓✗ |
4 |
if ((widget == nullptr) || row >= mRows || row < 0 |
98 |
✓✗✓✗
|
4 |
|| column >= mColumns || column < 0) |
99 |
|
|
{ |
100 |
|
|
// raise exn? |
101 |
|
|
return; |
102 |
|
|
} |
103 |
|
|
|
104 |
✓✓ |
8 |
if (DYN_SIZE(mHeight) |
105 |
✓✗✓✓
|
8 |
&& widget->getHeight() > mHeight) |
106 |
|
|
{ |
107 |
|
2 |
mHeight = widget->getHeight(); |
108 |
|
|
} |
109 |
|
|
|
110 |
✗✓ |
12 |
if (DYN_SIZE(mWidths[column]) |
111 |
✗✓✗✗
|
4 |
&& widget->getWidth() > mWidths[column]) |
112 |
|
|
{ |
113 |
|
|
mWidths[column] = widget->getWidth(); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
4 |
signalBeforeUpdate(); |
117 |
|
|
|
118 |
✗✓ |
8 |
delete mTableModel[WIDGET_AT(row, column)]; |
119 |
|
|
|
120 |
|
8 |
mTableModel[WIDGET_AT(row, column)] = widget; |
121 |
|
|
|
122 |
|
4 |
signalAfterUpdate(); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
12 |
Widget *StaticTableModel::getElementAt(const int row, |
126 |
|
|
const int column) const |
127 |
|
|
{ |
128 |
|
24 |
return mTableModel[WIDGET_AT(row, column)]; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
4 |
void StaticTableModel::fixColumnWidth(const int column, const int width) |
132 |
|
|
{ |
133 |
✓✗✓✗
|
4 |
if (width < 0 || column < 0 || column >= mColumns) |
134 |
|
|
return; |
135 |
|
|
|
136 |
|
8 |
mWidths[column] = -width; // Negate to tag as fixed |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
void StaticTableModel::fixRowHeight(const int height) |
140 |
|
|
{ |
141 |
|
|
if (height < 0) |
142 |
|
|
return; |
143 |
|
|
|
144 |
|
|
mHeight = -height; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
6 |
int StaticTableModel::getRowHeight() const |
148 |
|
|
{ |
149 |
|
6 |
return abs(mHeight); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
12 |
int StaticTableModel::getColumnWidth(const int column) const |
153 |
|
|
{ |
154 |
✓✗✓✗
|
12 |
if (column < 0 || column >= mColumns) |
155 |
|
|
return 0; |
156 |
|
|
|
157 |
|
24 |
return abs(mWidths[column]); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
12 |
int StaticTableModel::getRows() const |
161 |
|
|
{ |
162 |
|
12 |
return mRows; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
12 |
int StaticTableModel::getColumns() const |
166 |
|
|
{ |
167 |
|
12 |
return mColumns; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
int StaticTableModel::getWidth() const |
171 |
|
|
{ |
172 |
|
|
size_t width = 0; |
173 |
|
|
|
174 |
|
|
for (size_t i = 0, sz = mWidths.size(); i < sz; i++) |
175 |
|
|
width += CAST_SIZE(mWidths[i]); |
176 |
|
|
|
177 |
|
|
return CAST_S32(width); |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
int StaticTableModel::getHeight() const |
181 |
|
|
{ |
182 |
|
|
return mColumns * mHeight; |
183 |
|
|
} |