~aleteoryx/muditaos

muditaos/module-gui/gui/widgets/ImageBoxWithText.cpp -rw-r--r-- 1.8 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "ImageBoxWithText.hpp"

using namespace gui;

ImageBoxWithText::ImageBoxWithText(Item *parent,
                                   const Position &x,
                                   const Position &y,
                                   const Length &w,
                                   const Length &h,
                                   Image *image,
                                   const UTF8 &description)
    : ImageBox(parent, x, y, w, h, image)
{
    setText(description);
    setMinimumSize(imageBoxWithText::wh, imageBoxWithText::wh);
    image->setEdges(RectangleEdge::All);

    focusChangedCallback = [&](Item &item) {
        if (focus) {
            setEdges(RectangleEdge::Bottom | RectangleEdge::Top);
        }
        else {
            setEdges(RectangleEdge::None);
        }
        return true;
    };
}

ImageBoxWithText::ImageBoxWithText(Item *parent, Image *image, const UTF8 &description)
    : ImageBoxWithText(parent, 0, 0, 0, 0, image, description)
{}

void ImageBoxWithText::setText(const UTF8 &description)
{
    text = new TextFixedSize(this);
    text->drawUnderline(false);
    text->setMargins(Margins(0, imageBoxWithText::text_margin_top, 0, imageBoxWithText::text_margin_bottom));
    text->setFont(imageBoxWithText::font);
    text->setText(description);
    text->setMinimumWidthToFitText(description);
    text->setMinimumHeightToFitText();
    text->setAlignment(Alignment::Horizontal::Center);
    text->activeItem = false;
}

void ImageBoxWithText::setMinimumSizeToFitImage()
{
    auto minW = std::max(image->getWidth(), text->widgetMinimumArea.w);
    auto minH = image->getHeight() + imageBoxWithText::text_margin_top + text->widgetMinimumArea.h;
    setMinimumSize(minW, minH);
}