KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
tableheader.h
1/******************************************************************************************
2**
3** tableheader.h
4**
5** Moved from my Tesseract Engineering repo to open-source
6**
7** Author: Stephen Punak
8** Created: Fri Oct 27 18:08:12 2023
9**
10******************************************************************************************/
11#ifndef TABLEHEADER_H
12#define TABLEHEADER_H
13#include <QColor>
14#include <QMap>
15#include <Kanoop/gui/libkanoopgui.h>
16#include <Kanoop/entitymetadata.h>
17
18/**
19 * @brief Descriptor for a single column or row header in a model/view.
20 *
21 * TableHeader stores the header type (an application-defined integer), display
22 * text, orientation, optional text colour, and visibility flag. An EntityMetadata
23 * can also be attached for carrying additional role-keyed data.
24 *
25 * Two container classes are provided:
26 * - TableHeader::List — a QList with name- and type-search helpers
27 * - TableHeader::IntMap — a QMap<int,TableHeader> with sorted-list export
28 */
29class LIBKANOOPGUI_EXPORT TableHeader
30{
31public:
32 /** @brief Default constructor — creates an invalid horizontal header. */
33 TableHeader() : _type(0), _orientation(Qt::Horizontal) {}
34
35 /**
36 * @brief Construct a header with a type and optional orientation.
37 * @param type Application-defined header type integer
38 * @param orientation Header orientation (default Qt::Vertical)
39 */
40 TableHeader(int type, Qt::Orientation orientation = Qt::Vertical);
41
42 /**
43 * @brief Construct a header with a type, display text, and optional orientation.
44 * @param type Application-defined header type integer
45 * @param text Display text
46 * @param orientation Header orientation (default Qt::Vertical)
47 */
48 TableHeader(int type, const QString& text, Qt::Orientation orientation = Qt::Vertical);
49
50 /**
51 * @brief Return the header type integer.
52 * @return Application-defined type value
53 */
54 int type() const { return _type; }
55
56 /**
57 * @brief Return the display text.
58 * @return Header label string
59 */
60 QString text() const { return _text; }
61
62 /**
63 * @brief Set the display text.
64 * @param text New header label string
65 */
66 void setText(const QString& text) { _text = text; }
67
68 /**
69 * @brief Return the header orientation.
70 * @return Qt::Horizontal or Qt::Vertical
71 */
72 Qt::Orientation orientation() const { return _orientation; }
73
74 /**
75 * @brief Return the text colour for cells in this column.
76 * @return QColor for cell text, or invalid QColor if not set
77 */
78 QColor columnTextColor() const { return _columnTextColor; }
79
80 /**
81 * @brief Set the text colour for cells in this column.
82 * @param value New text colour
83 */
84 void setColumnTextColor(const QColor& value) { _columnTextColor = value; }
85
86 /**
87 * @brief Return whether this header (and its column) is visible.
88 * @return true if visible
89 */
90 bool isVisible() const { return _visible; }
91
92 /**
93 * @brief Set header (and column) visibility.
94 * @param value true to show, false to hide
95 */
96 void setVisible(bool value) { _visible = value; }
97
98 /**
99 * @brief Return the EntityMetadata attached to this header.
100 * @return Associated EntityMetadata
101 */
102 EntityMetadata entityMetadata() const { return _entityMetadata; }
103
104 /**
105 * @brief Return a mutable reference to the attached EntityMetadata.
106 * @return Reference to the EntityMetadata
107 */
108 EntityMetadata& entityMetadataRef() { return _entityMetadata; }
109
110 /**
111 * @brief Set the EntityMetadata attached to this header.
112 * @param value Metadata to associate
113 */
114 void setEntityMetadata(const EntityMetadata& value) { _entityMetadata = value; }
115
116 /**
117 * @brief Return data stored in the attached metadata for the given role.
118 * @param role Model role key
119 * @return Stored QVariant, or invalid QVariant if not set
120 */
121 QVariant data(int role) const { return _entityMetadata.data(role); }
122
123 /**
124 * @brief Store a value in the attached metadata under the given role.
125 * @param value Value to store
126 * @param role Model role key
127 */
128 void setData(const QVariant& value, int role) { _entityMetadata.setData(value, role); }
129
130 /**
131 * @brief Return the UUID stored in the attached metadata.
132 * @return UUID, or null UUID if not set
133 */
134 QUuid uuid() const { return _entityMetadata.data(KANOOP::UUidRole).toUuid(); }
135
136 /**
137 * @brief Return whether this header has a non-zero type.
138 * @return true if type != 0
139 */
140 bool isValid() const { return _type != 0; }
141
142 /**
143 * @brief A list of TableHeader objects with name and type search helpers.
144 */
145 class List : public QList<TableHeader>
146 {
147 public:
148 /**
149 * @brief Set visibility for the header with the given type.
150 * @param type Header type to find
151 * @param visible true to show, false to hide
152 */
153 void setHeaderVisible(int type, bool visible)
154 {
155 auto it = std::find_if(begin(), end(), [type](TableHeader& header) { return header.type() == type; });
156 if(it != end()) {
157 (*it).setVisible(visible);
158 }
159 }
160
161 /**
162 * @brief Find the first header with a matching display text.
163 * @param text Header label to search for
164 * @return Matching TableHeader, or an invalid one if not found
165 */
166 TableHeader findByName(const QString& text) const
167 {
168 TableHeader result;
169 auto it = std::find_if(constBegin(), constEnd(), [text](const TableHeader& header) { return header.text() == text; });
170 if(it != constEnd()) {
171 result = *it;
172 }
173 return result;
174 }
175 };
176
177 /**
178 * @brief An int-keyed map of TableHeader objects with bulk-operation helpers.
179 */
180 class IntMap : public QMap<int, TableHeader>
181 {
182 public:
183 /**
184 * @brief Return all headers as a list sorted by key (column index).
185 * @return Sorted TableHeader::List
186 */
188 {
189 List result;
190 QList<int> cols = keys();
191 std::sort(cols.begin(), cols.end());
192 for(int col : cols) {
193 result.append(this->value(col));
194 }
195 return result;
196 }
197
198 /**
199 * @brief Set the text colour for the header with the given type.
200 * @param type Header type to find
201 * @param color New text colour
202 */
203 void setTextColorForType(int type, const QColor& color)
204 {
205 auto it = std::find_if(begin(), end(), [type](TableHeader& header) { return header.type() == type; });
206 if(it != end()) {
207 TableHeader& header = *it;
208 header.setColumnTextColor(color);
209 }
210 }
211
212 /**
213 * @brief Set visibility for the header with the given type.
214 * @param type Header type to find
215 * @param visible true to show, false to hide
216 */
217 void setHeaderVisible(int type, bool visible)
218 {
219 auto it = std::find_if(begin(), end(), [type](TableHeader& header) { return header.type() == type; });
220 if(it != end()) {
221 (*it).setVisible(visible);
222 }
223 }
224
225 /**
226 * @brief Attach EntityMetadata to the header with the given type.
227 * @param type Header type to find
228 * @param metadata Metadata to associate
229 */
230 void setEntityMetadataForType(int type, const EntityMetadata& metadata)
231 {
232 auto it = std::find_if(begin(), end(), [type](TableHeader& header) { return header.type() == type; });
233 if(it != end()) {
234 TableHeader& header = *it;
235 header.setEntityMetadata(metadata);
236 }
237 }
238 };
239
240 /**
241 * @brief Look up the string name for a header type integer.
242 * @param type Header type integer
243 * @return Registered name string, or empty string if not found
244 */
245 static QString typeToString(int type) { return _TableHeaderTypeToStringMap.value(type); }
246
247private:
248 int _type = 0;
249 QString _text;
250 Qt::Orientation _orientation;
251 QColor _columnTextColor;
252 bool _visible = true;
253 EntityMetadata _entityMetadata;
254
255 class TableHeaderTypeToStringMap : public QMap<int, QString>
256 {};
257
258
259 static const TableHeaderTypeToStringMap _TableHeaderTypeToStringMap;
260};
261
262#endif // TABLEHEADER_H
An int-keyed map of TableHeader objects with bulk-operation helpers.
void setEntityMetadataForType(int type, const EntityMetadata &metadata)
Attach EntityMetadata to the header with the given type.
List toSortedList() const
Return all headers as a list sorted by key (column index).
void setTextColorForType(int type, const QColor &color)
Set the text colour for the header with the given type.
void setHeaderVisible(int type, bool visible)
Set visibility for the header with the given type.
A list of TableHeader objects with name and type search helpers.
void setHeaderVisible(int type, bool visible)
Set visibility for the header with the given type.
TableHeader findByName(const QString &text) const
Find the first header with a matching display text.
Descriptor for a single column or row header in a model/view.
Definition tableheader.h:30
QString text() const
Return the display text.
Definition tableheader.h:60
QVariant data(int role) const
Return data stored in the attached metadata for the given role.
void setVisible(bool value)
Set header (and column) visibility.
Definition tableheader.h:96
void setEntityMetadata(const EntityMetadata &value)
Set the EntityMetadata attached to this header.
bool isValid() const
Return whether this header has a non-zero type.
QUuid uuid() const
Return the UUID stored in the attached metadata.
static QString typeToString(int type)
Look up the string name for a header type integer.
EntityMetadata entityMetadata() const
Return the EntityMetadata attached to this header.
Qt::Orientation orientation() const
Return the header orientation.
Definition tableheader.h:72
int type() const
Return the header type integer.
Definition tableheader.h:54
TableHeader(int type, Qt::Orientation orientation=Qt::Vertical)
Construct a header with a type and optional orientation.
EntityMetadata & entityMetadataRef()
Return a mutable reference to the attached EntityMetadata.
void setData(const QVariant &value, int role)
Store a value in the attached metadata under the given role.
void setColumnTextColor(const QColor &value)
Set the text colour for cells in this column.
Definition tableheader.h:84
QColor columnTextColor() const
Return the text colour for cells in this column.
Definition tableheader.h:78
TableHeader()
Default constructor — creates an invalid horizontal header.
Definition tableheader.h:33
bool isVisible() const
Return whether this header (and its column) is visible.
Definition tableheader.h:90
void setText(const QString &text)
Set the display text.
Definition tableheader.h:66
TableHeader(int type, const QString &text, Qt::Orientation orientation=Qt::Vertical)
Construct a header with a type, display text, and optional orientation.