GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/tablemodel.h Lines: 3 3 100.0 %
Date: 2021-03-17 Branches: 0 0 0.0 %

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-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
#ifndef GUI_MODELS_TABLEMODEL_H
25
#define GUI_MODELS_TABLEMODEL_H
26
27
#include "utils/vector.h"
28
29
#include <set>
30
31
#include "localconsts.h"
32
33
class Widget;
34
35
class TableModelListener;
36
37
/**
38
 * A model for a regular table of widgets.
39
 */
40
class TableModel notfinal
41
{
42
    public:
43
        A_DELETE_COPY(TableModel)
44
45
        virtual ~TableModel()
46
8
        { }
47
48
        /**
49
         * Determines the number of rows (lines) in the table
50
         */
51
        virtual int getRows() const A_WARN_UNUSED = 0;
52
53
        /**
54
         * Determines the number of columns in each row
55
         */
56
        virtual int getColumns() const A_WARN_UNUSED = 0;
57
58
        /**
59
         * Determines the height for each row
60
         */
61
        virtual int getRowHeight() const A_WARN_UNUSED = 0;
62
63
        /**
64
         * Determines the width of each individual column
65
         */
66
        virtual int getColumnWidth(const int index) const A_WARN_UNUSED = 0;
67
68
        /**
69
         * Retrieves the widget stored at the specified location
70
         *  within the table.
71
         */
72
        virtual Widget *getElementAt(const int row, const int column)
73
                                     const A_WARN_UNUSED = 0;
74
75
        virtual void installListener(TableModelListener *const listener);
76
77
        virtual void removeListener(TableModelListener *const listener);
78
79
    protected:
80
4
        TableModel() :
81
8
            listeners()
82
        {
83
        }
84
85
        /**
86
         * Tells all listeners that the table is about to see an update
87
         */
88
        virtual void signalBeforeUpdate();
89
90
        /**
91
         * Tells all listeners that the table has seen an update
92
         */
93
        virtual void signalAfterUpdate();
94
95
    private:
96
        std::set<TableModelListener *> listeners;
97
};
98
99
100
class StaticTableModel final : public TableModel
101
{
102
    public:
103
        StaticTableModel(const int width, const int height);
104
105
        A_DELETE_COPY(StaticTableModel)
106
107
        ~StaticTableModel() override final;
108
109
        /**
110
         * Inserts a widget into the table model.
111
         * The model is resized to accomodate the widget's width and height,
112
         * unless column width / row height have been fixed.
113
         */
114
        void set(const int row, const int column, Widget *const widget);
115
116
        /**
117
         * Fixes the column width for a given column; this overrides dynamic
118
         * width inference.
119
         *
120
         * Semantics are undefined for width 0.
121
         */
122
        void fixColumnWidth(const int column, const int width);
123
124
        /**
125
         * Fixes the row height; this overrides dynamic height inference.
126
         *
127
         * Semantics are undefined for width 0.
128
         */
129
        void fixRowHeight(const int height);
130
131
        /**
132
         * Resizes the table model
133
         */
134
        void resize();
135
136
        int getRows() const override final A_WARN_UNUSED;
137
        int getColumns() const override final A_WARN_UNUSED;
138
        int getRowHeight() const override final A_WARN_UNUSED;
139
        int getWidth() const A_WARN_UNUSED;
140
        int getHeight() const A_WARN_UNUSED;
141
        int getColumnWidth(const int index) const override final A_WARN_UNUSED;
142
        Widget *getElementAt(const int row,
143
                             const int column) const override final
144
                             A_WARN_UNUSED;
145
146
    protected:
147
        int mRows;
148
        int mColumns;
149
        int mHeight;
150
        STD_VECTOR<Widget *> mTableModel;
151
        STD_VECTOR<int> mWidths;
152
};
153
154
#endif  // GUI_MODELS_TABLEMODEL_H