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.h"
|
|
|
|
|
2021-02-17 00:01:19 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2021-02-16 03:45:05 +00:00
|
|
|
#include "editor/color_picker.h"
|
2021-03-21 12:38:33 +00:00
|
|
|
#include "editor/controllers/controllers.h"
|
2021-02-05 05:44:04 +00:00
|
|
|
#include "editor/photo_editor_content.h"
|
|
|
|
#include "editor/photo_editor_controls.h"
|
2021-02-23 10:34:12 +00:00
|
|
|
#include "window/window_controller.h"
|
2022-12-15 19:54:58 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2022-12-14 08:20:01 +00:00
|
|
|
#include "ui/layers/layer_widget.h"
|
2021-02-05 09:24:26 +00:00
|
|
|
#include "styles/style_editor.h"
|
2021-02-05 05:44:04 +00:00
|
|
|
|
|
|
|
namespace Editor {
|
2021-02-17 00:01:19 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kPrecision = 100000;
|
|
|
|
|
|
|
|
[[nodiscard]] QByteArray Serialize(const Brush &brush) {
|
|
|
|
auto result = QByteArray();
|
|
|
|
auto stream = QDataStream(&result, QIODevice::WriteOnly);
|
|
|
|
stream.setVersion(QDataStream::Qt_5_3);
|
|
|
|
stream << qint32(brush.sizeRatio * kPrecision) << brush.color;
|
|
|
|
stream.device()->close();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Brush Deserialize(const QByteArray &data) {
|
|
|
|
auto stream = QDataStream(data);
|
|
|
|
auto result = Brush();
|
|
|
|
auto size = qint32(0);
|
|
|
|
stream >> size >> result.color;
|
|
|
|
result.sizeRatio = size / float(kPrecision);
|
|
|
|
return (stream.status() != QDataStream::Ok)
|
|
|
|
? Brush()
|
|
|
|
: result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2021-02-05 05:44:04 +00:00
|
|
|
|
|
|
|
PhotoEditor::PhotoEditor(
|
2022-12-14 08:20:01 +00:00
|
|
|
not_null<QWidget*> parent,
|
2021-02-23 10:34:12 +00:00
|
|
|
not_null<Window::Controller*> controller,
|
2021-02-20 04:27:10 +00:00
|
|
|
std::shared_ptr<Image> photo,
|
2021-02-21 07:28:37 +00:00
|
|
|
PhotoModifications modifications,
|
|
|
|
EditorData data)
|
2023-05-18 13:46:24 +00:00
|
|
|
: PhotoEditor(
|
|
|
|
parent,
|
|
|
|
controller->uiShow(),
|
|
|
|
(controller->sessionController()
|
|
|
|
? controller->sessionController()->uiShow()
|
|
|
|
: nullptr),
|
|
|
|
std::move(photo),
|
|
|
|
std::move(modifications),
|
|
|
|
std::move(data)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
PhotoEditor::PhotoEditor(
|
|
|
|
not_null<QWidget*> parent,
|
|
|
|
std::shared_ptr<Ui::Show> show,
|
|
|
|
std::shared_ptr<ChatHelpers::Show> sessionShow,
|
|
|
|
std::shared_ptr<Image> photo,
|
|
|
|
PhotoModifications modifications,
|
|
|
|
EditorData data)
|
2021-02-05 07:08:20 +00:00
|
|
|
: RpWidget(parent)
|
2021-02-13 04:29:31 +00:00
|
|
|
, _modifications(std::move(modifications))
|
2021-02-23 10:34:12 +00:00
|
|
|
, _controllers(std::make_shared<Controllers>(
|
2023-05-18 13:46:24 +00:00
|
|
|
sessionShow
|
2021-02-23 10:34:12 +00:00
|
|
|
? std::make_unique<StickersPanelController>(
|
|
|
|
this,
|
2023-05-18 13:46:24 +00:00
|
|
|
std::move(sessionShow))
|
2021-02-23 10:34:12 +00:00
|
|
|
: nullptr,
|
2021-05-06 21:47:49 +00:00
|
|
|
std::make_unique<UndoController>(),
|
2023-05-18 13:46:24 +00:00
|
|
|
std::move(show)))
|
2021-02-08 03:38:07 +00:00
|
|
|
, _content(base::make_unique_q<PhotoEditorContent>(
|
|
|
|
this,
|
|
|
|
photo,
|
2021-03-14 09:42:18 +00:00
|
|
|
_modifications,
|
2021-02-23 10:34:12 +00:00
|
|
|
_controllers,
|
2022-12-22 12:41:17 +00:00
|
|
|
data))
|
2021-02-22 08:14:23 +00:00
|
|
|
, _controls(base::make_unique_q<PhotoEditorControls>(
|
|
|
|
this,
|
2021-02-23 10:34:12 +00:00
|
|
|
_controllers,
|
2022-12-22 12:41:17 +00:00
|
|
|
_modifications,
|
|
|
|
data))
|
2021-02-17 00:01:19 +00:00
|
|
|
, _colorPicker(std::make_unique<ColorPicker>(
|
|
|
|
this,
|
|
|
|
Deserialize(Core::App().settings().photoEditorBrush()))) {
|
|
|
|
|
2021-02-05 07:08:20 +00:00
|
|
|
sizeValue(
|
|
|
|
) | rpl::start_with_next([=](const QSize &size) {
|
2021-02-16 03:45:05 +00:00
|
|
|
if (size.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-22 12:41:17 +00:00
|
|
|
_content->setGeometry(rect() - st::photoEditorContentMargins);
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
_content->innerRect(
|
|
|
|
) | rpl::start_with_next([=](QRect inner) {
|
|
|
|
if (inner.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto innerTop = _content->y() + inner.top();
|
|
|
|
const auto skip = st::photoEditorCropPointSize;
|
|
|
|
const auto controlsRect = rect()
|
|
|
|
- style::margins(0, innerTop + inner.height() + skip, 0, 0);
|
2021-02-05 09:24:26 +00:00
|
|
|
_controls->setGeometry(controlsRect);
|
2021-02-05 07:08:20 +00:00
|
|
|
}, lifetime());
|
2021-02-07 23:10:30 +00:00
|
|
|
|
2021-05-03 17:04:27 +00:00
|
|
|
_controls->colorLinePositionValue(
|
|
|
|
) | rpl::start_with_next([=](const QPoint &p) {
|
|
|
|
_colorPicker->moveLine(p);
|
|
|
|
}, _controls->lifetime());
|
|
|
|
|
2021-05-04 16:42:47 +00:00
|
|
|
_controls->colorLineShownValue(
|
|
|
|
) | rpl::start_with_next([=](bool shown) {
|
|
|
|
_colorPicker->setVisible(shown);
|
|
|
|
}, _controls->lifetime());
|
|
|
|
|
2021-03-14 09:39:55 +00:00
|
|
|
_mode.value(
|
|
|
|
) | rpl::start_with_next([=](const PhotoEditorMode &mode) {
|
|
|
|
_content->applyMode(mode);
|
|
|
|
_controls->applyMode(mode);
|
|
|
|
}, lifetime());
|
|
|
|
|
2021-02-07 23:10:30 +00:00
|
|
|
_controls->rotateRequests(
|
|
|
|
) | rpl::start_with_next([=](int angle) {
|
|
|
|
_modifications.angle += 90;
|
|
|
|
if (_modifications.angle >= 360) {
|
|
|
|
_modifications.angle -= 360;
|
|
|
|
}
|
|
|
|
_content->applyModifications(_modifications);
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
_controls->flipRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_modifications.flipped = !_modifications.flipped;
|
|
|
|
_content->applyModifications(_modifications);
|
|
|
|
}, lifetime());
|
2021-02-13 04:29:31 +00:00
|
|
|
|
|
|
|
_controls->paintModeRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
2021-03-14 09:39:55 +00:00
|
|
|
_mode = PhotoEditorMode{
|
|
|
|
.mode = PhotoEditorMode::Mode::Paint,
|
|
|
|
.action = PhotoEditorMode::Action::None,
|
|
|
|
};
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
_controls->doneRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
2021-02-18 00:06:44 +00:00
|
|
|
const auto mode = _mode.current().mode;
|
|
|
|
if (mode == PhotoEditorMode::Mode::Paint) {
|
2021-03-14 09:39:55 +00:00
|
|
|
_mode = PhotoEditorMode{
|
|
|
|
.mode = PhotoEditorMode::Mode::Transform,
|
|
|
|
.action = PhotoEditorMode::Action::Save,
|
|
|
|
};
|
2021-02-18 00:06:44 +00:00
|
|
|
} else if (mode == PhotoEditorMode::Mode::Transform) {
|
2021-07-07 18:14:50 +00:00
|
|
|
_mode = PhotoEditorMode{
|
|
|
|
.mode = PhotoEditorMode::Mode::Out,
|
|
|
|
.action = PhotoEditorMode::Action::Save,
|
|
|
|
};
|
2021-02-18 00:06:44 +00:00
|
|
|
save();
|
2021-03-14 09:39:55 +00:00
|
|
|
}
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
_controls->cancelRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
2021-02-18 00:06:44 +00:00
|
|
|
const auto mode = _mode.current().mode;
|
|
|
|
if (mode == PhotoEditorMode::Mode::Paint) {
|
2021-03-14 09:39:55 +00:00
|
|
|
_mode = PhotoEditorMode{
|
|
|
|
.mode = PhotoEditorMode::Mode::Transform,
|
|
|
|
.action = PhotoEditorMode::Action::Discard,
|
|
|
|
};
|
2021-02-18 00:06:44 +00:00
|
|
|
} else if (mode == PhotoEditorMode::Mode::Transform) {
|
2021-07-07 18:14:50 +00:00
|
|
|
_mode = PhotoEditorMode{
|
|
|
|
.mode = PhotoEditorMode::Mode::Out,
|
|
|
|
.action = PhotoEditorMode::Action::Discard,
|
|
|
|
};
|
2021-02-18 00:06:44 +00:00
|
|
|
_cancel.fire({});
|
2021-03-14 09:39:55 +00:00
|
|
|
}
|
2021-02-13 04:29:31 +00:00
|
|
|
}, lifetime());
|
2021-02-16 03:45:05 +00:00
|
|
|
|
2021-02-18 00:06:44 +00:00
|
|
|
_colorPicker->saveBrushRequests(
|
2021-02-16 03:45:05 +00:00
|
|
|
) | rpl::start_with_next([=](const Brush &brush) {
|
|
|
|
_content->applyBrush(brush);
|
2021-02-17 00:01:19 +00:00
|
|
|
|
|
|
|
const auto serialized = Serialize(brush);
|
|
|
|
if (Core::App().settings().photoEditorBrush() != serialized) {
|
|
|
|
Core::App().settings().setPhotoEditorBrush(serialized);
|
|
|
|
Core::App().saveSettingsDelayed();
|
|
|
|
}
|
2021-02-16 03:45:05 +00:00
|
|
|
}, lifetime());
|
2021-02-05 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 08:20:01 +00:00
|
|
|
void PhotoEditor::keyPressEvent(QKeyEvent *e) {
|
2021-05-08 15:21:07 +00:00
|
|
|
if (!_colorPicker->preventHandleKeyPress()) {
|
|
|
|
_content->handleKeyPress(e) || _controls->handleKeyPress(e);
|
|
|
|
}
|
2021-05-04 22:38:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 03:13:40 +00:00
|
|
|
void PhotoEditor::save() {
|
2021-02-13 04:29:31 +00:00
|
|
|
_content->save(_modifications);
|
2021-02-08 03:13:40 +00:00
|
|
|
_done.fire_copy(_modifications);
|
|
|
|
}
|
|
|
|
|
2021-02-18 00:06:44 +00:00
|
|
|
rpl::producer<PhotoModifications> PhotoEditor::doneRequests() const {
|
2021-02-08 03:13:40 +00:00
|
|
|
return _done.events();
|
|
|
|
}
|
|
|
|
|
2021-02-18 00:06:44 +00:00
|
|
|
rpl::producer<> PhotoEditor::cancelRequests() const {
|
|
|
|
return _cancel.events();
|
|
|
|
}
|
|
|
|
|
2022-12-14 08:20:01 +00:00
|
|
|
void InitEditorLayer(
|
|
|
|
not_null<Ui::LayerWidget*> layer,
|
|
|
|
not_null<PhotoEditor*> editor,
|
|
|
|
Fn<void(PhotoModifications)> doneCallback) {
|
|
|
|
editor->cancelRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
layer->closeLayer();
|
|
|
|
}, editor->lifetime());
|
|
|
|
|
|
|
|
const auto weak = Ui::MakeWeak(layer.get());
|
|
|
|
editor->doneRequests(
|
|
|
|
) | rpl::start_with_next([=, done = std::move(doneCallback)](
|
|
|
|
const PhotoModifications &mods) {
|
|
|
|
done(mods);
|
|
|
|
if (const auto strong = weak.data()) {
|
|
|
|
strong->closeLayer();
|
|
|
|
}
|
|
|
|
}, editor->lifetime());
|
|
|
|
}
|
|
|
|
|
2021-02-05 05:44:04 +00:00
|
|
|
} // namespace Editor
|