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"
|
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(
|
|
|
|
not_null<Ui::RpWidget*> 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)
|
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>(
|
|
|
|
controller->sessionController()
|
|
|
|
? std::make_unique<StickersPanelController>(
|
|
|
|
this,
|
|
|
|
controller->sessionController())
|
|
|
|
: nullptr,
|
2021-05-06 21:47:49 +00:00
|
|
|
std::make_unique<UndoController>(),
|
|
|
|
[=] (object_ptr<Ui::BoxContent> c) { controller->show(std::move(c)); }))
|
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,
|
2021-02-21 07:28:37 +00:00
|
|
|
std::move(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,
|
2021-02-22 08:14:23 +00:00
|
|
|
_modifications))
|
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;
|
|
|
|
}
|
2021-02-05 09:24:26 +00:00
|
|
|
const auto geometry = QRect(QPoint(), size);
|
2021-07-02 09:55:25 +00:00
|
|
|
const auto contentRect = geometry - st::photoEditorContentMargins;
|
2021-02-05 09:24:26 +00:00
|
|
|
_content->setGeometry(contentRect);
|
2021-07-02 09:55:25 +00:00
|
|
|
const auto contentBottom = contentRect.top() + contentRect.height();
|
2021-02-05 09:24:26 +00:00
|
|
|
const auto controlsRect = geometry
|
2021-07-02 09:55:25 +00:00
|
|
|
- style::margins(0, contentBottom, 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
|
|
|
}
|
|
|
|
|
2021-05-04 22:38:55 +00:00
|
|
|
void PhotoEditor::handleKeyPress(not_null<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();
|
|
|
|
}
|
|
|
|
|
2021-02-05 05:44:04 +00:00
|
|
|
} // namespace Editor
|