added file, img+text and file+text templates support, file send confirm

This commit is contained in:
John Preston 2014-08-22 13:35:22 +04:00
parent ceb899b69b
commit 63c38712a9
8 changed files with 148 additions and 48 deletions

View file

@ -363,6 +363,7 @@ lng_context_forward_selected: "Forward Selected";
lng_context_delete_selected: "Delete Selected";
lng_context_clear_selection: "Clear Selection";
lng_really_send_image: "Do you want to send this image?";
lng_really_send_file: "Do you want to send this file?";
lng_send_image_compressed: "Send compressed image";
lng_forward_choose: "Choose recipient...";

View file

@ -855,6 +855,8 @@ mediaMaxWidth: 250px;
mediaFont: font(fsize);
mediaPadding: margins(7px, 6px, 11px, 6px);
mediaThumbSize: 48px;
mediaNameTop: 3px;
mediaDetailsShift: 3px;
mediaDocOutImg: sprite(6px, 146px, 48px, 48px);
mediaDocInImg: sprite(56px, 146px, 48px, 48px);
mediaAudioOutImg: sprite(106px, 146px, 48px, 48px);

View file

@ -24,43 +24,72 @@ Copyright (c) 2014 John Preston, https://tdesktop.com
#include "photosendbox.h"
PhotoSendBox::PhotoSendBox(const ReadyLocalMedia &img) : _img(img),
_thumbx(0), _thumby(0), _thumbw(0), _thumbh(0), _namew(0), _textw(0),
_compressed(this, lang(lng_send_image_compressed), true),
_sendButton(this, lang(lng_send_button), st::btnSelectDone),
_cancelButton(this, lang(lng_cancel), st::btnSelectCancel),
a_opacity(0, 1) {
connect(&_sendButton, SIGNAL(clicked()), this, SLOT(onSend()));
connect(&_cancelButton, SIGNAL(clicked()), this, SLOT(onCancel()));
int32 maxW = 0, maxH = 0;
for (PreparedPhotoThumbs::const_iterator i = img.photoThumbs.cbegin(), e = img.photoThumbs.cend(); i != e; ++i) {
if (i->width() >= maxW && i->height() >= maxH) {
_thumb = *i;
maxW = _thumb.width();
maxH = _thumb.height();
}
}
int32 tw = _thumb.width(), th = _thumb.height();
if (!tw || !th) {
tw = th = 1;
}
_width = st::confirmWidth;
_thumbw = _width - st::boxPadding.left() - st::boxPadding.right();
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;
if (img.type == ToPreparePhoto) {
int32 maxW = 0, maxH = 0;
for (PreparedPhotoThumbs::const_iterator i = img.photoThumbs.cbegin(), e = img.photoThumbs.cend(); i != e; ++i) {
if (i->width() >= maxW && i->height() >= maxH) {
_thumb = *i;
maxW = _thumb.width();
maxH = _thumb.height();
}
}
}
_height = _thumbh + st::boxPadding.top() + st::boxFont->height + st::boxPadding.bottom() + st::boxPadding.bottom() + _compressed.height() + _sendButton.height();
int32 tw = _thumb.width(), th = _thumb.height();
if (!tw || !th) {
tw = th = 1;
}
_thumbw = _width - st::boxPadding.left() - st::boxPadding.right();
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;
}
}
_height = _thumbh + st::boxPadding.top() + st::boxFont->height + st::boxPadding.bottom() + st::boxPadding.bottom() + _compressed.height() + _sendButton.height();
_thumb = QPixmap::fromImage(_thumb.toImage().scaled(_thumbw, _thumbh, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
_thumb = QPixmap::fromImage(_thumb.toImage().scaled(_thumbw, _thumbh, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
} else {
_compressed.hide();
if (!img.photoThumbs.isEmpty()) {
_thumb = img.photoThumbs.cbegin().value();
int32 tw = _thumb.width(), th = _thumb.height();
if (_thumb.isNull() || !tw || !th) {
_thumbw = _thumbx = _thumby = 0;
} else if (tw > th) {
_thumbw = (tw * st::mediaThumbSize) / th;
_thumbx = (_thumbw - st::mediaThumbSize) / 2;
_thumby = 0;
} else {
_thumbw = st::mediaThumbSize;
_thumbx = 0;
_thumby = ((th * _thumbw) / tw - st::mediaThumbSize) / 2;
}
}
if (_thumbw) {
_thumb = QPixmap::fromImage(_thumb.toImage().scaledToWidth(_thumbw * cIntRetinaFactor(), Qt::SmoothTransformation));
_thumb.setDevicePixelRatio(cRetinaFactor());
}
_height = st::boxPadding.top() + st::boxFont->height + st::boxPadding.bottom() + st::mediaPadding.top() + st::mediaThumbSize + st::mediaPadding.bottom() + st::boxPadding.bottom() + _sendButton.height();
_name = img.filename;
_namew = st::mediaFont->m.width(_name);
_size = formatSizeText(img.filesize);
_textw = qMax(_namew, st::mediaFont->m.width(_size));
}
resize(_width, _height);
}
@ -97,8 +126,41 @@ void PhotoSendBox::paintEvent(QPaintEvent *e) {
p.setFont(st::boxFont->f);
p.setPen(st::boxGrayTitle->p);
p.drawText(QRect(st::boxPadding.left(), st::boxPadding.top(), _width - st::boxPadding.left() - st::boxPadding.right(), st::boxFont->height), lang(lng_really_send_image), style::al_center);
p.drawPixmap((_width - _thumbw) / 2, st::boxPadding.top() * 2 + st::boxFont->height, _thumb);
if (_img.type == ToPreparePhoto) {
p.drawText(QRect(st::boxPadding.left(), st::boxPadding.top(), _width - st::boxPadding.left() - st::boxPadding.right(), st::boxFont->height), lang(lng_really_send_image), style::al_center);
p.drawPixmap((_width - _thumbw) / 2, st::boxPadding.top() * 2 + st::boxFont->height, _thumb);
} else {
p.drawText(QRect(st::boxPadding.left(), st::boxPadding.top(), _width - st::boxPadding.left() - st::boxPadding.right(), st::boxFont->height), lang(lng_really_send_file), style::al_center);
int32 w = _width - st::boxPadding.left() - st::boxPadding.right(), h = st::mediaPadding.top() + st::mediaThumbSize + st::mediaPadding.bottom();
int32 tleft = st::mediaPadding.left() + st::mediaThumbSize + st::mediaPadding.right();
int32 twidth = w - tleft - st::mediaPadding.right();
if (twidth > _textw) {
w -= (twidth - _textw);
twidth = _textw;
}
int32 x = (_width - w) / 2, y = st::boxPadding.top() * 2 + st::boxFont->height;
p.fillRect(QRect(x, y, w, h), st::msgOutBG->b);
p.fillRect(x, y + h, w, st::msgShadow, st::msgOutShadow->b);
if (_thumbw) {
int32 rf(cIntRetinaFactor());
p.drawPixmap(QPoint(x + st::mediaPadding.left(), y + st::mediaPadding.top()), _thumb, QRect(_thumbx * rf, _thumby * rf, st::mediaThumbSize * rf, st::mediaThumbSize * rf));
} else {
p.drawPixmap(QPoint(x + st::mediaPadding.left(), y + st::mediaPadding.top()), App::sprite(), st::mediaDocOutImg);
}
p.setFont(st::mediaFont->f);
p.setPen(st::black->c);
if (twidth < _namew) {
p.drawText(x + tleft, y + st::mediaPadding.top() + st::mediaNameTop + st::mediaFont->ascent, st::mediaFont->m.elidedText(_name, Qt::ElideRight, twidth));
} else {
p.drawText(x + tleft, y + st::mediaPadding.top() + st::mediaNameTop + st::mediaFont->ascent, _name);
}
p.setPen(st::mediaOutColor->p);
p.drawText(x + tleft, y + st::mediaPadding.top() + st::mediaThumbSize - st::mediaDetailsShift - st::mediaFont->descent, _size);
}
}
void PhotoSendBox::animStep(float64 ms) {
@ -114,7 +176,7 @@ void PhotoSendBox::animStep(float64 ms) {
}
void PhotoSendBox::onSend() {
if (_compressed.checked()) {
if (_compressed.isHidden() || _compressed.checked()) {
if (App::main()) App::main()->confirmSendImage(_img);
} else {
if (App::main()) App::main()->confirmSendImageUncompressed();

View file

@ -41,7 +41,9 @@ public slots:
private:
ReadyLocalMedia _img;
int32 _width, _height, _thumbw, _thumbh;
int32 _width, _height, _thumbx, _thumby, _thumbw, _thumbh;
QString _name, _size;
int32 _namew, _textw;
FlatCheckbox _compressed;
FlatButton _sendButton, _cancelButton;
QPixmap _thumb;

View file

@ -2120,7 +2120,7 @@ void HistoryVideo::draw(QPainter &p, const HistoryItem *parent, bool selected, i
p.setFont(st::mediaFont->f);
p.setPen(st::black->c);
p.drawText(tleft, st::mediaPadding.top() + 3 + st::mediaFont->ascent, lang(lng_media_video));
p.drawText(tleft, st::mediaPadding.top() + st::mediaNameTop + st::mediaFont->ascent, lang(lng_media_video));
QString statusText;
@ -2146,7 +2146,7 @@ void HistoryVideo::draw(QPainter &p, const HistoryItem *parent, bool selected, i
statusText = _size;
}
}
p.drawText(tleft, st::mediaPadding.top() + st::mediaThumbSize - 3 - st::mediaFont->descent, statusText);
p.drawText(tleft, st::mediaPadding.top() + st::mediaThumbSize - st::mediaDetailsShift - st::mediaFont->descent, statusText);
p.setFont(st::msgDateFont->f);
@ -2249,7 +2249,7 @@ void HistoryAudio::draw(QPainter &p, const HistoryItem *parent, bool selected, i
p.setFont(st::mediaFont->f);
p.setPen(st::black->c);
p.drawText(tleft, st::mediaPadding.top() + 3 + st::mediaFont->ascent, lang(lng_media_audio));
p.drawText(tleft, st::mediaPadding.top() + st::mediaNameTop + st::mediaFont->ascent, lang(lng_media_audio));
QString statusText;
@ -2275,7 +2275,7 @@ void HistoryAudio::draw(QPainter &p, const HistoryItem *parent, bool selected, i
statusText = _size;
}
}
p.drawText(tleft, st::mediaPadding.top() + st::mediaThumbSize - 3 - st::mediaFont->descent, statusText);
p.drawText(tleft, st::mediaPadding.top() + st::mediaThumbSize - st::mediaDetailsShift - st::mediaFont->descent, statusText);
p.setFont(st::msgDateFont->f);
@ -2460,9 +2460,9 @@ void HistoryDocument::draw(QPainter &p, const HistoryItem *parent, bool selected
p.setFont(st::mediaFont->f);
p.setPen(st::black->c);
if (twidth < _namew) {
p.drawText(tleft, st::mediaPadding.top() + 3 + st::mediaFont->ascent, st::mediaFont->m.elidedText(_name, Qt::ElideRight, twidth));
p.drawText(tleft, st::mediaPadding.top() + st::mediaNameTop + st::mediaFont->ascent, st::mediaFont->m.elidedText(_name, Qt::ElideRight, twidth));
} else {
p.drawText(tleft, st::mediaPadding.top() + 3 + st::mediaFont->ascent, _name);
p.drawText(tleft, st::mediaPadding.top() + st::mediaNameTop + st::mediaFont->ascent, _name);
}
QString statusText;
@ -2489,7 +2489,7 @@ void HistoryDocument::draw(QPainter &p, const HistoryItem *parent, bool selected
statusText = _size;
}
}
p.drawText(tleft, st::mediaPadding.top() + st::mediaThumbSize - 3 - st::mediaFont->descent, statusText);
p.drawText(tleft, st::mediaPadding.top() + st::mediaThumbSize - st::mediaDetailsShift - st::mediaFont->descent, statusText);
p.setFont(st::msgDateFont->f);
@ -2664,15 +2664,15 @@ void HistoryContact::draw(QPainter &p, const HistoryItem *parent, bool selected,
p.setFont(st::mediaFont->f);
p.setPen(st::black->c);
if (twidth < phonew) {
p.drawText(tleft, st::mediaPadding.top() + 3 + st::mediaFont->ascent, st::mediaFont->m.elidedText(phone, Qt::ElideRight, twidth));
p.drawText(tleft, st::mediaPadding.top() + st::mediaNameTop + st::mediaFont->ascent, st::mediaFont->m.elidedText(phone, Qt::ElideRight, twidth));
} else {
p.drawText(tleft, st::mediaPadding.top() + 3 + st::mediaFont->ascent, phone);
p.drawText(tleft, st::mediaPadding.top() + st::mediaNameTop + st::mediaFont->ascent, phone);
}
style::color status(selected ? (out ? st::mediaOutSelectColor : st::mediaInSelectColor) : (out ? st::mediaOutColor : st::mediaInColor));
p.setPen(status->p);
name.drawElided(p, tleft, st::mediaPadding.top() + st::mediaThumbSize - 3 - st::mediaFont->height, secondwidth);
name.drawElided(p, tleft, st::mediaPadding.top() + st::mediaThumbSize - st::mediaDetailsShift - st::mediaFont->height, secondwidth);
p.setFont(st::msgDateFont->f);

View file

@ -1272,6 +1272,8 @@ private:
int32 w;
};
QString formatSizeText(qint64 size);
class HistoryVideo : public HistoryMedia {
public:

View file

@ -1517,6 +1517,7 @@ HistoryWidget::HistoryWidget(QWidget *parent) : QWidget(parent)
, loadingRequestId(0)
, serviceImageCacheSize(0)
, confirmImageId(0)
, confirmWithText(false)
, titlePeerTextWidth(0)
, bg(st::msgBG)
, hiderOffered(false)
@ -2669,20 +2670,30 @@ void HistoryWidget::onFieldFocused() {
if (_list) _list->clearSelectedItems(true);
}
void HistoryWidget::uploadImage(const QImage &img) {
void HistoryWidget::uploadImage(const QImage &img, bool withText) {
if (!hist || confirmImageId) return;
App::wnd()->activateWindow();
confirmImage = img;
confirmWithText = withText;
confirmImageId = imageLoader.append(img, histPeer->id, ToPreparePhoto);
}
void HistoryWidget::uploadFile(const QString &file, bool withText) {
if (!hist || confirmImageId) return;
App::wnd()->activateWindow();
confirmWithText = withText;
confirmImageId = imageLoader.append(file, histPeer->id, ToPrepareDocument);
}
void HistoryWidget::uploadConfirmImageUncompressed() {
if (!hist || !confirmImageId || confirmImage.isNull()) return;
App::wnd()->activateWindow();
imageLoader.append(confirmImage, histPeer->id, ToPrepareDocument);
confirmImageId = 0;
confirmWithText = false;
confirmImage = QImage();
}
@ -2719,7 +2730,11 @@ void HistoryWidget::onPhotoFailed(quint64 id) {
void HistoryWidget::confirmSendImage(const ReadyLocalMedia &img) {
if (img.id == confirmImageId) {
if (confirmWithText) {
onSend();
}
confirmImageId = 0;
confirmWithText = false;
confirmImage = QImage();
}
MsgId newId = clientMsgId();
@ -2751,7 +2766,9 @@ void HistoryWidget::confirmSendImage(const ReadyLocalMedia &img) {
}
void HistoryWidget::cancelSendImage() {
if (confirmImageId && confirmWithText) _field.setPlainText(QString());
confirmImageId = 0;
confirmWithText = false;
confirmImage = QImage();
}
@ -2986,11 +3003,23 @@ void HistoryWidget::keyPressEvent(QKeyEvent *e) {
void HistoryWidget::onFieldTabbed() {
QString v = _field.getText(), t = supportTemplate(v.trimmed());
if (!t.isEmpty()) {
if (t.indexOf(qsl("img:")) == 0) {
QImage img(cWorkingDir() + t.mid(4).trimmed());
if (!img.isNull()) {
_field.setPlainText(QString());
uploadImage(img);
bool isImg = (t.indexOf(qsl("img:")) == 0), isFile = (t.indexOf(qsl("file:")) == 0);
if (isImg || isFile) {
QString fname = t.mid(isImg ? 4 : 5).trimmed(), text;
int32 lineEnd = fname.indexOf(QChar('\n'));
if (lineEnd > 0) {
text = fname.mid(lineEnd + 1).trimmed();
fname = fname.mid(0, lineEnd).trimmed();
}
if (isImg) {
QImage img(cWorkingDir() + fname);
if (!img.isNull()) {
_field.setPlainText(text);
uploadImage(img, !text.isEmpty());
}
} else {
_field.setPlainText(text);
uploadFile(fname, !text.isEmpty());
}
} else {
_field.setPlainText(t);

View file

@ -288,7 +288,8 @@ public:
void updateTyping(bool typing = true);
void destroyData();
void uploadImage(const QImage &img);
void uploadImage(const QImage &img, bool withText = false);
void uploadFile(const QString &file, bool withText = false); // with confirmation
void uploadConfirmImageUncompressed();
void uploadMedias(const QStringList &files, ToPrepareMediaType type);
void uploadMedia(const QByteArray &fileContent, ToPrepareMediaType type);
@ -432,6 +433,7 @@ private:
int64 serviceImageCacheSize;
QImage confirmImage;
PhotoId confirmImageId;
bool confirmWithText;
QString titlePeerText;
int32 titlePeerTextWidth;