2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/photo_crop_box.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2019-09-13 12:22:54 +00:00
|
|
|
#include "ui/ui_utility.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "app.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-12-23 17:23:36 +00:00
|
|
|
PhotoCropBox::PhotoCropBox(
|
|
|
|
QWidget*,
|
|
|
|
const QImage &img,
|
|
|
|
const QString &title)
|
2018-12-24 08:16:05 +00:00
|
|
|
: _title(title)
|
|
|
|
, _img(img) {
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
2015-09-16 13:04:08 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void PhotoCropBox::prepare() {
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_settings_save(), [this] { sendPhoto(); });
|
|
|
|
addButton(tr::lng_cancel(), [this] { closeBox(); });
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-10-11 08:37:24 +00:00
|
|
|
int32 s = st::boxWideWidth - st::boxPhotoPadding.left() - st::boxPhotoPadding.right();
|
2016-12-13 17:07:56 +00:00
|
|
|
_thumb = App::pixmapFromImageInPlace(_img.scaled(s * cIntRetinaFactor(), s * cIntRetinaFactor(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
2016-10-08 22:06:21 +00:00
|
|
|
_thumb.setDevicePixelRatio(cRetinaFactor());
|
2016-11-24 19:28:23 +00:00
|
|
|
_mask = QImage(_thumb.size(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
_mask.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
_fade = QImage(_thumb.size(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
_fade.setDevicePixelRatio(cRetinaFactor());
|
2016-10-08 22:06:21 +00:00
|
|
|
_thumbw = _thumb.width() / cIntRetinaFactor();
|
|
|
|
_thumbh = _thumb.height() / cIntRetinaFactor();
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_thumbw > _thumbh) {
|
|
|
|
_cropw = _thumbh - 20;
|
|
|
|
} else {
|
|
|
|
_cropw = _thumbw - 20;
|
|
|
|
}
|
|
|
|
_cropx = (_thumbw - _cropw) / 2;
|
|
|
|
_cropy = (_thumbh - _cropw) / 2;
|
2015-04-02 10:33:19 +00:00
|
|
|
|
2015-10-03 10:09:09 +00:00
|
|
|
_thumbx = (st::boxWideWidth - _thumbw) / 2;
|
2015-10-11 08:37:24 +00:00
|
|
|
_thumby = st::boxPhotoPadding.top();
|
2014-05-30 08:53:19 +00:00
|
|
|
setMouseTracking(true);
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
setDimensions(st::boxWideWidth, st::boxPhotoPadding.top() + _thumbh + st::boxPhotoPadding.bottom() + st::boxTextFont->height + st::cropSkip);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoCropBox::mousePressEvent(QMouseEvent *e) {
|
2016-12-13 17:07:56 +00:00
|
|
|
if (e->button() == Qt::LeftButton) {
|
|
|
|
_downState = mouseState(e->pos());
|
|
|
|
_fromposx = e->pos().x();
|
|
|
|
_fromposy = e->pos().y();
|
|
|
|
_fromcropx = _cropx;
|
|
|
|
_fromcropy = _cropy;
|
|
|
|
_fromcropw = _cropw;
|
|
|
|
}
|
|
|
|
return BoxContent::mousePressEvent(e);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
int PhotoCropBox::mouseState(QPoint p) {
|
2014-05-30 08:53:19 +00:00
|
|
|
p -= QPoint(_thumbx, _thumby);
|
|
|
|
int32 delta = st::cropPointSize, mdelta(-delta / 2);
|
|
|
|
if (QRect(_cropx + mdelta, _cropy + mdelta, delta, delta).contains(p)) {
|
|
|
|
return 1;
|
|
|
|
} else if (QRect(_cropx + _cropw + mdelta, _cropy + mdelta, delta, delta).contains(p)) {
|
|
|
|
return 2;
|
|
|
|
} else if (QRect(_cropx + _cropw + mdelta, _cropy + _cropw + mdelta, delta, delta).contains(p)) {
|
|
|
|
return 3;
|
|
|
|
} else if (QRect(_cropx + mdelta, _cropy + _cropw + mdelta, delta, delta).contains(p)) {
|
|
|
|
return 4;
|
|
|
|
} else if (QRect(_cropx, _cropy, _cropw, _cropw).contains(p)) {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
rpl::producer<QImage> PhotoCropBox::ready() const {
|
|
|
|
return _readyImages.events();
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void PhotoCropBox::mouseReleaseEvent(QMouseEvent *e) {
|
|
|
|
if (_downState) {
|
|
|
|
_downState = 0;
|
|
|
|
mouseMoveEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoCropBox::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
if (_downState && !(e->buttons() & Qt::LeftButton)) {
|
|
|
|
mouseReleaseEvent(e);
|
|
|
|
}
|
|
|
|
if (_downState) {
|
|
|
|
if (_downState == 1) {
|
|
|
|
int32 dx = e->pos().x() - _fromposx, dy = e->pos().y() - _fromposy, d = (dx < dy) ? dx : dy;
|
|
|
|
if (_fromcropx + d < 0) {
|
|
|
|
d = -_fromcropx;
|
|
|
|
}
|
|
|
|
if (_fromcropy + d < 0) {
|
|
|
|
d = -_fromcropy;
|
|
|
|
}
|
|
|
|
if (_fromcropw - d < st::cropMinSize) {
|
|
|
|
d = _fromcropw - st::cropMinSize;
|
|
|
|
}
|
|
|
|
if (_cropx != _fromcropx + d || _cropy != _fromcropy + d || _cropw != _fromcropw - d) {
|
|
|
|
_cropx = _fromcropx + d;
|
|
|
|
_cropy = _fromcropy + d;
|
|
|
|
_cropw = _fromcropw - d;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else if (_downState == 2) {
|
|
|
|
int32 dx = _fromposx - e->pos().x(), dy = e->pos().y() - _fromposy, d = (dx < dy) ? dx : dy;
|
|
|
|
if (_fromcropx + _fromcropw - d > _thumbw) {
|
|
|
|
d = _fromcropx + _fromcropw - _thumbw;
|
|
|
|
}
|
|
|
|
if (_fromcropy + d < 0) {
|
|
|
|
d = -_fromcropy;
|
|
|
|
}
|
|
|
|
if (_fromcropw - d < st::cropMinSize) {
|
|
|
|
d = _fromcropw - st::cropMinSize;
|
|
|
|
}
|
|
|
|
if (_cropy != _fromcropy + d || _cropw != _fromcropw - d) {
|
|
|
|
_cropy = _fromcropy + d;
|
|
|
|
_cropw = _fromcropw - d;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else if (_downState == 3) {
|
|
|
|
int32 dx = _fromposx - e->pos().x(), dy = _fromposy - e->pos().y(), d = (dx < dy) ? dx : dy;
|
|
|
|
if (_fromcropx + _fromcropw - d > _thumbw) {
|
|
|
|
d = _fromcropx + _fromcropw - _thumbw;
|
|
|
|
}
|
|
|
|
if (_fromcropy + _fromcropw - d > _thumbh) {
|
|
|
|
d = _fromcropy + _fromcropw - _thumbh;
|
|
|
|
}
|
|
|
|
if (_fromcropw - d < st::cropMinSize) {
|
|
|
|
d = _fromcropw - st::cropMinSize;
|
|
|
|
}
|
|
|
|
if (_cropw != _fromcropw - d) {
|
|
|
|
_cropw = _fromcropw - d;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else if (_downState == 4) {
|
|
|
|
int32 dx = e->pos().x() - _fromposx, dy = _fromposy - e->pos().y(), d = (dx < dy) ? dx : dy;
|
|
|
|
if (_fromcropx + d < 0) {
|
|
|
|
d = -_fromcropx;
|
|
|
|
}
|
|
|
|
if (_fromcropy + _fromcropw - d > _thumbh) {
|
|
|
|
d = _fromcropy + _fromcropw - _thumbh;
|
|
|
|
}
|
|
|
|
if (_fromcropw - d < st::cropMinSize) {
|
|
|
|
d = _fromcropw - st::cropMinSize;
|
|
|
|
}
|
|
|
|
if (_cropx != _fromcropx + d || _cropw != _fromcropw - d) {
|
|
|
|
_cropx = _fromcropx + d;
|
|
|
|
_cropw = _fromcropw - d;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else if (_downState == 5) {
|
|
|
|
int32 dx = e->pos().x() - _fromposx, dy = e->pos().y() - _fromposy;
|
|
|
|
if (_fromcropx + dx < 0) {
|
|
|
|
dx = -_fromcropx;
|
|
|
|
} else if (_fromcropx + _fromcropw + dx > _thumbw) {
|
|
|
|
dx = _thumbw - _fromcropx - _fromcropw;
|
|
|
|
}
|
|
|
|
if (_fromcropy + dy < 0) {
|
|
|
|
dy = -_fromcropy;
|
|
|
|
} else if (_fromcropy + _fromcropw + dy > _thumbh) {
|
|
|
|
dy = _thumbh - _fromcropy - _fromcropw;
|
|
|
|
}
|
|
|
|
if (_cropx != _fromcropx + dx || _cropy != _fromcropy + dy) {
|
|
|
|
_cropx = _fromcropx + dx;
|
|
|
|
_cropy = _fromcropy + dy;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32 cursorState = _downState ? _downState : mouseState(e->pos());
|
|
|
|
QCursor cur(style::cur_default);
|
|
|
|
if (cursorState == 1 || cursorState == 3) {
|
|
|
|
cur = style::cur_sizefdiag;
|
|
|
|
} else if (cursorState == 2 || cursorState == 4) {
|
|
|
|
cur = style::cur_sizebdiag;
|
|
|
|
} else if (cursorState == 5) {
|
|
|
|
cur = style::cur_sizeall;
|
|
|
|
}
|
|
|
|
setCursor(cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoCropBox::keyPressEvent(QKeyEvent *e) {
|
|
|
|
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
|
2016-12-13 17:07:56 +00:00
|
|
|
sendPhoto();
|
2015-04-02 10:33:19 +00:00
|
|
|
} else {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::keyPressEvent(e);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoCropBox::paintEvent(QPaintEvent *e) {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::paintEvent(e);
|
2016-11-19 14:47:28 +00:00
|
|
|
|
2015-10-11 08:37:24 +00:00
|
|
|
Painter p(this);
|
2016-04-21 17:57:29 +00:00
|
|
|
|
2015-10-11 08:37:24 +00:00
|
|
|
p.setFont(st::boxTextFont);
|
|
|
|
p.setPen(st::boxPhotoTextFg);
|
|
|
|
p.drawText(QRect(st::boxPhotoPadding.left(), st::boxPhotoPadding.top() + _thumbh + st::boxPhotoPadding.bottom(), width() - st::boxPhotoPadding.left() - st::boxPhotoPadding.right(), st::boxTextFont->height), _title, style::al_top);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
p.translate(_thumbx, _thumby);
|
|
|
|
p.drawPixmap(0, 0, _thumb);
|
2016-11-24 19:28:23 +00:00
|
|
|
_mask.fill(Qt::white);
|
|
|
|
{
|
|
|
|
Painter p(&_mask);
|
2016-12-03 12:10:35 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(Qt::black);
|
|
|
|
p.drawEllipse(_cropx, _cropy, _cropw, _cropw);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-11-24 19:28:23 +00:00
|
|
|
style::colorizeImage(_mask, st::photoCropFadeBg->c, &_fade);
|
|
|
|
p.drawImage(0, 0, _fade);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-31 12:29:26 +00:00
|
|
|
int delta = st::cropPointSize;
|
|
|
|
int mdelta = -delta / 2;
|
|
|
|
p.fillRect(QRect(_cropx + mdelta, _cropy + mdelta, delta, delta), st::photoCropPointFg);
|
|
|
|
p.fillRect(QRect(_cropx + _cropw + mdelta, _cropy + mdelta, delta, delta), st::photoCropPointFg);
|
|
|
|
p.fillRect(QRect(_cropx + _cropw + mdelta, _cropy + _cropw + mdelta, delta, delta), st::photoCropPointFg);
|
|
|
|
p.fillRect(QRect(_cropx + mdelta, _cropy + _cropw + mdelta, delta, delta), st::photoCropPointFg);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void PhotoCropBox::sendPhoto() {
|
|
|
|
auto from = _img;
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_img.width() < _thumb.width()) {
|
|
|
|
from = _thumb.toImage();
|
|
|
|
}
|
|
|
|
float64 x = float64(_cropx) / _thumbw, y = float64(_cropy) / _thumbh, w = float64(_cropw) / _thumbw;
|
|
|
|
int32 ix = int32(x * from.width()), iy = int32(y * from.height()), iw = int32(w * from.width());
|
|
|
|
if (ix < 0) {
|
|
|
|
ix = 0;
|
|
|
|
}
|
|
|
|
if (ix + iw > from.width()) {
|
|
|
|
iw = from.width() - ix;
|
|
|
|
}
|
|
|
|
if (iy < 0) {
|
|
|
|
iy = 0;
|
|
|
|
}
|
|
|
|
if (iy + iw > from.height()) {
|
|
|
|
iw = from.height() - iy;
|
|
|
|
}
|
|
|
|
int32 offset = ix * from.depth() / 8 + iy * from.bytesPerLine();
|
2015-11-04 22:12:13 +00:00
|
|
|
QImage cropped(from.constBits() + offset, iw, iw, from.bytesPerLine(), from.format()), tosend;
|
|
|
|
if (from.format() == QImage::Format_Indexed8) {
|
|
|
|
cropped.setColorCount(from.colorCount());
|
|
|
|
cropped.setColorTable(from.colorTable());
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
if (cropped.width() > 1280) {
|
|
|
|
tosend = cropped.scaled(1280, 1280, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
|
|
} else if (cropped.width() < 320) {
|
|
|
|
tosend = cropped.scaled(320, 320, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
|
|
} else {
|
|
|
|
tosend = cropped.copy();
|
|
|
|
}
|
|
|
|
|
2019-09-13 12:22:54 +00:00
|
|
|
auto weak = Ui::MakeWeak(this);
|
2017-11-13 12:02:53 +00:00
|
|
|
_readyImages.fire(std::move(tosend));
|
2017-11-30 18:04:13 +00:00
|
|
|
if (weak) {
|
2017-11-13 12:02:53 +00:00
|
|
|
closeBox();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|