improved media caption edit in box, need to make post edit in message field

This commit is contained in:
John Preston 2016-02-23 17:31:06 +03:00
parent 0ffc2ce141
commit 9c8ae7f32b
7 changed files with 157 additions and 64 deletions

View File

@ -124,6 +124,9 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
"lng_flood_error" = "Too many tries. Please try again later."; "lng_flood_error" = "Too many tries. Please try again later.";
"lng_gif_error" = "An error has occured while reading GIF animation :("; "lng_gif_error" = "An error has occured while reading GIF animation :(";
"lng_edit_error" = "You cannot edit this message"; "lng_edit_error" = "You cannot edit this message";
"lng_edit_deleted" = "This message was deleted";
"lng_edit_too_long" = "Your message text is too long";
"lng_edit_message" = "Edit message";
"lng_deleted" = "Unknown"; "lng_deleted" = "Unknown";
"lng_deleted_message" = "Deleted message"; "lng_deleted_message" = "Deleted message";

View File

@ -178,6 +178,14 @@ void AbstractBox::resizeMaxHeight(int32 newWidth, int32 maxHeight) {
_maxHeight = maxHeight; _maxHeight = maxHeight;
resize(newWidth, countHeight()); resize(newWidth, countHeight());
if (parentWidget()) { if (parentWidget()) {
QRect r = geometry();
int32 parenth = parentWidget()->height();
if (r.top() + r.height() + st::boxVerticalMargin > parenth) {
int32 newTop = qMax(parenth - int(st::boxVerticalMargin) - r.height(), (parenth - r.height()) / 2);
if (newTop != r.top()) {
move(r.left(), newTop);
}
}
parentWidget()->update(geometry().united(g).marginsAdded(QMargins(st::boxShadow.pxWidth(), st::boxShadow.pxHeight(), st::boxShadow.pxWidth(), st::boxShadow.pxHeight()))); parentWidget()->update(geometry().united(g).marginsAdded(QMargins(st::boxShadow.pxWidth(), st::boxShadow.pxHeight(), st::boxShadow.pxWidth(), st::boxShadow.pxHeight())));
} }
} }

View File

