photo_editor_layer_widget.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "editor/photo_editor_layer_widget.h"
  8. #include "lang/lang_keys.h"
  9. #include "ui/boxes/confirm_box.h" // InformBox
  10. #include "editor/editor_layer_widget.h"
  11. #include "editor/photo_editor.h"
  12. #include "storage/localimageloader.h"
  13. #include "storage/storage_media_prepare.h"
  14. #include "ui/chat/attach/attach_prepare.h"
  15. #include "window/window_controller.h"
  16. #include "window/window_session_controller.h"
  17. #include <QtGui/QGuiApplication>
  18. namespace Editor {
  19. void OpenWithPreparedFile(
  20. not_null<QWidget*> parent,
  21. std::shared_ptr<ChatHelpers::Show> show,
  22. not_null<Ui::PreparedFile*> file,
  23. int previewWidth,
  24. Fn<void(bool ok)> &&doneCallback,
  25. QSize exactSize) {
  26. using ImageInfo = Ui::PreparedFileInformation::Image;
  27. const auto image = std::get_if<ImageInfo>(&file->information->media);
  28. if (!image) {
  29. doneCallback(false);
  30. return;
  31. }
  32. const auto photoType = (file->type == Ui::PreparedFile::Type::Photo);
  33. const auto modifiedFileType = (file->type == Ui::PreparedFile::Type::File)
  34. && !image->modifications.empty();
  35. if (!photoType && !modifiedFileType) {
  36. doneCallback(false);
  37. return;
  38. }
  39. const auto sideLimit = PhotoSideLimit();
  40. const auto accepted = std::make_shared<bool>();
  41. auto callback = [=](const PhotoModifications &mods) {
  42. *accepted = true;
  43. image->modifications = mods;
  44. Storage::UpdateImageDetails(*file, previewWidth, sideLimit);
  45. {
  46. using namespace Ui;
  47. const auto size = file->preview.size();
  48. file->type = ValidateThumbDimensions(size.width(), size.height())
  49. ? PreparedFile::Type::Photo
  50. : PreparedFile::Type::File;
  51. }
  52. doneCallback(true);
  53. };
  54. auto copy = image->data;
  55. const auto fileImage = std::make_shared<Image>(std::move(copy));
  56. const auto keepRatio = !exactSize.isEmpty();
  57. auto editor = base::make_unique_q<PhotoEditor>(
  58. parent,
  59. show,
  60. show,
  61. fileImage,
  62. image->modifications,
  63. EditorData{ .exactSize = exactSize, .keepAspectRatio = keepRatio });
  64. const auto raw = editor.get();
  65. auto layer = std::make_unique<LayerWidget>(parent, std::move(editor));
  66. InitEditorLayer(layer.get(), raw, std::move(callback));
  67. QObject::connect(layer.get(), &QObject::destroyed, [=] {
  68. if (!*accepted) {
  69. doneCallback(false);
  70. }
  71. });
  72. show->showLayer(std::move(layer), Ui::LayerOption::KeepOther);
  73. }
  74. void PrepareProfilePhoto(
  75. not_null<QWidget*> parent,
  76. not_null<Window::Controller*> controller,
  77. EditorData data,
  78. Fn<void(QImage &&image)> &&doneCallback,
  79. QImage &&image) {
  80. const auto resizeToMinSize = [=](
  81. QImage &&image,
  82. Qt::AspectRatioMode mode) {
  83. const auto &minSize = kProfilePhotoSize;
  84. if ((image.width() < minSize) || (image.height() < minSize)) {
  85. return image.scaled(
  86. minSize,
  87. minSize,
  88. mode,
  89. Qt::SmoothTransformation);
  90. }
  91. return std::move(image);
  92. };
  93. if (image.isNull()
  94. || (image.width() > (10 * image.height()))
  95. || (image.height() > (10 * image.width()))) {
  96. controller->show(Ui::MakeInformBox(tr::lng_bad_photo()));
  97. return;
  98. }
  99. image = resizeToMinSize(
  100. std::move(image),
  101. Qt::KeepAspectRatioByExpanding);
  102. const auto fileImage = std::make_shared<Image>(std::move(image));
  103. auto applyModifications = [=, done = std::move(doneCallback)](
  104. const PhotoModifications &mods) {
  105. done(resizeToMinSize(
  106. ImageModified(fileImage->original(), mods),
  107. Qt::KeepAspectRatio));
  108. };
  109. auto crop = [&] {
  110. const auto &i = fileImage;
  111. const auto minSide = std::min(i->width(), i->height());
  112. return QRect(
  113. (i->width() - minSide) / 2,
  114. (i->height() - minSide) / 2,
  115. minSide,
  116. minSide);
  117. }();
  118. auto editor = base::make_unique_q<PhotoEditor>(
  119. parent,
  120. controller,
  121. fileImage,
  122. PhotoModifications{ .crop = std::move(crop) },
  123. data);
  124. const auto raw = editor.get();
  125. auto layer = std::make_unique<LayerWidget>(parent, std::move(editor));
  126. InitEditorLayer(layer.get(), raw, std::move(applyModifications));
  127. controller->showLayer(std::move(layer), Ui::LayerOption::KeepOther);
  128. }
  129. void PrepareProfilePhotoFromFile(
  130. not_null<QWidget*> parent,
  131. not_null<Window::Controller*> controller,
  132. EditorData data,
  133. Fn<void(QImage &&image)> &&doneCallback) {
  134. const auto callback = [=, done = std::move(doneCallback)](
  135. const FileDialog::OpenResult &result) mutable {
  136. if (result.paths.isEmpty() && result.remoteContent.isEmpty()) {
  137. return;
  138. }
  139. auto image = Images::Read({
  140. .path = result.paths.isEmpty() ? QString() : result.paths.front(),
  141. .content = result.remoteContent,
  142. .forceOpaque = true,
  143. }).image;
  144. PrepareProfilePhoto(
  145. parent,
  146. controller,
  147. data,
  148. std::move(done),
  149. std::move(image));
  150. };
  151. FileDialog::GetOpenPath(
  152. parent.get(),
  153. tr::lng_choose_image(tr::now),
  154. FileDialog::ImagesOrAllFilter(),
  155. crl::guard(parent, callback));
  156. }
  157. } // namespace Editor