Progressbar.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Assertions.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGUI/Progressbar.h>
  11. #include <LibGfx/Palette.h>
  12. REGISTER_WIDGET(GUI, Progressbar)
  13. REGISTER_WIDGET(GUI, VerticalProgressbar)
  14. REGISTER_WIDGET(GUI, HorizontalProgressbar)
  15. namespace GUI {
  16. Progressbar::Progressbar(Orientation orientation)
  17. : m_orientation(orientation)
  18. {
  19. REGISTER_DEPRECATED_STRING_PROPERTY("text", text, set_text);
  20. REGISTER_ENUM_PROPERTY("format", format, set_format, Format,
  21. { Format::NoText, "NoText" },
  22. { Format::Percentage, "Percentage" },
  23. { Format::ValueSlashMax, "ValueSlashMax" });
  24. REGISTER_INT_PROPERTY("min", min, set_min);
  25. REGISTER_INT_PROPERTY("max", max, set_max);
  26. set_preferred_size(SpecialDimension::Fit);
  27. }
  28. void Progressbar::set_value(int value)
  29. {
  30. if (m_value == value)
  31. return;
  32. m_value = value;
  33. update();
  34. }
  35. void Progressbar::set_range(int min, int max)
  36. {
  37. VERIFY(min <= max);
  38. m_min = min;
  39. m_max = max;
  40. m_value = clamp(m_value, m_min, m_max);
  41. }
  42. void Progressbar::paint_event(PaintEvent& event)
  43. {
  44. Frame::paint_event(event);
  45. Painter painter(*this);
  46. auto rect = frame_inner_rect();
  47. painter.add_clip_rect(rect);
  48. painter.add_clip_rect(event.rect());
  49. DeprecatedString progress_text;
  50. if (m_format != Format::NoText) {
  51. // Then we draw the progress text over the gradient.
  52. // We draw it twice, once offset (1, 1) for a drop shadow look.
  53. StringBuilder builder;
  54. builder.append(m_text);
  55. if (m_format == Format::Percentage) {
  56. float range_size = m_max - m_min;
  57. float progress = (m_value - m_min) / range_size;
  58. builder.appendff("{}%", (int)(progress * 100));
  59. } else if (m_format == Format::ValueSlashMax) {
  60. builder.appendff("{}/{}", m_value, m_max);
  61. }
  62. progress_text = builder.to_deprecated_string();
  63. }
  64. Gfx::StylePainter::paint_progressbar(painter, rect, palette(), m_min, m_max, m_value, progress_text, m_orientation);
  65. }
  66. void Progressbar::set_orientation(Orientation value)
  67. {
  68. if (m_orientation == value)
  69. return;
  70. m_orientation = value;
  71. update();
  72. }
  73. Optional<UISize> Progressbar::calculated_preferred_size() const
  74. {
  75. if (orientation() == Gfx::Orientation::Vertical)
  76. return { { 22, SpecialDimension::OpportunisticGrow } };
  77. return { { SpecialDimension::OpportunisticGrow, 22 } };
  78. }
  79. }