tdesktop/Telegram/SourceFiles/gui/flatinput.h

278 lines
5.9 KiB
C
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014 John Preston, https://desktop.telegram.org
*/
#pragma once
#include <QtWidgets/QLineEdit>
#include "style.h"
#include "animation.h"
class FlatInput : public QLineEdit, public Animated {
Q_OBJECT
public:
FlatInput(QWidget *parent, const style::flatInput &st, const QString &ph = QString(), const QString &val = QString());
bool event(QEvent *e);
void touchEvent(QTouchEvent *e);
void paintEvent(QPaintEvent *e);
void focusInEvent(QFocusEvent *e);
void focusOutEvent(QFocusEvent *e);
void keyPressEvent(QKeyEvent *e);
2015-04-07 23:03:32 +00:00
void resizeEvent(QResizeEvent *e);
void notaBene();
void setPlaceholder(const QString &ph);
void setPlaceholderFast(bool fast);
void updatePlaceholder();
const QString &placeholder() const;
QRect placeholderRect() const;
QRect getTextRect() const;
bool animStep(float64 ms);
QSize sizeHint() const;
QSize minimumSizeHint() const;
void customUpDown(bool isCustom);
QString getLastText() const {
return text();
}
public slots:
void onTextChange(const QString &text);
void onTextEdited();
void onTouchTimer();
signals:
void changed();
void cancelled();
void accepted();
void focused();
void blurred();
protected:
virtual void correctValue(QKeyEvent *e, const QString &was);
style::font phFont() {
return _st.font;
}
void phPrepare(Painter &p);
private:
2015-04-07 23:03:32 +00:00
QString _ph, _fullph, _oldtext;
bool _fastph;
QKeyEvent *_kev;
bool _customUpDown;
bool _phVisible;
anim::ivalue a_phLeft;
anim::fvalue a_phAlpha;
anim::cvalue a_phColor, a_borderColor, a_bgColor;
int _notingBene;
style::flatInput _st;
style::font _font;
QTimer _touchTimer;
bool _touchPress, _touchRightButton, _touchMove;
QPoint _touchStart;
};
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(QKeyEvent *e, const QString &was);
private:
bool _nosignal;
};
2015-08-18 19:37:57 +00:00
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);
2015-08-18 19:37:57 +00:00
void mousePressEvent(QMouseEvent *e);
void contextMenuEvent(QContextMenuEvent *e);
void resizeEvent(QResizeEvent *e);
2015-08-18 16:10:01 +00:00
const QString &getLastText() const;
void updatePlaceholder();
2015-08-18 16:10:01 +00:00
int32 fakeMargin() const;
bool placeholderFgStep(float64 ms);
bool placeholderShiftStep(float64 ms);
bool borderStep(float64 ms);
QSize sizeHint() const;
QSize minimumSizeHint() const;
2015-08-18 16:10:01 +00:00
QString getText(int32 start = 0, int32 end = -1) const;
bool hasText() const;
2015-08-18 16:10:01 +00:00
bool isUndoAvailable() const;
bool isRedoAvailable() const;
2015-08-18 16:10:01 +00:00
void customUpDown(bool isCustom);
2015-08-18 19:37:57 +00:00
void setTextCursor(const QTextCursor &cursor) {
return _inner.setTextCursor(cursor);
}
QTextCursor textCursor() const {
return _inner.textCursor();
}
void setText(const QString &text) {
return _inner.setText(text);
}
void clear() {
return _inner.clear();
}
2015-08-18 16:10:01 +00:00
public slots:
void onTouchTimer();
2015-08-18 16:10:01 +00:00
void onDocumentContentsChange(int position, int charsRemoved, int charsAdded);
void onDocumentContentsChanged();
void onUndoAvailable(bool avail);
void onRedoAvailable(bool avail);
signals:
void changed();
2015-08-18 16:10:01 +00:00
void submitted(bool ctrlShiftEnter);
void cancelled();
2015-08-18 16:10:01 +00:00
void tabbed();
void focused();
void blurred();
protected:
virtual void correctValue(QKeyEvent *e, const QString &was);
2015-08-18 16:10:01 +00:00
void insertEmoji(EmojiPtr emoji, QTextCursor c);
TWidget *tparent() {
return qobject_cast<TWidget*>(parentWidget());
}
const TWidget *tparent() const {
return qobject_cast<const TWidget*>(parentWidget());
}
private:
2015-08-18 19:37:57 +00:00
friend class InputFieldInner;
class InputFieldInner : public QTextEdit {
public:
InputFieldInner(InputField *parent, const QString &val = QString());
bool viewportEvent(QEvent *e);
void focusInEvent(QFocusEvent *e);
void focusOutEvent(QFocusEvent *e);
void keyPressEvent(QKeyEvent *e);
void paintEvent(QPaintEvent *e);
QMimeData *createMimeDataFromSelection() const;
QVariant loadResource(int type, const QUrl &name);
private:
InputField *f() const {
return static_cast<InputField*>(parentWidget());
}
friend class InputField;
};
InputFieldInner _inner;
void focusInInner();
void focusOutInner();
2015-08-18 16:10:01 +00:00
void processDocumentContentsChange(int position, int charsAdded);
QString _oldtext;
QKeyEvent *_keyEvent;
2015-08-18 16:10:01 +00:00
bool _undoAvailable, _redoAvailable;
2015-08-18 16:10:01 +00:00
int32 _fakeMargin;
bool _customUpDown;
QString _placeholder, _placeholderFull;
bool _placeholderVisible;
anim::ivalue a_placeholderLeft;
anim::fvalue a_placeholderOpacity;
anim::cvalue a_placeholderFg;
Animation _placeholderFgAnim, _placeholderShiftAnim;
2015-08-18 16:10:01 +00:00
anim::fvalue a_borderOpacityActive;
anim::cvalue a_borderFg;
Animation _borderAnim;
2015-08-18 16:10:01 +00:00
bool _focused, _error;
2015-08-18 16:10:01 +00:00
const style::InputField *_st;
QTimer _touchTimer;
bool _touchPress, _touchRightButton, _touchMove;
QPoint _touchStart;
2015-08-18 16:10:01 +00:00
bool _replacingEmojis;
typedef QPair<int, int> Insertion;
typedef QList<Insertion> Insertions;
Insertions _insertions;
};