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"
|
|
|
|
|
|
|
|
#include "ui/widgets/input_fields.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2017-12-28 13:06:06 +00:00
|
|
|
#include "ui/text_options.h"
|
2018-11-22 12:48:50 +00:00
|
|
|
#include "ui/special_buttons.h"
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/clip/media_clip_reader.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
2018-01-14 16:02:25 +00:00
|
|
|
#include "data/data_media_types.h"
|
|
|
|
#include "data/data_photo.h"
|
|
|
|
#include "data/data_document.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
2017-12-22 05:16:23 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2018-11-22 12:48:50 +00:00
|
|
|
#include "core/event_filter.h"
|
2018-05-24 13:03:21 +00:00
|
|
|
#include "chat_helpers/message_field.h"
|
2018-11-22 12:48:50 +00:00
|
|
|
#include "chat_helpers/tabbed_panel.h"
|
|
|
|
#include "chat_helpers/tabbed_selector.h"
|
2018-11-21 10:09:46 +00:00
|
|
|
#include "chat_helpers/emoji_suggestions_widget.h"
|
2017-12-22 05:16:23 +00:00
|
|
|
#include "window/window_controller.h"
|
2018-01-11 19:33:26 +00:00
|
|
|
#include "layout.h"
|
2018-08-02 14:31:54 +00:00
|
|
|
#include "auth_session.h"
|
2018-11-26 11:55:02 +00:00
|
|
|
#include "apiwrap.h"
|
2017-12-22 05:16:23 +00:00
|
|
|
#include "styles/style_history.h"
|
|
|
|
#include "styles/style_boxes.h"
|
2018-11-22 12:48:50 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
2017-12-22 05:16:23 +00:00
|
|
|
|
|
|
|
EditCaptionBox::EditCaptionBox(
|
|
|
|
QWidget*,
|
2018-05-26 14:58:21 +00:00
|
|
|
not_null<Window::Controller*> 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());
|
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()) {
|
|
|
|
dimensions = document->dimensions;
|
2019-01-25 14:37:28 +00:00
|
|
|
image = document->thumbnail();
|
2018-01-14 16:02:25 +00:00
|
|
|
if (document->isAnimation()) {
|
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
|
|
|
}
|
2018-05-24 13:03:21 +00:00
|
|
|
const auto original = item->originalText();
|
|
|
|
const auto editData = TextWithTags {
|
|
|
|
original.text,
|
|
|
|
ConvertEntitiesToTextTags(original.entities)
|
|
|
|
};
|
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 {
|
|
|
|
int32 tw = image->width(), th = image->height();
|
|
|
|
if (tw > th) {
|
|
|
|
_thumbw = (tw * st::msgFileThumbSize) / th;
|
|
|
|
} else {
|
|
|
|
_thumbw = st::msgFileThumbSize;
|
|
|
|
}
|
2018-08-02 14:31:54 +00:00
|
|
|
_thumbnailImage = image;
|
|
|
|
_refreshThumbnail = [=] {
|
|
|
|
auto options = Images::Option::Smooth
|
|
|
|
| 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) {
|
|
|
|
auto nameString = doc->isVoiceMessage()
|
|
|
|
? lang(lng_media_audio)
|
|
|
|
: doc->composeNameString();
|
|
|
|
_name.setText(
|
|
|
|
st::semiboldTextStyle,
|
|
|
|
nameString,
|
2017-12-28 13:06:06 +00:00
|
|
|
Ui::NameTextOptions());
|
2017-12-22 05:16:23 +00:00
|
|
|
_status = formatSizeText(doc->size);
|
|
|
|
_statusw = qMax(
|
|
|
|
_name.maxWidth(),
|
|
|
|
st::normalFont->width(_status));
|
|
|
|
_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 {
|
|
|
|
int32 maxW = 0, maxH = 0;
|
|
|
|
if (_animated) {
|
|
|
|
int32 limitW = st::sendMediaPreviewSize;
|
|
|
|
int32 limitH = st::confirmMaxHeight;
|
|
|
|
maxW = qMax(dimensions.width(), 1);
|
|
|
|
maxH = qMax(dimensions.height(), 1);
|
|
|
|
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);
|
|
|
|
};
|
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();
|
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
int32 tw = _thumb.width(), th = _thumb.height();
|
|
|
|
if (!tw || !th) {
|
|
|
|
tw = th = 1;
|
|
|
|
}
|
|
|
|
_thumbw = st::sendMediaPreviewSize;
|
|
|
|
if (_thumb.width() < _thumbw) {
|
|
|
|
_thumbw = (_thumb.width() > 20) ? _thumb.width() : 20;
|
|
|
|
}
|
|
|
|
int32 maxthumbh = qMin(qRound(1.5 * _thumbw), int(st::confirmMaxHeight));
|
|
|
|
_thumbh = qRound(th * float64(_thumbw) / tw);
|
|
|
|
if (_thumbh > maxthumbh) {
|
|
|
|
_thumbw = qRound(_thumbw * float64(maxthumbh) / _thumbh);
|
|
|
|
_thumbh = maxthumbh;
|
|
|
|
if (_thumbw < 10) {
|
|
|
|
_thumbw = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_thumbx = (st::boxWideWidth - _thumbw) / 2;
|
|
|
|
|
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(),
|
|
|
|
Qt::IgnoreAspectRatio,
|
|
|
|
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;
|
|
|
|
subscribe(Auth().downloaderTaskFinished(), [=] {
|
|
|
|
if (!_thumbnailImageLoaded && _thumbnailImage->loaded()) {
|
|
|
|
_thumbnailImageLoaded = true;
|
|
|
|
_refreshThumbnail();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
if (doc && doc->isAnimation() && doc->loaded() && !_gifPreview) {
|
|
|
|
prepareGifPreview(doc);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-20 17:42:30 +00:00
|
|
|
_field.create(
|
|
|
|
this,
|
|
|
|
st::confirmCaptionArea,
|
|
|
|
Ui::InputField::Mode::MultiLine,
|
|
|
|
langFactory(lng_photo_caption),
|
2018-05-24 13:03:21 +00:00
|
|
|
editData);
|
2018-10-17 11:38:07 +00:00
|
|
|
_field->setMaxLength(Global::CaptionLengthMax());
|
2018-05-21 21:31:46 +00:00
|
|
|
_field->setSubmitSettings(Ui::InputField::SubmitSettings::Both);
|
2018-05-20 17:42:30 +00:00
|
|
|
_field->setInstantReplaces(Ui::InstantReplaces::Default());
|
2018-05-24 13:03:21 +00:00
|
|
|
_field->setInstantReplacesEnabled(Global::ReplaceEmojiValue());
|
2018-05-25 17:47:29 +00:00
|
|
|
_field->setMarkdownReplacesEnabled(rpl::single(true));
|
2018-11-04 07:32:26 +00:00
|
|
|
_field->setEditLinkCallback(DefaultEditLinkCallback(_field));
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-22 12:48:50 +00:00
|
|
|
bool EditCaptionBox::emojiFilter(not_null<QEvent*> event) {
|
|
|
|
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(); });
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:31:54 +00:00
|
|
|
void EditCaptionBox::prepareGifPreview(not_null<DocumentData*> document) {
|
|
|
|
if (_gifPreview) {
|
|
|
|
return;
|
|
|
|
} else if (document->isAnimation() && document->loaded()) {
|
2017-12-22 05:16:23 +00:00
|
|
|
_gifPreview = Media::Clip::MakeReader(document, _msgId, [this](Media::Clip::Notification notification) {
|
|
|
|
clipCallback(notification);
|
|
|
|
});
|
|
|
|
if (_gifPreview) _gifPreview->setAutoplay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
|
|
|
auto s = QSize(_thumbw, _thumbh);
|
|
|
|
_gifPreview->start(s.width(), s.height(), s.width(), s.height(), ImageRoundRadius::None, RectPart::None);
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NotificationRepaint: {
|
|
|
|
if (_gifPreview && !_gifPreview->currentDisplayed()) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::prepare() {
|
|
|
|
addButton(langFactory(lng_settings_save), [this] { save(); });
|
|
|
|
addButton(langFactory(lng_cancel), [this] { closeBox(); });
|
|
|
|
|
|
|
|
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(); });
|
2018-11-21 10:09:46 +00:00
|
|
|
Ui::Emoji::SuggestionsController::Init(
|
|
|
|
getDelegate()->outerContainer(),
|
|
|
|
_field);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
_emojiPanel->getSelector()->emojiChosen(
|
|
|
|
) | rpl::start_with_next([=](EmojiPtr emoji) {
|
|
|
|
Ui::InsertEmojiAtCursor(_field->textCursor(), emoji);
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
_emojiFilter.reset(Core::InstallEventFilter(
|
|
|
|
container,
|
|
|
|
[=](not_null<QEvent*> event) { return emojiFilter(event); }));
|
|
|
|
|
|
|
|
_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;
|
|
|
|
if (_photo || _animated) {
|
|
|
|
newHeight += _thumbh;
|
|
|
|
} else if (_thumbw) {
|
|
|
|
newHeight += 0 + st::msgFileThumbSize + 0;
|
|
|
|
} else if (_doc) {
|
|
|
|
newHeight += 0 + st::msgFileSize + 0;
|
|
|
|
} else {
|
|
|
|
newHeight += st::boxTitleFont->height;
|
|
|
|
}
|
|
|
|
setDimensions(st::boxWideWidth, newHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EditCaptionBox::errorTopSkip() const {
|
|
|
|
return (st::boxButtonPadding.top() / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::paintEvent(QPaintEvent *e) {
|
|
|
|
BoxContent::paintEvent(e);
|
|
|
|
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
if (_photo || _animated) {
|
|
|
|
if (_thumbx > st::boxPhotoPadding.left()) {
|
|
|
|
p.fillRect(st::boxPhotoPadding.left(), st::boxPhotoPadding.top(), _thumbx - st::boxPhotoPadding.left(), _thumbh, st::confirmBg);
|
|
|
|
}
|
|
|
|
if (_thumbx + _thumbw < width() - st::boxPhotoPadding.right()) {
|
|
|
|
p.fillRect(_thumbx + _thumbw, st::boxPhotoPadding.top(), width() - st::boxPhotoPadding.right() - _thumbx - _thumbw, _thumbh, st::confirmBg);
|
|
|
|
}
|
|
|
|
if (_gifPreview && _gifPreview->started()) {
|
|
|
|
auto s = QSize(_thumbw, _thumbh);
|
2018-04-06 16:23:09 +00:00
|
|
|
auto paused = _controller->isGifPausedAtLeastFor(Window::GifPauseReason::Layer);
|
2017-12-22 05:16:23 +00:00
|
|
|
auto frame = _gifPreview->current(s.width(), s.height(), s.width(), s.height(), ImageRoundRadius::None, RectPart::None, paused ? 0 : getms());
|
|
|
|
p.drawPixmap(_thumbx, st::boxPhotoPadding.top(), frame);
|
|
|
|
} else {
|
|
|
|
p.drawPixmap(_thumbx, st::boxPhotoPadding.top(), _thumb);
|
|
|
|
}
|
|
|
|
if (_animated && !_gifPreview) {
|
|
|
|
QRect inner(_thumbx + (_thumbw - st::msgFileSize) / 2, st::boxPhotoPadding.top() + (_thumbh - st::msgFileSize) / 2, st::msgFileSize, st::msgFileSize);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(st::msgDateImgBg);
|
|
|
|
|
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.drawEllipse(inner);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto icon = &st::historyFileInPlay;
|
|
|
|
icon->paintInCenter(p, inner);
|
|
|
|
}
|
|
|
|
} else if (_doc) {
|
|
|
|
int32 w = width() - st::boxPhotoPadding.left() - st::boxPhotoPadding.right();
|
|
|
|
int32 h = _thumbw ? (0 + st::msgFileThumbSize + 0) : (0 + st::msgFileSize + 0);
|
|
|
|
int32 nameleft = 0, nametop = 0, nameright = 0, statustop = 0;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
int32 namewidth = w - nameleft - 0;
|
|
|
|
if (namewidth > _statusw) {
|
|
|
|
//w -= (namewidth - _statusw);
|
|
|
|
//namewidth = _statusw;
|
|
|
|
}
|
|
|
|
int32 x = (width() - w) / 2, y = st::boxPhotoPadding.top();
|
|
|
|
|
|
|
|
// App::roundRect(p, x, y, w, h, st::msgInBg, MessageInCorners, &st::msgInShadow);
|
|
|
|
|
|
|
|
if (_thumbw) {
|
|
|
|
QRect rthumb(rtlrect(x + 0, y + 0, st::msgFileThumbSize, st::msgFileThumbSize, width()));
|
|
|
|
p.drawPixmap(rthumb.topLeft(), _thumb);
|
|
|
|
} else {
|
|
|
|
QRect inner(rtlrect(x + 0, y + 0, st::msgFileSize, st::msgFileSize, width()));
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(st::msgFileInBg);
|
|
|
|
|
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.drawEllipse(inner);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto icon = &(_isAudio ? st::historyFileInPlay : _isImage ? st::historyFileInImage : st::historyFileInDocument);
|
|
|
|
icon->paintInCenter(p, inner);
|
|
|
|
}
|
|
|
|
p.setFont(st::semiboldFont);
|
|
|
|
p.setPen(st::historyFileNameInFg);
|
|
|
|
_name.drawLeftElided(p, x + nameleft, y + nametop, namewidth, width());
|
|
|
|
|
|
|
|
auto &status = st::mediaInFg;
|
|
|
|
p.setFont(st::normalFont);
|
|
|
|
p.setPen(status);
|
|
|
|
p.drawTextLeft(x + nameleft, y + statustop, width(), _status);
|
|
|
|
} else {
|
|
|
|
p.setFont(st::boxTitleFont);
|
|
|
|
p.setPen(st::boxTextFg);
|
|
|
|
p.drawTextLeft(_field->x(), st::boxPhotoPadding.top(), width(), lang(lng_edit_message));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_error.isEmpty()) {
|
|
|
|
p.setFont(st::normalFont);
|
|
|
|
p.setPen(st::boxTextFgError);
|
|
|
|
p.drawTextLeft(_field->x(), _field->y() + _field->height() + errorTopSkip(), width(), _error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::resizeEvent(QResizeEvent *e) {
|
|
|
|
BoxContent::resizeEvent(e);
|
|
|
|
_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;
|
|
|
|
|
|
|
|
auto item = App::histItemById(_msgId);
|
|
|
|
if (!item) {
|
|
|
|
_error = lang(lng_edit_deleted);
|
|
|
|
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,
|
|
|
|
ConvertTextTagsToEntities(textWithTags.tags)
|
|
|
|
};
|
|
|
|
const auto prepareFlags = Ui::ItemTextOptions(
|
|
|
|
item->history(),
|
2018-09-11 12:50:40 +00:00
|
|
|
Auth().user()).flags;
|
2018-05-24 13:03:21 +00:00
|
|
|
TextUtilities::PrepareForSending(sending, prepareFlags);
|
|
|
|
TextUtilities::Trim(sending);
|
|
|
|
|
|
|
|
const auto sentEntities = TextUtilities::EntitiesToMTP(
|
|
|
|
sending.entities,
|
|
|
|
TextUtilities::ConvertOption::SkipLocal);
|
2017-12-22 05:16:23 +00:00
|
|
|
if (!sentEntities.v.isEmpty()) {
|
|
|
|
flags |= MTPmessages_EditMessage::Flag::f_entities;
|
|
|
|
}
|
|
|
|
_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(),
|
2017-12-22 05:16:23 +00:00
|
|
|
MTPnullMarkup,
|
2018-12-17 07:43:47 +00:00
|
|
|
sentEntities),
|
2017-12-22 05:16:23 +00:00
|
|
|
rpcDone(&EditCaptionBox::saveDone),
|
|
|
|
rpcFail(&EditCaptionBox::saveFail));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::saveDone(const MTPUpdates &updates) {
|
|
|
|
_saveRequestId = 0;
|
|
|
|
closeBox();
|
2018-11-26 11:55:02 +00:00
|
|
|
Auth().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;
|
|
|
|
QString err = error.type();
|
|
|
|
if (err == qstr("MESSAGE_ID_INVALID") || err == qstr("CHAT_ADMIN_REQUIRED") || err == qstr("MESSAGE_EDIT_TIME_EXPIRED")) {
|
|
|
|
_error = lang(lng_edit_error);
|
|
|
|
} else if (err == qstr("MESSAGE_NOT_MODIFIED")) {
|
|
|
|
closeBox();
|
|
|
|
return true;
|
|
|
|
} else if (err == qstr("MESSAGE_EMPTY")) {
|
|
|
|
_field->setFocus();
|
|
|
|
_field->showError();
|
|
|
|
} else {
|
|
|
|
_error = lang(lng_edit_error);
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
return true;
|
|
|
|
}
|