Added custom layer widget with photo editor.

This commit is contained in:
23rd 2021-02-18 03:06:44 +03:00
parent 4909ba5a1e
commit c9affe0da5
7 changed files with 131 additions and 10 deletions

View file

@ -522,6 +522,8 @@ PRIVATE
editor/photo_editor_content.h
editor/photo_editor_controls.cpp
editor/photo_editor_controls.h
editor/photo_editor_layer_widget.cpp
editor/photo_editor_layer_widget.h
editor/undo_controller.cpp
editor/undo_controller.h
export/export_manager.cpp

View file

@ -308,7 +308,7 @@ void ColorPicker::setVisible(bool visible) {
}
rpl::producer<Brush> ColorPicker::saveBrushRequests() const {
return _saveBrushRequests.events();
return _saveBrushRequests.events_starting_with_copy(_brush);
}
int ColorPicker::colorToPosition(const QColor &color) const {

View file

@ -110,28 +110,31 @@ PhotoEditor::PhotoEditor(
_controls->doneRequests(
) | rpl::start_with_next([=] {
if (_mode.current().mode == PhotoEditorMode::Mode::Paint) {
const auto mode = _mode.current().mode;
if (mode == PhotoEditorMode::Mode::Paint) {
_mode = PhotoEditorMode{
.mode = PhotoEditorMode::Mode::Transform,
.action = PhotoEditorMode::Action::Save,
};
} else if (mode == PhotoEditorMode::Mode::Transform) {
save();
}
}, lifetime());
_controls->cancelRequests(
) | rpl::start_with_next([=] {
if (_mode.current().mode == PhotoEditorMode::Mode::Paint) {
const auto mode = _mode.current().mode;
if (mode == PhotoEditorMode::Mode::Paint) {
_mode = PhotoEditorMode{
.mode = PhotoEditorMode::Mode::Transform,
.action = PhotoEditorMode::Action::Discard,
};
} else if (mode == PhotoEditorMode::Mode::Transform) {
_cancel.fire({});
}
}, lifetime());
rpl::single(
Deserialize(Core::App().settings().photoEditorBrush())
) | rpl::then(
_colorPicker->saveBrushRequests()
_colorPicker->saveBrushRequests(
) | rpl::start_with_next([=](const Brush &brush) {
_content->applyBrush(brush);
@ -148,8 +151,12 @@ void PhotoEditor::save() {
_done.fire_copy(_modifications);
}
rpl::producer<PhotoModifications> PhotoEditor::done() const {
rpl::producer<PhotoModifications> PhotoEditor::doneRequests() const {
return _done.events();
}
rpl::producer<> PhotoEditor::cancelRequests() const {
return _cancel.events();
}
} // namespace Editor

View file

@ -28,7 +28,8 @@ public:
PhotoModifications modifications);
void save();
rpl::producer<PhotoModifications> done() const;
rpl::producer<PhotoModifications> doneRequests() const;
rpl::producer<> cancelRequests() const;
private:
@ -45,6 +46,7 @@ private:
.action = PhotoEditorMode::Action::None,
};
rpl::event_stream<PhotoModifications> _done;
rpl::event_stream<> _cancel;
};

View file

@ -186,9 +186,13 @@ PhotoEditorControls::PhotoEditorControls(
) | rpl::start_with_next([=](const QRect &clip) {
Painter p(this);
const auto &current = _transformButtons->isHidden()
? _paintButtons
: _transformButtons;
p.setPen(Qt::NoPen);
p.setBrush(_bg);
p.drawRect(_transformButtons->geometry());
p.drawRect(current->geometry());
}, lifetime());

View file

@ -0,0 +1,63 @@
/*
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_layer_widget.h"
#include "editor/photo_editor.h"
namespace Editor {
LayerWidget::LayerWidget(
not_null<Ui::RpWidget*> parent,
not_null<Window::Controller*> window,
std::shared_ptr<QPixmap> photo,
PhotoModifications modifications,
Fn<void(PhotoModifications)> &&doneCallback)
: Ui::LayerWidget(parent)
, _content(base::make_unique_q<PhotoEditor>(
this,
std::move(photo),
std::move(modifications))) {
paintRequest(
) | rpl::start_with_next([=](const QRect &clip) {
Painter p(this);
p.fillRect(clip, st::boxBg);
}, lifetime());
_content->cancelRequests(
) | rpl::start_with_next([=] {
closeLayer();
}, lifetime());
_content->doneRequests(
) | rpl::start_with_next([=, done = std::move(doneCallback)](
const PhotoModifications &mods) {
doneCallback(mods);
closeLayer();
}, lifetime());
sizeValue(
) | rpl::start_with_next([=](const QSize &size) {
_content->resize(size);
}, lifetime());
}
void LayerWidget::parentResized() {
resizeToWidth(parentWidget()->width());
}
int LayerWidget::resizeGetHeight(int newWidth) {
return parentWidget()->height();
}
bool LayerWidget::closeByOutsideClick() const {
return false;
}
} // namespace Editor

View file

@ -0,0 +1,43 @@
/*
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
*/
#pragma once
#include "ui/layers/layer_widget.h"
#include "editor/photo_editor_common.h"
#include "base/unique_qptr.h"
namespace Window {
class Controller;
} // namespace Window
namespace Editor {
class PhotoEditor;
class LayerWidget : public Ui::LayerWidget {
public:
LayerWidget(
not_null<Ui::RpWidget*> parent,
not_null<Window::Controller*> window,
std::shared_ptr<QPixmap> photo,
PhotoModifications modifications,
Fn<void(PhotoModifications)> &&doneCallback);
void parentResized() override;
bool closeByOutsideClick() const override;
protected:
int resizeGetHeight(int newWidth) override;
private:
const base::unique_qptr<PhotoEditor> _content;
};
} // namespace Editor