@ -360,12 +360,12 @@ void PhotoSendBox::onSend(bool ctrlShiftEnter) {
onClose(); onClose();
} }
EditPostBox::EditPostBox(HistoryItem *msg) : AbstractBox(st::boxWideWidth) EditCaptionBox::EditCaptionBox(HistoryItem *msg) : AbstractBox(st::boxWideWidth)
, _msg(msg) , _msgId(msg->fullId())
, _animated(false) , _animated(false)
, _photo(false) , _photo(false)
, _doc(false) , _doc(false)
, _text(0) , _field(0)
, _save(this, lang(lng_settings_save), st::defaultBoxButton) , _save(this, lang(lng_settings_save), st::defaultBoxButton)
, _cancel(this, lang(lng_cancel), st::cancelBoxButton) , _cancel(this, lang(lng_cancel), st::cancelBoxButton)
, _thumbx(0) , _thumbx(0)
@ -383,7 +383,7 @@ EditPostBox::EditPostBox(HistoryItem *msg) : AbstractBox(st::boxWideWidth)
ImagePtr image; ImagePtr image;
QString caption; QString caption;
DocumentData *doc = 0; DocumentData *doc = 0;
if (HistoryMedia *media = _msg->getMedia()) { if (HistoryMedia *media = msg->getMedia()) {
HistoryMediaType t = media->type(); HistoryMediaType t = media->type();
switch (t) { switch (t) {
case MediaTypeGif: { case MediaTypeGif: {
@ -489,45 +489,51 @@ EditPostBox::EditPostBox(HistoryItem *msg) : AbstractBox(st::boxWideWidth)
} }
if (_animated || _photo || _doc) { if (_animated || _photo || _doc) {
_text = new InputArea(this, st::confirmCaptionArea, lang(lng_photo_caption), caption); _field = new InputArea(this, st::confirmCaptionArea, lang(lng_photo_caption), caption);
_text->setMaxLength(MaxPhotoCaption); _field->setMaxLength(MaxPhotoCaption);
_text->setCtrlEnterSubmit(CtrlEnterSubmitBoth); _field->setCtrlEnterSubmit(CtrlEnterSubmitBoth);
} else { } else {
_text = new InputArea(this, st::editTextArea, lang(lng_edit_placeholder), msg->originalText()); QString text = textApplyEntities(msg->originalText(), msg->originalEntities());
_text->setMaxLength(MaxMessageSize); _field = new InputArea(this, st::editTextArea, lang(lng_edit_placeholder), text);
_text->setCtrlEnterSubmit(cCtrlEnter() ? CtrlEnterSubmitCtrlEnter : CtrlEnterSubmitEnter); // _field->setMaxLength(MaxMessageSize); // entities can make text in input field larger but still valid
_field->setCtrlEnterSubmit(cCtrlEnter() ? CtrlEnterSubmitCtrlEnter : CtrlEnterSubmitEnter);
} }
updateBoxSize(); updateBoxSize();
connect(_text, SIGNAL(resized()), this, SLOT(onCaptionResized())); connect(_field, SIGNAL(submitted(bool)), this, SLOT(onSave(bool)));
connect(_text, SIGNAL(submitted(bool)), this, SLOT(onSave(bool))); connect(_field, SIGNAL(cancelled()), this, SLOT(onClose()));
connect(_text, SIGNAL(cancelled()), this, SLOT(onClose())); connect(_field, SIGNAL(resized()), this, SLOT(onCaptionResized()));
QTextCursor c(_text->textCursor()); QTextCursor c(_field->textCursor());
c.movePosition(QTextCursor::End); c.movePosition(QTextCursor::End);
_text->setTextCursor(c); _field->setTextCursor(c);
prepare(); prepare();
} }
void EditPostBox::onCaptionResized() { bool EditCaptionBox::captionFound() const {
return _animated || _photo || _doc;
}
void EditCaptionBox::onCaptionResized() {
updateBoxSize(); updateBoxSize();
resizeEvent(0); resizeEvent(0);
update(); update();
} }
void EditPostBox::updateBoxSize() { void EditCaptionBox::updateBoxSize() {
int32 bottomh = st::boxPhotoCompressedPadding.bottom() + _field->height() + st::normalFont->height + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom();
if (_photo || _animated) { if (_photo || _animated) {
setMaxHeight(st::boxPhotoPadding.top() + _thumbh + st::boxPhotoPadding.bottom() + st::boxPhotoCompressedPadding.bottom() + _text->height() + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom()); setMaxHeight(st::boxPhotoPadding.top() + _thumbh + bottomh);
} else if (_thumbw) { } else if (_thumbw) {
setMaxHeight(st::boxPhotoPadding.top() + st::msgFileThumbPadding.top() + st::msgFileThumbSize + st::msgFileThumbPadding.bottom() + st::boxPhotoPadding.bottom() + st::boxPhotoCompressedPadding.bottom() + _text->height() + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom()); setMaxHeight(st::boxPhotoPadding.top() + 0 + st::msgFileThumbSize + 0 + bottomh);
} else if (_doc) { } else if (_doc) {
setMaxHeight(st::boxPhotoPadding.top() + st::msgFilePadding.top() + st::msgFileSize + st::msgFilePadding.bottom() + st::boxPhotoPadding.bottom() + st::boxPhotoCompressedPadding.bottom() + _text->height() + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom()); setMaxHeight(st::boxPhotoPadding.top() + 0 + st::msgFileSize + 0 + bottomh);
} else { } else {
setMaxHeight(st::boxPhotoPadding.top() + _text->height() + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom()); setMaxHeight(st::boxPhotoPadding.top() + st::boxTitleFont->height + bottomh);
} }
} }
void EditPostBox::paintEvent(QPaintEvent *e) { void EditCaptionBox::paintEvent(QPaintEvent *e) {
Painter p(this); Painter p(this);
if (paint(p)) return; if (paint(p)) return;
@ -552,93 +558,108 @@ void EditPostBox::paintEvent(QPaintEvent *e) {
} }
} else if (_doc) { } else if (_doc) {
int32 w = width() - st::boxPhotoPadding.left() - st::boxPhotoPadding.right(); int32 w = width() - st::boxPhotoPadding.left() - st::boxPhotoPadding.right();
int32 h = _thumbw ? (st::msgFileThumbPadding.top() + st::msgFileThumbSize + st::msgFileThumbPadding.bottom()) : (st::msgFilePadding.top() + st::msgFileSize + st::msgFilePadding.bottom()); int32 h = _thumbw ? (0 + st::msgFileThumbSize + 0) : (0 + st::msgFileSize + 0);
int32 nameleft = 0, nametop = 0, nameright = 0, statustop = 0, linktop = 0; int32 nameleft = 0, nametop = 0, nameright = 0, statustop = 0;
if (_thumbw) { if (_thumbw) {
nameleft = st::msgFileThumbPadding.left() + st::msgFileThumbSize + st::msgFileThumbPadding.right(); nameleft = 0 + st::msgFileThumbSize + st::msgFileThumbPadding.right();
nametop = st::msgFileThumbNameTop; nametop = st::msgFileThumbNameTop - st::msgFileThumbPadding.top();
nameright = st::msgFileThumbPadding.left(); nameright = 0;
statustop = st::msgFileThumbStatusTop; statustop = st::msgFileThumbStatusTop - st::msgFileThumbPadding.top();
linktop = st::msgFileThumbLinkTop;
} else { } else {
nameleft = st::msgFilePadding.left() + st::msgFileSize + st::msgFilePadding.right(); nameleft = 0 + st::msgFileSize + st::msgFilePadding.right();
nametop = st::msgFileNameTop; nametop = st::msgFileNameTop - st::msgFilePadding.top();
nameright = st::msgFilePadding.left(); nameright = 0;
statustop = st::msgFileStatusTop; statustop = st::msgFileStatusTop - st::msgFilePadding.top();
} }
int32 namewidth = w - nameleft - (_thumbw ? st::msgFileThumbPadding.left() : st::msgFilePadding.left()); int32 namewidth = w - nameleft - 0;
if (namewidth > _statusw) { if (namewidth > _statusw) {
w -= (namewidth - _statusw); //w -= (namewidth - _statusw);
namewidth = _statusw; //namewidth = _statusw;
} }
int32 x = (width() - w) / 2, y = st::boxPhotoPadding.top(); int32 x = (width() - w) / 2, y = st::boxPhotoPadding.top();
App::roundRect(p, x, y, w, h, st::msgOutBg, MessageOutCorners, &st::msgOutShadow); // App::roundRect(p, x, y, w, h, st::msgInBg, MessageInCorners, &st::msgInShadow);
if (_thumbw) { if (_thumbw) {
QRect rthumb(rtlrect(x + st::msgFileThumbPadding.left(), y + st::msgFileThumbPadding.top(), st::msgFileThumbSize, st::msgFileThumbSize, width())); QRect rthumb(rtlrect(x + 0, y + 0, st::msgFileThumbSize, st::msgFileThumbSize, width()));
p.drawPixmap(rthumb.topLeft(), _thumb); p.drawPixmap(rthumb.topLeft(), _thumb);
} else { } else {
QRect inner(rtlrect(x + st::msgFilePadding.left(), y + st::msgFilePadding.top(), st::msgFileSize, st::msgFileSize, width())); QRect inner(rtlrect(x + 0, y + 0, st::msgFileSize, st::msgFileSize, width()));
p.setPen(Qt::NoPen); p.setPen(Qt::NoPen);
p.setBrush(st::msgFileOutBg); p.setBrush(st::msgFileInBg);
p.setRenderHint(QPainter::HighQualityAntialiasing); p.setRenderHint(QPainter::HighQualityAntialiasing);
p.drawEllipse(inner); p.drawEllipse(inner);
p.setRenderHint(QPainter::HighQualityAntialiasing, false); p.setRenderHint(QPainter::HighQualityAntialiasing, false);
p.drawSpriteCenter(inner, _isImage ? st::msgFileOutImage : st::msgFileOutFile); p.drawSpriteCenter(inner, _isImage ? st::msgFileInImage : st::msgFileInFile);
} }
p.setFont(st::semiboldFont); p.setFont(st::semiboldFont);
p.setPen(st::black); p.setPen(st::black);
_name.drawLeftElided(p, x + nameleft, y + nametop, namewidth, width()); _name.drawLeftElided(p, x + nameleft, y + nametop, namewidth, width());
style::color status(st::mediaOutFg); style::color status(st::mediaInFg);
p.setFont(st::normalFont); p.setFont(st::normalFont);
p.setPen(status); p.setPen(status);
p.drawTextLeft(x + nameleft, y + statustop, width(), _status); p.drawTextLeft(x + nameleft, y + statustop, width(), _status);
} else {
p.setFont(st::boxTitleFont);
p.setPen(st::black);
p.drawTextLeft(_field->x(), st::boxPhotoPadding.top(), width(), lang(lng_edit_message));
}
if (!_error.isEmpty()) {
p.setFont(st::normalFont);
p.setPen(st::setErrColor);
p.drawTextLeft(_field->x(), _field->y() + _field->height() + (st::boxButtonPadding.top() / 2), width(), _error);
} }
} }
void EditPostBox::resizeEvent(QResizeEvent *e) { void EditCaptionBox::resizeEvent(QResizeEvent *e) {
_save.moveToRight(st::boxButtonPadding.right(), height() - st::boxButtonPadding.bottom() - _save.height()); _save.moveToRight(st::boxButtonPadding.right(), height() - st::boxButtonPadding.bottom() - _save.height());
_cancel.moveToRight(st::boxButtonPadding.right() + _save.width() + st::boxButtonPadding.left(), _save.y()); _cancel.moveToRight(st::boxButtonPadding.right() + _save.width() + st::boxButtonPadding.left(), _save.y());
_text->resize(st::boxWideWidth - st::boxPhotoPadding.left() - st::boxPhotoPadding.right(), _text->height()); _field->resize(st::boxWideWidth - st::boxPhotoPadding.left() - st::boxPhotoPadding.right(), _field->height());
_text->moveToLeft(st::boxPhotoPadding.left(), _save.y() - st::boxButtonPadding.top() - _text->height()); _field->moveToLeft(st::boxPhotoPadding.left(), _save.y() - st::boxButtonPadding.top() - st::normalFont->height - _field->height());
} }
void EditPostBox::hideAll() { void EditCaptionBox::hideAll() {
_save.hide(); _save.hide();
_cancel.hide(); _cancel.hide();
_text->hide(); _field->hide();
} }
void EditPostBox::showAll() { void EditCaptionBox::showAll() {
_save.show(); _save.show();
_cancel.show(); _cancel.show();
_text->show(); _field->show();
} }
void EditPostBox::showDone() { void EditCaptionBox::showDone() {
setInnerFocus(); setInnerFocus();
} }
void EditPostBox::onSave(bool ctrlShiftEnter) { void EditCaptionBox::onSave(bool ctrlShiftEnter) {
if (_saveRequestId) return; if (_saveRequestId) return;
HistoryItem *item = App::histItemById(_msgId);
if (!item) {
_error = lang(lng_edit_deleted);
update();
return;
}
int32 flags = 0; int32 flags = 0;
if (_previewCancelled) { if (_previewCancelled) {
flags |= MTPchannels_EditMessage::flag_no_webpage; flags |= MTPchannels_EditMessage::flag_no_webpage;
} }
EntitiesInText sendingEntities; MTPVector<MTPMessageEntity> sentEntities;
MTPVector<MTPMessageEntity> sentEntities = linksToMTP(sendingEntities, true);
if (!sentEntities.c_vector().v.isEmpty()) { if (!sentEntities.c_vector().v.isEmpty()) {
flags |= MTPmessages_SendMessage::flag_entities; flags |= MTPmessages_SendMessage::flag_entities;
} }
_saveRequestId = MTP::send(MTPchannels_EditMessage(MTP_int(flags), _msg->history()->peer->asChannel()->inputChannel, MTP_int(_msg->id), MTP_string(_text->getLastText()), sentEntities), rpcDone(&EditPostBox::saveDone), rpcFail(&EditPostBox::saveFail)); _saveRequestId = MTP::send(MTPchannels_EditMessage(MTP_int(flags), item->history()->peer->asChannel()->inputChannel, MTP_int(item->id), MTP_string(_field->getLastText()), sentEntities), rpcDone(&EditCaptionBox::saveDone), rpcFail(&EditCaptionBox::saveFail));
} }
void EditPostBox::saveDone(const MTPUpdates &updates) { void EditCaptionBox::saveDone(const MTPUpdates &updates) {
_saveRequestId = 0; _saveRequestId = 0;
onClose(); onClose();
if (App::main()) { if (App::main()) {
@ -646,7 +667,7 @@ void EditPostBox::saveDone(const MTPUpdates &updates) {
} }
} }
bool EditPostBox::saveFail(const RPCError &error) { bool EditCaptionBox::saveFail(const RPCError &error) {
if (mtpIsFlood(error)) return false; if (mtpIsFlood(error)) return false;
_saveRequestId = 0; _saveRequestId = 0;
@ -657,8 +678,8 @@ bool EditPostBox::saveFail(const RPCError &error) {
onClose(); onClose();
return true; return true;
} else if (err == qstr("MESSAGE_EMPTY")) { } else if (err == qstr("MESSAGE_EMPTY")) {
_text->setFocus(); _field->setFocus();
_text->showError(); _field->showError();
} else { } else {
_error = lang(lng_edit_error); _error = lang(lng_edit_error);
} }

View File

@ -83,17 +83,19 @@ private:
}; };
class EditPostBox : public AbstractBox, public RPCSender { class EditCaptionBox : public AbstractBox, public RPCSender {
Q_OBJECT Q_OBJECT
public: public:
EditPostBox(HistoryItem *msg); EditCaptionBox(HistoryItem *msg);
void paintEvent(QPaintEvent *e); void paintEvent(QPaintEvent *e);
void resizeEvent(QResizeEvent *e); void resizeEvent(QResizeEvent *e);
bool captionFound() const;
void setInnerFocus() { void setInnerFocus() {
_text->setFocus(); _field->setFocus();
} }
public slots: public slots:
@ -114,12 +116,12 @@ private:
void saveDone(const MTPUpdates &updates); void saveDone(const MTPUpdates &updates);
bool saveFail(const RPCError &error); bool saveFail(const RPCError &error);
HistoryItem *_msg; FullMsgId _msgId;
bool _animated, _photo, _doc; bool _animated, _photo, _doc;
QPixmap _thumb; QPixmap _thumb;
InputArea *_text; InputArea *_field;
BoxButton _save, _cancel; BoxButton _save, _cancel;
int32 _thumbx, _thumby, _thumbw, _thumbh; int32 _thumbx, _thumby, _thumbw, _thumbh;

View File

@ -5049,6 +5049,58 @@ EntitiesInText textParseEntities(QString &text, int32 flags, bool rich) { // som
return result; return result;
} }
QString textApplyEntities(const QString &text, const EntitiesInText &entities) {
if (entities.isEmpty()) return text;
QMultiMap<int32, QString> closingTags;
QString code(qsl("`")), pre(qsl("```"));
QString result;
int32 size = text.size();
const QChar *b = text.constData(), *already = b, *e = b + size;
EntitiesInText::const_iterator entity = entities.cbegin(), end = entities.cend();
while (entity != end && ((entity->type != EntityInTextCode && entity->type != EntityInTextPre) || entity->length <= 0 || entity->offset >= size)) {
++entity;
}
while (entity != end || !closingTags.isEmpty()) {
int32 nextOpenEntity = (entity == end) ? (size + 1) : entity->offset;
int32 nextCloseEntity = closingTags.isEmpty() ? (size + 1) : closingTags.cbegin().key();
if (nextOpenEntity <= nextCloseEntity) {
QString tag = (entity->type == EntityInTextCode) ? code : pre;
if (result.isEmpty()) result.reserve(text.size() + entities.size() * pre.size() * 2);
const QChar *offset = b + nextOpenEntity;
if (offset > already) {
result.append(already, offset - already);
already = offset;
}
result.append(tag);
closingTags.insert(qMin(entity->offset + entity->length, size), tag);
++entity;
while (entity != end && ((entity->type != EntityInTextCode && entity->type != EntityInTextPre) || entity->length <= 0 || entity->offset >= size)) {
++entity;
}
} else {
const QChar *offset = b + nextCloseEntity;
if (offset > already) {
result.append(already, offset - already);
already = offset;
}
result.append(closingTags.cbegin().value());
closingTags.erase(closingTags.begin());
}
}
if (result.isEmpty()) {
return text;
}
const QChar *offset = b + size;
if (offset > already) {
result.append(already, offset - already);
}
return result;
}
void emojiDraw(QPainter &p, EmojiPtr e, int x, int y) { void emojiDraw(QPainter &p, EmojiPtr e, int x, int y) {
p.drawPixmap(QPoint(x, y), App::emoji(), QRect(e->x * ESize, e->y * ESize, ESize, ESize)); p.drawPixmap(QPoint(x, y), App::emoji(), QRect(e->x * ESize, e->y * ESize, ESize, ESize));
} }

View File

@ -110,6 +110,7 @@ inline MTPVector<MTPMessageEntity> linksToMTP(const EntitiesInText &links, bool
return result; return result;
} }
EntitiesInText textParseEntities(QString &text, int32 flags, bool rich = false); // changes text if (flags & TextParseMono) EntitiesInText textParseEntities(QString &text, int32 flags, bool rich = false); // changes text if (flags & TextParseMono)
QString textApplyEntities(const QString &text, const EntitiesInText &entities);
#include "gui/emoji_config.h" #include "gui/emoji_config.h"

View File

@ -6637,7 +6637,13 @@ void HistoryWidget::onEditMessage() {
HistoryItem *to = App::contextItem(); HistoryItem *to = App::contextItem();
if (!to || !to->history()->peer->isChannel()) return; if (!to || !to->history()->peer->isChannel()) return;
Ui::showLayer(new EditPostBox(to)); EditCaptionBox *box = new EditCaptionBox(to);
if (box->captionFound()) {
Ui::showLayer(box);
} else {
delete box;
// edit post
}
} }
bool HistoryWidget::lastForceReplyReplied(const FullMsgId &replyTo) const { bool HistoryWidget::lastForceReplyReplied(const FullMsgId &replyTo) const {