From c773bffec60335c519f31d318242f51099db215e Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 20 Oct 2016 18:26:55 +0300 Subject: [PATCH] Applying the ReplyMarkup returned in updateNewMessage. #2543 Also InputField now can be forced to hide placeholder. --- Telegram/SourceFiles/app.cpp | 1 + Telegram/SourceFiles/history/history_item.h | 2 + .../SourceFiles/history/history_message.h | 3 + Telegram/SourceFiles/ui/flatinput.cpp | 77 +++++------ Telegram/SourceFiles/ui/flatinput.h | 121 +++++++----------- 5 files changed, 96 insertions(+), 108 deletions(-) diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index 902b3a85a2..facd674d11 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -1105,6 +1105,7 @@ namespace { auto entities = m.has_entities() ? entitiesFromMTP(m.ventities.c_vector().v) : EntitiesInText(); existing->setText({ text, entities }); existing->updateMedia(m.has_media() ? (&m.vmedia) : nullptr); + existing->updateReplyMarkup(m.has_reply_markup() ? (&m.vreply_markup) : nullptr); existing->setViewsCount(m.has_views() ? m.vviews.v : -1); existing->addToOverview(AddToOverviewNew); diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index d1cd61d501..c445d2b480 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -623,6 +623,8 @@ public: } virtual void updateMedia(const MTPMessageMedia *media) { } + virtual void updateReplyMarkup(const MTPReplyMarkup *markup) { + } virtual int32 addToOverview(AddToOverviewMethod method) { return 0; } diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h index 0a1408c9b3..86058657d6 100644 --- a/Telegram/SourceFiles/history/history_message.h +++ b/Telegram/SourceFiles/history/history_message.h @@ -97,6 +97,9 @@ public: void applyEdition(const MTPDmessage &message) override; void applyEdition(const MTPDmessageService &message) override; void updateMedia(const MTPMessageMedia *media) override; + void updateReplyMarkup(const MTPReplyMarkup *markup) override { + setReplyMarkup(markup); + } int32 addToOverview(AddToOverviewMethod method) override; void eraseFromOverview() override; diff --git a/Telegram/SourceFiles/ui/flatinput.cpp b/Telegram/SourceFiles/ui/flatinput.cpp index 44a8cad4c3..cc63eb4fed 100644 --- a/Telegram/SourceFiles/ui/flatinput.cpp +++ b/Telegram/SourceFiles/ui/flatinput.cpp @@ -454,12 +454,12 @@ void PhonePartInput::paintEvent(QPaintEvent *e) { FlatInput::paintEvent(e); Painter p(this); - QString t(text()); - if (!pattern.isEmpty() && !t.isEmpty()) { - QString ph = placeholder().mid(t.size()); + auto t = text(); + if (!_pattern.isEmpty() && !t.isEmpty()) { + auto ph = placeholder().mid(t.size()); if (!ph.isEmpty()) { p.setClipRect(rect()); - QRect phRect(placeholderRect()); + auto phRect = placeholderRect(); int tw = phFont()->width(t); if (tw < phRect.width()) { phRect.setLeft(phRect.left() + tw); @@ -488,7 +488,7 @@ void PhonePartInput::correctValue(const QString &was, QString &now) { } if (digitCount > MaxPhoneTailLength) digitCount = MaxPhoneTailLength; - bool inPart = !pattern.isEmpty(); + bool inPart = !_pattern.isEmpty(); int curPart = -1, leftInPart = 0; newText.reserve(oldLen); for (int i = 0; i < oldLen; ++i) { @@ -507,8 +507,8 @@ void PhonePartInput::correctValue(const QString &was, QString &now) { } else { newText += ' '; ++curPart; - inPart = curPart < pattern.size(); - leftInPart = inPart ? (pattern.at(curPart) - 1) : 0; + inPart = curPart < _pattern.size(); + leftInPart = inPart ? (_pattern.at(curPart) - 1) : 0; ++oldPos; } @@ -520,8 +520,8 @@ void PhonePartInput::correctValue(const QString &was, QString &now) { } else { newText += ch; ++curPart; - inPart = curPart < pattern.size(); - leftInPart = inPart ? pattern.at(curPart) : 0; + inPart = curPart < _pattern.size(); + leftInPart = inPart ? _pattern.at(curPart) : 0; } } else { newText += ch; @@ -554,26 +554,26 @@ void PhonePartInput::addedToNumber(const QString &added) { } void PhonePartInput::onChooseCode(const QString &code) { - pattern = phoneNumberParse(code); - if (!pattern.isEmpty() && pattern.at(0) == code.size()) { - pattern.pop_front(); + _pattern = phoneNumberParse(code); + if (!_pattern.isEmpty() && _pattern.at(0) == code.size()) { + _pattern.pop_front(); } else { - pattern.clear(); + _pattern.clear(); } - if (pattern.isEmpty()) { + if (_pattern.isEmpty()) { setPlaceholder(lang(lng_phone_ph)); } else { QString ph; ph.reserve(20); - for (int i = 0, l = pattern.size(); i < l; ++i) { + for (int i = 0, l = _pattern.size(); i < l; ++i) { ph.append(' '); - ph.append(QString(pattern.at(i), QChar(0x2212))); + ph.append(QString(_pattern.at(i), QChar(0x2212))); } setPlaceholder(ph); } - QString newText(getLastText()); + auto newText = getLastText(); correctValue(newText, newText); - setPlaceholderFast(!pattern.isEmpty()); + setPlaceholderFast(!_pattern.isEmpty()); updatePlaceholder(); } @@ -1913,7 +1913,7 @@ void InputField::step_border(float64 ms, bool timer) { } void InputField::updatePlaceholder() { - bool placeholderVisible = _oldtext.isEmpty(); + bool placeholderVisible = _oldtext.isEmpty() && !_forcePlaceholderHidden; if (placeholderVisible != _placeholderVisible) { _placeholderVisible = placeholderVisible; @@ -1923,6 +1923,11 @@ void InputField::updatePlaceholder() { } } +void InputField::setPlaceholderHidden(bool forcePlaceholderHidden) { + _forcePlaceholderHidden = forcePlaceholderHidden; + updatePlaceholder(); +} + QMimeData *InputField::InputFieldInner::createMimeDataFromSelection() const { QMimeData *result = new QMimeData(); QTextCursor c(textCursor()); @@ -2524,12 +2529,12 @@ void PhoneInput::clearText() { } void PhoneInput::paintPlaceholder(Painter &p) { - QString t(getLastText()); - if (!pattern.isEmpty() && !t.isEmpty()) { - QString ph = placeholder().mid(t.size()); + auto t = getLastText(); + if (!_pattern.isEmpty() && !t.isEmpty()) { + auto ph = placeholder().mid(t.size()); if (!ph.isEmpty()) { p.setClipRect(rect()); - QRect phRect(placeholderRect()); + auto phRect = placeholderRect(); int tw = phFont()->width(t); if (tw < phRect.width()) { phRect.setLeft(phRect.left() + tw); @@ -2545,34 +2550,34 @@ void PhoneInput::paintPlaceholder(Painter &p) { void PhoneInput::correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) { QString digits(now); digits.replace(QRegularExpression(qsl("[^\\d]")), QString()); - pattern = phoneNumberParse(digits); + _pattern = phoneNumberParse(digits); QString newPlaceholder; - if (pattern.isEmpty()) { + if (_pattern.isEmpty()) { newPlaceholder = lang(lng_contact_phone); - } else if (pattern.size() == 1 && pattern.at(0) == digits.size()) { - newPlaceholder = QString(pattern.at(0) + 2, ' ') + lang(lng_contact_phone); + } else if (_pattern.size() == 1 && _pattern.at(0) == digits.size()) { + newPlaceholder = QString(_pattern.at(0) + 2, ' ') + lang(lng_contact_phone); } else { newPlaceholder.reserve(20); - for (int i = 0, l = pattern.size(); i < l; ++i) { + for (int i = 0, l = _pattern.size(); i < l; ++i) { if (i) { newPlaceholder.append(' '); } else { newPlaceholder.append('+'); } - newPlaceholder.append(i ? QString(pattern.at(i), QChar(0x2212)) : digits.mid(0, pattern.at(i))); + newPlaceholder.append(i ? QString(_pattern.at(i), QChar(0x2212)) : digits.mid(0, _pattern.at(i))); } } if (setPlaceholder(newPlaceholder)) { - setPlaceholderFast(!pattern.isEmpty()); + setPlaceholderFast(!_pattern.isEmpty()); updatePlaceholder(); } QString newText; int oldPos(nowCursor), newPos(-1), oldLen(now.length()), digitCount = qMin(digits.size(), MaxPhoneCodeLength + MaxPhoneTailLength); - bool inPart = !pattern.isEmpty(), plusFound = false; - int curPart = 0, leftInPart = inPart ? pattern.at(curPart) : 0; + bool inPart = !_pattern.isEmpty(), plusFound = false; + int curPart = 0, leftInPart = inPart ? _pattern.at(curPart) : 0; newText.reserve(oldLen + 1); newText.append('+'); for (int i = 0; i < oldLen; ++i) { @@ -2591,8 +2596,8 @@ void PhoneInput::correctValue(const QString &was, int32 wasCursor, QString &now, } else { newText += ' '; ++curPart; - inPart = curPart < pattern.size(); - leftInPart = inPart ? (pattern.at(curPart) - 1) : 0; + inPart = curPart < _pattern.size(); + leftInPart = inPart ? (_pattern.at(curPart) - 1) : 0; ++oldPos; } @@ -2604,8 +2609,8 @@ void PhoneInput::correctValue(const QString &was, int32 wasCursor, QString &now, } else { newText += ch; ++curPart; - inPart = curPart < pattern.size(); - leftInPart = inPart ? pattern.at(curPart) : 0; + inPart = curPart < _pattern.size(); + leftInPart = inPart ? _pattern.at(curPart) : 0; } } else { newText += ch; diff --git a/Telegram/SourceFiles/ui/flatinput.h b/Telegram/SourceFiles/ui/flatinput.h index 43877a8242..cd802fa8a6 100644 --- a/Telegram/SourceFiles/ui/flatinput.h +++ b/Telegram/SourceFiles/ui/flatinput.h @@ -117,25 +117,20 @@ class CountryCodeInput : public FlatInput { Q_OBJECT public: - CountryCodeInput(QWidget *parent, const style::flatInput &st); public slots: - void startErasing(QKeyEvent *e); void codeSelected(const QString &code); signals: - void codeChanged(const QString &code); void addedToNumber(const QString &added); protected: - - void correctValue(const QString &was, QString &now); + void correctValue(const QString &was, QString &now) override; private: - bool _nosignal; }; @@ -144,28 +139,23 @@ class PhonePartInput : public FlatInput { Q_OBJECT public: - PhonePartInput(QWidget *parent, const style::flatInput &st); - void paintEvent(QPaintEvent *e); - void keyPressEvent(QKeyEvent *e); - public slots: - void addedToNumber(const QString &added); void onChooseCode(const QString &code); signals: - void voidBackspace(QKeyEvent *e); protected: + void paintEvent(QPaintEvent *e) override; + void keyPressEvent(QKeyEvent *e) override; - void correctValue(const QString &was, QString &now); + void correctValue(const QString &was, QString &now) override; private: - - QVector pattern; + QVector _pattern; }; @@ -179,16 +169,8 @@ class InputArea : public TWidget { Q_OBJECT public: - InputArea(QWidget *parent, const style::InputArea &st, const QString &ph = QString(), const QString &val = QString()); - void touchEvent(QTouchEvent *e); - void paintEvent(QPaintEvent *e); - void focusInEvent(QFocusEvent *e); - void mousePressEvent(QMouseEvent *e); - void contextMenuEvent(QContextMenuEvent *e); - void resizeEvent(QResizeEvent *e); - void showError(); void setMaxLength(int32 maxLength) { @@ -241,7 +223,6 @@ public: } public slots: - void onTouchTimer(); void onDocumentContentsChange(int position, int charsRemoved, int charsAdded); @@ -251,7 +232,6 @@ public slots: void onRedoAvailable(bool avail); signals: - void changed(); void submitted(bool ctrlShiftEnter); void cancelled(); @@ -262,7 +242,6 @@ signals: void resized(); protected: - void insertEmoji(EmojiPtr emoji, QTextCursor c); TWidget *tparent() { return qobject_cast(parentWidget()); @@ -271,8 +250,14 @@ protected: return qobject_cast(parentWidget()); } -private: + void touchEvent(QTouchEvent *e); + void paintEvent(QPaintEvent *e) override; + void focusInEvent(QFocusEvent *e) override; + void mousePressEvent(QMouseEvent *e) override; + void contextMenuEvent(QContextMenuEvent *e) override; + void resizeEvent(QResizeEvent *e) override; +private: int32 _maxLength; bool heightAutoupdated(); void checkContentHeight(); @@ -282,23 +267,24 @@ private: public: InputAreaInner(InputArea *parent); - bool viewportEvent(QEvent *e); - void focusInEvent(QFocusEvent *e); - void focusOutEvent(QFocusEvent *e); - void keyPressEvent(QKeyEvent *e); - void paintEvent(QPaintEvent *e); - void contextMenuEvent(QContextMenuEvent *e); - - QMimeData *createMimeDataFromSelection() const; - QVariant loadResource(int type, const QUrl &name); - private: + protected: + bool viewportEvent(QEvent *e) override; + void focusInEvent(QFocusEvent *e) override; + void focusOutEvent(QFocusEvent *e) override; + void keyPressEvent(QKeyEvent *e) override; + void paintEvent(QPaintEvent *e) override; + void contextMenuEvent(QContextMenuEvent *e) override; + QMimeData *createMimeDataFromSelection() const override; + + private: InputArea *f() const { return static_cast(parentWidget()); } friend class InputArea; + }; void focusInInner(); @@ -337,22 +323,15 @@ private: QPoint _touchStart; bool _correcting; + }; class InputField : public TWidget { Q_OBJECT public: - InputField(QWidget *parent, const style::InputField &st, const QString &ph = QString(), const QString &val = QString()); - void touchEvent(QTouchEvent *e); - void paintEvent(QPaintEvent *e); - void focusInEvent(QFocusEvent *e); - void mousePressEvent(QMouseEvent *e); - void contextMenuEvent(QContextMenuEvent *e); - void resizeEvent(QResizeEvent *e); - void setMaxLength(int32 maxLength) { _maxLength = maxLength; } @@ -363,6 +342,7 @@ public: return _oldtext; } void updatePlaceholder(); + void setPlaceholderHidden(bool forcePlaceholderHidden); void step_placeholderFg(float64 ms, bool timer); void step_placeholderShift(float64 ms, bool timer); @@ -412,7 +392,6 @@ public: } public slots: - void onTouchTimer(); void onDocumentContentsChange(int position, int charsRemoved, int charsAdded); @@ -424,7 +403,6 @@ public slots: void selectAll(); signals: - void changed(); void submitted(bool ctrlShiftEnter); void cancelled(); @@ -434,7 +412,6 @@ signals: void blurred(); protected: - void insertEmoji(EmojiPtr emoji, QTextCursor c); TWidget *tparent() { return qobject_cast(parentWidget()); @@ -443,32 +420,40 @@ protected: return qobject_cast(parentWidget()); } -private: + void touchEvent(QTouchEvent *e); + void paintEvent(QPaintEvent *e) override; + void focusInEvent(QFocusEvent *e) override; + void mousePressEvent(QMouseEvent *e) override; + void contextMenuEvent(QContextMenuEvent *e) override; + void resizeEvent(QResizeEvent *e) override; +private: int32 _maxLength; + bool _forcePlaceholderHidden = false; friend class InputFieldInner; class InputFieldInner : public QTextEdit { public: InputFieldInner(InputField *parent); - bool viewportEvent(QEvent *e); - void focusInEvent(QFocusEvent *e); - void focusOutEvent(QFocusEvent *e); - void keyPressEvent(QKeyEvent *e); - void paintEvent(QPaintEvent *e); - void contextMenuEvent(QContextMenuEvent *e); - QMimeData *createMimeDataFromSelection() const; QVariant loadResource(int type, const QUrl &name); - private: + protected: + bool viewportEvent(QEvent *e) override; + void focusInEvent(QFocusEvent *e) override; + void focusOutEvent(QFocusEvent *e) override; + void keyPressEvent(QKeyEvent *e) override; + void paintEvent(QPaintEvent *e) override; + void contextMenuEvent(QContextMenuEvent *e) override; + private: InputField *f() const { return static_cast(parentWidget()); } friend class InputField; + }; void focusInInner(); @@ -624,54 +609,46 @@ private: class PasswordField : public MaskedInputField { public: - PasswordField(QWidget *parent, const style::InputField &st, const QString &ph = QString(), const QString &val = QString()); }; class PortInput : public MaskedInputField { public: - PortInput(QWidget *parent, const style::InputField &st, const QString &ph, const QString &val); protected: - - void correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor); + void correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) override; }; class UsernameInput : public MaskedInputField { public: - UsernameInput(QWidget *parent, const style::InputField &st, const QString &ph, const QString &val, bool isLink); - void paintPlaceholder(Painter &p); protected: - - void correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor); + void correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) override; + void paintPlaceholder(Painter &p) override; private: - QString _linkPlaceholder; }; class PhoneInput : public MaskedInputField { public: - PhoneInput(QWidget *parent, const style::InputField &st, const QString &ph, const QString &val); - void focusInEvent(QFocusEvent *e); void clearText(); protected: + void focusInEvent(QFocusEvent *e) override; - void paintPlaceholder(Painter &p); - void correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor); + void correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) override; + void paintPlaceholder(Painter &p) override; private: - QString _defaultPlaceholder; - QVector pattern; + QVector _pattern; };