GLabel.cpp 943 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "GLabel.h"
  2. #include <SharedGraphics/Painter.h>
  3. #include <SharedGraphics/GraphicsBitmap.h>
  4. GLabel::GLabel(GWidget* parent)
  5. : GWidget(parent)
  6. {
  7. }
  8. GLabel::~GLabel()
  9. {
  10. }
  11. void GLabel::set_icon(RetainPtr<GraphicsBitmap>&& icon)
  12. {
  13. m_icon = move(icon);
  14. }
  15. void GLabel::set_text(String&& text)
  16. {
  17. if (text == m_text)
  18. return;
  19. m_text = move(text);
  20. update();
  21. }
  22. void GLabel::paint_event(GPaintEvent& event)
  23. {
  24. Painter painter(*this);
  25. painter.set_clip_rect(event.rect());
  26. if (fill_with_background_color())
  27. painter.fill_rect({ 0, 0, width(), height() }, background_color());
  28. if (m_icon) {
  29. auto icon_location = rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  30. painter.blit(icon_location, *m_icon, m_icon->rect());
  31. }
  32. if (!text().is_empty())
  33. painter.draw_text({ 0, 0, width(), height() }, text(), m_text_alignment, foreground_color());
  34. }