countryinput.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/countryinput.h"
  8. #include "lang/lang_keys.h"
  9. #include "ui/widgets/scroll_area.h"
  10. #include "ui/widgets/multi_select.h"
  11. #include "ui/effects/ripple_animation.h"
  12. #include "ui/boxes/country_select_box.h"
  13. #include "ui/painter.h"
  14. #include "countries/countries_instance.h"
  15. #include "window/window_session_controller.h"
  16. #include "styles/style_layers.h"
  17. #include "styles/style_boxes.h"
  18. #include "styles/style_intro.h"
  19. #include "styles/style_widgets.h"
  20. CountryInput::CountryInput(
  21. QWidget *parent,
  22. std::shared_ptr<Ui::Show> show,
  23. const style::InputField &st)
  24. : RpWidget(parent)
  25. , _show(show)
  26. , _st(st)
  27. , _text(tr::lng_country_code(tr::now)) {
  28. resize(_st.width, _st.heightMin);
  29. }
  30. void CountryInput::paintEvent(QPaintEvent *e) {
  31. auto p = QPainter(this);
  32. QRect r(rect().intersected(e->rect()));
  33. if (_st.textBg->c.alphaF() > 0.) {
  34. p.fillRect(r, _st.textBg);
  35. }
  36. if (_st.border) {
  37. p.fillRect(
  38. 0,
  39. height() - _st.border,
  40. width(),
  41. _st.border,
  42. _st.borderFg);
  43. }
  44. st::introCountryIcon.paint(
  45. p,
  46. width()
  47. - st::introCountryIcon.width()
  48. - st::introCountryIconPosition.x(),
  49. st::introCountryIconPosition.y(),
  50. width());
  51. p.setFont(_st.style.font);
  52. p.setPen(_st.textFg);
  53. p.drawText(rect().marginsRemoved(_st.textMargins), _text, _st.textAlign);
  54. }
  55. void CountryInput::mouseMoveEvent(QMouseEvent *e) {
  56. bool newActive = rect().contains(e->pos());
  57. if (_active != newActive) {
  58. _active = newActive;
  59. setCursor(_active ? style::cur_pointer : style::cur_default);
  60. }
  61. }
  62. void CountryInput::mousePressEvent(QMouseEvent *e) {
  63. mouseMoveEvent(e);
  64. if (_active) {
  65. auto object = Box<Ui::CountrySelectBox>();
  66. const auto box = Ui::MakeWeak(object.data());
  67. _show->showBox(std::move(object), Ui::LayerOption::CloseOther);
  68. box->entryChosen(
  69. ) | rpl::start_with_next([=](
  70. const Ui::CountrySelectBox::Entry &entry) {
  71. if (box) {
  72. box->closeBox();
  73. }
  74. const auto &list = Countries::Instance().list();
  75. const auto infoIt = ranges::find(
  76. list,
  77. entry.iso2,
  78. &Countries::Info::iso2);
  79. if (infoIt == end(list)) {
  80. return;
  81. }
  82. const auto info = *infoIt;
  83. const auto it = ranges::find(
  84. info.codes,
  85. entry.code,
  86. &Countries::CallingCodeInfo::callingCode);
  87. if (it != end(info.codes)) {
  88. chooseCountry(
  89. &info,
  90. std::distance(begin(info.codes), it));
  91. }
  92. }, lifetime());
  93. }
  94. }
  95. void CountryInput::enterEventHook(QEnterEvent *e) {
  96. setMouseTracking(true);
  97. }
  98. void CountryInput::leaveEventHook(QEvent *e) {
  99. setMouseTracking(false);
  100. _active = false;
  101. setCursor(style::cur_default);
  102. }
  103. void CountryInput::onChooseCode(const QString &code) {
  104. _show->hideLayer();
  105. _chosenIso = QString();
  106. if (code.length()) {
  107. const auto &byCode = Countries::Instance().byCode();
  108. const auto i = byCode.constFind(code);
  109. if (i != byCode.cend()) {
  110. const auto info = *i;
  111. _chosenIso = info->iso2;
  112. setText(info->name);
  113. } else {
  114. setText(tr::lng_bad_country_code(tr::now));
  115. }
  116. } else {
  117. setText(tr::lng_country_code(tr::now));
  118. }
  119. update();
  120. }
  121. bool CountryInput::chooseCountry(const QString &iso) {
  122. const auto &byISO2 = Countries::Instance().byISO2();
  123. const auto i = byISO2.constFind(iso);
  124. const auto info = (i != byISO2.cend()) ? (*i) : nullptr;
  125. _chosenIso = QString();
  126. if (info) {
  127. chooseCountry(info, 0);
  128. return true;
  129. }
  130. return false;
  131. }
  132. void CountryInput::chooseCountry(
  133. not_null<const Countries::Info*> info,
  134. int codeIndex) {
  135. _chosenIso = info->iso2;
  136. setText(info->name);
  137. _codeChanged.fire_copy(info->codes[codeIndex].callingCode);
  138. update();
  139. }
  140. rpl::producer<QString> CountryInput::codeChanged() const {
  141. return _codeChanged.events();
  142. }
  143. void CountryInput::setText(const QString &newText) {
  144. _text = _st.style.font->elided(
  145. newText,
  146. width() - _st.textMargins.left() - _st.textMargins.right());
  147. }