6#ifndef EGT_PROGRESSBAR_H
7#define EGT_PROGRESSBAR_H
14#include <egt/detail/alignment.h>
15#include <egt/detail/enum.h>
16#include <egt/detail/math.h>
17#include <egt/detail/meta.h>
18#include <egt/detail/screen/composerscreen.h>
20#include <egt/painter.h>
21#include <egt/serialize.h>
22#include <egt/textwidget.h>
23#include <egt/valuewidget.h>
59 T start = {}, T end = 100, T value = {})
noexcept
62 this->name(
"ProgressBar" + std::to_string(this->m_widgetid));
63 this->fill_flags(Theme::FillFlag::blend);
64 this->border(this->theme().default_border());
75 T
start = {}, T end = 100, T value = {})
noexcept
76 : ProgressBarType(rect, start, end, value)
97 this->deserialize_leaf(props);
110 widget.
draw_box(painter, Palette::ColorId::bg, Palette::ColorId::border);
113 auto style = widget.
style();
114 auto draw_bar =
false;
117 if (style == ProgressBarStyle::left_to_right ||
118 style == ProgressBarStyle::right_to_left)
126 widget.
ending(), 0, b.width());
128 if (width && b.height())
131 if (style == ProgressBarStyle::right_to_left)
132 x = b.x() + b.width() - width;
133 r =
Rect(x, b.y(), width, b.height());
137 else if (style == ProgressBarStyle::top_to_bottom ||
138 style == ProgressBarStyle::bottom_to_top)
146 widget.
ending(), 0, b.height());
148 if (height && b.width())
151 if (style == ProgressBarStyle::bottom_to_top)
152 y = b.y() + b.height() - height;
153 r =
Rect(b.x(), y, b.width(), height);
161 Theme::FillFlag::blend,
164 widget.
color(Palette::ColorId::button_bg),
172 std::string text = std::to_string(widget.
value());
175 auto percentage = detail::normalize<float>(widget.
value(),
178 text = std::to_string(
static_cast<int>(percentage)) +
"%";
180 auto f = TextWidget::scale_font(
Size(b.width() * 0.75,
182 text, widget.
font());
184 painter.
set(widget.
color(Palette::ColorId::label_text));
187 auto target = detail::align_algorithm(size, b, AlignFlag::center);
188 painter.
draw(target.point());
202 if (!this->m_min_size.empty())
203 return this->m_min_size;
205 return default_size() + Widget::min_size_hint();
215 if (m_show_label != value)
217 m_show_label = value;
225 EGT_NODISCARD
bool show_label()
const {
return m_show_label; }
234 if (m_show_percentage != enable)
236 m_show_percentage = enable;
253 if (detail::change_if_diff<>(m_style, style))
270 bool m_show_label{
true};
275 bool m_show_percentage{
false};
284 static Size m_default_size;
286 static void register_handler();
287 static void unregister_handler();
298 if (show_percentage())
299 serializer.
add_property(
"show_percentage", show_percentage());
307 props.erase(std::remove_if(props.begin(), props.end(), [&](
auto & p)
309 if (std::get<0>(p) ==
"show_label")
311 m_show_label = detail::from_string(std::get<1>(p));
314 else if (std::get<0>(p) ==
"show_percentage")
319 else if (std::get<0>(p) ==
"style")
321 style(detail::enum_from_string<ProgressBarStyle>(std::get<1>(p)));
338 EGT_NODISCARD std::string
type()
const override
340 return "ProgressBar";
354 EGT_NODISCARD std::string
type()
const override
356 return "ProgressBarF";
373 m_default_size.clear();
379void ProgressBarType<T>::unregister_handler()
382 m_default_size_handle = Signal<>::INVALID_HANDLE;
391 auto ss = egt::Application::instance().
screen()->
size();
402 unregister_handler();
430 T
start = 0, T end = 100, T value = 0)
noexcept
433 this->name(
"SpinProgress" + std::to_string(this->m_widgetid));
434 this->fill_flags(Theme::FillFlag::blend);
445 T
start = 0, T end = 100, T value = 0)
noexcept
467 this->deserialize_leaf(props);
480 detail::ignoreparam(rect);
482 widget.
draw_box(painter, Palette::ColorId::bg, Palette::ColorId::border);
486 auto dim = std::min(b.width(), b.height());
487 float linew = dim / 10.0f;
488 float radius = dim / 2.0f - (linew / 2.0f);
489 auto angle1 = detail::to_radians<float>(180, 0);
493 auto angle2 = detail::to_radians<float>(180.0f,
494 detail::normalize_to_angle(
static_cast<float>(widget.
value()),
495 static_cast<float>(min),
static_cast<float>(max), 0.0f, 360.0f,
true));
498 painter.
set(widget.
color(Palette::ColorId::button_fg, Palette::GroupId::disabled));
499 painter.
draw(
Arc(widget.
center(), radius, 0.0f, 2 * detail::pi<float>()));
502 painter.
set(widget.
color(Palette::ColorId::button_fg));
508 std::string text = std::to_string(widget.
value());
509 auto f = TextWidget::scale_font(
Size(dim * 0.75f, dim * 0.75f), text, widget.
font());
511 painter.
set(widget.
color(Palette::ColorId::text));
513 auto target = detail::align_algorithm(size, b, AlignFlag::center);
514 painter.
draw(target.point());
528 if (!this->m_min_size.empty())
529 return this->m_min_size;
531 return default_size() + Widget::min_size_hint();
541 if (m_show_label != value)
543 m_show_label = value;
551 EGT_NODISCARD
bool show_label()
const {
return m_show_label; }
557 bool m_show_label{
true};
561 static Size m_default_size;
563 static void register_handler();
564 static void unregister_handler();
579 EGT_NODISCARD std::string
type()
const override
581 return "SpinProgress";
595 EGT_NODISCARD std::string
type()
const override
597 return "SpinProgressF";
614 m_default_size.clear();
620void SpinProgressType<T>::unregister_handler()
623 m_default_size_handle = Signal<>::INVALID_HANDLE;
632 auto ss = egt::Application::instance().
screen()->
size();
642 unregister_handler();
652 serializer.
add_property(
"show_label", detail::to_string(this->m_show_label));
658 props.erase(std::remove_if(props.begin(), props.end(), [&](
auto & p)
660 if (std::get<0>(p) ==
"show_label")
662 m_show_label = detail::from_string(std::get<1>(p));
691 T
start = 0, T end = 100, T value = 0)
noexcept
694 this->
name(
"LevelMeter" + std::to_string(this->m_widgetid));
708 T
start = 0, T end = 100, T value = 0)
noexcept
743 widget.
draw_box(painter, Palette::ColorId::bg, Palette::ColorId::border);
747 const auto limit = egt::detail::normalize<float>(widget.
value(),
751 const auto barheight = b.height() / widget.
num_bars();
755 for (
size_t i = 0; i < widget.
num_bars(); i++)
757 auto color = widget.
color(Palette::ColorId::button_fg, Palette::GroupId::disabled);
759 color = widget.
color(Palette::ColorId::button_fg);
761 Rect rect(b.x(), b.y() + i * barheight, b.width(), barheight - widget.
padding());
764 Theme::FillFlag::blend,
766 widget.
color(Palette::ColorId::border),
777 if (bars && detail::change_if_diff(m_num_bars, bars))
784 EGT_NODISCARD
size_t num_bars()
const {
return m_num_bars; }
795 if (!this->m_min_size.empty())
798 return default_size() + Widget::min_size_hint();
806 size_t m_num_bars{10};
810 static Size m_default_size;
812 static void register_handler();
813 static void unregister_handler();
828 EGT_NODISCARD std::string
type()
const override
844 EGT_NODISCARD std::string
type()
const override
846 return "LevelMeterF";
861 m_default_size_handle = detail::ComposerScreen::register_screen_resize_hook([]()
863 m_default_size.clear();
869void LevelMeterType<T>::unregister_handler()
871 detail::ComposerScreen::unregister_screen_resize_hook(m_default_size_handle);
872 m_default_size_handle = Signal<>::INVALID_HANDLE;
881 auto ss = egt::Application::instance().
screen()->
size();
891 unregister_handler();
901 serializer.
add_property(
"num_bars", std::to_string(this->m_num_bars));
907 props.erase(std::remove_if(props.begin(), props.end(), [&](
auto & p)
909 if (std::get<0>(p) ==
"num_bars")
911 m_num_bars = std::stoi(std::get<1>(p));
939 this->
name(
"AnalogMeter" + std::to_string(this->m_widgetid));
966 this->
name(
"AnalogMeter" + std::to_string(this->m_widgetid));
983 detail::ignoreparam(rect);
985 static const auto tick_width = 1.0f;
987 widget.
draw_box(painter, Palette::ColorId::bg, Palette::ColorId::border);
992 const auto text_size = painter.
text_size(
"999");
994 painter.
translate(
Point(b.center().x(), b.y() + b.height() - text_size.height()));
996 const auto dim = std::min<DefaultDim>(b.width() / 2, b.height());
997 const float hw = dim - (text_size.width() * 2.0);
1000 for (
auto tick = 0; tick <= 100; tick += 10)
1002 const auto xangle = std::cos(detail::pi<float>() * tick * 0.01f);
1003 const auto yangle = std::sin(detail::pi<float>() * tick * 0.01f);
1004 painter.
set(widget.
color(Palette::ColorId::button_fg,
1005 Palette::GroupId::disabled));
1008 Point((hw + 10.0f) * xangle,
1009 -(hw + 10.0f) * yangle));
1012 int text =
static_cast<int>(detail::normalize<float>(tick, 0, 100, widget.
starting(), widget.
ending()));
1013 painter.
set(widget.
color(Palette::ColorId::text));
1014 const auto size = painter.
text_size(std::to_string(text));
1015 painter.
draw(
Point(-(hw + 30.0f) * xangle - size.width() / 2.0f,
1016 -(hw + 30.0f) * yangle - size.height() / 2.0f));
1017 painter.
draw(std::to_string(text));
1022 auto avalue = detail::normalize<float>(widget.
value(), widget.
starting(), widget.
ending(), 0, 100);
1023 const auto dest =
Point((-hw - 15.0f) * std::cos(detail::pi<float>() * avalue * 0.01f),
1024 (-hw - 15.0f) * std::sin(detail::pi<float>() * avalue * 0.01f));
1026 painter.
set(widget.
color(Palette::ColorId::button_fg));
1044 if (!this->m_min_size.empty())
1047 return default_size() + Widget::min_size_hint();
1053 static Size m_default_size;
1055 static void register_handler();
1056 static void unregister_handler();
1069 EGT_NODISCARD std::string
type()
const override
1071 return "AnalogMeter";
1085 EGT_NODISCARD std::string
type()
const override
1087 return "AnalogMeterF";
1102 m_default_size_handle = detail::ComposerScreen::register_screen_resize_hook([]()
1104 m_default_size.clear();
1110void AnalogMeterType<T>::unregister_handler()
1112 detail::ComposerScreen::unregister_screen_resize_hook(m_default_size_handle);
1113 m_default_size_handle = Signal<>::INVALID_HANDLE;
1122 auto ss = egt::Application::instance().
screen()->
size();
1132 unregister_handler();
This is a level meter that can be used to display floating values.
Definition progressbar.h:1081
EGT_NODISCARD std::string type() const override
Returns a string representation of the type of this widget.
Definition progressbar.h:1085
Displays an analog meter based on a percentage value.
Definition progressbar.h:930
EGT_NODISCARD Size min_size_hint() const override
Get a minimum size hint for the Widget.
Definition progressbar.h:1042
static void default_draw(AnalogMeterType< T > &widget, Painter &painter, const Rect &rect)
Default draw method for the widget.
Definition progressbar.h:981
AnalogMeterType(Frame &parent, const Rect &rect={}) noexcept
Definition progressbar.h:947
static Size default_size()
Default ProgressBar size.
Definition progressbar.h:1117
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
Definition progressbar.h:975
static void default_size(const Size &size)
Change default ProgressBar size.
Definition progressbar.h:1129
AnalogMeterType(const Rect &rect={}) noexcept
Definition progressbar.h:936
AnalogMeterType(Serializer::Properties &props) noexcept
Definition progressbar.h:956
AnalogMeterType(Serializer::Properties &props, bool is_derived) noexcept
Definition progressbar.h:963
This is a level meter that can be used to display integer values.
Definition progressbar.h:1065
EGT_NODISCARD std::string type() const override
Returns a string representation of the type of this widget.
Definition progressbar.h:1069
EGT_NODISCARD Screen * screen() const
Get a pointer to the Screen instance.
Definition app.h:119
An Arc consists of a radius and two angles.
Definition geometry.h:1123
32 bit RGBA color.
Definition color.h:41
Manager of the Drawable for each widget type.
Definition theme.h:104
A Frame is a Widget that has children widgets.
Definition frame.h:45
virtual void add(const std::shared_ptr< Widget > &widget)
Add a child widget.
This is a level meter that can be used to display floating values.
Definition progressbar.h:840
EGT_NODISCARD std::string type() const override
Returns a string representation of the type of this widget.
Definition progressbar.h:844
Displays a level meter based on a value.
Definition progressbar.h:681
EGT_NODISCARD Size min_size_hint() const override
Get a minimum size hint for the Widget.
Definition progressbar.h:793
static Size default_size()
Default LevelMeter size.
Definition progressbar.h:876
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
Definition progressbar.h:735
static void default_size(const Size &size)
Change default LevelMeter size.
Definition progressbar.h:888
EGT_NODISCARD size_t num_bars() const
Get the number of bars to show.
Definition progressbar.h:784
LevelMeterType(Serializer::Properties &props) noexcept
Definition progressbar.h:717
LevelMeterType(Frame &parent, const Rect &rect={}, T start=0, T end=100, T value=0) noexcept
Definition progressbar.h:707
static void default_draw(LevelMeterType< T > &widget, Painter &painter, const Rect &)
Default draw method for the widget.
Definition progressbar.h:741
LevelMeterType(const Rect &rect={}, T start=0, T end=100, T value=0) noexcept
Definition progressbar.h:690
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
Definition progressbar.h:897
void num_bars(size_t bars)
Set the number of bars to show.
Definition progressbar.h:775
LevelMeterType(Serializer::Properties &props, bool is_derived) noexcept
Definition progressbar.h:724
This is a level meter that can be used to display integer values.
Definition progressbar.h:824
EGT_NODISCARD std::string type() const override
Returns a string representation of the type of this widget.
Definition progressbar.h:828
EGT_NODISCARD const std::string & name() const
Get the name of the Object.
Definition object.h:42
Drawing interface for 2D graphics.
Definition painter.h:45
Painter & set(const Pattern &pattern)
Set the current color.
Painter & line_width(float width)
Set the current line width.
Painter & draw(const PointType< T, detail::Compatible::normal > &point)
Move to a point.
Definition painter.h:182
Painter & translate(const PointF &point)
Size text_size(const std::string &text)
This is a progress bar that can be used to display floating values.
Definition progressbar.h:350
EGT_NODISCARD std::string type() const override
Returns a string representation of the type of this widget.
Definition progressbar.h:354
Displays a progress bar based on a value.
Definition progressbar.h:49
EGT_NODISCARD Size min_size_hint() const override
Get a minimum size hint for the Widget.
Definition progressbar.h:200
ProgressBarType(Serializer::Properties &props) noexcept
Definition progressbar.h:84
static Size default_size()
Default ProgressBar size.
Definition progressbar.h:386
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
Definition progressbar.h:102
EGT_NODISCARD bool show_label() const
Get the show label state.
Definition progressbar.h:225
static void default_size(const Size &size)
Change default ProgressBar size.
Definition progressbar.h:399
static void default_draw(ProgressBarType< T > &widget, Painter &painter, const Rect &)
Default draw method for the widget.
Definition progressbar.h:108
void show_percentage(bool enable)
Enable/disable showing the label text in percentage.
Definition progressbar.h:232
ProgressBarType(Serializer::Properties &props, bool is_derived) noexcept
Definition progressbar.h:91
EGT_NODISCARD ProgressBarStyle style() const
Get the style of the progress bar.
Definition progressbar.h:262
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
Definition progressbar.h:293
void show_label(bool value)
Enable/disable showing the label text.
Definition progressbar.h:213
void style(ProgressBarStyle style)
Set the style of the progress bar.
Definition progressbar.h:251
ProgressBarType(Frame &parent, const Rect &rect={}, T start={}, T end=100, T value={}) noexcept
Definition progressbar.h:74
EGT_NODISCARD bool show_percentage() const
Get the show label in percentage.
Definition progressbar.h:244
This is a progess bar that can be used to display integer values.
Definition progressbar.h:334
EGT_NODISCARD std::string type() const override
Returns a string representation of the type of this widget.
Definition progressbar.h:338
EGT_NODISCARD Size size() const
Size of the screen.
Definition screen.h:75
Abstract base serializer class.
Definition serialize.h:34
virtual void add_property(const std::string &name, const std::string &value, const Attributes &attrs={})=0
Add a property.
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
uint32_t RegisterHandle
Handle type.
Definition signal.h:46
This is a spinning progress meter that can be used to display floating values.
Definition progressbar.h:591
EGT_NODISCARD std::string type() const override
Returns a string representation of the type of this widget.
Definition progressbar.h:595
Displays a spinning progress meter.
Definition progressbar.h:420
EGT_NODISCARD Size min_size_hint() const override
Get a minimum size hint for the Widget.
Definition progressbar.h:526
static Size default_size()
Default SpinProgress size.
Definition progressbar.h:627
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
Definition progressbar.h:472
EGT_NODISCARD bool show_label() const
Get the show label state.
Definition progressbar.h:551
static void default_size(const Size &size)
Change default SpinProgress size.
Definition progressbar.h:639
SpinProgressType(const Rect &rect={}, T start=0, T end=100, T value=0) noexcept
Definition progressbar.h:429
SpinProgressType(Serializer::Properties &props) noexcept
Definition progressbar.h:454
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
Definition progressbar.h:648
void show_label(bool value)
Enable/disable showing the label text.
Definition progressbar.h:539
static void default_draw(SpinProgressType< T > &widget, Painter &painter, const Rect &rect)
Default draw method for the widget.
Definition progressbar.h:478
SpinProgressType(Serializer::Properties &props, bool is_derived) noexcept
Definition progressbar.h:461
SpinProgressType(Frame &parent, const Rect &rect={}, T start=0, T end=100, T value=0) noexcept
Definition progressbar.h:444
This is a spinning progress meter that can be used to display integer values.
Definition progressbar.h:575
EGT_NODISCARD std::string type() const override
Returns a string representation of the type of this widget.
Definition progressbar.h:579
virtual void draw_box(Painter &painter, const Widget &widget, Palette::ColorId bg, Palette::ColorId border) const
Draw a box using properties directly from the widget.
static Signal ::RegisterHandle register_screen_resize_hook(const Signal<>::EventCallback &handler)
Register a handler to manage screen size changes.
static void unregister_screen_resize_hook(Signal<>::RegisterHandle handle)
Unregister a handler that managed screen size changes.
CircleType< DefaultDim > Circle
Helper type alias.
Definition geometry.h:1259
SizeType< DefaultDim, detail::Compatible::normal > Size
Helper type alias.
Definition geometry.h:573
PointType< DefaultDim, detail::Compatible::normal > Point
Helper type alias.
Definition geometry.h:314
EGT_API bool from_string(const std::string &x)
Convert a lexical std::string to a bool.
constexpr const char * enum_to_string(T const &e)
Convert an enum to a string.
Definition enum.h:55
std::string to_string(const T &x)
Convert a type to a std::string using std::ostringstream.
Definition detail/string.h:110
ProgressBarStyle
ProgressBar Style.
Definition progressbar.h:33
int DefaultDim
Define the default dimension type used for geometry.
Definition geometry.h:34
EGT framework namespace.
Definition animation.h:24
When using enum_to_string() and enum_from_string(), this type needs to be defined and specialized to ...
Definition enum.h:48