2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <AK/Assertions.h>
|
2019-03-25 04:25:25 +01:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Painter.h>
|
2021-04-13 16:18:20 +02:00
|
|
|
#include <LibGUI/Progressbar.h>
|
2020-02-14 23:53:11 +01:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-03-22 02:49:14 +01:00
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
REGISTER_WIDGET(GUI, Progressbar)
|
|
|
|
|
REGISTER_WIDGET(GUI, VerticalProgressbar)
|
|
|
|
|
REGISTER_WIDGET(GUI, HorizontalProgressbar)
|
2021-01-02 16:30:13 -07:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
Progressbar::Progressbar(Orientation orientation)
|
2021-03-07 17:39:01 -05:00
|
|
|
: m_orientation(orientation)
|
2019-03-22 02:49:14 +01:00
|
|
|
{
|
2023-03-10 18:02:14 +01:00
|
|
|
REGISTER_DEPRECATED_STRING_PROPERTY("text", text, set_text);
|
2020-12-30 13:58:38 +03:30
|
|
|
REGISTER_ENUM_PROPERTY("format", format, set_format, Format,
|
|
|
|
|
{ Format::NoText, "NoText" },
|
|
|
|
|
{ Format::Percentage, "Percentage" },
|
|
|
|
|
{ Format::ValueSlashMax, "ValueSlashMax" });
|
|
|
|
|
REGISTER_INT_PROPERTY("min", min, set_min);
|
|
|
|
|
REGISTER_INT_PROPERTY("max", max, set_max);
|
2023-02-10 01:10:19 +01:00
|
|
|
|
|
|
|
|
set_preferred_size(SpecialDimension::Fit);
|
2019-03-22 02:49:14 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
void Progressbar::set_value(int value)
|
2019-03-22 02:49:14 +01:00
|
|
|
{
|
|
|
|
|
if (m_value == value)
|
|
|
|
|
return;
|
|
|
|
|
m_value = value;
|
|
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
void Progressbar::set_range(int min, int max)
|
2019-03-22 02:49:14 +01:00
|
|
|
{
|
2021-04-13 10:08:38 +02:00
|
|
|
VERIFY(min <= max);
|
2019-03-22 02:49:14 +01:00
|
|
|
m_min = min;
|
|
|
|
|
m_max = max;
|
2020-01-20 13:16:58 +01:00
|
|
|
m_value = clamp(m_value, m_min, m_max);
|
2019-03-22 02:49:14 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
void Progressbar::paint_event(PaintEvent& event)
|
2019-03-22 02:49:14 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
Frame::paint_event(event);
|
2019-03-28 20:15:13 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Painter painter(*this);
|
2019-03-28 20:15:13 +01:00
|
|
|
auto rect = frame_inner_rect();
|
2019-03-29 15:01:54 +01:00
|
|
|
painter.add_clip_rect(rect);
|
|
|
|
|
painter.add_clip_rect(event.rect());
|
2019-03-22 02:49:14 +01:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString progress_text;
|
2019-04-11 06:08:06 +02:00
|
|
|
if (m_format != Format::NoText) {
|
|
|
|
|
// Then we draw the progress text over the gradient.
|
|
|
|
|
// We draw it twice, once offset (1, 1) for a drop shadow look.
|
|
|
|
|
StringBuilder builder;
|
2020-12-20 12:29:40 +01:00
|
|
|
builder.append(m_text);
|
2019-08-14 20:31:46 +02:00
|
|
|
if (m_format == Format::Percentage) {
|
|
|
|
|
float range_size = m_max - m_min;
|
|
|
|
|
float progress = (m_value - m_min) / range_size;
|
2021-05-07 16:32:31 +02:00
|
|
|
builder.appendff("{}%", (int)(progress * 100));
|
2019-08-14 20:31:46 +02:00
|
|
|
} else if (m_format == Format::ValueSlashMax) {
|
2021-05-07 16:32:31 +02:00
|
|
|
builder.appendff("{}/{}", m_value, m_max);
|
2019-08-14 20:31:46 +02:00
|
|
|
}
|
2023-12-16 17:49:34 +03:30
|
|
|
progress_text = builder.to_byte_string();
|
2019-04-11 06:08:06 +02:00
|
|
|
}
|
2019-03-22 02:49:14 +01:00
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
Gfx::StylePainter::paint_progressbar(painter, rect, palette(), m_min, m_max, m_value, progress_text, m_orientation);
|
2021-03-07 17:39:01 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
void Progressbar::set_orientation(Orientation value)
|
2021-03-07 17:39:01 -05:00
|
|
|
{
|
|
|
|
|
if (m_orientation == value)
|
|
|
|
|
return;
|
|
|
|
|
m_orientation = value;
|
|
|
|
|
update();
|
2019-03-22 02:49:14 +01:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
2023-02-10 01:10:19 +01:00
|
|
|
Optional<UISize> Progressbar::calculated_preferred_size() const
|
|
|
|
|
{
|
|
|
|
|
if (orientation() == Gfx::Orientation::Vertical)
|
|
|
|
|
return { { 22, SpecialDimension::OpportunisticGrow } };
|
|
|
|
|
return { { SpecialDimension::OpportunisticGrow, 22 } };
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|