max_invite_box.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "boxes/max_invite_box.h"
  8. #include "api/api_invite_links.h"
  9. #include "apiwrap.h"
  10. #include "data/data_changes.h"
  11. #include "data/data_channel.h"
  12. #include "lang/lang_keys.h"
  13. #include "main/main_session.h"
  14. #include "mtproto/mtproto_config.h"
  15. #include "ui/layers/generic_box.h"
  16. #include "ui/text/text_utilities.h"
  17. #include "ui/toast/toast.h"
  18. #include "ui/painter.h"
  19. #include "styles/style_layers.h"
  20. #include "styles/style_boxes.h"
  21. #include <QtGui/QGuiApplication>
  22. #include <QtGui/QClipboard>
  23. namespace {
  24. TextParseOptions kInformBoxTextOptions = {
  25. (TextParseLinks
  26. | TextParseMultiline
  27. | TextParseMarkdown), // flags
  28. 0, // maxw
  29. 0, // maxh
  30. Qt::LayoutDirectionAuto, // dir
  31. };
  32. } // namespace
  33. MaxInviteBox::MaxInviteBox(QWidget*, not_null<ChannelData*> channel)
  34. : BoxContent()
  35. , _channel(channel)
  36. , _text(
  37. st::boxLabelStyle,
  38. tr::lng_participant_invite_sorry(
  39. tr::now,
  40. lt_count,
  41. channel->session().serverConfig().chatSizeMax),
  42. kInformBoxTextOptions,
  43. (st::boxWidth
  44. - st::boxPadding.left()
  45. - st::defaultBox.buttonPadding.right())) {
  46. }
  47. void MaxInviteBox::prepare() {
  48. setMouseTracking(true);
  49. addButton(tr::lng_box_ok(), [=] { closeBox(); });
  50. _textWidth = st::boxWidth
  51. - st::boxPadding.left()
  52. - st::defaultBox.buttonPadding.right();
  53. _textHeight = std::min(
  54. _text.countHeight(_textWidth),
  55. 16 * st::boxLabelStyle.lineHeight);
  56. setDimensions(
  57. st::boxWidth,
  58. st::boxPadding.top()
  59. + _textHeight
  60. + st::boxTextFont->height
  61. + st::boxTextFont->height * 2
  62. + st::newGroupLinkPadding.bottom());
  63. if (_channel->inviteLink().isEmpty()) {
  64. _channel->session().api().requestFullPeer(_channel);
  65. }
  66. _channel->session().changes().peerUpdates(
  67. _channel,
  68. Data::PeerUpdate::Flag::InviteLinks
  69. ) | rpl::start_with_next([=] {
  70. rtlupdate(_invitationLink);
  71. }, lifetime());
  72. }
  73. void MaxInviteBox::mouseMoveEvent(QMouseEvent *e) {
  74. updateSelected(e->globalPos());
  75. }
  76. void MaxInviteBox::mousePressEvent(QMouseEvent *e) {
  77. mouseMoveEvent(e);
  78. if (_linkOver) {
  79. if (!_channel->inviteLink().isEmpty()) {
  80. QGuiApplication::clipboard()->setText(_channel->inviteLink());
  81. showToast(tr::lng_create_channel_link_copied(tr::now));
  82. } else if (_channel->isFullLoaded() && !_creatingInviteLink) {
  83. _creatingInviteLink = true;
  84. _channel->session().api().inviteLinks().create({ _channel });
  85. }
  86. }
  87. }
  88. void MaxInviteBox::leaveEventHook(QEvent *e) {
  89. updateSelected(QCursor::pos());
  90. }
  91. void MaxInviteBox::updateSelected(const QPoint &cursorGlobalPosition) {
  92. const auto p = QPoint(mapFromGlobal(cursorGlobalPosition));
  93. const auto linkOver = _invitationLink.contains(p);
  94. if (linkOver != _linkOver) {
  95. _linkOver = linkOver;
  96. update();
  97. setCursor(_linkOver ? style::cur_pointer : style::cur_default);
  98. }
  99. }
  100. void MaxInviteBox::paintEvent(QPaintEvent *e) {
  101. BoxContent::paintEvent(e);
  102. Painter p(this);
  103. // draw box title / text
  104. p.setPen(st::boxTextFg);
  105. _text.drawLeftElided(
  106. p,
  107. st::boxPadding.left(),
  108. st::boxPadding.top(),
  109. _textWidth,
  110. width(),
  111. 16,
  112. style::al_left);
  113. auto option = QTextOption(style::al_left);
  114. option.setWrapMode(QTextOption::WrapAnywhere);
  115. p.setFont(_linkOver
  116. ? st::defaultInputField.style.font->underline()
  117. : st::defaultInputField.style.font);
  118. p.setPen(st::defaultLinkButton.color);
  119. const auto inviteLinkText = _channel->inviteLink().isEmpty()
  120. ? tr::lng_group_invite_create(tr::now)
  121. : _channel->inviteLink();
  122. p.drawText(_invitationLink, inviteLinkText, option);
  123. }
  124. void MaxInviteBox::resizeEvent(QResizeEvent *e) {
  125. BoxContent::resizeEvent(e);
  126. _invitationLink = myrtlrect(
  127. st::boxPadding.left(),
  128. st::boxPadding.top() + _textHeight + st::boxTextFont->height,
  129. width() - st::boxPadding.left() - st::boxPadding.right(),
  130. 2 * st::boxTextFont->height);
  131. }