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