1.10
grid.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_GRID_H
7#define EGT_GRID_H
8
14#include <egt/detail/meta.h>
15#include <egt/frame.h>
16#include <egt/widget.h>
17#include <memory>
18#include <vector>
19
20namespace egt
21{
22inline namespace v1
23{
24
42class EGT_API StaticGrid : public Frame
43{
44public:
45
47 enum class GridFlag
48 {
52 show_border = detail::bit(0),
53 };
54
57
60
63
67 explicit StaticGrid(const GridSize& size = GridSize(1, 1));
68
73 explicit StaticGrid(const Rect& rect,
74 const GridSize& size = GridSize(1, 1));
75
81 StaticGrid(Frame& parent, const Rect& rect,
82 const GridSize& size = GridSize(1, 1));
83
88 explicit StaticGrid(Frame& parent,
89 const GridSize& size = GridSize(1, 1));
90
95 : StaticGrid(props, false)
96 {
97 }
98
99protected:
100
101 explicit StaticGrid(Serializer::Properties& props, bool is_derived);
102
103public:
104
105 using Frame::add;
106
115 void add(const std::shared_ptr<Widget>& widget) override;
116
127 virtual void add(const std::shared_ptr<Widget>& widget, size_t column, size_t row);
128
136 virtual void add(Widget& widget, size_t column, size_t row)
137 {
138 // Nasty, but it gets the job done. If a widget is passed in as a
139 // reference, we don't own it, so create a "pointless" shared_ptr that
140 // will not delete it.
141 auto w = std::shared_ptr<Widget>(&widget, [](Widget*) {});
142 add(w, column, row);
143 }
144
154 virtual void add(const std::shared_ptr<Widget>& widget, const GridPoint& point)
155 {
156 add(widget, point.x(), point.y());
157 }
158
165 virtual void add(Widget& widget, const GridPoint& point)
166 {
167 // Nasty, but it gets the job done. If a widget is passed in as a
168 // reference, we don't own it, so create a "pointless" shared_ptr that
169 // will not delete it.
170 auto w = std::shared_ptr<Widget>(&widget, [](Widget*) {});
171 add(w, point);
172 }
173
181 Widget* get(const GridPoint& point);
182
183 void remove(Widget* widget) override;
184
185 void layout() override;
186
192 EGT_NODISCARD int last_add_column() const
193 {
194 return m_last_add_column;
195 }
196
202 EGT_NODISCARD int last_add_row() const
203 {
204 return m_last_add_row;
205 }
206
210 EGT_NODISCARD bool column_priority() const { return m_column_priority; }
211
218 void column_priority(bool value) { m_column_priority = value; }
219
223 EGT_NODISCARD size_t n_col() const
224 {
225 return m_grid_size.width();
226 }
227
231 EGT_NODISCARD size_t n_row() const
232 {
233 return m_grid_size.height();
234 }
235
239 EGT_NODISCARD GridSize grid_size() const
240 {
241 return m_grid_size;
242 }
243
247 void grid_size(const GridSize size)
248 {
249 if (detail::change_if_diff<>(m_grid_size, size))
250 {
251 reallocate(size);
252 reposition();
253 }
254 }
255
260 {
261 if (detail::change_if_diff<>(m_horizontal_space, space))
262 {
263 layout();
264 damage();
265 }
266 }
267
271 EGT_NODISCARD DefaultDim horizontal_space() const
272 {
273 return m_horizontal_space;
274 }
275
280 {
281 if (detail::change_if_diff<>(m_vertical_space, space))
282 {
283 layout();
284 damage();
285 }
286 }
287
291 EGT_NODISCARD DefaultDim vertical_space() const
292 {
293 return m_vertical_space;
294 }
295
296 void serialize(Serializer& serializer) const override;
297
298 void serialize_children(Serializer& serializer) const override;
299
300 void deserialize_children(const Deserializer& deserializer) override;
301
302protected:
303
305 void reallocate(const GridSize& size);
306
314
316 using CellArray = std::vector<std::vector<std::weak_ptr<Widget>>>;
317
320
322 int m_last_add_column{-1};
324 int m_last_add_row{-1};
326 bool m_column_priority{false};
328 GridSize m_grid_size{};
330 DefaultDim m_horizontal_space{};
332 DefaultDim m_vertical_space{};
333
334private:
335
336 void deserialize(Serializer::Properties& props);
337};
338
351// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
352class EGT_API SelectableGrid : public StaticGrid
353{
354public:
355
366 explicit SelectableGrid(const GridSize& size = GridSize(1, 1));
367
372 explicit SelectableGrid(const Rect& rect,
373 const GridSize& size = GridSize(1, 1));
374
380 SelectableGrid(Frame& parent, const Rect& rect,
381 const GridSize& size = GridSize(1, 1));
382
387 explicit SelectableGrid(Frame& parent,
388 const GridSize& size = GridSize(1, 1));
393 : SelectableGrid(props, false)
394 {
395 }
396
397 void draw(Painter& painter, const Rect& rect) override;
398
400 EGT_NODISCARD GridPoint selected() const
401 {
402 return {m_selected_column, m_selected_row};
403 }
404
405 void handle(Event& event) override;
406
413 void selected(size_t column, size_t row);
414
419 {
420 if (detail::change_if_diff<>(m_selection_highlight, highlight))
421 {
422 layout();
423 damage();
424 }
425 }
426
430 EGT_NODISCARD DefaultDim selection_highlight() const
431 {
432 return m_selection_highlight;
433 }
434
435 void serialize(Serializer& serializer) const override;
436
437protected:
439 size_t m_selected_column{0};
441 size_t m_selected_row{0};
443 DefaultDim m_selection_highlight{5};
444
445 explicit SelectableGrid(Serializer::Properties& props, bool is_derived);
446
447private:
448
449 void deserialize(Serializer::Properties& props);
450};
451
452}
453}
454
455#endif
Definition serialize.h:200
Utility class for managing a set of flags with the ability to observe changes to the flags.
Definition flags.h:40
A Frame is a Widget that has children widgets.
Definition frame.h:45
Drawing interface for 2D graphics.
Definition painter.h:45
Simple x,y coordinate.
Definition geometry.h:63
EGT_NODISCARD constexpr Dim x() const noexcept
Get the x value.
Definition geometry.h:192
EGT_NODISCARD constexpr Dim y() const noexcept
Get the y value.
Definition geometry.h:194
StaticGrid with selectable cells.
Definition grid.h:353
EGT_NODISCARD GridPoint selected() const
Get the selected cell.
Definition grid.h:400
SelectableGrid(Serializer::Properties &props)
Definition grid.h:392
Signal on_selected_changed
Event signal.
Definition grid.h:361
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
void selected(size_t column, size_t row)
Set the selected cell.
SelectableGrid(Frame &parent, const Rect &rect, const GridSize &size=GridSize(1, 1))
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
SelectableGrid(Frame &parent, const GridSize &size=GridSize(1, 1))
SelectableGrid(const GridSize &size=GridSize(1, 1))
void handle(Event &event) override
Handle an event.
void selection_highlight(DefaultDim highlight)
Set the dimension of the selection highlight.
Definition grid.h:418
SelectableGrid(const Rect &rect, const GridSize &size=GridSize(1, 1))
EGT_NODISCARD DefaultDim selection_highlight() const
Get the dimension of the selection highlight.
Definition grid.h:430
SelectableGrid(Serializer::Properties &props, bool is_derived)
Abstract base serializer class.
Definition serialize.h:34
std::list< std::tuple< std::string, std::string, Serializer::Attributes > > Properties
Definition serialize.h:47
Signal class used for defining a signal and dispatching events.
Definition signal.h:30
Static grid organization for widgets.
Definition grid.h:43
EGT_NODISCARD size_t n_row() const
Get the number of rows.
Definition grid.h:231
virtual void add(Widget &widget, const GridPoint &point)
Add a widget to the grid into a specific cell.
Definition grid.h:165
void reallocate(const GridSize &size)
Reallocate the size of the grid keeping any existing cells intact.
void serialize_children(Serializer &serializer) const override
Serialize the widget's children to the specified serializer.
StaticGrid(Serializer::Properties &props)
Definition grid.h:94
CellArray m_cells
Cell array of the grid.
Definition grid.h:319
void reposition()
Re-position all child widgets.
EGT_NODISCARD size_t n_col() const
Get the number of columns.
Definition grid.h:223
void add(const std::shared_ptr< Widget > &widget) override
Add a widget to the next empty cell.
StaticGrid(const Rect &rect, const GridSize &size=GridSize(1, 1))
EGT_NODISCARD GridSize grid_size() const
Get the GridSize.
Definition grid.h:239
void layout() override
Perform layout of the Widget.
std::vector< std::vector< std::weak_ptr< Widget > > > CellArray
Type for cell array.
Definition grid.h:316
EGT_NODISCARD DefaultDim horizontal_space() const
Get the horizontal space.
Definition grid.h:271
Widget * get(const GridPoint &point)
Get a widget at the specified row and column.
void deserialize_children(const Deserializer &deserializer) override
Deserialize the children of this widget.
StaticGrid(Frame &parent, const GridSize &size=GridSize(1, 1))
EGT_NODISCARD bool column_priority() const
Get the column priority status.
Definition grid.h:210
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
virtual void add(const std::shared_ptr< Widget > &widget, size_t column, size_t row)
Add a widget to the grid into a specific cell.
virtual void add(Widget &widget, size_t column, size_t row)
Add a widget to the grid into a specific cell.
Definition grid.h:136
void horizontal_space(DefaultDim space)
Set the horizontal space i.e.
Definition grid.h:259
void remove(Widget *widget) override
Remove a child widget.
EGT_NODISCARD int last_add_row() const
Returns the last row used for an add() call.
Definition grid.h:202
void column_priority(bool value)
Set the column priority status.
Definition grid.h:218
virtual void add(const std::shared_ptr< Widget > &widget, const GridPoint &point)
Add a widget to the grid into a specific cell.
Definition grid.h:154
GridFlag
Grid flags.
Definition grid.h:48
EGT_NODISCARD DefaultDim vertical_space() const
Get the vertical space.
Definition grid.h:291
EGT_NODISCARD int last_add_column() const
Returns the last column used for an add() call.
Definition grid.h:192
void vertical_space(DefaultDim space)
Set the vertical space i.e.
Definition grid.h:279
StaticGrid(Frame &parent, const Rect &rect, const GridSize &size=GridSize(1, 1))
StaticGrid(const GridSize &size=GridSize(1, 1))
StaticGrid(Serializer::Properties &props, bool is_derived)
void grid_size(const GridSize size)
Set the GridSize.
Definition grid.h:247
Base Widget class.
Definition widget.h:53
int DefaultDim
Define the default dimension type used for geometry.
Definition geometry.h:34
EGT framework namespace.
Definition animation.h:24
A single event that has information about the event and state for the event.
Definition event.h:255