ManaPlus
tablemodel.cpp
Go to the documentation of this file.
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 
31 
32 #include "debug.h"
33 
35 {
36  if (listener != nullptr)
37  listeners.insert(listener);
38 }
39 
41 {
42  if (listener != nullptr)
43  listeners.erase(listener);
44 }
45 
47 {
48  for (std::set<TableModelListener *>::const_iterator it = listeners.begin();
49  it != listeners.end(); ++it)
50  {
51  (*it)->modelUpdated(false);
52  }
53 }
54 
56 {
57  for (std::set<TableModelListener *>::const_iterator it = listeners.begin();
58  it != listeners.end(); ++it)
59  {
60  if (*it != nullptr)
61  (*it)->modelUpdated(true);
62  }
63 }
64 
65 
66 #define WIDGET_AT(row, column) (((row) * mColumns) + (column))
67 #define DYN_SIZE(h) ((h) >= 0)
68 
69 StaticTableModel::StaticTableModel(const int row, const int column) :
70  TableModel(),
71  mRows(row),
72  mColumns(column),
73  mHeight(1),
74  mTableModel(),
75  mWidths()
76 {
77  mTableModel.resize(row * column, nullptr);
78  mWidths.resize(column, 1);
79 }
80 
82 {
84  mTableModel.clear();
85 }
86 
88 {
89  mRows = getRows();
90  mColumns = getColumns();
91  mTableModel.resize(mRows * mColumns, nullptr);
92 }
93 
94 void StaticTableModel::set(const int row, const int column,
95  Widget *const widget)
96 {
97  if ((widget == nullptr) || row >= mRows || row < 0
98  || column >= mColumns || column < 0)
99  {
100  // raise exn?
101  return;
102  }
103 
104  if (DYN_SIZE(mHeight)
105  && widget->getHeight() > mHeight)
106  {
107  mHeight = widget->getHeight();
108  }
109 
110  if (DYN_SIZE(mWidths[column])
111  && widget->getWidth() > mWidths[column])
112  {
113  mWidths[column] = widget->getWidth();
114  }
115 
117 
118  delete mTableModel[WIDGET_AT(row, column)];
119 
120  mTableModel[WIDGET_AT(row, column)] = widget;
121 
123 }
124 
126  const int column) const
127 {
128  return mTableModel[WIDGET_AT(row, column)];
129 }
130 
131 void StaticTableModel::fixColumnWidth(const int column, const int width)
132 {
133  if (width < 0 || column < 0 || column >= mColumns)
134  return;
135 
136  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 
148 {
149  return abs(mHeight);
150 }
151 
152 int StaticTableModel::getColumnWidth(const int column) const
153 {
154  if (column < 0 || column >= mColumns)
155  return 0;
156 
157  return abs(mWidths[column]);
158 }
159 
161 {
162  return mRows;
163 }
164 
166 {
167  return mColumns;
168 }
169 
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 
181 {
182  return mColumns * mHeight;
183 }
#define CAST_S32
Definition: cast.h:30
#define CAST_SIZE
Definition: cast.h:34
int getRows() const
Definition: tablemodel.cpp:160
void fixRowHeight(const int height)
Definition: tablemodel.cpp:139
StaticTableModel(const int width, const int height)
Definition: tablemodel.cpp:69
std::vector< int > mWidths
Definition: tablemodel.h:151
int getColumnWidth(const int index) const
Definition: tablemodel.cpp:152
std::vector< Widget * > mTableModel
Definition: tablemodel.h:150
int getWidth() const
Definition: tablemodel.cpp:170
void set(const int row, const int column, Widget *const widget)
Definition: tablemodel.cpp:94
int getHeight() const
Definition: tablemodel.cpp:180
Widget * getElementAt(const int row, const int column) const
Definition: tablemodel.cpp:125
void fixColumnWidth(const int column, const int width)
Definition: tablemodel.cpp:131
int getRowHeight() const
Definition: tablemodel.cpp:147
int getColumns() const
Definition: tablemodel.cpp:165
virtual void removeListener(TableModelListener *const listener)
Definition: tablemodel.cpp:40
virtual void signalAfterUpdate()
Definition: tablemodel.cpp:55
std::set< TableModelListener * > listeners
Definition: tablemodel.h:96
virtual void installListener(TableModelListener *const listener)
Definition: tablemodel.cpp:34
virtual void signalBeforeUpdate()
Definition: tablemodel.cpp:46
Definition: widget.h:99
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
void delete_all(Container &c)
Definition: dtor.h:56
#define WIDGET_AT(row, column)
Definition: tablemodel.cpp:66
#define DYN_SIZE(h)
Definition: tablemodel.cpp:67