tdesktop/Telegram/SourceFiles/ui/flattextarea.h

172 lines
4.6 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.
2015-10-03 13:16:42 +00:00
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
2016-02-08 10:56:18 +00:00
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
*/
#pragma once
#include <QtWidgets/QTextEdit>
#include "ui/style.h"
#include "animation.h"
class UserData;
static UserData * const LookingUpInlineBot = SharedMemoryLocation<UserData, 0>();
2015-12-08 12:33:37 +00:00
class FlatTextarea : public QTextEdit {
Q_OBJECT
2015-09-16 13:04:08 +00:00
T_WIDGET
public:
FlatTextarea(QWidget *parent, const style::flatTextarea &st, const QString &ph = QString(), const QString &val = QString());
2015-04-04 20:01:34 +00:00
bool viewportEvent(QEvent *e) override;
void touchEvent(QTouchEvent *e);
void paintEvent(QPaintEvent *e) override;
void focusInEvent(QFocusEvent *e) override;
void focusOutEvent(QFocusEvent *e) override;
void keyPressEvent(QKeyEvent *e) override;
void resizeEvent(QResizeEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void dropEvent(QDropEvent *e) override;
void contextMenuEvent(QContextMenuEvent *e) override;
2015-09-16 21:15:13 +00:00
void setMaxLength(int32 maxLength);
2015-09-16 13:04:08 +00:00
void setMinHeight(int32 minHeight);
void setMaxHeight(int32 maxHeight);
2015-10-06 19:49:23 +00:00
const QString &getLastText() const {
return _oldtext;
}
void setPlaceholder(const QString &ph, int32 afterSymbols = 0);
void updatePlaceholder();
void finishPlaceholder();
QRect getTextRect() const;
int32 fakeMargin() const;
2015-12-08 12:33:37 +00:00
void step_appearance(float64 ms, bool timer);
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
EmojiPtr getSingleEmoji() const;
QString getMentionHashtagBotCommandPart(bool &start) const;
// Get the current inline bot and request string for it.
// The *outInlineBot can be filled by LookingUpInlineBot shared ptr.
// In that case the caller should lookup the bot by *outInlineBotUsername.
QString getInlineBotQuery(UserData **outInlineBot, QString *outInlineBotUsername) const;
void removeSingleEmoji();
bool hasText() const;
bool isUndoAvailable() const;
bool isRedoAvailable() const;
2015-04-06 22:15:29 +00:00
void parseLinks();
QStringList linksList() const;
void insertFromMimeData(const QMimeData *source) override;
2015-04-06 22:15:29 +00:00
QMimeData *createMimeDataFromSelection() const override;
enum class SubmitSettings {
None,
Enter,
CtrlEnter,
Both,
};
void setSubmitSettings(SubmitSettings settings);
2015-08-18 16:10:01 +00:00
void setTextFast(const QString &text, bool clearUndoHistory = true);
2015-10-17 14:52:26 +00:00
public slots:
void onTouchTimer();
void onDocumentContentsChange(int position, int charsRemoved, int charsAdded);
void onDocumentContentsChanged();
void onUndoAvailable(bool avail);
void onRedoAvailable(bool avail);
void onMentionHashtagOrBotCommandInsert(QString str);
signals:
2015-09-16 13:04:08 +00:00
void resized();
void changed();
void submitted(bool ctrlShiftEnter);
void cancelled();
void tabbed();
2015-04-06 22:15:29 +00:00
void spacedReturnedPasted();
void linksChanged();
protected:
2015-10-06 19:49:23 +00:00
QString getText(int32 start = 0, int32 end = -1) const;
virtual void correctValue(const QString &was, QString &now);
void insertEmoji(EmojiPtr emoji, QTextCursor c);
QVariant loadResource(int type, const QUrl &name) override;
2015-08-18 16:10:01 +00:00
2015-09-16 13:04:08 +00:00
void checkContentHeight();
private:
void getSingleEmojiFragment(QString &text, QTextFragment &fragment) const;
void processDocumentContentsChange(int position, int charsAdded);
2015-09-16 13:04:08 +00:00
bool heightAutoupdated();
int _minHeight = -1; // < 0 - no autosize
int _maxHeight = -1;
int _maxLength = -1;
SubmitSettings _submitSettings = SubmitSettings::Enter;
QString _ph, _phelided, _oldtext;
int _phAfter = 0;
bool _phVisible;
anim::ivalue a_phLeft;
anim::fvalue a_phAlpha;
anim::cvalue a_phColor;
2015-12-08 12:33:37 +00:00
Animation _a_appearance;
style::flatTextarea _st;
bool _undoAvailable = false;
bool _redoAvailable = false;
bool _inDrop = false;
bool _inHeightCheck = false;
int _fakeMargin = 0;
QTimer _touchTimer;
bool _touchPress = false;
bool _touchRightButton = false;
bool _touchMove = false;
QPoint _touchStart;
bool _correcting = false;
2015-04-06 22:15:29 +00:00
typedef QPair<int, int> LinkRange;
typedef QList<LinkRange> LinkRanges;
LinkRanges _links;
};