2021-02-05 05:44:04 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "editor/photo_editor_content.h"
|
|
|
|
|
2021-02-08 07:01:15 +00:00
|
|
|
#include "editor/editor_crop.h"
|
2021-02-13 04:29:31 +00:00
|
|
|
#include "editor/editor_paint.h"
|
2021-02-07 23:10:30 +00:00
|
|
|
#include "media/view/media_view_pip.h"
|
|
|
|
|
2021-02-05 05:44:04 +00:00
|
|
|
namespace Editor {
|
|
|
|
|
2021-02-08 07:01:15 +00:00
|
|
|
using Media::View::FlipSizeByRotation;
|
|
|
|
using Media::View::RotatedRect;
|
|
|
|
|
2021-02-05 05:44:04 +00:00
|
|
|
PhotoEditorContent::PhotoEditorContent(
|
|
|
|
not_null<Ui::RpWidget*> parent,
|
2021-02-20 04:27:10 +00:00
|
|
|
std::shared_ptr<Image> photo,
|
2021-03-14 09:42:18 +00:00
|
|
|
PhotoModifications modifications,
|
2021-02-23 10:34:12 +00:00
|
|
|
std::shared_ptr<Controllers> controllers,
|
2021-02-21 07:28:37 +00:00
|
|
|
EditorData data)
|
2021-02-08 03:38:07 +00:00
|
|
|
: RpWidget(parent)
|
2021-02-20 04:27:10 +00:00
|
|
|
, _photoSize(photo->size())
|
2021-03-14 09:42:18 +00:00
|
|
|
, _paint(base::make_unique_q<Paint>(
|
|
|
|
this,
|
|
|
|
modifications,
|
2021-02-20 04:27:10 +00:00
|
|
|
_photoSize,
|
2021-02-23 10:34:12 +00:00
|
|
|
std::move(controllers)))
|
2021-02-21 07:28:37 +00:00
|
|
|
, _crop(base::make_unique_q<Crop>(
|
|
|
|
this,
|
|
|
|
modifications,
|
|
|
|
_photoSize,
|
|
|
|
std::move(data)))
|
2021-02-20 04:27:10 +00:00
|
|
|
, _photo(std::move(photo))
|
2021-02-08 03:38:07 +00:00
|
|
|
, _modifications(modifications) {
|
2021-02-05 07:08:20 +00:00
|
|
|
|
2021-02-07 23:10:30 +00:00
|
|
|
rpl::combine(
|
|
|
|
_modifications.value(),
|
|
|
|
sizeValue()
|
|
|
|
) | rpl::start_with_next([=](
|
|
|
|
const PhotoModifications &mods, const QSize &size) {
|
2021-02-08 07:01:15 +00:00
|
|
|
if (size.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-18 06:37:12 +00:00
|
|
|
const auto imageSizeF = [&] {
|
2021-02-08 07:01:15 +00:00
|
|
|
const auto rotatedSize =
|
|
|
|
FlipSizeByRotation(size, mods.angle);
|
|
|
|
const auto m = _crop->cropMargins();
|
|
|
|
const auto sizeForCrop = rotatedSize
|
|
|
|
- QSize(m.left() + m.right(), m.top() + m.bottom());
|
2021-02-20 04:27:10 +00:00
|
|
|
const auto originalSize = QSizeF(_photoSize);
|
2021-02-08 07:01:15 +00:00
|
|
|
if ((originalSize.width() > sizeForCrop.width())
|
|
|
|
|| (originalSize.height() > sizeForCrop.height())) {
|
2021-02-05 07:08:20 +00:00
|
|
|
return originalSize.scaled(
|
2021-02-08 07:01:15 +00:00
|
|
|
sizeForCrop,
|
2021-02-05 07:08:20 +00:00
|
|
|
Qt::KeepAspectRatio);
|
|
|
|
}
|
|
|
|
return originalSize;
|
|
|
|
}();
|
2021-02-18 06:37:12 +00:00
|
|
|
const auto imageSize = QSize(imageSizeF.width(), imageSizeF.height());
|
2021-02-05 07:08:20 +00:00
|
|
|
_imageRect = QRect(
|
2021-02-07 23:10:30 +00:00
|
|
|
QPoint(-imageSize.width() / 2, -imageSize.height() / 2),
|
2021-02-05 07:08:20 +00:00
|
|
|
imageSize);
|
2021-02-07 23:10:30 +00:00
|
|
|
|
|
|
|
_imageMatrix.reset();
|
|
|
|
_imageMatrix.translate(size.width() / 2, size.height() / 2);
|
|
|
|
if (mods.flipped) {
|
|
|
|
_imageMatrix.scale(-1, 1);
|
|
|
|
}
|
|
|
|
_imageMatrix.rotate(mods.angle);
|
2021-02-08 07:01:15 +00:00
|
|
|
|
2021-02-13 04:29:31 +00:00
|
|
|
const auto geometry = _imageMatrix.mapRect(_imageRect);
|
2021-02-08 07:01:15 +00:00
|
|
|
_crop->applyTransform(
|
2021-02-13 04:29:31 +00:00
|
|
|
geometry + _crop->cropMargins(),
|
2021-02-08 07:01:15 +00:00
|
|
|
mods.angle,
|
2021-02-18 06:37:12 +00:00
|
|
|
mods.flipped, imageSizeF);
|
2021-02-13 04:29:31 +00:00
|
|
|
_paint->applyTransform(geometry, mods.angle, mods.flipped);
|
2021-02-05 07:08:20 +00:00
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
paintRequest(
|
|
|
|
) | rpl::start_with_next([=](const QRect &clip) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
p.fillRect(clip, Qt::transparent);
|
2021-02-07 23:10:30 +00:00
|
|
|
|
|
|
|
p.setMatrix(_imageMatrix);
|
|
|
|
|
2021-02-20 04:27:10 +00:00
|
|
|
p.drawPixmap(
|
|
|
|
_imageRect,
|
|
|
|
_photo->pix(_imageRect.width(), _imageRect.height()));
|
2021-02-05 07:08:20 +00:00
|
|
|
}, lifetime());
|
2021-02-05 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:10:30 +00:00
|
|
|
void PhotoEditorContent::applyModifications(
|
|
|
|
PhotoModifications modifications) {
|
|
|
|
_modifications = std::move(modifications);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-02-13 04:29:31 +00:00
|
|
|
void PhotoEditorContent::save(PhotoModifications &modifications) {
|
2021-02-18 06:37:12 +00:00
|
|
|
modifications.crop = _crop->saveCropRect();
|
2021-03-14 09:42:18 +00:00
|
|
|
_paint->keepResult();
|
2021-02-16 03:45:05 +00:00
|
|
|
if (!modifications.paint) {
|
|
|
|
modifications.paint = _paint->saveScene();
|
|
|
|
}
|
2021-02-13 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 09:39:55 +00:00
|
|
|
void PhotoEditorContent::applyMode(const PhotoEditorMode &mode) {
|
|
|
|
const auto isTransform = (mode.mode == PhotoEditorMode::Mode::Transform);
|
|
|
|
_crop->setVisible(isTransform);
|
|
|
|
|
|
|
|
_paint->setAttribute(Qt::WA_TransparentForMouseEvents, isTransform);
|
2021-03-14 09:42:18 +00:00
|
|
|
if (!isTransform) {
|
|
|
|
_paint->updateUndoState();
|
|
|
|
}
|
2021-03-14 09:39:55 +00:00
|
|
|
|
|
|
|
if (mode.action == PhotoEditorMode::Action::Discard) {
|
|
|
|
_paint->cancel();
|
|
|
|
} else if (mode.action == PhotoEditorMode::Action::Save) {
|
|
|
|
_paint->keepResult();
|
|
|
|
}
|
2021-02-13 04:29:31 +00:00
|
|
|
_mode = mode;
|
2021-02-08 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 03:45:05 +00:00
|
|
|
void PhotoEditorContent::applyBrush(const Brush &brush) {
|
|
|
|
_paint->applyBrush(brush);
|
|
|
|
}
|
|
|
|
|
2021-02-05 05:44:04 +00:00
|
|
|
} // namespace Editor
|