1.11
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
187 void add_at(const std::shared_ptr<Widget>& widget, size_t pos) override;
188
196 Widget* get(const GridPoint& point);
197
198 void remove(Widget* widget) override;
199
208 void remove_at(size_t pos) override;
209
210 void layout() override;
211
217 EGT_NODISCARD int last_add_column() const
218 {
219 return m_last_add_column;
220 }
221
227 EGT_NODISCARD int last_add_row() const
228 {
229 return m_last_add_row;
230 }
231
235 EGT_NODISCARD bool column_priority() const { return m_column_priority; }
236
243 void column_priority(bool value) { m_column_priority = value; }
244
248 EGT_NODISCARD size_t n_col() const
249 {
250 return m_grid_size.width();
251 }
252
256 EGT_NODISCARD size_t n_row() const
257 {
258 return m_grid_size.height();
259 }
260
264 EGT_NODISCARD GridSize grid_size() const
265 {
266 return m_grid_size;
267 }
268
272 void grid_size(const GridSize size)
273 {
274 if (detail::change_if_diff<>(m_grid_size, size))
275 {
276 reallocate(size);
277 reposition();
278 }
279 }
280
285 {
286 if (detail::change_if_diff<>(m_horizontal_space, space))
287 {
288 layout();
289 damage();
290 }
291 }
292
296 EGT_NODISCARD DefaultDim horizontal_space() const
297 {
298 return m_horizontal_space;
299 }
300
305 {
306 if (detail::change_if_diff<>(m_vertical_space, space))
307 {
308 layout();
309 damage();
310 }
311 }
312
316 EGT_NODISCARD DefaultDim vertical_space() const
317 {
318 return m_vertical_space;
319 }
320
321 void serialize(Serializer& serializer) const override;
322
323 void serialize_children(Serializer& serializer) const override;
324
325 void deserialize_children(const Deserializer& deserializer) override;
326
327protected:
328
330 void reallocate(const GridSize& size);
331
339
341 using CellArray = std::vector<std::vector<std::weak_ptr<Widget>>>;
342
345
347 int m_last_add_column{-1};
349 int m_last_add_row{-1};
351 bool m_column_priority{false};
353 GridSize m_grid_size{};
355 DefaultDim m_horizontal_space{};
357 DefaultDim m_vertical_space{};
358
359private:
360
361 void deserialize(Serializer::Properties& props);
362};
363
376// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
377class EGT_API SelectableGrid : public StaticGrid
378{
379public:
380
391 explicit SelectableGrid(const GridSize& size = GridSize(1, 1));
392
397 explicit SelectableGrid(const Rect& rect,
398 const GridSize& size = GridSize(1, 1));
399
405 SelectableGrid(Frame& parent, const Rect& rect,
406 const GridSize& size = GridSize(1, 1));
407
412 explicit SelectableGrid(Frame& parent,
413 const GridSize& size = GridSize(1, 1));
418 : SelectableGrid(props, false)
419 {
420 }
421
422 void draw(Painter& painter, const Rect& rect) override;
423
425 EGT_NODISCARD GridPoint selected() const
426 {
427 return {m_selected_column, m_selected_row};
428 }
429
430 void handle(Event& event) override;
431
438 void selected(size_t column, size_t row);
439
444 {
445 if (detail::change_if_diff<>(m_selection_highlight, highlight))
446 {
447 layout();
448 damage();
449 }
450 }
451
455 EGT_NODISCARD DefaultDim selection_highlight() const
456 {
457 return m_selection_highlight;
458 }
459
460 void serialize(Serializer& serializer) const override;
461
462protected:
464 size_t m_selected_column{0};
466 size_t m_selected_row{0};
468 DefaultDim m_selection_highlight{5};
469
470 explicit SelectableGrid(Serializer::Properties& props, bool is_derived);
471
472private:
473
474 void deserialize(Serializer::Properties& props);
475};
476
477}
478}
479
480#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:54
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:378
EGT_NODISCARD GridPoint selected() const
Get the selected cell.
Definition grid.h:425
SelectableGrid(Serializer::Properties &props)
Definition grid.h:417
Signal on_selected_changed
Event signal.
Definition grid.h:386
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:443
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:455
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:256
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:344
void reposition()
Re-position all child widgets.
EGT_NODISCARD size_t n_col() const
Get the number of columns.
Definition grid.h:248
void add(const std::shared_ptr< Widget > &widget) override
Add a widget to the next empty cell.
void add_at(const std::shared_ptr< Widget > &widget, size_t pos) override
Add a widget at a specific position.
StaticGrid(const Rect &rect, const GridSize &size=GridSize(1, 1))
EGT_NODISCARD GridSize grid_size() const
Get the GridSize.
Definition grid.h:264
void layout() override
Perform layout of the Widget.
std::vector< std::vector< std::weak_ptr< Widget > > > CellArray
Type for cell array.
Definition grid.h:341
EGT_NODISCARD DefaultDim horizontal_space() const
Get the horizontal space.
Definition grid.h:296
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:235
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:284
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:227
void column_priority(bool value)
Set the column priority status.
Definition grid.h:243
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:316
void remove_at(size_t pos) override
Remove the widget at the specific position.
EGT_NODISCARD int last_add_column() const
Returns the last column used for an add() call.
Definition grid.h:217
void vertical_space(DefaultDim space)
Set the vertical space i.e.
Definition grid.h:304
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:272
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