GLabel.cpp 896 B

1234567891011121314151617181920212223242526272829303132333435363738
  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&)
  23. {
  24. Painter painter(*this);
  25. if (fill_with_background_color())
  26. painter.fill_rect({ 0, 0, width(), height() }, background_color());
  27. if (m_icon) {
  28. auto icon_location = rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  29. painter.blit(icon_location, *m_icon, m_icon->rect());
  30. }
  31. if (!text().is_empty())
  32. painter.draw_text({ 0, 0, width(), height() }, text(), m_text_alignment, foreground_color());
  33. }