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_controls.h"
|
|
|
|
|
2021-03-21 12:38:33 +00:00
|
|
|
#include "editor/controllers/controllers.h"
|
2021-02-13 08:23:21 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "ui/image/image_prepare.h"
|
2021-02-05 09:24:26 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2021-02-13 08:23:21 +00:00
|
|
|
|
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-13 08:23:21 +00:00
|
|
|
class EdgeButton final : public Ui::RippleButton {
|
|
|
|
public:
|
|
|
|
EdgeButton(
|
|
|
|
not_null<Ui::RpWidget*> parent,
|
|
|
|
const QString &text,
|
|
|
|
int height,
|
|
|
|
bool left,
|
|
|
|
const style::color &bg,
|
|
|
|
const style::color &fg,
|
|
|
|
const style::RippleAnimation &st);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QImage prepareRippleMask() const override;
|
|
|
|
QPoint prepareRippleStartPosition() const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void init();
|
|
|
|
|
|
|
|
const style::color &_fg;
|
|
|
|
Ui::Text::String _text;
|
|
|
|
const int _width;
|
|
|
|
const QRect _rippleRect;
|
|
|
|
const QColor _bg;
|
|
|
|
const bool _left;
|
|
|
|
|
|
|
|
QImage rounded(std::optional<QColor> color) const;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
EdgeButton::EdgeButton(
|
|
|
|
not_null<Ui::RpWidget*> parent,
|
|
|
|
const QString &text,
|
|
|
|
int height,
|
|
|
|
bool left,
|
|
|
|
const style::color &bg,
|
|
|
|
const style::color &fg,
|
|
|
|
const style::RippleAnimation &st)
|
|
|
|
: Ui::RippleButton(parent, st)
|
|
|
|
, _fg(fg)
|
|
|
|
, _text(st::semiboldTextStyle, text.toUpper())
|
|
|
|
, _width(_text.maxWidth()
|
|
|
|
+ st::photoEditorTextButtonPadding.left()
|
|
|
|
+ st::photoEditorTextButtonPadding.right())
|
|
|
|
, _rippleRect(QRect(0, 0, _width, height))
|
|
|
|
, _bg(bg->c)
|
|
|
|
, _left(left) {
|
|
|
|
resize(_width, height);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EdgeButton::init() {
|
|
|
|
const auto bg = rounded(_bg);
|
|
|
|
|
|
|
|
paintRequest(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
p.drawImage(QPoint(), bg);
|
|
|
|
|
|
|
|
paintRipple(p, _rippleRect.x(), _rippleRect.y());
|
|
|
|
|
|
|
|
p.setPen(_fg);
|
|
|
|
const auto textTop = (height() - _text.minHeight()) / 2;
|
|
|
|
_text.draw(p, 0, textTop, width(), style::al_center);
|
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage EdgeButton::rounded(std::optional<QColor> color) const {
|
|
|
|
auto result = QImage(
|
|
|
|
_rippleRect.size() * cIntRetinaFactor(),
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
result.setDevicePixelRatio(cIntRetinaFactor());
|
|
|
|
result.fill(color.value_or(Qt::white));
|
|
|
|
|
|
|
|
using Option = Images::Option;
|
|
|
|
const auto options = Option::Smooth
|
|
|
|
| Option::RoundedLarge
|
|
|
|
| (_left ? Option::RoundedTopLeft : Option::RoundedTopRight)
|
|
|
|
| (_left ? Option::RoundedBottomLeft : Option::RoundedBottomRight);
|
|
|
|
return Images::prepare(std::move(result), 0, 0, options, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage EdgeButton::prepareRippleMask() const {
|
|
|
|
return rounded(std::nullopt);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint EdgeButton::prepareRippleStartPosition() const {
|
|
|
|
return mapFromGlobal(QCursor::pos()) - _rippleRect.topLeft();
|
|
|
|
}
|
|
|
|
|
2021-02-05 08:58:10 +00:00
|
|
|
class HorizontalContainer final : public Ui::RpWidget {
|
|
|
|
public:
|
|
|
|
HorizontalContainer(not_null<Ui::RpWidget*> parent);
|
|
|
|
|
|
|
|
void updateChildrenPosition();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
HorizontalContainer::HorizontalContainer(not_null<Ui::RpWidget*> parent)
|
2021-02-05 05:44:04 +00:00
|
|
|
: RpWidget(parent) {
|
|
|
|
}
|
|
|
|
|
2021-02-05 08:58:10 +00:00
|
|
|
void HorizontalContainer::updateChildrenPosition() {
|
|
|
|
auto left = 0;
|
|
|
|
auto height = 0;
|
|
|
|
for (auto child : RpWidget::children()) {
|
|
|
|
if (child->isWidgetType()) {
|
|
|
|
const auto widget = static_cast<QWidget*>(child);
|
|
|
|
widget->move(left, 0);
|
|
|
|
left += widget->width();
|
|
|
|
height = std::max(height, widget->height());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resize(left, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
PhotoEditorControls::PhotoEditorControls(
|
|
|
|
not_null<Ui::RpWidget*> parent,
|
2021-02-23 10:34:12 +00:00
|
|
|
std::shared_ptr<Controllers> controllers,
|
2021-02-22 08:14:23 +00:00
|
|
|
const PhotoModifications modifications,
|
2021-02-05 08:58:10 +00:00
|
|
|
bool doneControls)
|
|
|
|
: RpWidget(parent)
|
2021-02-13 08:23:21 +00:00
|
|
|
, _bg(st::mediaviewSaveMsgBg)
|
2021-03-14 09:39:55 +00:00
|
|
|
, _transformButtons(base::make_unique_q<HorizontalContainer>(this))
|
|
|
|
, _paintButtons(base::make_unique_q<HorizontalContainer>(this))
|
2021-02-05 09:24:26 +00:00
|
|
|
, _rotateButton(base::make_unique_q<Ui::IconButton>(
|
2021-03-14 09:39:55 +00:00
|
|
|
_transformButtons,
|
2021-02-05 09:24:26 +00:00
|
|
|
st::photoEditorRotateButton))
|
|
|
|
, _flipButton(base::make_unique_q<Ui::IconButton>(
|
2021-03-14 09:39:55 +00:00
|
|
|
_transformButtons,
|
2021-02-13 04:29:31 +00:00
|
|
|
st::photoEditorFlipButton))
|
|
|
|
, _paintModeButton(base::make_unique_q<Ui::IconButton>(
|
2021-03-14 09:39:55 +00:00
|
|
|
_transformButtons,
|
|
|
|
st::photoEditorPaintModeButton))
|
|
|
|
, _undoButton(base::make_unique_q<Ui::IconButton>(
|
|
|
|
_paintButtons,
|
|
|
|
st::photoEditorUndoButton))
|
|
|
|
, _redoButton(base::make_unique_q<Ui::IconButton>(
|
|
|
|
_paintButtons,
|
|
|
|
st::photoEditorRedoButton))
|
|
|
|
, _paintModeButtonActive(base::make_unique_q<Ui::IconButton>(
|
|
|
|
_paintButtons,
|
2021-02-13 08:23:21 +00:00
|
|
|
st::photoEditorPaintModeButton))
|
2021-02-23 10:35:23 +00:00
|
|
|
, _stickersButton(controllers->stickersPanelController
|
|
|
|
? base::make_unique_q<Ui::IconButton>(
|
|
|
|
_paintButtons,
|
|
|
|
st::photoEditorStickersButton)
|
|
|
|
: nullptr)
|
2021-02-13 08:23:21 +00:00
|
|
|
, _cancel(base::make_unique_q<EdgeButton>(
|
|
|
|
this,
|
|
|
|
tr::lng_cancel(tr::now),
|
|
|
|
_flipButton->height(),
|
|
|
|
true,
|
|
|
|
_bg,
|
|
|
|
st::activeButtonFg,
|
|
|
|
st::photoEditorRotateButton.ripple))
|
|
|
|
, _done(base::make_unique_q<EdgeButton>(
|
|
|
|
this,
|
|
|
|
tr::lng_box_done(tr::now),
|
|
|
|
_flipButton->height(),
|
|
|
|
false,
|
|
|
|
_bg,
|
|
|
|
st::lightButtonFg,
|
2021-02-22 08:14:23 +00:00
|
|
|
st::photoEditorRotateButton.ripple))
|
|
|
|
, _flipped(modifications.flipped) {
|
2021-02-05 08:58:10 +00:00
|
|
|
|
2021-03-14 09:39:55 +00:00
|
|
|
_transformButtons->updateChildrenPosition();
|
|
|
|
_paintButtons->updateChildrenPosition();
|
|
|
|
|
|
|
|
_paintModeButtonActive->setIconOverride(
|
|
|
|
&st::photoEditorPaintModeButton.iconOver);
|
|
|
|
_paintModeButtonActive->setAttribute(Qt::WA_TransparentForMouseEvents);
|
2021-02-05 08:58:10 +00:00
|
|
|
|
2021-02-05 09:24:26 +00:00
|
|
|
paintRequest(
|
|
|
|
) | rpl::start_with_next([=](const QRect &clip) {
|
|
|
|
Painter p(this);
|
|
|
|
|
2021-02-18 00:06:44 +00:00
|
|
|
const auto ¤t = _transformButtons->isHidden()
|
|
|
|
? _paintButtons
|
|
|
|
: _transformButtons;
|
|
|
|
|
2021-02-13 08:23:21 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(_bg);
|
2021-02-18 00:06:44 +00:00
|
|
|
p.drawRect(current->geometry());
|
2021-02-05 09:24:26 +00:00
|
|
|
|
|
|
|
}, lifetime());
|
|
|
|
|
2021-02-16 03:45:05 +00:00
|
|
|
const auto &buttonsTop = st::photoEditorControlsTopSkip;
|
|
|
|
|
2021-03-14 09:39:55 +00:00
|
|
|
rpl::combine(
|
|
|
|
sizeValue(),
|
|
|
|
_mode.value()
|
|
|
|
) | rpl::start_with_next([=](
|
|
|
|
const QSize &size,
|
|
|
|
const PhotoEditorMode &mode) {
|
|
|
|
if (size.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto ¤t = _transformButtons->isHidden()
|
|
|
|
? _paintButtons
|
|
|
|
: _transformButtons;
|
2021-02-05 08:58:10 +00:00
|
|
|
|
2021-03-14 09:39:55 +00:00
|
|
|
current->moveToLeft(
|
|
|
|
(size.width() - current->width()) / 2,
|
2021-02-16 03:45:05 +00:00
|
|
|
buttonsTop);
|
2021-02-05 08:58:10 +00:00
|
|
|
|
2021-02-16 03:45:05 +00:00
|
|
|
_cancel->moveToLeft(current->x() - _cancel->width(), buttonsTop);
|
|
|
|
_done->moveToLeft(current->x() + current->width(), buttonsTop);
|
2021-02-13 08:23:21 +00:00
|
|
|
|
2021-02-05 08:58:10 +00:00
|
|
|
}, lifetime());
|
|
|
|
|
2021-02-23 10:34:12 +00:00
|
|
|
controllers->undoController->setPerformRequestChanges(rpl::merge(
|
2021-03-14 09:42:18 +00:00
|
|
|
_undoButton->clicks() | rpl::map_to(Undo::Undo),
|
|
|
|
_redoButton->clicks() | rpl::map_to(Undo::Redo)));
|
|
|
|
|
2021-02-23 10:34:12 +00:00
|
|
|
controllers->undoController->canPerformChanges(
|
2021-03-14 09:42:18 +00:00
|
|
|
) | rpl::start_with_next([=](const UndoController::EnableRequest &r) {
|
|
|
|
const auto isUndo = (r.command == Undo::Undo);
|
|
|
|
const auto &button = isUndo ? _undoButton : _redoButton;
|
|
|
|
button->setAttribute(Qt::WA_TransparentForMouseEvents, !r.enable);
|
|
|
|
if (!r.enable) {
|
|
|
|
button->clearState();
|
|
|
|
}
|
|
|
|
|
|
|
|
button->setIconOverride(r.enable
|
|
|
|
? nullptr
|
|
|
|
: isUndo
|
|
|
|
? &st::photoEditorUndoButtonInactive
|
|
|
|
: &st::photoEditorRedoButtonInactive);
|
|
|
|
}, lifetime());
|
|
|
|
|
2021-02-23 10:35:23 +00:00
|
|
|
if (_stickersButton) {
|
|
|
|
controllers->stickersPanelController->setShowRequestChanges(
|
|
|
|
_stickersButton->clicks(
|
|
|
|
) | rpl::map_to(std::optional<bool>(std::nullopt)));
|
|
|
|
|
|
|
|
controllers->stickersPanelController->setMoveRequestChanges(
|
|
|
|
_paintButtons->positionValue(
|
|
|
|
) | rpl::map([=](const QPoint &containerPos) {
|
|
|
|
return QPoint(
|
|
|
|
(x() + width()) / 2,
|
|
|
|
y() + containerPos.y() + _stickersButton->y());
|
|
|
|
}));
|
|
|
|
|
|
|
|
controllers->stickersPanelController->panelShown(
|
|
|
|
) | rpl::start_with_next([=](bool shown) {
|
|
|
|
_stickersButton->setIconOverride(shown
|
|
|
|
? &st::photoEditorStickersButton.iconOver
|
|
|
|
: nullptr);
|
|
|
|
}, _stickersButton->lifetime());
|
|
|
|
}
|
|
|
|
|
2021-02-22 08:14:23 +00:00
|
|
|
_flipButton->clicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_flipped = !_flipped;
|
|
|
|
_flipButton->setIconOverride(_flipped
|
|
|
|
? &st::photoEditorFlipButton.iconOver
|
|
|
|
: nullptr);
|
|
|
|
}, _flipButton->lifetime());
|
|
|
|
|
2021-02-05 08:58:10 +00:00
|
|
|
}
|
2021-02-07 23:10:30 +00:00
|
|
|
|
|
|
|
rpl::producer<int> PhotoEditorControls::rotateRequests() const {
|
2021-02-13 08:23:21 +00:00
|
|
|
return _rotateButton->clicks() | rpl::map_to(90);
|
2021-02-07 23:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> PhotoEditorControls::flipRequests() const {
|
|
|
|
return _flipButton->clicks() | rpl::to_empty;
|
|
|
|
}
|
|
|
|
|
2021-02-13 04:29:31 +00:00
|
|
|
rpl::producer<> PhotoEditorControls::paintModeRequests() const {
|
|
|
|
return _paintModeButton->clicks() | rpl::to_empty;
|
|
|
|
}
|
|
|
|
|
2021-03-14 09:39:55 +00:00
|
|
|
rpl::producer<> PhotoEditorControls::doneRequests() const {
|
|
|
|
return _done->clicks() | rpl::to_empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> PhotoEditorControls::cancelRequests() const {
|
|
|
|
return _cancel->clicks() | rpl::to_empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoEditorControls::applyMode(const PhotoEditorMode &mode) {
|
|
|
|
using Mode = PhotoEditorMode::Mode;
|
|
|
|
_transformButtons->setVisible(mode.mode == Mode::Transform);
|
|
|
|
_paintButtons->setVisible(mode.mode == Mode::Paint);
|
|
|
|
_mode = mode;
|
|
|
|
}
|
|
|
|
|
2021-02-05 05:44:04 +00:00
|
|
|
} // namespace Editor
|