info_media_empty_widget.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "info/media/info_media_empty_widget.h"
  8. #include "ui/widgets/labels.h"
  9. #include "styles/style_info.h"
  10. #include "lang/lang_keys.h"
  11. namespace Info {
  12. namespace Media {
  13. EmptyWidget::EmptyWidget(QWidget *parent)
  14. : RpWidget(parent)
  15. , _text(this, st::infoEmptyLabel) {
  16. }
  17. void EmptyWidget::setFullHeight(rpl::producer<int> fullHeightValue) {
  18. std::move(
  19. fullHeightValue
  20. ) | rpl::start_with_next([this](int fullHeight) {
  21. // Make icon center be on 1/3 height.
  22. auto iconCenter = fullHeight / 3;
  23. auto iconHeight = st::infoEmptyFile.height();
  24. auto iconTop = iconCenter - iconHeight / 2;
  25. _height = iconTop + st::infoEmptyIconTop;
  26. resizeToWidth(width());
  27. }, lifetime());
  28. }
  29. void EmptyWidget::setType(Type type) {
  30. _type = type;
  31. _icon = [&] {
  32. switch (_type) {
  33. case Type::Photo:
  34. case Type::GIF: return &st::infoEmptyPhoto;
  35. case Type::Video: return &st::infoEmptyVideo;
  36. case Type::MusicFile: return &st::infoEmptyAudio;
  37. case Type::File: return &st::infoEmptyFile;
  38. case Type::Link: return &st::infoEmptyLink;
  39. case Type::RoundVoiceFile: return &st::infoEmptyVoice;
  40. }
  41. Unexpected("Bad type in EmptyWidget::setType()");
  42. }();
  43. update();
  44. }
  45. void EmptyWidget::setSearchQuery(const QString &query) {
  46. _text->setText([&] {
  47. switch (_type) {
  48. case Type::Photo:
  49. return tr::lng_media_photo_empty(tr::now);
  50. case Type::GIF:
  51. return tr::lng_media_gif_empty(tr::now);
  52. case Type::Video:
  53. return tr::lng_media_video_empty(tr::now);
  54. case Type::MusicFile:
  55. return query.isEmpty()
  56. ? tr::lng_media_song_empty(tr::now)
  57. : tr::lng_media_song_empty_search(tr::now);
  58. case Type::File:
  59. return query.isEmpty()
  60. ? tr::lng_media_file_empty(tr::now)
  61. : tr::lng_media_file_empty_search(tr::now);
  62. case Type::Link:
  63. return query.isEmpty()
  64. ? tr::lng_media_link_empty(tr::now)
  65. : tr::lng_media_link_empty_search(tr::now);
  66. case Type::RoundVoiceFile:
  67. return tr::lng_media_audio_empty(tr::now);
  68. }
  69. Unexpected("Bad type in EmptyWidget::setSearchQuery()");
  70. }());
  71. resizeToWidth(width());
  72. }
  73. void EmptyWidget::paintEvent(QPaintEvent *e) {
  74. if (!_icon) {
  75. return;
  76. }
  77. auto p = QPainter(this);
  78. auto iconLeft = (width() - _icon->width()) / 2;
  79. auto iconTop = height() - st::infoEmptyIconTop;
  80. _icon->paint(p, iconLeft, iconTop, width());
  81. }
  82. int EmptyWidget::resizeGetHeight(int newWidth) {
  83. auto labelTop = _height - st::infoEmptyLabelTop;
  84. auto labelWidth = newWidth - 2 * st::infoEmptyLabelSkip;
  85. _text->resizeToNaturalWidth(labelWidth);
  86. auto labelLeft = (newWidth - _text->width()) / 2;
  87. _text->moveToLeft(labelLeft, labelTop, newWidth);
  88. update();
  89. return _height;
  90. }
  91. } // namespace Media
  92. } // namespace Info