send_as_button.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "ui/controls/send_as_button.h"
  8. #include "ui/effects/cross_animation.h"
  9. #include "ui/painter.h"
  10. #include "styles/style_chat.h"
  11. namespace Ui {
  12. SendAsButton::SendAsButton(QWidget *parent, const style::SendAsButton &st)
  13. : AbstractButton(parent)
  14. , _st(st) {
  15. resize(_st.width, _st.height);
  16. }
  17. void SendAsButton::setUserpic(QImage userpic) {
  18. _userpic = std::move(userpic);
  19. update();
  20. }
  21. void SendAsButton::setActive(bool active) {
  22. if (_active == active) {
  23. return;
  24. }
  25. _active = active;
  26. _activeAnimation.start(
  27. [=] { update(); },
  28. _active ? 0. : 1.,
  29. _active ? 1. : 0.,
  30. _st.duration);
  31. }
  32. void SendAsButton::paintEvent(QPaintEvent *e) {
  33. auto p = QPainter(this);
  34. const auto left = (width() - _st.size) / 2;
  35. const auto top = (height() - _st.size) / 2;
  36. const auto active = _activeAnimation.value(_active ? 1. : 0.);
  37. if (active < 1. && !_userpic.isNull()) {
  38. p.drawImage(QRect(left, top, _st.size, _st.size), _userpic);
  39. }
  40. if (active > 0.) {
  41. p.setOpacity(active);
  42. p.setPen(Qt::NoPen);
  43. p.setBrush(_st.activeBg);
  44. {
  45. PainterHighQualityEnabler hq(p);
  46. p.drawEllipse(left, top, _st.size, _st.size);
  47. }
  48. CrossAnimation::paint(
  49. p,
  50. _st.cross,
  51. _st.activeFg,
  52. left,
  53. top,
  54. width(),
  55. active);
  56. }
  57. }
  58. } // namespace Ui