GProgressBar.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <LibGUI/GProgressBar.h>
  2. #include <LibGUI/GPainter.h>
  3. #include <AK/StringBuilder.h>
  4. GProgressBar::GProgressBar(GWidget* parent)
  5. : GFrame(parent)
  6. {
  7. set_frame_shape(GFrame::Shape::Container);
  8. set_frame_shadow(GFrame::Shadow::Sunken);
  9. set_frame_thickness(2);
  10. }
  11. GProgressBar::~GProgressBar()
  12. {
  13. }
  14. void GProgressBar::set_value(int value)
  15. {
  16. if (m_value == value)
  17. return;
  18. m_value = value;
  19. update();
  20. }
  21. void GProgressBar::set_range(int min, int max)
  22. {
  23. ASSERT(min < max);
  24. m_min = min;
  25. m_max = max;
  26. if (m_value > m_max)
  27. m_value = m_max;
  28. if (m_value < m_min)
  29. m_value = m_min;
  30. }
  31. void GProgressBar::paint_event(GPaintEvent& event)
  32. {
  33. GFrame::paint_event(event);
  34. GPainter painter(*this);
  35. auto rect = frame_inner_rect();
  36. painter.add_clip_rect(rect);
  37. painter.add_clip_rect(event.rect());
  38. // First we fill the entire widget with the gradient. This incurs a bit of
  39. // overdraw but ensures a consistent look throughout the progression.
  40. Color start_color(110, 34, 9);
  41. Color end_color(244, 202, 158);
  42. painter.fill_rect_with_gradient(rect, start_color, end_color);
  43. float range_size = m_max - m_min;
  44. float progress = (m_value - m_min) / range_size;
  45. // Then we draw the progress text over the gradient.
  46. // We draw it twice, once offset (1, 1) for a drop shadow look.
  47. StringBuilder builder;
  48. builder.append(m_caption);
  49. if (m_format == Format::Percentage)
  50. builder.appendf("%d%%", (int)(progress * 100));
  51. else if (m_format == Format::ValueSlashMax)
  52. builder.appendf("%d/%d", m_value, m_max);
  53. auto progress_text = builder.to_string();
  54. painter.draw_text(rect.translated(1, 1), progress_text, TextAlignment::Center, Color::Black);
  55. painter.draw_text(rect, progress_text, TextAlignment::Center, Color::White);
  56. // Then we carve out a hole in the remaining part of the widget.
  57. // We draw the text a third time, clipped and inverse, for sharp contrast.
  58. float progress_width = progress * width();
  59. Rect hole_rect { (int)progress_width, 0, (int)(width() - progress_width), height() };
  60. painter.add_clip_rect(hole_rect);
  61. painter.fill_rect(hole_rect, Color::White);
  62. painter.draw_text(rect.translated(0, 0), progress_text, TextAlignment::Center, Color::Black);
  63. }