Added allowsEditMedia() to Media. Slightly refactored.

This commit is contained in:
23rd 2019-03-29 13:51:19 +03:00 committed by John Preston
parent c84f99cf3a
commit 25e3674819
5 changed files with 107 additions and 72 deletions

View File

@ -45,6 +45,7 @@ EditCaptionBox::EditCaptionBox(
, _msgId(item->fullId()) {
Expects(item->media() != nullptr);
Expects(item->media()->allowsEditCaption());
_isAllowedEditMedia = item->media()->allowsEditMedia();
QSize dimensions;
auto image = (Image*)nullptr;
@ -331,80 +332,78 @@ void EditCaptionBox::clipCallback(Media::Clip::Notification notification) {
}
}
void EditCaptionBox::prepare() {
addButton(langFactory(lng_settings_save), [this] { save(); });
addButton(langFactory(lng_edit_media), [this] {
const auto callback = [=](const FileDialog::OpenResult &result) {
if (result.paths.isEmpty() && result.remoteContent.isEmpty()) {
return;
void EditCaptionBox::createEditMediaButton() {
const auto callback = [=](const FileDialog::OpenResult &result) {
if (result.paths.isEmpty() && result.remoteContent.isEmpty()) {
return;
}
if (!result.paths.isEmpty()) {
const auto filePath = result.paths.front();
_newMediaPath = filePath;
_preparedList = Storage::PrepareMediaList(
QStringList(_newMediaPath),
st::sendMediaPreviewSize);
const auto file = &_preparedList.files.front();
const auto fileMedia = &file->information->media;
const auto fileinfo = QFileInfo(_newMediaPath);
const auto filename = fileinfo.fileName();
_isImage = fileIsImage(filename, Core::MimeTypeForFile(fileinfo).name());
_isAudio = false;
_animated = false;
_photo = false;
_doc = false;
_gifPreview = nullptr;
_thumbw = _thumbh = _thumbx = 0;
_gifw = _gifh = _gifx = 0;
auto isGif = false;
_wayWrap->toggle(_isImage, anim::type::instant);
using Info = FileMediaInformation;
if (const auto image = base::get_if<Info::Image>(fileMedia)
&& _isImage) {
_photo = true;
} else if (const auto video =
base::get_if<Info::Video>(fileMedia)) {
_animated = true;
isGif = video->isGifv;
} else {
auto nameString = filename;
if (const auto song =
base::get_if<Info::Song>(fileMedia)) {
nameString = DocumentData::ComposeNameString(
filename,
song->title,
song->performer);
_isAudio = true;
}
setName(nameString, fileinfo.size());
_doc = true;
}
if (!result.paths.isEmpty()) {
const auto filePath = result.paths.front();
LOG(("FILE PATH: %1").arg(filePath));
_newMediaPath = filePath;
_preparedList = Storage::PrepareMediaList(
QStringList(_newMediaPath),
st::sendMediaPreviewSize);
const auto file = &_preparedList.files.front();
const auto fileMedia = &file->information->media;
const auto fileinfo = QFileInfo(_newMediaPath);
const auto filename = fileinfo.fileName();
_isImage = fileIsImage(filename, Core::MimeTypeForFile(fileinfo).name());
_isAudio = false;
_animated = false;
_photo = false;
_doc = false;
_gifPreview = nullptr;
_thumbw = _thumbh = _thumbx = 0;
_gifw = _gifh = _gifx = 0;
auto isGif = false;
_wayWrap->toggle(_isImage, anim::type::instant);
using Info = FileMediaInformation;
if (const auto image = base::get_if<Info::Image>(fileMedia)
&& _isImage) {
_photo = true;
} else if (const auto video =
base::get_if<Info::Video>(fileMedia)) {
_animated = true;
isGif = video->isGifv;
} else {
auto nameString = filename;
if (const auto song =
base::get_if<Info::Song>(fileMedia)) {
nameString = DocumentData::ComposeNameString(
filename,
song->title,
song->performer);
_isAudio = true;
}
setName(nameString, fileinfo.size());
_doc = true;
if (!_doc) {
_thumb = App::pixmapFromImageInPlace(
file->preview.scaled(st::sendMediaPreviewSize,
st::confirmMaxHeight,
Qt::KeepAspectRatio));
_thumbw = _thumb.width();
_thumbh = _thumb.height();
_thumbx = (st::boxWideWidth - _thumbw) / 2;
if (isGif) {
_gifw = _thumbw;
_gifh = _thumbh;
_gifx = _thumbx;
prepareGifPreview();
}
if (!_doc) {
_thumb = App::pixmapFromImageInPlace(
file->preview.scaled(st::sendMediaPreviewSize,
st::confirmMaxHeight,
Qt::KeepAspectRatio));
_thumbw = _thumb.width();
_thumbh = _thumb.height();
_thumbx = (st::boxWideWidth - _thumbw) / 2;
if (isGif) {
_gifw = _thumbw;
_gifh = _thumbh;
_gifx = _thumbx;
prepareGifPreview();
}
}
captionResized();
}
};
captionResized();
}
};
addButton(langFactory(lng_edit_media), [=] {
const auto filters = QStringList(FileDialog::AllFilesFilter());
FileDialog::GetOpenPath(
this,
@ -412,6 +411,15 @@ void EditCaptionBox::prepare() {
filters.join(qsl(";;")),
crl::guard(this, callback));
});
}
void EditCaptionBox::prepare() {
addButton(langFactory(lng_settings_save), [this] { save(); });
if (_isAllowedEditMedia) {
createEditMediaButton();
} else {
_newMediaPath = QString();
}
addButton(langFactory(lng_cancel), [this] { closeBox(); });
updateBoxSize();

View File

@ -66,6 +66,8 @@ private:
int errorTopSkip() const;
void createEditMediaButton();
not_null<Window::Controller*> _controller;
FullMsgId _msgId;
Image *_thumbnailImage = nullptr;
@ -104,6 +106,7 @@ private:
bool _asFile = false;
Ui::SlideWrap<Ui::RpWidget> *_wayWrap = nullptr;
QString _newMediaPath;
bool _isAllowedEditMedia = false;
QString _error;

View File

@ -214,6 +214,10 @@ bool Media::allowsEditCaption() const {
return false;
}
bool Media::allowsEditMedia() const {
return false;
}
bool Media::allowsRevoke() const {
return true;
}
@ -323,6 +327,10 @@ bool MediaPhoto::allowsEditCaption() const {
return true;
}
bool MediaPhoto::allowsEditMedia() const {
return true;
}
QString MediaPhoto::errorTextForForward(not_null<PeerData*> peer) const {
const auto errorKey = Data::RestrictionErrorKey(
peer,
@ -648,6 +656,13 @@ bool MediaFile::allowsEditCaption() const {
return !_document->isVideoMessage() && !_document->sticker();
}
bool MediaFile::allowsEditMedia() const {
return !_document->isVideoMessage()
&& !_document->sticker()
&& !_document->isGifv()
&& !_document->isVoiceMessage();
}
bool MediaFile::forwardedBecomesUnread() const {
return _document->isVoiceMessage()
//|| _document->isVideoFile()

View File

@ -93,6 +93,7 @@ public:
virtual bool allowsForward() const;
virtual bool allowsEdit() const;
virtual bool allowsEditCaption() const;
virtual bool allowsEditMedia() const;
virtual bool allowsRevoke() const;
virtual bool forwardedBecomesUnread() const;
virtual QString errorTextForForward(not_null<PeerData*> peer) const;
@ -141,6 +142,7 @@ public:
QString pinnedTextSubstring() const override;
TextWithEntities clipboardText() const override;
bool allowsEditCaption() const override;
bool allowsEditMedia() const override;
QString errorTextForForward(not_null<PeerData*> peer) const override;
bool updateInlineResultMedia(const MTPMessageMedia &media) override;
@ -176,6 +178,7 @@ public:
QString pinnedTextSubstring() const override;
TextWithEntities clipboardText() const override;
bool allowsEditCaption() const override;
bool allowsEditMedia() const override;
bool forwardedBecomesUnread() const override;
QString errorTextForForward(not_null<PeerData*> peer) const override;

View File

@ -313,6 +313,8 @@ void Uploader::sendNext() {
if (requestsSent.empty() && docRequestsSent.empty()) {
const auto silent = uploadingData.file
&& uploadingData.file->to.silent;
const auto edit = uploadingData.file &&
uploadingData.file->edit;
if (uploadingData.type() == SendMediaType::Photo) {
auto photoFilename = uploadingData.filename();
if (!photoFilename.endsWith(qstr(".jpg"), Qt::CaseInsensitive)) {
@ -329,7 +331,7 @@ void Uploader::sendNext() {
MTP_int(uploadingData.partsCount),
MTP_string(photoFilename),
MTP_bytes(md5));
_photoReady.fire({ uploadingId, silent, file, uploadingData.file->edit });
_photoReady.fire({ uploadingId, silent, file, edit });
} else if (uploadingData.type() == SendMediaType::File
|| uploadingData.type() == SendMediaType::WallPaper
|| uploadingData.type() == SendMediaType::Audio) {
@ -363,9 +365,13 @@ void Uploader::sendNext() {
silent,
file,
thumb,
uploadingData.file->edit });
edit });
} else {
_documentReady.fire({ uploadingId, silent, file, uploadingData.file->edit });
_documentReady.fire({
uploadingId,
silent,
file,
edit });
}
} else if (uploadingData.type() == SendMediaType::Secure) {
_secureReady.fire({