6#ifndef EGT_IMAGE_HOLDER_H
7#define EGT_IMAGE_HOLDER_H
14#include "egt/detail/alignment.h"
15#include <egt/detail/meta.h>
17#include <egt/geometry.h>
19#include <egt/painter.h>
20#include "egt/serialize.h"
22#include <egt/textwidget.h>
23#include <egt/widget.h>
42 const AlignFlags& text_align = T::default_text_align())
noexcept
52 const std::string& text = {},
53 const AlignFlags& text_align = T::default_text_align())
noexcept
64 const std::string& text,
66 const AlignFlags& text_align = T::default_text_align()) noexcept
67 : T(text, rect, text_align)
69 this->name(type() + std::to_string(this->m_widgetid));
74 image_align(AlignFlag::center | AlignFlag::expand);
86 const Image& image = {},
87 const std::string& text = {},
88 const AlignFlags& text_align = T::default_text_align())
noexcept
103 const std::string& text,
105 const AlignFlags& text_align = T::default_text_align()) noexcept
125 EGT_NODISCARD std::
string type()
const override
127 auto t = detail::demangle(
typeid(T).name());
128 return std::string(
"Image") + detail::replace_all(t,
"egt::v1::", {});
134 EGT_NODISCARD std::string
uri()
const
136 return m_image.uri();
144 void uri(
const std::string& uri)
162 EGT_NODISCARD
float hscale()
const {
return m_image.hscale(); }
169 m_image.scale(hscale, m_image.vscale());
176 EGT_NODISCARD
float vscale()
const {
return m_image.vscale(); }
183 m_image.scale(m_image.hscale(), vscale);
192 m_image.scale(scale);
238 bool approximate =
false)
240 m_image.scale(hscale, vscale, approximate);
241 this->parent_layout();
255 scale_image(scale, scale, approximate);
267 m_image_align.set(AlignFlag::expand);
269 m_image_align.clear(AlignFlag::expand);
278 return m_image_align.is_set(AlignFlag::expand);
318 if (detail::change_if_diff<>(m_image_align,
align))
334 if (detail::change_if_diff<>(m_show_label, value))
341 EGT_NODISCARD
bool show_label()
const {
return m_show_label; }
351 detail::ignoreparam(rect);
353 widget.draw_box(painter, id_bg, id_border);
357 const std::string& text = widget.text();
359 if (!widget.
image().empty())
361 detail::draw_text(painter,
362 widget.content_area(),
367 Justification::middle,
368 widget.color(id_text),
374 detail::draw_text(painter,
375 widget.content_area(),
380 Justification::middle,
381 widget.color(id_text));
384 else if (!widget.
image().empty())
386 auto target = widget.
image().align(widget.content_area(), widget.
image_align());
387 painter.
draw(target.point());
399 this->deserialize_leaf(props);
409 void do_set_image(
const Image& image)
411 if (this->size().empty() && !image.
empty())
412 this->resize(image.
size() +
Size(this->moat() * 2, this->moat() * 2));
419 using T::min_size_hint;
423 if (!this->m_min_size.empty())
424 return this->m_min_size;
426 Size min_size_constraint;
427 if (this->show_label() && !this->text().empty())
428 min_size_constraint = T::min_size_hint();
430 min_size_constraint = Widget::min_size_hint();
432 if (!show_label() && auto_scale_image())
433 return min_size_constraint;
435 Rect size = min_size_constraint -
Size(this->moat() * 2, this->moat() * 2);
437 if (!m_image.size().empty())
439 if (image_align().is_set(AlignFlag::left) ||
440 image_align().is_set(AlignFlag::right))
442 size +=
Size(m_image.width(), 0);
444 else if (image_align().is_set(AlignFlag::top) ||
445 image_align().is_set(AlignFlag::bottom))
447 size +=
Size(0, m_image.height());
450 size = Rect::merge(size, m_image.size());
453 auto res = size.size() +
Size(this->moat() * 2, this->moat() * 2);
459 T::serialize(serializer);
462 if (!m_image.empty())
463 m_image.serialize(
"image", serializer);
464 if (!image_align().empty())
472 m_image_align.clear();
473 props.erase(std::remove_if(props.begin(), props.end(), [&](
auto & p)
475 switch (detail::hash(std::get<0>(p)))
477 case detail::hash(
"showlabel"):
478 show_label(egt::detail::from_string(std::get<1>(p)));
480 case detail::hash(
"image"):
481 m_image.deserialize(std::get<0>(p), std::get<1>(p), std::get<2>(p));
483 case detail::hash(
"image_align"):
484 image_align(AlignFlags(std::get<1>(p)));
497 bool m_show_label{
true};
static constexpr ExpandBitField expand
Definition widgetflags.h:301
static constexpr HorizontalBitField left
Definition widgetflags.h:290
Alignment flags.
Definition widgetflags.h:379
Manager of the Drawable for each widget type.
Definition theme.h:104
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
virtual void add(const std::shared_ptr< Widget > &widget)
Add a child widget.
Definition imageholder.h:35
void image_scale(float scale)
Scale the image in both direction with the same ratio.
Definition imageholder.h:190
EGT_NODISCARD float hscale() const
Get the horizontal scale value.
Definition imageholder.h:162
EGT_NODISCARD Size min_size_hint() const override
Definition imageholder.h:421
ImageHolder(Serializer::Properties &props, bool is_derived) noexcept
Definition imageholder.h:393
void draw(Painter &painter, const Rect &rect) override
Definition imageholder.h:343
EGT_NODISCARD bool show_label() const
Get the show label state.
Definition imageholder.h:341
void vscale(float vscale)
Set the vertical scale value.
Definition imageholder.h:181
void deserialize(Serializer::Properties &props)
Definition imageholder.h:469
void keep_image_ratio(bool enable)
Enable/disable ratio preservation while scaling the image.
Definition imageholder.h:287
bool keep_image_ratio() const
Get the keep image ratio state.
Definition imageholder.h:296
Image & image()
Get a non-const reference to the image.
Definition imageholder.h:309
EGT_NODISCARD float vscale() const
Get the vertical scale value.
Definition imageholder.h:176
void image_align(const AlignFlags &align)
Set the alignment of the image relative to the text.
Definition imageholder.h:316
void refresh()
Definition imageholder.h:402
void hscale(float hscale)
Set the horizontal scale value.
Definition imageholder.h:167
static void default_draw(ImageHolder &widget, Painter &painter, const Rect &rect)
Default draw method for the widget.
Definition imageholder.h:349
ImageHolder(Frame &parent, const Image &image, const std::string &text, const Rect &rect, const AlignFlags &text_align=T::default_text_align()) noexcept
Definition imageholder.h:101
ImageHolder(const std::string &text={}, const AlignFlags &text_align=T::default_text_align()) noexcept
Definition imageholder.h:41
void image(const Image &image)
Set a new Image.
Definition imageholder.h:215
void scale_image(float hscale, float vscale, bool approximate=false)
Scale the image.
Definition imageholder.h:237
EGT_NODISCARD std::string uri() const
Get the URI of the current image.
Definition imageholder.h:134
ImageHolder & operator=(const ImageHolder &) noexcept=delete
void image_scale(const SizeF &scale)
Scale the image in both direction.
Definition imageholder.h:199
EGT_NODISCARD AlignFlags image_align() const
Get the image alignment.
Definition imageholder.h:325
ImageHolder(Frame &parent, const Image &image={}, const std::string &text={}, const AlignFlags &text_align=T::default_text_align()) noexcept
Definition imageholder.h:85
void uri(const std::string &uri)
Load a new Image from an uri.
Definition imageholder.h:144
EGT_NODISCARD const Image & image() const
Get a const reference of the image.
Definition imageholder.h:304
void serialize(Serializer &serializer) const override
Definition imageholder.h:457
void auto_scale_image(bool enable)
Enable/disable the auto scale of the image.
Definition imageholder.h:264
ImageHolder(ImageHolder &&) noexcept=default
void show_label(bool value)
Enable/disable showing the label text.
Definition imageholder.h:332
EGT_NODISCARD SizeF image_scale() const
Get both scale values.
Definition imageholder.h:208
ImageHolder(Serializer::Properties &props) noexcept
Definition imageholder.h:114
ImageHolder(const Image &image, const std::string &text={}, const AlignFlags &text_align=T::default_text_align()) noexcept
Definition imageholder.h:51
void scale_image(float scale, bool approximate=false)
Scale the image.
Definition imageholder.h:253
ImageHolder(const ImageHolder &) noexcept=delete
bool auto_scale_image() const
Get the auto scale image state.
Definition imageholder.h:276
void reset_uri()
Reset the URI, therefore clear the current image, if any.
Definition imageholder.h:153
Image m_image
The image. Allowed to be empty.
Definition imageholder.h:494
ImageHolder(const Image &image, const std::string &text, const Rect &rect, const AlignFlags &text_align=T::default_text_align()) noexcept
Definition imageholder.h:63
Raster image resource used for drawing or displaying.
Definition image.h:39
void keep_image_ratio(bool enable)
Enable/disable ratio preservation while scaling the image.
Definition image.h:223
EGT_NODISCARD Size size() const
Get the absolute size of the image.
Definition image.h:158
EGT_NODISCARD bool empty() const
Returns true if no internal surface is set.
Definition image.h:180
Drawing interface for 2D graphics.
Definition painter.h:45
Painter & draw(const PointType< T, detail::Compatible::normal > &point)
Move to a point.
Definition painter.h:182
ColorId
The Pattern identifier in the Palette.
Definition palette.h:239
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
EGT_NODISCARD constexpr Dim width() const noexcept
Get the width value.
Definition geometry.h:457
EGT_NODISCARD constexpr Dim height() const noexcept
Get the height value.
Definition geometry.h:459
T & align(T &widget, const AlignFlags &a)
Helper to set alignment of a widget.
Definition widgetflags.h:624
EGT framework namespace.
Definition animation.h:24