2017-12-22 05:16:23 +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.
|
2017-12-22 05:16:23 +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
|
2017-12-22 05:16:23 +00:00
|
|
|
*/
|
|
|
|
#include "boxes/edit_caption_box.h"
|
|
|
|
|
2019-03-26 21:14:51 +00:00
|
|
|
#include "apiwrap.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "api/api_text_entities.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-03-26 21:14:51 +00:00
|
|
|
#include "chat_helpers/emoji_suggestions_widget.h"
|
|
|
|
#include "chat_helpers/message_field.h"
|
|
|
|
#include "chat_helpers/tabbed_panel.h"
|
|
|
|
#include "chat_helpers/tabbed_selector.h"
|
2019-09-19 09:28:36 +00:00
|
|
|
#include "base/event_filter.h"
|
2019-03-26 21:14:51 +00:00
|
|
|
#include "core/file_utilities.h"
|
|
|
|
#include "core/mime_type.h"
|
|
|
|
#include "data/data_document.h"
|
2018-01-14 16:02:25 +00:00
|
|
|
#include "data/data_media_types.h"
|
|
|
|
#include "data/data_photo.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
2019-04-25 12:45:15 +00:00
|
|
|
#include "data/data_session.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "data/data_file_origin.h"
|
2020-04-09 14:02:09 +00:00
|
|
|
#include "data/data_document_media.h"
|
2019-03-26 21:14:51 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
2017-12-22 05:16:23 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2018-01-11 19:33:26 +00:00
|
|
|
#include "layout.h"
|
2019-03-26 21:14:51 +00:00
|
|
|
#include "media/clip/media_clip_reader.h"
|
|
|
|
#include "storage/storage_media_prepare.h"
|
|
|
|
#include "ui/image/image.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "ui/widgets/input_fields.h"
|
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/checkbox.h"
|
2019-03-26 21:14:51 +00:00
|
|
|
#include "ui/special_buttons.h"
|
|
|
|
#include "ui/text_options.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2019-04-02 12:46:08 +00:00
|
|
|
#include "confirm_box.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
|
|
|
#include "app.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
#include "styles/style_chat_helpers.h"
|
|
|
|
#include "styles/style_history.h"
|
2019-03-24 12:39:19 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QMimeData>
|
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
EditCaptionBox::EditCaptionBox(
|
|
|
|
QWidget*,
|
2019-06-06 10:21:40 +00:00
|
|
|
not_null<Window::SessionController*> controller,
|
2018-01-18 13:59:22 +00:00
|
|
|
not_null<HistoryItem*> item)
|
2018-05-26 14:58:21 +00:00
|
|
|
: _controller(controller)
|
|
|
|
, _msgId(item->fullId()) {
|
2018-01-18 13:59:22 +00:00
|
|
|
Expects(item->media() != nullptr);
|
|
|
|
Expects(item->media()->allowsEditCaption());
|
2019-04-02 10:38:53 +00:00
|
|
|
|
2019-03-29 10:51:19 +00:00
|
|
|
_isAllowedEditMedia = item->media()->allowsEditMedia();
|
2019-04-02 10:38:53 +00:00
|
|
|
_isAlbum = !item->groupId().empty();
|
2017-12-22 05:16:23 +00:00
|
|
|
|
|
|
|
QSize dimensions;
|
2019-01-25 14:37:28 +00:00
|
|
|
auto image = (Image*)nullptr;
|
2017-12-22 05:16:23 +00:00
|
|
|
DocumentData *doc = nullptr;
|
|
|
|
|
2018-01-18 13:59:22 +00:00
|
|
|
const auto media = item->media();
|
2018-01-14 16:02:25 +00:00
|
|
|
if (const auto photo = media->photo()) {
|
2017-12-22 05:16:23 +00:00
|
|
|
_photo = true;
|
2019-01-25 14:37:28 +00:00
|
|
|
dimensions = QSize(photo->width(), photo->height());
|
|
|
|
image = photo->large();
|
2018-01-14 16:02:25 +00:00
|
|
|
} else if (const auto document = media->document()) {
|
2019-01-25 14:37:28 +00:00
|
|
|
image = document->thumbnail();
|
2019-03-21 12:17:18 +00:00
|
|
|
dimensions = image
|
|
|
|
? image->size()
|
|
|
|
: document->dimensions;
|
2018-01-14 16:02:25 +00:00
|
|
|
if (document->isAnimation()) {
|
2019-03-21 12:17:18 +00:00
|
|
|
_gifw = document->dimensions.width();
|
|
|
|
_gifh = document->dimensions.height();
|
2017-12-22 05:16:23 +00:00
|
|
|
_animated = true;
|
2018-01-14 16:02:25 +00:00
|
|
|
} else if (document->isVideoFile()) {
|
|
|
|
_animated = true;
|
|
|
|
} else {
|
|
|
|
_doc = true;
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
doc = document;
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
2019-06-05 18:40:21 +00:00
|
|
|
const auto editData = PrepareEditText(item);
|
2017-12-22 05:16:23 +00:00
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
if (!_animated && (dimensions.isEmpty() || doc || !image)) {
|
|
|
|
if (!image) {
|
2017-12-22 05:16:23 +00:00
|
|
|
_thumbw = 0;
|
|
|
|
} else {
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto tw = image->width(), th = image->height();
|
2017-12-22 05:16:23 +00:00
|
|
|
if (tw > th) {
|
|
|
|
_thumbw = (tw * st::msgFileThumbSize) / th;
|
|
|
|
} else {
|
|
|
|
_thumbw = st::msgFileThumbSize;
|
|
|
|
}
|
2018-08-02 14:31:54 +00:00
|
|
|
_thumbnailImage = image;
|
|
|
|
_refreshThumbnail = [=] {
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto options = Images::Option::Smooth
|
2018-08-02 14:31:54 +00:00
|
|
|
| Images::Option::RoundedSmall
|
|
|
|
| Images::Option::RoundedTopLeft
|
|
|
|
| Images::Option::RoundedTopRight
|
|
|
|
| Images::Option::RoundedBottomLeft
|
|
|
|
| Images::Option::RoundedBottomRight;
|
2018-10-23 09:44:42 +00:00
|
|
|
_thumb = App::pixmapFromImageInPlace(Images::prepare(
|
2018-07-13 21:25:47 +00:00
|
|
|
image->pix(_msgId).toImage(),
|
2018-08-02 14:31:54 +00:00
|
|
|
_thumbw * cIntRetinaFactor(),
|
|
|
|
0,
|
|
|
|
options,
|
|
|
|
st::msgFileThumbSize,
|
2018-10-23 09:44:42 +00:00
|
|
|
st::msgFileThumbSize));
|
2018-08-02 14:31:54 +00:00
|
|
|
};
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (doc) {
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto nameString = doc->isVoiceMessage()
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_media_audio(tr::now)
|
2017-12-22 05:16:23 +00:00
|
|
|
: doc->composeNameString();
|
2019-03-26 21:14:51 +00:00
|
|
|
setName(nameString, doc->size);
|
2017-12-22 05:16:23 +00:00
|
|
|
_isImage = doc->isImage();
|
|
|
|
_isAudio = (doc->isVoiceMessage() || doc->isAudioFile());
|
|
|
|
}
|
2018-08-02 14:31:54 +00:00
|
|
|
if (_refreshThumbnail) {
|
|
|
|
_refreshThumbnail();
|
|
|
|
}
|
2017-12-22 05:16:23 +00:00
|
|
|
} else {
|
2019-02-17 08:31:04 +00:00
|
|
|
if (!image) {
|
|
|
|
image = Image::BlankMedia();
|
|
|
|
}
|
2019-03-21 12:28:42 +00:00
|
|
|
auto maxW = 0, maxH = 0;
|
|
|
|
const auto limitW = st::sendMediaPreviewSize;
|
|
|
|
auto limitH = std::min(st::confirmMaxHeight, _gifh ? _gifh : INT_MAX);
|
2017-12-22 05:16:23 +00:00
|
|
|
if (_animated) {
|
2019-03-21 12:28:42 +00:00
|
|
|
maxW = std::max(dimensions.width(), 1);
|
|
|
|
maxH = std::max(dimensions.height(), 1);
|
2017-12-22 05:16:23 +00:00
|
|
|
if (maxW * limitH > maxH * limitW) {
|
|
|
|
if (maxW < limitW) {
|
|
|
|
maxH = maxH * limitW / maxW;
|
|
|
|
maxW = limitW;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (maxH < limitH) {
|
|
|
|
maxW = maxW * limitH / maxH;
|
|
|
|
maxH = limitH;
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 14:31:54 +00:00
|
|
|
_thumbnailImage = image;
|
|
|
|
_refreshThumbnail = [=] {
|
|
|
|
const auto options = Images::Option::Smooth
|
|
|
|
| Images::Option::Blurred;
|
|
|
|
_thumb = image->pixNoCache(
|
2018-07-13 21:25:47 +00:00
|
|
|
_msgId,
|
2018-08-02 14:31:54 +00:00
|
|
|
maxW * cIntRetinaFactor(),
|
|
|
|
maxH * cIntRetinaFactor(),
|
|
|
|
options,
|
|
|
|
maxW,
|
|
|
|
maxH);
|
|
|
|
};
|
2020-04-09 14:02:09 +00:00
|
|
|
if (doc) {
|
|
|
|
_gifMedia = doc->createMediaView();
|
|
|
|
}
|
2017-12-22 05:16:23 +00:00
|
|
|
prepareGifPreview(doc);
|
|
|
|
} else {
|
|
|
|
maxW = dimensions.width();
|
|
|
|
maxH = dimensions.height();
|
2018-08-02 14:31:54 +00:00
|
|
|
_thumbnailImage = image;
|
|
|
|
_refreshThumbnail = [=] {
|
|
|
|
_thumb = image->pixNoCache(
|
2018-07-13 21:25:47 +00:00
|
|
|
_msgId,
|
2018-08-02 14:31:54 +00:00
|
|
|
maxW * cIntRetinaFactor(),
|
|
|
|
maxH * cIntRetinaFactor(),
|
|
|
|
Images::Option::Smooth,
|
|
|
|
maxW,
|
|
|
|
maxH);
|
|
|
|
};
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
2018-08-02 14:31:54 +00:00
|
|
|
_refreshThumbnail();
|
|
|
|
|
2019-03-21 12:17:18 +00:00
|
|
|
const auto resizeDimensions = [&](int &thumbWidth, int &thumbHeight, int &thumbX) {
|
2019-03-21 12:28:42 +00:00
|
|
|
auto tw = thumbWidth, th = thumbHeight;
|
2019-03-21 12:17:18 +00:00
|
|
|
if (!tw || !th) {
|
|
|
|
tw = th = 1;
|
|
|
|
}
|
2019-04-04 16:31:34 +00:00
|
|
|
|
|
|
|
// Edit media button takes place on thumb preview
|
|
|
|
// And its height can be greater than height of thumb.
|
|
|
|
const auto minThumbHeight = st::editMediaButtonSize
|
|
|
|
+ st::editMediaButtonSkip * 2;
|
|
|
|
const auto minThumbWidth = minThumbHeight * tw / th;
|
|
|
|
|
2019-03-21 12:17:18 +00:00
|
|
|
if (thumbWidth < st::sendMediaPreviewSize) {
|
2019-04-04 16:31:34 +00:00
|
|
|
thumbWidth = (thumbWidth > minThumbWidth)
|
|
|
|
? thumbWidth
|
|
|
|
: minThumbWidth;
|
2019-03-21 12:17:18 +00:00
|
|
|
} else {
|
|
|
|
thumbWidth = st::sendMediaPreviewSize;
|
|
|
|
}
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto maxThumbHeight = std::min(int(std::round(1.5 * thumbWidth)), limitH);
|
|
|
|
thumbHeight = int(std::round(th * float64(thumbWidth) / tw));
|
2019-03-21 12:17:18 +00:00
|
|
|
if (thumbHeight > maxThumbHeight) {
|
2019-03-21 12:28:42 +00:00
|
|
|
thumbWidth = int(std::round(thumbWidth * float64(maxThumbHeight) / thumbHeight));
|
2019-03-21 12:17:18 +00:00
|
|
|
thumbHeight = maxThumbHeight;
|
|
|
|
if (thumbWidth < 10) {
|
|
|
|
thumbWidth = 10;
|
|
|
|
}
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
2019-03-21 12:17:18 +00:00
|
|
|
thumbX = (st::boxWideWidth - thumbWidth) / 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (doc && doc->isAnimation()) {
|
|
|
|
resizeDimensions(_gifw, _gifh, _gifx);
|
|
|
|
}
|
|
|
|
limitH = std::min(st::confirmMaxHeight, _gifh ? _gifh : INT_MAX);
|
|
|
|
|
|
|
|
_thumbw = _thumb.width();
|
|
|
|
_thumbh = _thumb.height();
|
|
|
|
// If thumb's and resized gif's sizes are equal,
|
|
|
|
// Then just take made values.
|
|
|
|
if (_thumbw == _gifw && _thumbh == _gifh) {
|
|
|
|
_thumbx = (st::boxWideWidth - _thumbw) / 2;
|
|
|
|
} else {
|
|
|
|
resizeDimensions(_thumbw, _thumbh, _thumbx);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:31:54 +00:00
|
|
|
const auto prepareBasicThumb = _refreshThumbnail;
|
|
|
|
const auto scaleThumbDown = [=] {
|
|
|
|
_thumb = App::pixmapFromImageInPlace(_thumb.toImage().scaled(
|
|
|
|
_thumbw * cIntRetinaFactor(),
|
|
|
|
_thumbh * cIntRetinaFactor(),
|
2019-04-04 16:31:34 +00:00
|
|
|
Qt::KeepAspectRatio,
|
2018-08-02 14:31:54 +00:00
|
|
|
Qt::SmoothTransformation));
|
|
|
|
_thumb.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
};
|
|
|
|
_refreshThumbnail = [=] {
|
|
|
|
prepareBasicThumb();
|
|
|
|
scaleThumbDown();
|
|
|
|
};
|
|
|
|
scaleThumbDown();
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
Assert(_animated || _photo || _doc);
|
|
|
|
|
2018-08-02 14:31:54 +00:00
|
|
|
_thumbnailImageLoaded = _thumbnailImage
|
|
|
|
? _thumbnailImage->loaded()
|
|
|
|
: true;
|
2019-07-24 14:00:30 +00:00
|
|
|
subscribe(_controller->session().downloaderTaskFinished(), [=] {
|
2019-02-17 08:31:04 +00:00
|
|
|
if (!_thumbnailImageLoaded
|
|
|
|
&& _thumbnailImage
|
|
|
|
&& _thumbnailImage->loaded()) {
|
2018-08-02 14:31:54 +00:00
|
|
|
_thumbnailImageLoaded = true;
|
|
|
|
_refreshThumbnail();
|
|
|
|
update();
|
|
|
|
}
|
2020-04-09 14:02:09 +00:00
|
|
|
if (doc && doc->isAnimation()) {
|
|
|
|
if (!_gifMedia) {
|
|
|
|
_gifMedia = doc->createMediaView();
|
|
|
|
}
|
|
|
|
if (_gifMedia->loaded() && !_gifPreview) {
|
|
|
|
prepareGifPreview(doc);
|
|
|
|
}
|
2018-08-02 14:31:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-20 17:42:30 +00:00
|
|
|
_field.create(
|
|
|
|
this,
|
|
|
|
st::confirmCaptionArea,
|
|
|
|
Ui::InputField::Mode::MultiLine,
|
2019-06-18 14:07:45 +00:00
|
|
|
tr::lng_photo_caption(),
|
2018-05-24 13:03:21 +00:00
|
|
|
editData);
|
2018-10-17 11:38:07 +00:00
|
|
|
_field->setMaxLength(Global::CaptionLengthMax());
|
2020-02-08 18:45:38 +00:00
|
|
|
_field->setSubmitSettings(_controller->session().settings().sendSubmitWay());
|
2018-05-20 17:42:30 +00:00
|
|
|
_field->setInstantReplaces(Ui::InstantReplaces::Default());
|
2019-08-02 10:40:35 +00:00
|
|
|
_field->setInstantReplacesEnabled(
|
|
|
|
_controller->session().settings().replaceEmojiValue());
|
2018-05-25 17:47:29 +00:00
|
|
|
_field->setMarkdownReplacesEnabled(rpl::single(true));
|
2019-08-02 10:40:35 +00:00
|
|
|
_field->setEditLinkCallback(
|
|
|
|
DefaultEditLinkCallback(&_controller->session(), _field));
|
2019-03-27 10:44:57 +00:00
|
|
|
|
2019-11-05 11:27:21 +00:00
|
|
|
InitSpellchecker(&_controller->session(), _field);
|
2019-10-18 13:54:26 +00:00
|
|
|
|
2019-03-27 10:44:57 +00:00
|
|
|
auto r = object_ptr<Ui::SlideWrap<Ui::Checkbox>>(
|
|
|
|
this,
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
this,
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_send_file(tr::now),
|
2019-03-27 10:44:57 +00:00
|
|
|
false,
|
|
|
|
st::defaultBoxCheckbox),
|
|
|
|
st::editMediaCheckboxMargins);
|
|
|
|
_wayWrap = r.data();
|
|
|
|
_wayWrap->toggle(false, anim::type::instant);
|
|
|
|
|
|
|
|
r->entity()->checkedChanges(
|
|
|
|
) | rpl::start_with_next([&](bool checked) {
|
|
|
|
_asFile = checked;
|
|
|
|
}, _wayWrap->lifetime());
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 10:51:36 +00:00
|
|
|
void EditCaptionBox::emojiFilterForGeometry(not_null<QEvent*> event) {
|
2018-11-22 12:48:50 +00:00
|
|
|
const auto type = event->type();
|
|
|
|
if (type == QEvent::Move || type == QEvent::Resize) {
|
|
|
|
// updateEmojiPanelGeometry uses not only container geometry, but
|
|
|
|
// also container children geometries that will be updated later.
|
|
|
|
crl::on_main(this, [=] { updateEmojiPanelGeometry(); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::updateEmojiPanelGeometry() {
|
|
|
|
const auto parent = _emojiPanel->parentWidget();
|
|
|
|
const auto global = _emojiToggle->mapToGlobal({ 0, 0 });
|
|
|
|
const auto local = parent->mapFromGlobal(global);
|
|
|
|
_emojiPanel->moveBottomRight(
|
|
|
|
local.y(),
|
|
|
|
local.x() + _emojiToggle->width() * 3);
|
|
|
|
}
|
|
|
|
|
2019-03-27 06:28:18 +00:00
|
|
|
void EditCaptionBox::prepareGifPreview(DocumentData* document) {
|
2020-04-09 14:02:09 +00:00
|
|
|
Expects(!document || (_gifMedia != nullptr));
|
|
|
|
|
2019-04-08 18:13:18 +00:00
|
|
|
const auto isListEmpty = _preparedList.files.empty();
|
2018-08-02 14:31:54 +00:00
|
|
|
if (_gifPreview) {
|
|
|
|
return;
|
2019-04-08 18:13:18 +00:00
|
|
|
} else if (!document && isListEmpty) {
|
2019-03-27 06:28:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto callback = [=](Media::Clip::Notification notification) {
|
|
|
|
clipCallback(notification);
|
|
|
|
};
|
2020-04-09 14:02:09 +00:00
|
|
|
if (document && document->isAnimation() && _gifMedia->loaded()) {
|
2019-03-27 06:28:18 +00:00
|
|
|
_gifPreview = Media::Clip::MakeReader(
|
2020-04-09 12:27:53 +00:00
|
|
|
_gifMedia.get(),
|
2019-03-27 06:28:18 +00:00
|
|
|
_msgId,
|
|
|
|
callback);
|
2019-04-08 18:13:18 +00:00
|
|
|
} else if (!isListEmpty) {
|
|
|
|
const auto file = &_preparedList.files.front();
|
|
|
|
if (file->path.isEmpty()) {
|
|
|
|
_gifPreview = Media::Clip::MakeReader(
|
|
|
|
file->content,
|
|
|
|
callback);
|
|
|
|
} else {
|
|
|
|
_gifPreview = Media::Clip::MakeReader(
|
|
|
|
file->path,
|
|
|
|
callback);
|
|
|
|
}
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
2019-03-27 06:28:18 +00:00
|
|
|
if (_gifPreview) _gifPreview->setAutoplay();
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::clipCallback(Media::Clip::Notification notification) {
|
|
|
|
using namespace Media::Clip;
|
|
|
|
switch (notification) {
|
|
|
|
case NotificationReinit: {
|
|
|
|
if (_gifPreview && _gifPreview->state() == State::Error) {
|
|
|
|
_gifPreview.setBad();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_gifPreview && _gifPreview->ready() && !_gifPreview->started()) {
|
2019-04-08 18:13:18 +00:00
|
|
|
const auto calculateGifDimensions = [&]() {
|
|
|
|
const auto scaled = QSize(
|
|
|
|
_gifPreview->width(),
|
|
|
|
_gifPreview->height()).scaled(
|
|
|
|
st::sendMediaPreviewSize * cIntRetinaFactor(),
|
|
|
|
st::confirmMaxHeight * cIntRetinaFactor(),
|
|
|
|
Qt::KeepAspectRatio);
|
|
|
|
_thumbw = _gifw = scaled.width();
|
|
|
|
_thumbh = _gifh = scaled.height();
|
|
|
|
_thumbx = _gifx = (st::boxWideWidth - _gifw) / 2;
|
|
|
|
updateBoxSize();
|
|
|
|
};
|
|
|
|
// If gif file is not mp4,
|
|
|
|
// Its dimension values will be known only after reading.
|
|
|
|
if (_gifw <= 0 || _gifh <= 0) {
|
|
|
|
calculateGifDimensions();
|
|
|
|
}
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto s = QSize(_gifw, _gifh);
|
2017-12-22 05:16:23 +00:00
|
|
|
_gifPreview->start(s.width(), s.height(), s.width(), s.height(), ImageRoundRadius::None, RectPart::None);
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NotificationRepaint: {
|
|
|
|
if (_gifPreview && !_gifPreview->currentDisplayed()) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:09:14 +00:00
|
|
|
void EditCaptionBox::updateEditPreview() {
|
|
|
|
using Info = FileMediaInformation;
|
|
|
|
|
|
|
|
const auto file = &_preparedList.files.front();
|
|
|
|
const auto fileMedia = &file->information->media;
|
|
|
|
|
2019-04-02 10:38:53 +00:00
|
|
|
const auto fileinfo = QFileInfo(file->path);
|
2019-04-01 19:09:14 +00:00
|
|
|
const auto filename = fileinfo.fileName();
|
|
|
|
|
2019-04-04 07:05:27 +00:00
|
|
|
_isImage = fileIsImage(filename, file->mime);
|
2019-04-01 19:09:14 +00:00
|
|
|
_isAudio = false;
|
|
|
|
_animated = false;
|
|
|
|
_photo = false;
|
|
|
|
_doc = false;
|
|
|
|
_gifPreview = nullptr;
|
|
|
|
_thumbw = _thumbh = _thumbx = 0;
|
|
|
|
_gifw = _gifh = _gifx = 0;
|
|
|
|
|
2019-04-08 18:13:18 +00:00
|
|
|
auto isGif = false;
|
2019-04-04 16:04:41 +00:00
|
|
|
auto shouldAsDoc = true;
|
2019-04-08 20:50:18 +00:00
|
|
|
auto docPhotoSize = QSize();
|
2019-04-01 19:09:14 +00:00
|
|
|
if (const auto image = base::get_if<Info::Image>(fileMedia)) {
|
2019-04-04 16:04:41 +00:00
|
|
|
shouldAsDoc = !Storage::ValidateThumbDimensions(
|
|
|
|
image->data.width(),
|
|
|
|
image->data.height());
|
2019-04-08 20:50:18 +00:00
|
|
|
if (shouldAsDoc) {
|
|
|
|
docPhotoSize.setWidth(image->data.width());
|
|
|
|
docPhotoSize.setHeight(image->data.height());
|
|
|
|
}
|
2019-04-08 18:13:18 +00:00
|
|
|
isGif = image->animated;
|
|
|
|
_animated = isGif;
|
|
|
|
_photo = !isGif && !shouldAsDoc;
|
2019-04-01 19:09:14 +00:00
|
|
|
_isImage = true;
|
|
|
|
} else if (const auto video = base::get_if<Info::Video>(fileMedia)) {
|
2019-04-08 18:13:18 +00:00
|
|
|
isGif = video->isGifv;
|
2019-04-01 19:09:14 +00:00
|
|
|
_animated = true;
|
2019-04-04 16:04:41 +00:00
|
|
|
shouldAsDoc = false;
|
|
|
|
}
|
|
|
|
if (shouldAsDoc) {
|
2019-04-01 19:09:14 +00:00
|
|
|
auto nameString = filename;
|
|
|
|
if (const auto song = base::get_if<Info::Song>(fileMedia)) {
|
|
|
|
nameString = DocumentData::ComposeNameString(
|
|
|
|
filename,
|
|
|
|
song->title,
|
|
|
|
song->performer);
|
|
|
|
_isAudio = true;
|
|
|
|
}
|
2019-04-09 17:46:07 +00:00
|
|
|
|
|
|
|
const auto getExt = [&] {
|
|
|
|
auto patterns = Core::MimeTypeForName(file->mime).globPatterns();
|
|
|
|
if (!patterns.isEmpty()) {
|
|
|
|
return patterns.front().replace('*', QString());
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
};
|
2019-04-01 19:09:14 +00:00
|
|
|
setName(
|
|
|
|
nameString.isEmpty()
|
2019-04-09 17:46:07 +00:00
|
|
|
? filedialogDefaultName(
|
|
|
|
_isImage ? qsl("image") : qsl("file"),
|
|
|
|
getExt(),
|
|
|
|
QString(),
|
|
|
|
true)
|
2019-04-01 19:09:14 +00:00
|
|
|
: nameString,
|
|
|
|
fileinfo.size()
|
|
|
|
? fileinfo.size()
|
|
|
|
: _preparedList.files.front().content.size());
|
2019-04-08 20:50:18 +00:00
|
|
|
// Show image dimensions if it should be sent as doc.
|
2019-04-16 04:49:58 +00:00
|
|
|
if (_isImage && docPhotoSize.isValid()) {
|
2019-04-08 20:50:18 +00:00
|
|
|
_status = qsl("%1x%2")
|
|
|
|
.arg(docPhotoSize.width())
|
|
|
|
.arg(docPhotoSize.height());
|
|
|
|
}
|
2019-04-01 19:09:14 +00:00
|
|
|
_doc = true;
|
|
|
|
}
|
|
|
|
|
2019-06-28 10:53:06 +00:00
|
|
|
const auto showCheckbox = _photo && !_isAlbum;
|
|
|
|
_wayWrap->toggle(showCheckbox, anim::type::instant);
|
2019-04-01 19:09:14 +00:00
|
|
|
|
|
|
|
if (!_doc) {
|
|
|
|
_thumb = App::pixmapFromImageInPlace(
|
2019-04-03 19:26:10 +00:00
|
|
|
file->preview.scaled(
|
|
|
|
st::sendMediaPreviewSize * cIntRetinaFactor(),
|
2019-06-28 10:53:06 +00:00
|
|
|
(st::confirmMaxHeight - (showCheckbox
|
|
|
|
? st::confirmMaxHeightSkip
|
|
|
|
: 0)) * cIntRetinaFactor(),
|
2019-04-01 19:09:14 +00:00
|
|
|
Qt::KeepAspectRatio));
|
2019-04-03 19:26:10 +00:00
|
|
|
_thumbw = _thumb.width() / cIntRetinaFactor();
|
|
|
|
_thumbh = _thumb.height() / cIntRetinaFactor();
|
2019-04-01 19:09:14 +00:00
|
|
|
_thumbx = (st::boxWideWidth - _thumbw) / 2;
|
2019-04-08 18:13:18 +00:00
|
|
|
if (isGif) {
|
|
|
|
_gifw = _thumbw;
|
|
|
|
_gifh = _thumbh;
|
|
|
|
_gifx = _thumbx;
|
|
|
|
prepareGifPreview();
|
|
|
|
}
|
2019-04-01 19:09:14 +00:00
|
|
|
}
|
2019-04-04 16:31:34 +00:00
|
|
|
updateEditMediaButton();
|
2019-04-01 19:09:14 +00:00
|
|
|
captionResized();
|
|
|
|
}
|
|
|
|
|
2019-04-04 16:31:34 +00:00
|
|
|
void EditCaptionBox::updateEditMediaButton() {
|
|
|
|
const auto icon = _doc
|
|
|
|
? &st::editMediaButtonIconFile
|
|
|
|
: &st::editMediaButtonIconPhoto;
|
|
|
|
const auto color = _doc ? &st::windowBgRipple : &st::callFingerprintBg;
|
|
|
|
_editMedia->setIconOverride(icon);
|
|
|
|
_editMedia->setRippleColorOverride(color);
|
|
|
|
_editMedia->setForceRippled(!_doc, anim::type::instant);
|
|
|
|
}
|
|
|
|
|
2019-03-29 10:51:19 +00:00
|
|
|
void EditCaptionBox::createEditMediaButton() {
|
2019-04-01 19:09:14 +00:00
|
|
|
const auto callback = [=](FileDialog::OpenResult &&result) {
|
2020-01-21 20:31:34 +00:00
|
|
|
auto showBoxErrorCallback = [](tr::phrase<> t) {
|
|
|
|
Ui::show(Box<InformBox>(t(tr::now)), Ui::LayerOption::KeepOther);
|
2019-04-02 16:24:34 +00:00
|
|
|
};
|
|
|
|
|
2020-01-21 20:31:34 +00:00
|
|
|
auto list = Storage::PreparedList::PreparedFileFromFilesDialog(
|
2020-01-03 21:01:09 +00:00
|
|
|
std::move(result),
|
|
|
|
_isAlbum,
|
2020-01-21 20:31:34 +00:00
|
|
|
std::move(showBoxErrorCallback),
|
2020-01-03 21:01:09 +00:00
|
|
|
st::sendMediaPreviewSize);
|
2019-04-02 16:24:34 +00:00
|
|
|
|
2020-01-03 21:01:09 +00:00
|
|
|
if (list) {
|
|
|
|
_preparedList = std::move(*list);
|
|
|
|
updateEditPreview();
|
2019-03-29 10:51:19 +00:00
|
|
|
}
|
|
|
|
};
|
2019-03-24 12:39:19 +00:00
|
|
|
|
2019-03-30 08:53:28 +00:00
|
|
|
const auto buttonCallback = [=] {
|
2019-04-02 10:38:53 +00:00
|
|
|
const auto filters = _isAlbum
|
2020-01-05 20:37:41 +00:00
|
|
|
? FileDialog::AlbumFilesFilter()
|
|
|
|
: FileDialog::AllFilesFilter();
|
2019-03-24 12:39:19 +00:00
|
|
|
FileDialog::GetOpenPath(
|
|
|
|
this,
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_choose_file(tr::now),
|
2020-01-05 20:37:41 +00:00
|
|
|
filters,
|
2019-03-24 12:39:19 +00:00
|
|
|
crl::guard(this, callback));
|
2019-03-30 08:53:28 +00:00
|
|
|
};
|
|
|
|
|
2019-04-04 16:31:34 +00:00
|
|
|
_editMediaClicks.events(
|
|
|
|
) | rpl::start_with_next(
|
|
|
|
buttonCallback,
|
|
|
|
lifetime());
|
2019-03-30 08:53:28 +00:00
|
|
|
|
2019-04-04 16:31:34 +00:00
|
|
|
// Create edit media button.
|
|
|
|
_editMedia.create(this, st::editMediaButton);
|
|
|
|
updateEditMediaButton();
|
2019-04-08 17:05:31 +00:00
|
|
|
_editMedia->setClickedCallback(
|
|
|
|
App::LambdaDelayed(st::historyAttach.ripple.hideDuration, this, [=] {
|
|
|
|
buttonCallback();
|
|
|
|
}));
|
2019-03-29 10:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::prepare() {
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_settings_save(), [this] { save(); });
|
2019-03-29 10:51:19 +00:00
|
|
|
if (_isAllowedEditMedia) {
|
|
|
|
createEditMediaButton();
|
|
|
|
} else {
|
2019-04-02 10:38:53 +00:00
|
|
|
_preparedList.files.clear();
|
2019-03-29 10:51:19 +00:00
|
|
|
}
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_cancel(), [this] { closeBox(); });
|
2017-12-22 05:16:23 +00:00
|
|
|
|
|
|
|
updateBoxSize();
|
2018-05-31 12:20:28 +00:00
|
|
|
connect(_field, &Ui::InputField::submitted, [=] { save(); });
|
|
|
|
connect(_field, &Ui::InputField::cancelled, [=] { closeBox(); });
|
|
|
|
connect(_field, &Ui::InputField::resized, [=] { captionResized(); });
|
2019-04-01 19:09:14 +00:00
|
|
|
_field->setMimeDataHook([=](
|
|
|
|
not_null<const QMimeData*> data,
|
|
|
|
Ui::InputField::MimeAction action) {
|
|
|
|
if (action == Ui::InputField::MimeAction::Check) {
|
2019-04-02 10:38:53 +00:00
|
|
|
if (!data->hasText() && !_isAllowedEditMedia) {
|
|
|
|
return false;
|
2020-05-01 10:43:02 +00:00
|
|
|
} else if (data->hasImage()) {
|
|
|
|
return true;
|
|
|
|
} else if (const auto urls = data->urls(); !urls.empty()) {
|
2020-05-15 12:47:10 +00:00
|
|
|
if (ranges::all_of(urls, &QUrl::isLocalFile)) {
|
2019-04-01 19:09:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data->hasText();
|
|
|
|
} else if (action == Ui::InputField::MimeAction::Insert) {
|
|
|
|
return fileFromClipboard(data);
|
|
|
|
}
|
|
|
|
Unexpected("action in MimeData hook.");
|
|
|
|
});
|
2018-11-21 10:09:46 +00:00
|
|
|
Ui::Emoji::SuggestionsController::Init(
|
|
|
|
getDelegate()->outerContainer(),
|
2019-08-02 10:40:35 +00:00
|
|
|
_field,
|
|
|
|
&_controller->session());
|
2017-12-22 05:16:23 +00:00
|
|
|
|
2018-11-22 12:48:50 +00:00
|
|
|
setupEmojiPanel();
|
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
auto cursor = _field->textCursor();
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
_field->setTextCursor(cursor);
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:09:14 +00:00
|
|
|
bool EditCaptionBox::fileFromClipboard(not_null<const QMimeData*> data) {
|
2019-04-02 10:38:53 +00:00
|
|
|
if (!_isAllowedEditMedia) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-10 07:47:44 +00:00
|
|
|
using Error = Storage::PreparedList::Error;
|
2019-04-02 10:38:53 +00:00
|
|
|
|
2019-04-01 19:09:14 +00:00
|
|
|
auto list = [&] {
|
|
|
|
auto url = QList<QUrl>();
|
|
|
|
auto canAddUrl = false;
|
|
|
|
// When we edit media, we need only 1 file.
|
|
|
|
if (data->hasUrls()) {
|
|
|
|
const auto first = data->urls().front();
|
|
|
|
url.push_front(first);
|
|
|
|
canAddUrl = first.isLocalFile();
|
|
|
|
}
|
|
|
|
auto result = canAddUrl
|
|
|
|
? Storage::PrepareMediaList(url, st::sendMediaPreviewSize)
|
|
|
|
: Storage::PreparedList(
|
2019-04-10 07:47:44 +00:00
|
|
|
Error::EmptyFile,
|
2019-04-01 19:09:14 +00:00
|
|
|
QString());
|
2019-04-10 07:47:44 +00:00
|
|
|
if (result.error == Error::None) {
|
2019-04-01 19:09:14 +00:00
|
|
|
return result;
|
|
|
|
} else if (data->hasImage()) {
|
|
|
|
auto image = qvariant_cast<QImage>(data->imageData());
|
|
|
|
if (!image.isNull()) {
|
|
|
|
_isImage = true;
|
|
|
|
_photo = true;
|
|
|
|
return Storage::PrepareMediaFromImage(
|
|
|
|
std::move(image),
|
|
|
|
QByteArray(),
|
|
|
|
st::sendMediaPreviewSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}();
|
2019-04-10 07:47:44 +00:00
|
|
|
if (list.error != Error::None || list.files.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (list.files.front().type == Storage::PreparedFile::AlbumType::None
|
|
|
|
&& _isAlbum) {
|
|
|
|
Ui::show(
|
2019-06-19 15:09:03 +00:00
|
|
|
Box<InformBox>(tr::lng_edit_media_album_error(tr::now)),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2019-04-01 19:09:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-10 07:47:44 +00:00
|
|
|
|
|
|
|
_preparedList = std::move(list);
|
2019-04-01 19:09:14 +00:00
|
|
|
updateEditPreview();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
void EditCaptionBox::captionResized() {
|
|
|
|
updateBoxSize();
|
|
|
|
resizeEvent(0);
|
2018-11-22 12:48:50 +00:00
|
|
|
updateEmojiPanelGeometry();
|
2017-12-22 05:16:23 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-11-22 12:48:50 +00:00
|
|
|
void EditCaptionBox::setupEmojiPanel() {
|
|
|
|
const auto container = getDelegate()->outerContainer();
|
|
|
|
_emojiPanel = base::make_unique_q<ChatHelpers::TabbedPanel>(
|
|
|
|
container,
|
|
|
|
_controller,
|
|
|
|
object_ptr<ChatHelpers::TabbedSelector>(
|
|
|
|
nullptr,
|
|
|
|
_controller,
|
|
|
|
ChatHelpers::TabbedSelector::Mode::EmojiOnly));
|
|
|
|
_emojiPanel->setDesiredHeightValues(
|
|
|
|
1.,
|
|
|
|
st::emojiPanMinHeight / 2,
|
|
|
|
st::emojiPanMinHeight);
|
|
|
|
_emojiPanel->hide();
|
2019-08-16 12:44:20 +00:00
|
|
|
_emojiPanel->selector()->emojiChosen(
|
2018-11-22 12:48:50 +00:00
|
|
|
) | rpl::start_with_next([=](EmojiPtr emoji) {
|
|
|
|
Ui::InsertEmojiAtCursor(_field->textCursor(), emoji);
|
|
|
|
}, lifetime());
|
|
|
|
|
2019-09-05 10:51:36 +00:00
|
|
|
const auto filterCallback = [=](not_null<QEvent*> event) {
|
|
|
|
emojiFilterForGeometry(event);
|
2019-09-19 09:28:36 +00:00
|
|
|
return base::EventFilterResult::Continue;
|
2019-09-05 10:51:36 +00:00
|
|
|
};
|
2019-09-19 09:28:36 +00:00
|
|
|
_emojiFilter.reset(base::install_event_filter(container, filterCallback));
|
2018-11-22 12:48:50 +00:00
|
|
|
|
|
|
|
_emojiToggle.create(this, st::boxAttachEmoji);
|
|
|
|
_emojiToggle->installEventFilter(_emojiPanel);
|
|
|
|
_emojiToggle->addClickHandler([=] {
|
|
|
|
_emojiPanel->toggleAnimated();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
void EditCaptionBox::updateBoxSize() {
|
|
|
|
auto newHeight = st::boxPhotoPadding.top() + st::boxPhotoCaptionSkip + _field->height() + errorTopSkip() + st::normalFont->height;
|
2019-04-04 16:04:41 +00:00
|
|
|
if (_photo) {
|
2019-03-27 10:44:57 +00:00
|
|
|
newHeight += _wayWrap->height() / 2;
|
|
|
|
}
|
2017-12-22 05:16:23 +00:00
|
|
|
if (_photo || _animated) {
|
2019-03-21 12:17:18 +00:00
|
|
|
newHeight += std::max(_thumbh, _gifh);
|
2017-12-22 05:16:23 +00:00
|
|
|
} else if (_thumbw) {
|
|
|
|
newHeight += 0 + st::msgFileThumbSize + 0;
|
|
|
|
} else if (_doc) {
|
|
|
|
newHeight += 0 + st::msgFileSize + 0;
|
|
|
|
} else {
|
|
|
|
newHeight += st::boxTitleFont->height;
|
|
|
|
}
|
2019-03-30 19:56:17 +00:00
|
|
|
setDimensions(st::boxWideWidth, newHeight, true);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int EditCaptionBox::errorTopSkip() const {
|
2019-11-02 17:06:47 +00:00
|
|
|
return (st::defaultBox.buttonPadding.top() / 2);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::paintEvent(QPaintEvent *e) {
|
|
|
|
BoxContent::paintEvent(e);
|
|
|
|
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
if (_photo || _animated) {
|
2019-03-21 12:17:18 +00:00
|
|
|
const auto th = std::max(_gifh, _thumbh);
|
2017-12-22 05:16:23 +00:00
|
|
|
if (_thumbx > st::boxPhotoPadding.left()) {
|
2019-03-21 12:17:18 +00:00
|
|
|
p.fillRect(st::boxPhotoPadding.left(), st::boxPhotoPadding.top(), _thumbx - st::boxPhotoPadding.left(), th, st::confirmBg);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
if (_thumbx + _thumbw < width() - st::boxPhotoPadding.right()) {
|
2019-03-21 12:17:18 +00:00
|
|
|
p.fillRect(_thumbx + _thumbw, st::boxPhotoPadding.top(), width() - st::boxPhotoPadding.right() - _thumbx - _thumbw, th, st::confirmBg);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
if (_gifPreview && _gifPreview->started()) {
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto s = QSize(_gifw, _gifh);
|
|
|
|
const auto paused = _controller->isGifPausedAtLeastFor(Window::GifPauseReason::Layer);
|
|
|
|
const auto frame = _gifPreview->current(s.width(), s.height(), s.width(), s.height(), ImageRoundRadius::None, RectPart::None, paused ? 0 : crl::now());
|
2019-03-21 12:17:18 +00:00
|
|
|
p.drawPixmap(_gifx, st::boxPhotoPadding.top(), frame);
|
2017-12-22 05:16:23 +00:00
|
|
|
} else {
|
2019-03-21 12:17:18 +00:00
|
|
|
const auto offset = _gifh ? ((_gifh - _thumbh) / 2) : 0;
|
|
|
|
p.drawPixmap(_thumbx, st::boxPhotoPadding.top() + offset, _thumb);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
if (_animated && !_gifPreview) {
|
2019-03-21 12:17:18 +00:00
|
|
|
QRect inner(_thumbx + (_thumbw - st::msgFileSize) / 2, st::boxPhotoPadding.top() + (th - st::msgFileSize) / 2, st::msgFileSize, st::msgFileSize);
|
2017-12-22 05:16:23 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(st::msgDateImgBg);
|
|
|
|
|
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.drawEllipse(inner);
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto icon = &st::historyFileInPlay;
|
2017-12-22 05:16:23 +00:00
|
|
|
icon->paintInCenter(p, inner);
|
|
|
|
}
|
|
|
|
} else if (_doc) {
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto w = width() - st::boxPhotoPadding.left() - st::boxPhotoPadding.right();
|
|
|
|
const auto h = _thumbw ? (0 + st::msgFileThumbSize + 0) : (0 + st::msgFileSize + 0);
|
|
|
|
auto nameleft = 0, nametop = 0, nameright = 0, statustop = 0;
|
2017-12-22 05:16:23 +00:00
|
|
|
if (_thumbw) {
|
|
|
|
nameleft = 0 + st::msgFileThumbSize + st::msgFileThumbPadding.right();
|
|
|
|
nametop = st::msgFileThumbNameTop - st::msgFileThumbPadding.top();
|
|
|
|
nameright = 0;
|
|
|
|
statustop = st::msgFileThumbStatusTop - st::msgFileThumbPadding.top();
|
|
|
|
} else {
|
|
|
|
nameleft = 0 + st::msgFileSize + st::msgFilePadding.right();
|
|
|
|
nametop = st::msgFileNameTop - st::msgFilePadding.top();
|
|
|
|
nameright = 0;
|
|
|
|
statustop = st::msgFileStatusTop - st::msgFilePadding.top();
|
|
|
|
}
|
2019-04-06 07:09:55 +00:00
|
|
|
const auto editButton = _isAllowedEditMedia
|
|
|
|
? _editMedia->width() + st::editMediaButtonSkip
|
|
|
|
: 0;
|
2019-04-04 16:31:34 +00:00
|
|
|
const auto namewidth = w - nameleft - editButton;
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto x = (width() - w) / 2, y = st::boxPhotoPadding.top();
|
2017-12-22 05:16:23 +00:00
|
|
|
|
|
|
|
// App::roundRect(p, x, y, w, h, st::msgInBg, MessageInCorners, &st::msgInShadow);
|
|
|
|
|
|
|
|
if (_thumbw) {
|
2019-09-13 12:22:54 +00:00
|
|
|
QRect rthumb(style::rtlrect(x + 0, y + 0, st::msgFileThumbSize, st::msgFileThumbSize, width()));
|
2017-12-22 05:16:23 +00:00
|
|
|
p.drawPixmap(rthumb.topLeft(), _thumb);
|
|
|
|
} else {
|
2019-09-13 12:22:54 +00:00
|
|
|
const QRect inner(style::rtlrect(x + 0, y + 0, st::msgFileSize, st::msgFileSize, width()));
|
2017-12-22 05:16:23 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(st::msgFileInBg);
|
|
|
|
|
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.drawEllipse(inner);
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto icon = &(_isAudio ? st::historyFileInPlay : _isImage ? st::historyFileInImage : st::historyFileInDocument);
|
2017-12-22 05:16:23 +00:00
|
|
|
icon->paintInCenter(p, inner);
|
|
|
|
}
|
|
|
|
p.setFont(st::semiboldFont);
|
|
|
|
p.setPen(st::historyFileNameInFg);
|
|
|
|
_name.drawLeftElided(p, x + nameleft, y + nametop, namewidth, width());
|
|
|
|
|
2019-03-21 12:28:42 +00:00
|
|
|
const auto &status = st::mediaInFg;
|
2017-12-22 05:16:23 +00:00
|
|
|
p.setFont(st::normalFont);
|
|
|
|
p.setPen(status);
|
|
|
|
p.drawTextLeft(x + nameleft, y + statustop, width(), _status);
|
|
|
|
} else {
|
|
|
|
p.setFont(st::boxTitleFont);
|
|
|
|
p.setPen(st::boxTextFg);
|
2019-06-19 15:09:03 +00:00
|
|
|
p.drawTextLeft(_field->x(), st::boxPhotoPadding.top(), width(), tr::lng_edit_message(tr::now));
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_error.isEmpty()) {
|
|
|
|
p.setFont(st::normalFont);
|
|
|
|
p.setPen(st::boxTextFgError);
|
|
|
|
p.drawTextLeft(_field->x(), _field->y() + _field->height() + errorTopSkip(), width(), _error);
|
|
|
|
}
|
2019-04-04 16:31:34 +00:00
|
|
|
|
|
|
|
if (_isAllowedEditMedia) {
|
|
|
|
_editMedia->moveToRight(
|
|
|
|
st::boxPhotoPadding.right() + (_doc
|
|
|
|
? st::editMediaButtonFileSkipRight
|
|
|
|
: st::editMediaButtonSkip),
|
|
|
|
st::boxPhotoPadding.top() + (_doc
|
|
|
|
? st::editMediaButtonFileSkipTop
|
|
|
|
: st::editMediaButtonSkip));
|
|
|
|
}
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::resizeEvent(QResizeEvent *e) {
|
|
|
|
BoxContent::resizeEvent(e);
|
2019-03-27 10:44:57 +00:00
|
|
|
|
2019-04-04 16:04:41 +00:00
|
|
|
if (_photo) {
|
2019-03-27 10:44:57 +00:00
|
|
|
_wayWrap->resize(st::sendMediaPreviewSize, _wayWrap->height());
|
|
|
|
_wayWrap->moveToLeft(
|
|
|
|
st::boxPhotoPadding.left(),
|
|
|
|
st::boxPhotoPadding.top() + _thumbh);
|
|
|
|
}
|
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
_field->resize(st::sendMediaPreviewSize, _field->height());
|
|
|
|
_field->moveToLeft(st::boxPhotoPadding.left(), height() - st::normalFont->height - errorTopSkip() - _field->height());
|
2018-11-22 12:48:50 +00:00
|
|
|
_emojiToggle->moveToLeft(
|
|
|
|
(st::boxPhotoPadding.left()
|
|
|
|
+ st::sendMediaPreviewSize
|
|
|
|
- _emojiToggle->width()),
|
|
|
|
_field->y() + st::boxAttachEmojiTop);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::setInnerFocus() {
|
|
|
|
_field->setFocusFast();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::save() {
|
|
|
|
if (_saveRequestId) return;
|
|
|
|
|
2019-07-24 14:00:30 +00:00
|
|
|
const auto item = _controller->session().data().message(_msgId);
|
2017-12-22 05:16:23 +00:00
|
|
|
if (!item) {
|
2019-06-19 15:09:03 +00:00
|
|
|
_error = tr::lng_edit_deleted(tr::now);
|
2017-12-22 05:16:23 +00:00
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flags = MTPmessages_EditMessage::Flag::f_message | 0;
|
|
|
|
if (_previewCancelled) {
|
|
|
|
flags |= MTPmessages_EditMessage::Flag::f_no_webpage;
|
|
|
|
}
|
2018-05-31 18:28:37 +00:00
|
|
|
const auto textWithTags = _field->getTextWithAppliedMarkdown();
|
2018-05-24 13:03:21 +00:00
|
|
|
auto sending = TextWithEntities{
|
|
|
|
textWithTags.text,
|
2019-09-16 11:14:06 +00:00
|
|
|
TextUtilities::ConvertTextTagsToEntities(textWithTags.tags)
|
2018-05-24 13:03:21 +00:00
|
|
|
};
|
|
|
|
const auto prepareFlags = Ui::ItemTextOptions(
|
|
|
|
item->history(),
|
2019-07-24 14:00:30 +00:00
|
|
|
_controller->session().user()).flags;
|
2018-05-24 13:03:21 +00:00
|
|
|
TextUtilities::PrepareForSending(sending, prepareFlags);
|
|
|
|
TextUtilities::Trim(sending);
|
|
|
|
|
2019-09-16 11:14:06 +00:00
|
|
|
const auto sentEntities = Api::EntitiesToMTP(
|
2018-05-24 13:03:21 +00:00
|
|
|
sending.entities,
|
2019-09-16 11:14:06 +00:00
|
|
|
Api::ConvertOption::SkipLocal);
|
2017-12-22 05:16:23 +00:00
|
|
|
if (!sentEntities.v.isEmpty()) {
|
|
|
|
flags |= MTPmessages_EditMessage::Flag::f_entities;
|
|
|
|
}
|
2019-03-24 12:39:19 +00:00
|
|
|
|
2019-04-01 19:09:14 +00:00
|
|
|
if (!_preparedList.files.empty()) {
|
2019-03-24 12:39:19 +00:00
|
|
|
const auto textWithTags = _field->getTextWithAppliedMarkdown();
|
|
|
|
auto sending = TextWithEntities{
|
|
|
|
textWithTags.text,
|
2019-09-16 11:14:06 +00:00
|
|
|
TextUtilities::ConvertTextTagsToEntities(textWithTags.tags)
|
2019-03-24 12:39:19 +00:00
|
|
|
};
|
|
|
|
item->setText(sending);
|
|
|
|
|
2019-07-24 14:00:30 +00:00
|
|
|
_controller->session().api().editMedia(
|
2019-03-26 21:14:51 +00:00
|
|
|
std::move(_preparedList),
|
2019-04-04 16:04:41 +00:00
|
|
|
(!_asFile && _photo) ? SendMediaType::Photo : SendMediaType::File,
|
2019-03-24 12:39:19 +00:00
|
|
|
_field->getTextWithAppliedMarkdown(),
|
2019-08-12 12:11:34 +00:00
|
|
|
Api::SendAction(item->history()),
|
2019-04-02 10:38:53 +00:00
|
|
|
item->fullId().msg);
|
2019-03-24 12:39:19 +00:00
|
|
|
closeBox();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
_saveRequestId = MTP::send(
|
|
|
|
MTPmessages_EditMessage(
|
|
|
|
MTP_flags(flags),
|
|
|
|
item->history()->peer->input,
|
|
|
|
MTP_int(item->id),
|
2018-05-24 13:03:21 +00:00
|
|
|
MTP_string(sending.text),
|
2018-04-09 17:48:29 +00:00
|
|
|
MTPInputMedia(),
|
2019-03-15 15:15:56 +00:00
|
|
|
MTPReplyMarkup(),
|
2019-08-08 10:15:59 +00:00
|
|
|
sentEntities,
|
|
|
|
MTP_int(0)), // schedule_date
|
2017-12-22 05:16:23 +00:00
|
|
|
rpcDone(&EditCaptionBox::saveDone),
|
|
|
|
rpcFail(&EditCaptionBox::saveFail));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::saveDone(const MTPUpdates &updates) {
|
|
|
|
_saveRequestId = 0;
|
2019-07-24 14:00:30 +00:00
|
|
|
const auto controller = _controller;
|
2017-12-22 05:16:23 +00:00
|
|
|
closeBox();
|
2019-07-24 14:00:30 +00:00
|
|
|
controller->session().api().applyUpdates(updates);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditCaptionBox::saveFail(const RPCError &error) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
|
|
|
|
|
|
|
_saveRequestId = 0;
|
2019-07-24 14:00:30 +00:00
|
|
|
const auto &type = error.type();
|
|
|
|
if (type == qstr("MESSAGE_ID_INVALID")
|
|
|
|
|| type == qstr("CHAT_ADMIN_REQUIRED")
|
|
|
|
|| type == qstr("MESSAGE_EDIT_TIME_EXPIRED")) {
|
2019-06-19 15:09:03 +00:00
|
|
|
_error = tr::lng_edit_error(tr::now);
|
2019-07-24 14:00:30 +00:00
|
|
|
} else if (type == qstr("MESSAGE_NOT_MODIFIED")) {
|
2017-12-22 05:16:23 +00:00
|
|
|
closeBox();
|
|
|
|
return true;
|
2019-07-24 14:00:30 +00:00
|
|
|
} else if (type == qstr("MESSAGE_EMPTY")) {
|
2017-12-22 05:16:23 +00:00
|
|
|
_field->setFocus();
|
|
|
|
_field->showError();
|
|
|
|
} else {
|
2019-06-19 15:09:03 +00:00
|
|
|
_error = tr::lng_edit_error(tr::now);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
update();
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-26 21:14:51 +00:00
|
|
|
|
|
|
|
void EditCaptionBox::setName(QString nameString, qint64 size) {
|
|
|
|
_name.setText(
|
|
|
|
st::semiboldTextStyle,
|
|
|
|
nameString,
|
|
|
|
Ui::NameTextOptions());
|
|
|
|
_status = formatSizeText(size);
|
|
|
|
}
|
2019-03-30 08:53:28 +00:00
|
|
|
|
|
|
|
void EditCaptionBox::keyPressEvent(QKeyEvent *e) {
|
|
|
|
if ((e->key() == Qt::Key_E || e->key() == Qt::Key_O)
|
|
|
|
&& e->modifiers() == Qt::ControlModifier) {
|
|
|
|
_editMediaClicks.fire({});
|
|
|
|
} else {
|
|
|
|
e->ignore();
|
|
|
|
}
|
|
|
|
}
|