2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
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
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "flattextarea.h"
|
2016-04-04 21:09:46 +00:00
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
QByteArray FlatTextarea::serializeTagsList(const TagList &tags) {
|
2016-04-30 17:04:14 +00:00
|
|
|
if (tags.isEmpty()) {
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray tagsSerialized;
|
|
|
|
{
|
|
|
|
QBuffer buffer(&tagsSerialized);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
QDataStream stream(&buffer);
|
|
|
|
stream.setVersion(QDataStream::Qt_5_1);
|
|
|
|
stream << qint32(tags.size());
|
|
|
|
for_const (auto &tag, tags) {
|
|
|
|
stream << qint32(tag.offset) << qint32(tag.length) << tag.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tagsSerialized;
|
|
|
|
}
|
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
FlatTextarea::TagList FlatTextarea::deserializeTagsList(QByteArray data, int textLength) {
|
|
|
|
TagList result;
|
|
|
|
if (data.isEmpty()) {
|
|
|
|
return result;
|
|
|
|
}
|
2016-04-30 17:04:14 +00:00
|
|
|
|
|
|
|
QBuffer buffer(&data);
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
QDataStream stream(&buffer);
|
|
|
|
stream.setVersion(QDataStream::Qt_5_1);
|
|
|
|
|
|
|
|
qint32 tagCount = 0;
|
|
|
|
stream >> tagCount;
|
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
|
|
return result;
|
|
|
|
}
|
2016-05-05 16:04:17 +00:00
|
|
|
if (tagCount <= 0 || tagCount > textLength) {
|
2016-04-30 17:04:14 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < tagCount; ++i) {
|
|
|
|
qint32 offset = 0, length = 0;
|
|
|
|
QString id;
|
|
|
|
stream >> offset >> length >> id;
|
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
|
|
return result;
|
|
|
|
}
|
2016-05-05 16:04:17 +00:00
|
|
|
if (offset < 0 || length <= 0 || offset + length > textLength) {
|
2016-04-30 17:04:14 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result.push_back({ offset, length, id });
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
QString FlatTextarea::tagsMimeType() {
|
|
|
|
return qsl("application/x-td-field-tags");
|
|
|
|
}
|
2016-04-30 17:04:14 +00:00
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
FlatTextarea::FlatTextarea(QWidget *parent, const style::flatTextarea &st, const QString &pholder, const QString &v, const TagList &tags) : QTextEdit(parent)
|
2015-12-08 12:33:37 +00:00
|
|
|
, _phVisible(!v.length())
|
|
|
|
, a_phLeft(_phVisible ? 0 : st.phShift)
|
|
|
|
, a_phAlpha(_phVisible ? 1 : 0)
|
|
|
|
, a_phColor(st.phColor->c)
|
|
|
|
, _a_appearance(animation(this, &FlatTextarea::step_appearance))
|
2016-05-10 13:39:42 +00:00
|
|
|
, _lastTextWithTags { v, tags }
|
2016-04-09 11:02:50 +00:00
|
|
|
, _st(st) {
|
2014-05-30 08:53:19 +00:00
|
|
|
setAcceptRichText(false);
|
|
|
|
resize(_st.width, _st.font->height);
|
2015-12-31 15:27:21 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
setFont(_st.font->f);
|
|
|
|
setAlignment(_st.align);
|
2015-12-31 15:27:21 +00:00
|
|
|
|
2015-09-21 20:57:42 +00:00
|
|
|
setPlaceholder(pholder);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
QPalette p(palette());
|
|
|
|
p.setColor(QPalette::Text, _st.textColor->c);
|
|
|
|
setPalette(p);
|
|
|
|
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
|
|
|
setFrameStyle(QFrame::NoFrame | QFrame::Plain);
|
|
|
|
viewport()->setAutoFillBackground(false);
|
|
|
|
|
|
|
|
setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
|
|
|
switch (cScale()) {
|
|
|
|
case dbisOneAndQuarter: _fakeMargin = 1; break;
|
|
|
|
case dbisOneAndHalf: _fakeMargin = 2; break;
|
|
|
|
case dbisTwo: _fakeMargin = 4; break;
|
|
|
|
}
|
|
|
|
setStyleSheet(qsl("QTextEdit { margin: %1px; }").arg(_fakeMargin));
|
|
|
|
|
|
|
|
viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
|
|
|
|
_touchTimer.setSingleShot(true);
|
|
|
|
connect(&_touchTimer, SIGNAL(timeout()), this, SLOT(onTouchTimer()));
|
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
connect(document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(onDocumentContentsChange(int,int,int)));
|
2014-05-30 08:53:19 +00:00
|
|
|
connect(document(), SIGNAL(contentsChanged()), this, SLOT(onDocumentContentsChanged()));
|
2014-11-18 12:40:43 +00:00
|
|
|
connect(this, SIGNAL(undoAvailable(bool)), this, SLOT(onUndoAvailable(bool)));
|
|
|
|
connect(this, SIGNAL(redoAvailable(bool)), this, SLOT(onRedoAvailable(bool)));
|
|
|
|
if (App::wnd()) connect(this, SIGNAL(selectionChanged()), App::wnd(), SLOT(updateGlobalMenu()));
|
2015-09-21 20:57:42 +00:00
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
if (!_lastTextWithTags.text.isEmpty()) {
|
|
|
|
setTextWithTags(_lastTextWithTags, ClearUndoHistory);
|
2015-10-17 14:52:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
FlatTextarea::TextWithTags FlatTextarea::getTextWithTagsPart(int start, int end) {
|
|
|
|
TextWithTags result;
|
|
|
|
result.text = getTextPart(start, end, &result.tags);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::setTextWithTags(const TextWithTags &textWithTags, UndoHistoryAction undoHistoryAction) {
|
|
|
|
_insertedTags = textWithTags.tags;
|
|
|
|
_insertedTagsAreFromMime = false;
|
|
|
|
_realInsertPosition = 0;
|
|
|
|
_realCharsAdded = textWithTags.text.size();
|
|
|
|
auto doc = document();
|
|
|
|
auto cursor = QTextCursor(doc->docHandle(), 0);
|
|
|
|
if (undoHistoryAction == ClearUndoHistory) {
|
|
|
|
doc->setUndoRedoEnabled(false);
|
|
|
|
cursor.beginEditBlock();
|
|
|
|
} else if (undoHistoryAction == MergeWithUndoHistory) {
|
|
|
|
cursor.joinPreviousEditBlock();
|
2016-01-02 00:58:43 +00:00
|
|
|
} else {
|
2016-05-05 16:04:17 +00:00
|
|
|
cursor.beginEditBlock();
|
2015-12-31 15:27:21 +00:00
|
|
|
}
|
2016-05-05 16:04:17 +00:00
|
|
|
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
|
|
|
|
cursor.insertText(textWithTags.text);
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
cursor.endEditBlock();
|
|
|
|
if (undoHistoryAction == ClearUndoHistory) {
|
|
|
|
doc->setUndoRedoEnabled(true);
|
|
|
|
}
|
|
|
|
_insertedTags.clear();
|
|
|
|
_realInsertPosition = -1;
|
2015-12-31 15:27:21 +00:00
|
|
|
finishPlaceholder();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::finishPlaceholder() {
|
2015-12-08 12:33:37 +00:00
|
|
|
if (_a_appearance.animating()) {
|
2015-10-17 14:52:26 +00:00
|
|
|
a_phLeft.finish();
|
|
|
|
a_phAlpha.finish();
|
2015-12-08 12:33:37 +00:00
|
|
|
_a_appearance.stop();
|
2015-10-17 14:52:26 +00:00
|
|
|
update();
|
2015-09-21 20:57:42 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 21:15:13 +00:00
|
|
|
void FlatTextarea::setMaxLength(int32 maxLength) {
|
|
|
|
_maxLength = maxLength;
|
|
|
|
}
|
|
|
|
|
2015-09-16 13:04:08 +00:00
|
|
|
void FlatTextarea::setMinHeight(int32 minHeight) {
|
|
|
|
_minHeight = minHeight;
|
|
|
|
heightAutoupdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::setMaxHeight(int32 maxHeight) {
|
|
|
|
_maxHeight = maxHeight;
|
|
|
|
heightAutoupdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FlatTextarea::heightAutoupdated() {
|
2015-09-21 20:57:42 +00:00
|
|
|
if (_minHeight < 0 || _maxHeight < 0 || _inHeightCheck) return false;
|
|
|
|
_inHeightCheck = true;
|
|
|
|
|
|
|
|
myEnsureResized(this);
|
|
|
|
|
2015-09-16 13:04:08 +00:00
|
|
|
int newh = ceil(document()->size().height()) + 2 * fakeMargin();
|
|
|
|
if (newh > _maxHeight) {
|
|
|
|
newh = _maxHeight;
|
|
|
|
} else if (newh < _minHeight) {
|
|
|
|
newh = _minHeight;
|
|
|
|
}
|
|
|
|
if (height() != newh) {
|
|
|
|
resize(width(), newh);
|
2015-09-22 09:58:40 +00:00
|
|
|
_inHeightCheck = false;
|
2015-09-16 13:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-09-22 09:58:40 +00:00
|
|
|
_inHeightCheck = false;
|
2015-09-16 13:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void FlatTextarea::onTouchTimer() {
|
|
|
|
_touchRightButton = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FlatTextarea::viewportEvent(QEvent *e) {
|
|
|
|
if (e->type() == QEvent::TouchBegin || e->type() == QEvent::TouchUpdate || e->type() == QEvent::TouchEnd || e->type() == QEvent::TouchCancel) {
|
|
|
|
QTouchEvent *ev = static_cast<QTouchEvent*>(e);
|
|
|
|
if (ev->device()->type() == QTouchDevice::TouchScreen) {
|
|
|
|
touchEvent(ev);
|
|
|
|
return QTextEdit::viewportEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QTextEdit::viewportEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::touchEvent(QTouchEvent *e) {
|
|
|
|
switch (e->type()) {
|
|
|
|
case QEvent::TouchBegin:
|
|
|
|
if (_touchPress || e->touchPoints().isEmpty()) return;
|
|
|
|
_touchTimer.start(QApplication::startDragTime());
|
|
|
|
_touchPress = true;
|
|
|
|
_touchMove = _touchRightButton = false;
|
|
|
|
_touchStart = e->touchPoints().cbegin()->screenPos().toPoint();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::TouchUpdate:
|
|
|
|
if (!_touchPress || e->touchPoints().isEmpty()) return;
|
|
|
|
if (!_touchMove && (e->touchPoints().cbegin()->screenPos().toPoint() - _touchStart).manhattanLength() >= QApplication::startDragDistance()) {
|
|
|
|
_touchMove = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::TouchEnd:
|
|
|
|
if (!_touchPress) return;
|
|
|
|
if (!_touchMove && window()) {
|
|
|
|
Qt::MouseButton btn(_touchRightButton ? Qt::RightButton : Qt::LeftButton);
|
|
|
|
QPoint mapped(mapFromGlobal(_touchStart)), winMapped(window()->mapFromGlobal(_touchStart));
|
|
|
|
|
|
|
|
if (_touchRightButton) {
|
|
|
|
QContextMenuEvent contextEvent(QContextMenuEvent::Mouse, mapped, _touchStart);
|
|
|
|
contextMenuEvent(&contextEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_touchTimer.stop();
|
|
|
|
_touchPress = _touchMove = _touchRightButton = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::TouchCancel:
|
|
|
|
_touchPress = false;
|
|
|
|
_touchTimer.stop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect FlatTextarea::getTextRect() const {
|
|
|
|
return rect().marginsRemoved(_st.textMrg + st::textRectMargins);
|
|
|
|
}
|
|
|
|
|
2014-10-25 15:40:20 +00:00
|
|
|
int32 FlatTextarea::fakeMargin() const {
|
|
|
|
return _fakeMargin;
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void FlatTextarea::paintEvent(QPaintEvent *e) {
|
|
|
|
QPainter p(viewport());
|
2015-07-13 11:14:15 +00:00
|
|
|
QRect r(rect().intersected(e->rect()));
|
|
|
|
p.fillRect(r, _st.bgColor->b);
|
2014-05-30 08:53:19 +00:00
|
|
|
bool phDraw = _phVisible;
|
2015-12-08 12:33:37 +00:00
|
|
|
if (_a_appearance.animating()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
p.setOpacity(a_phAlpha.current());
|
|
|
|
phDraw = true;
|
|
|
|
}
|
|
|
|
if (phDraw) {
|
|
|
|
p.save();
|
2015-07-13 11:14:15 +00:00
|
|
|
p.setClipRect(r);
|
2015-12-31 15:27:21 +00:00
|
|
|
p.setFont(_st.font);
|
2014-05-30 08:53:19 +00:00
|
|
|
p.setPen(a_phColor.current());
|
2015-12-31 15:27:21 +00:00
|
|
|
if (_st.phAlign == style::al_topleft && _phAfter > 0) {
|
2016-05-05 16:04:17 +00:00
|
|
|
int skipWidth = _st.font->width(getTextWithTags().text.mid(0, _phAfter));
|
|
|
|
p.drawText(_st.textMrg.left() - _fakeMargin + a_phLeft.current() + skipWidth, _st.textMrg.top() - _fakeMargin - st::lineWidth + _st.font->ascent, _ph);
|
2015-12-31 15:27:21 +00:00
|
|
|
} else {
|
|
|
|
QRect phRect(_st.textMrg.left() - _fakeMargin + _st.phPos.x() + a_phLeft.current(), _st.textMrg.top() - _fakeMargin + _st.phPos.y(), width() - _st.textMrg.left() - _st.textMrg.right(), height() - _st.textMrg.top() - _st.textMrg.bottom());
|
|
|
|
p.drawText(phRect, _ph, QTextOption(_st.phAlign));
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
p.restore();
|
2015-10-15 10:18:24 +00:00
|
|
|
p.setOpacity(1);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
QTextEdit::paintEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::focusInEvent(QFocusEvent *e) {
|
|
|
|
a_phColor.start(_st.phFocusColor->c);
|
2015-12-08 12:33:37 +00:00
|
|
|
_a_appearance.start();
|
2014-05-30 08:53:19 +00:00
|
|
|
QTextEdit::focusInEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::focusOutEvent(QFocusEvent *e) {
|
|
|
|
a_phColor.start(_st.phColor->c);
|
2015-12-08 12:33:37 +00:00
|
|
|
_a_appearance.start();
|
2014-05-30 08:53:19 +00:00
|
|
|
QTextEdit::focusOutEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize FlatTextarea::sizeHint() const {
|
|
|
|
return geometry().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize FlatTextarea::minimumSizeHint() const {
|
|
|
|
return geometry().size();
|
|
|
|
}
|
|
|
|
|
2015-01-02 14:55:24 +00:00
|
|
|
EmojiPtr FlatTextarea::getSingleEmoji() const {
|
|
|
|
QString text;
|
|
|
|
QTextFragment fragment;
|
|
|
|
|
|
|
|
getSingleEmojiFragment(text, fragment);
|
2015-12-31 15:27:21 +00:00
|
|
|
|
2015-01-02 14:55:24 +00:00
|
|
|
if (!text.isEmpty()) {
|
2015-01-02 16:11:18 +00:00
|
|
|
QTextCharFormat format = fragment.charFormat();
|
2015-07-13 10:45:09 +00:00
|
|
|
QString imageName = static_cast<QTextImageFormat*>(&format)->name();
|
|
|
|
if (imageName.startsWith(qstr("emoji://e."))) {
|
|
|
|
return emojiFromUrl(imageName);
|
|
|
|
}
|
2015-01-02 14:55:24 +00:00
|
|
|
}
|
2016-05-04 16:46:24 +00:00
|
|
|
return nullptr;
|
2015-01-02 14:55:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 11:02:50 +00:00
|
|
|
QString FlatTextarea::getInlineBotQuery(UserData **outInlineBot, QString *outInlineBotUsername) const {
|
|
|
|
t_assert(outInlineBot != nullptr);
|
|
|
|
t_assert(outInlineBotUsername != nullptr);
|
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
auto &text = getTextWithTags().text;
|
2016-01-02 01:28:11 +00:00
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
int32 inlineUsernameStart = 1, inlineUsernameLength = 0, size = text.size();
|
2015-12-28 21:20:04 +00:00
|
|
|
if (size > 2 && text.at(0) == '@' && text.at(1).isLetter()) {
|
2015-12-30 19:09:20 +00:00
|
|
|
inlineUsernameLength = 1;
|
|
|
|
for (int32 i = inlineUsernameStart + 1, l = text.size(); i < l; ++i) {
|
2015-12-28 21:20:04 +00:00
|
|
|
if (text.at(i).isLetterOrNumber() || text.at(i).unicode() == '_') {
|
2015-12-30 19:09:20 +00:00
|
|
|
++inlineUsernameLength;
|
2015-12-28 21:20:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!text.at(i).isSpace()) {
|
2015-12-30 19:09:20 +00:00
|
|
|
inlineUsernameLength = 0;
|
2015-12-28 21:20:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-12-30 19:09:20 +00:00
|
|
|
if (inlineUsernameLength && inlineUsernameStart + inlineUsernameLength < text.size() && text.at(inlineUsernameStart + inlineUsernameLength).isSpace()) {
|
|
|
|
QStringRef username = text.midRef(inlineUsernameStart, inlineUsernameLength);
|
2016-04-09 11:02:50 +00:00
|
|
|
if (username != *outInlineBotUsername) {
|
|
|
|
*outInlineBotUsername = username.toString();
|
|
|
|
PeerData *peer = App::peerByName(*outInlineBotUsername);
|
2015-12-28 21:20:04 +00:00
|
|
|
if (peer) {
|
|
|
|
if (peer->isUser()) {
|
2016-04-09 11:02:50 +00:00
|
|
|
*outInlineBot = peer->asUser();
|
2015-12-28 21:20:04 +00:00
|
|
|
} else {
|
2016-04-09 11:02:50 +00:00
|
|
|
*outInlineBot = nullptr;
|
2015-12-28 21:20:04 +00:00
|
|
|
}
|
2015-12-28 22:06:27 +00:00
|
|
|
} else {
|
2016-04-09 11:02:50 +00:00
|
|
|
*outInlineBot = LookingUpInlineBot;
|
2015-12-28 21:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 11:02:50 +00:00
|
|
|
if (*outInlineBot == LookingUpInlineBot) return QString();
|
2015-12-28 22:06:27 +00:00
|
|
|
|
2016-04-09 11:02:50 +00:00
|
|
|
if (*outInlineBot && (!(*outInlineBot)->botInfo || (*outInlineBot)->botInfo->inlinePlaceholder.isEmpty())) {
|
|
|
|
*outInlineBot = nullptr;
|
2015-12-28 22:06:27 +00:00
|
|
|
} else {
|
2016-01-01 14:48:32 +00:00
|
|
|
return text.mid(inlineUsernameStart + inlineUsernameLength + 1);
|
2015-12-28 22:06:27 +00:00
|
|
|
}
|
2015-12-31 05:34:43 +00:00
|
|
|
} else {
|
|
|
|
inlineUsernameLength = 0;
|
2015-12-28 21:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-31 18:34:56 +00:00
|
|
|
if (inlineUsernameLength < 3) {
|
2016-04-09 11:02:50 +00:00
|
|
|
*outInlineBot = nullptr;
|
|
|
|
*outInlineBotUsername = QString();
|
2015-12-30 00:15:22 +00:00
|
|
|
}
|
2016-01-02 01:28:11 +00:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FlatTextarea::getMentionHashtagBotCommandPart(bool &start) const {
|
|
|
|
start = false;
|
2015-12-28 21:20:04 +00:00
|
|
|
|
2015-12-31 15:27:21 +00:00
|
|
|
int32 pos = textCursor().position();
|
2016-01-01 14:48:32 +00:00
|
|
|
if (textCursor().anchor() != pos) return QString();
|
2015-12-31 15:27:21 +00:00
|
|
|
|
2015-12-28 21:20:04 +00:00
|
|
|
// check mention / hashtag / bot command
|
2015-03-19 09:18:19 +00:00
|
|
|
QTextDocument *doc(document());
|
|
|
|
QTextBlock block = doc->findBlock(pos);
|
|
|
|
for (QTextBlock::Iterator iter = block.begin(); !iter.atEnd(); ++iter) {
|
|
|
|
QTextFragment fr(iter.fragment());
|
|
|
|
if (!fr.isValid()) continue;
|
|
|
|
|
|
|
|
int32 p = fr.position(), e = (p + fr.length());
|
|
|
|
if (p >= pos || e < pos) continue;
|
|
|
|
|
|
|
|
QTextCharFormat f = fr.charFormat();
|
|
|
|
if (f.isImageFormat()) continue;
|
|
|
|
|
2015-06-15 17:19:24 +00:00
|
|
|
bool mentionInCommand = false;
|
2015-03-19 09:18:19 +00:00
|
|
|
QString t(fr.text());
|
|
|
|
for (int i = pos - p; i > 0; --i) {
|
|
|
|
if (t.at(i - 1) == '@') {
|
2015-03-24 10:00:27 +00:00
|
|
|
if ((pos - p - i < 1 || t.at(i).isLetter()) && (i < 2 || !(t.at(i - 2).isLetterOrNumber() || t.at(i - 2) == '_'))) {
|
2016-01-01 14:48:32 +00:00
|
|
|
start = (i == 1) && (p == 0);
|
|
|
|
return t.mid(i - 1, pos - p - i + 1);
|
2015-06-15 17:19:24 +00:00
|
|
|
} else if ((pos - p - i < 1 || t.at(i).isLetter()) && i > 2 && (t.at(i - 2).isLetterOrNumber() || t.at(i - 2) == '_') && !mentionInCommand) {
|
|
|
|
mentionInCommand = true;
|
|
|
|
--i;
|
|
|
|
continue;
|
2015-03-24 10:00:27 +00:00
|
|
|
}
|
2016-01-01 14:48:32 +00:00
|
|
|
return QString();
|
2015-03-24 10:00:27 +00:00
|
|
|
} else if (t.at(i - 1) == '#') {
|
|
|
|
if (i < 2 || !(t.at(i - 2).isLetterOrNumber() || t.at(i - 2) == '_')) {
|
2016-01-01 14:48:32 +00:00
|
|
|
start = (i == 1) && (p == 0);
|
|
|
|
return t.mid(i - 1, pos - p - i + 1);
|
2015-03-24 10:00:27 +00:00
|
|
|
}
|
2016-01-01 14:48:32 +00:00
|
|
|
return QString();
|
2015-06-15 17:19:24 +00:00
|
|
|
} else if (t.at(i - 1) == '/') {
|
|
|
|
if (i < 2) {
|
2016-01-01 14:48:32 +00:00
|
|
|
start = (i == 1) && (p == 0);
|
|
|
|
return t.mid(i - 1, pos - p - i + 1);
|
2015-06-15 17:19:24 +00:00
|
|
|
}
|
2016-01-01 14:48:32 +00:00
|
|
|
return QString();
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
2015-06-15 17:19:24 +00:00
|
|
|
if (pos - p - i > 127 || (!mentionInCommand && (pos - p - i > 63))) break;
|
2015-03-19 09:18:19 +00:00
|
|
|
if (!t.at(i - 1).isLetterOrNumber() && t.at(i - 1) != '_') break;
|
|
|
|
}
|
2016-01-01 14:48:32 +00:00
|
|
|
break;
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
2016-01-01 14:48:32 +00:00
|
|
|
return QString();
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
void FlatTextarea::insertTag(const QString &text, QString tagId) {
|
|
|
|
auto cursor = textCursor();
|
|
|
|
int32 pos = cursor.position();
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
auto doc = document();
|
|
|
|
auto block = doc->findBlock(pos);
|
|
|
|
for (auto iter = block.begin(); !iter.atEnd(); ++iter) {
|
|
|
|
auto fragment = iter.fragment();
|
|
|
|
t_assert(fragment.isValid());
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
int fragmentPosition = fragment.position();
|
|
|
|
int fragmentEnd = (fragmentPosition + fragment.length());
|
|
|
|
if (fragmentPosition >= pos || fragmentEnd < pos) continue;
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
auto format = fragment.charFormat();
|
|
|
|
if (format.isImageFormat()) continue;
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2015-06-15 17:19:24 +00:00
|
|
|
bool mentionInCommand = false;
|
2016-05-04 20:38:37 +00:00
|
|
|
auto fragmentText = fragment.text();
|
|
|
|
for (int i = pos - fragmentPosition; i > 0; --i) {
|
|
|
|
auto previousChar = fragmentText.at(i - 1);
|
|
|
|
if (previousChar == '@' || previousChar == '#' || previousChar == '/') {
|
|
|
|
if ((i == pos - fragmentPosition || (previousChar == '/' ? fragmentText.at(i).isLetterOrNumber() : fragmentText.at(i).isLetter()) || previousChar == '#') &&
|
|
|
|
(i < 2 || !(fragmentText.at(i - 2).isLetterOrNumber() || fragmentText.at(i - 2) == '_'))) {
|
2016-05-06 17:33:48 +00:00
|
|
|
cursor.setPosition(fragmentPosition + i - 1);
|
2016-05-04 20:38:37 +00:00
|
|
|
int till = fragmentPosition + i;
|
2016-06-21 18:09:48 +00:00
|
|
|
for (; (till < fragmentEnd && till < pos); ++till) {
|
2016-05-08 16:11:47 +00:00
|
|
|
auto ch = fragmentText.at(till - fragmentPosition);
|
2016-06-21 18:09:48 +00:00
|
|
|
if (!ch.isLetterOrNumber() && ch != '_' && ch != '@') {
|
2015-03-19 09:18:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-08 16:11:47 +00:00
|
|
|
if (till < fragmentEnd && fragmentText.at(till - fragmentPosition) == ' ') {
|
2015-03-19 09:18:19 +00:00
|
|
|
++till;
|
|
|
|
}
|
2016-05-04 20:38:37 +00:00
|
|
|
cursor.setPosition(till, QTextCursor::KeepAnchor);
|
2016-04-29 13:46:16 +00:00
|
|
|
break;
|
2016-05-04 20:38:37 +00:00
|
|
|
} else if ((i == pos - fragmentPosition || fragmentText.at(i).isLetter()) && fragmentText.at(i - 1) == '@' && i > 2 && (fragmentText.at(i - 2).isLetterOrNumber() || fragmentText.at(i - 2) == '_') && !mentionInCommand) {
|
2015-06-15 17:19:24 +00:00
|
|
|
mentionInCommand = true;
|
|
|
|
--i;
|
|
|
|
continue;
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 20:38:37 +00:00
|
|
|
if (pos - fragmentPosition - i > 127 || (!mentionInCommand && (pos - fragmentPosition - i > 63))) break;
|
|
|
|
if (!fragmentText.at(i - 1).isLetterOrNumber() && fragmentText.at(i - 1) != '_') break;
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
2015-06-15 17:19:24 +00:00
|
|
|
break;
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
2016-04-30 17:04:14 +00:00
|
|
|
if (tagId.isEmpty()) {
|
2016-05-04 20:38:37 +00:00
|
|
|
QTextCharFormat format = cursor.charFormat();
|
2016-05-04 16:46:24 +00:00
|
|
|
format.setAnchor(false);
|
|
|
|
format.setAnchorName(QString());
|
|
|
|
format.clearForeground();
|
2016-05-04 20:38:37 +00:00
|
|
|
cursor.insertText(text + ' ', format);
|
2016-04-29 13:46:16 +00:00
|
|
|
} else {
|
2016-05-04 16:46:24 +00:00
|
|
|
_insertedTags.clear();
|
2016-05-04 20:38:37 +00:00
|
|
|
_insertedTags.push_back({ 0, text.size(), tagId });
|
2016-05-05 16:04:17 +00:00
|
|
|
_insertedTagsAreFromMime = false;
|
2016-05-04 20:38:37 +00:00
|
|
|
cursor.insertText(text + ' ');
|
2016-05-04 16:46:24 +00:00
|
|
|
_insertedTags.clear();
|
2016-04-29 13:46:16 +00:00
|
|
|
}
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
void FlatTextarea::setTagMimeProcessor(std_::unique_ptr<TagMimeProcessor> &&processor) {
|
|
|
|
_tagMimeProcessor = std_::move(processor);
|
|
|
|
}
|
|
|
|
|
2015-01-02 14:55:24 +00:00
|
|
|
void FlatTextarea::getSingleEmojiFragment(QString &text, QTextFragment &fragment) const {
|
|
|
|
int32 end = textCursor().position(), start = end - 1;
|
|
|
|
if (textCursor().anchor() != end) return;
|
|
|
|
|
|
|
|
if (start < 0) start = 0;
|
|
|
|
|
|
|
|
QTextDocument *doc(document());
|
|
|
|
QTextBlock from = doc->findBlock(start), till = doc->findBlock(end);
|
|
|
|
if (till.isValid()) till = till.next();
|
|
|
|
|
|
|
|
for (QTextBlock b = from; b != till; b = b.next()) {
|
|
|
|
for (QTextBlock::Iterator iter = b.begin(); !iter.atEnd(); ++iter) {
|
|
|
|
QTextFragment fr(iter.fragment());
|
|
|
|
if (!fr.isValid()) continue;
|
|
|
|
|
|
|
|
int32 p = fr.position(), e = (p + fr.length());
|
|
|
|
if (p >= end || e <= start) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextCharFormat f = fr.charFormat();
|
|
|
|
QString t(fr.text());
|
|
|
|
if (p < start) {
|
|
|
|
t = t.mid(start - p, end - start);
|
|
|
|
} else if (e > end) {
|
|
|
|
t = t.mid(0, end - p);
|
|
|
|
}
|
|
|
|
if (f.isImageFormat() && !t.isEmpty() && t.at(0).unicode() == QChar::ObjectReplacementCharacter) {
|
|
|
|
QString imageName = static_cast<QTextImageFormat*>(&f)->name();
|
2015-06-27 13:02:00 +00:00
|
|
|
if (imageName.startsWith(qstr("emoji://e."))) {
|
2015-01-02 14:55:24 +00:00
|
|
|
fragment = fr;
|
|
|
|
text = t;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::removeSingleEmoji() {
|
|
|
|
QString text;
|
|
|
|
QTextFragment fragment;
|
|
|
|
|
|
|
|
getSingleEmojiFragment(text, fragment);
|
|
|
|
|
|
|
|
if (!text.isEmpty()) {
|
|
|
|
QTextCursor t(textCursor());
|
|
|
|
t.setPosition(fragment.position());
|
|
|
|
t.setPosition(fragment.position() + fragment.length(), QTextCursor::KeepAnchor);
|
|
|
|
t.removeSelectedText();
|
|
|
|
setTextCursor(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TagAccumulator {
|
|
|
|
public:
|
|
|
|
TagAccumulator(FlatTextarea::TagList *tags) : _tags(tags) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool changed() const {
|
|
|
|
return _changed;
|
|
|
|
}
|
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
void feed(const QString &randomTagId, int currentPosition) {
|
|
|
|
if (randomTagId == _currentTagId) return;
|
2016-04-30 17:04:14 +00:00
|
|
|
|
|
|
|
if (!_currentTagId.isEmpty()) {
|
2016-05-04 20:38:37 +00:00
|
|
|
int randomPartPosition = _currentTagId.lastIndexOf('/');
|
|
|
|
t_assert(randomPartPosition > 0);
|
|
|
|
|
|
|
|
bool tagChanged = true;
|
|
|
|
if (_currentTag < _tags->size()) {
|
|
|
|
auto &alreadyTag = _tags->at(_currentTag);
|
|
|
|
if (alreadyTag.offset == _currentStart &&
|
|
|
|
alreadyTag.length == currentPosition - _currentStart &&
|
|
|
|
alreadyTag.id == _currentTagId.midRef(0, randomPartPosition)) {
|
|
|
|
tagChanged = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tagChanged) {
|
2016-04-30 17:04:14 +00:00
|
|
|
_changed = true;
|
2016-05-04 20:38:37 +00:00
|
|
|
FlatTextarea::Tag tag = {
|
|
|
|
_currentStart,
|
|
|
|
currentPosition - _currentStart,
|
|
|
|
_currentTagId.mid(0, randomPartPosition),
|
|
|
|
};
|
|
|
|
if (_currentTag < _tags->size()) {
|
|
|
|
(*_tags)[_currentTag] = tag;
|
|
|
|
} else {
|
|
|
|
_tags->push_back(tag);
|
|
|
|
}
|
2016-04-30 17:04:14 +00:00
|
|
|
}
|
|
|
|
++_currentTag;
|
|
|
|
}
|
2016-05-04 20:38:37 +00:00
|
|
|
_currentTagId = randomTagId;
|
2016-04-30 17:04:14 +00:00
|
|
|
_currentStart = currentPosition;
|
|
|
|
};
|
|
|
|
|
2016-05-06 17:33:48 +00:00
|
|
|
void finish() {
|
|
|
|
if (_currentTag < _tags->size()) {
|
|
|
|
_tags->resize(_currentTag);
|
|
|
|
_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
private:
|
|
|
|
FlatTextarea::TagList *_tags;
|
|
|
|
bool _changed = false;
|
|
|
|
|
|
|
|
int _currentTag = 0;
|
|
|
|
int _currentStart = 0;
|
|
|
|
QString _currentTagId;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
QString FlatTextarea::getTextPart(int start, int end, TagList *outTagsList, bool *outTagsChanged) const {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (end >= 0 && end <= start) return QString();
|
|
|
|
|
|
|
|
if (start < 0) start = 0;
|
|
|
|
bool full = (start == 0) && (end < 0);
|
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
TagAccumulator tagAccumulator(outTagsList);
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
QTextDocument *doc(document());
|
|
|
|
QTextBlock from = full ? doc->begin() : doc->findBlock(start), till = (end < 0) ? doc->end() : doc->findBlock(end);
|
|
|
|
if (till.isValid()) till = till.next();
|
|
|
|
|
|
|
|
int32 possibleLen = 0;
|
|
|
|
for (QTextBlock b = from; b != till; b = b.next()) {
|
|
|
|
possibleLen += b.length();
|
|
|
|
}
|
|
|
|
QString result;
|
|
|
|
result.reserve(possibleLen + 1);
|
|
|
|
if (!full && end < 0) {
|
|
|
|
end = possibleLen;
|
|
|
|
}
|
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
bool tillFragmentEnd = full;
|
|
|
|
for (auto b = from; b != till; b = b.next()) {
|
|
|
|
for (auto iter = b.begin(); !iter.atEnd(); ++iter) {
|
2014-05-30 08:53:19 +00:00
|
|
|
QTextFragment fragment(iter.fragment());
|
|
|
|
if (!fragment.isValid()) continue;
|
|
|
|
|
|
|
|
int32 p = full ? 0 : fragment.position(), e = full ? 0 : (p + fragment.length());
|
|
|
|
if (!full) {
|
2016-04-30 17:04:14 +00:00
|
|
|
tillFragmentEnd = (e <= end);
|
2016-05-05 16:04:17 +00:00
|
|
|
if (p == end) {
|
2016-04-30 17:04:14 +00:00
|
|
|
tagAccumulator.feed(fragment.charFormat().anchorName(), result.size());
|
|
|
|
}
|
|
|
|
if (p >= end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (e <= start) {
|
2014-05-30 08:53:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 16:04:17 +00:00
|
|
|
if (full || p >= start) {
|
2016-04-30 17:04:14 +00:00
|
|
|
tagAccumulator.feed(fragment.charFormat().anchorName(), result.size());
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
QTextCharFormat f = fragment.charFormat();
|
|
|
|
QString emojiText;
|
|
|
|
QString t(fragment.text());
|
|
|
|
if (!full) {
|
|
|
|
if (p < start) {
|
2014-11-12 20:18:00 +00:00
|
|
|
t = t.mid(start - p, end - start);
|
2014-05-30 08:53:19 +00:00
|
|
|
} else if (e > end) {
|
|
|
|
t = t.mid(0, end - p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QChar *ub = t.data(), *uc = ub, *ue = uc + t.size();
|
|
|
|
for (; uc != ue; ++uc) {
|
|
|
|
switch (uc->unicode()) {
|
|
|
|
case 0xfdd0: // QTextBeginningOfFrame
|
|
|
|
case 0xfdd1: // QTextEndOfFrame
|
|
|
|
case QChar::ParagraphSeparator:
|
|
|
|
case QChar::LineSeparator:
|
|
|
|
*uc = QLatin1Char('\n');
|
|
|
|
break;
|
|
|
|
case QChar::Nbsp:
|
|
|
|
*uc = QLatin1Char(' ');
|
|
|
|
break;
|
|
|
|
case QChar::ObjectReplacementCharacter:
|
|
|
|
if (emojiText.isEmpty() && f.isImageFormat()) {
|
|
|
|
QString imageName = static_cast<QTextImageFormat*>(&f)->name();
|
2015-06-27 13:02:00 +00:00
|
|
|
if (imageName.startsWith(qstr("emoji://e."))) {
|
2015-05-08 12:45:14 +00:00
|
|
|
if (EmojiPtr emoji = emojiFromUrl(imageName)) {
|
2015-05-12 15:01:49 +00:00
|
|
|
emojiText = emojiString(emoji);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uc > ub) result.append(ub, uc - ub);
|
|
|
|
if (!emojiText.isEmpty()) result.append(emojiText);
|
|
|
|
ub = uc + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uc > ub) result.append(ub, uc - ub);
|
|
|
|
}
|
|
|
|
result.append('\n');
|
|
|
|
}
|
|
|
|
result.chop(1);
|
2016-04-30 17:04:14 +00:00
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
if (tillFragmentEnd) tagAccumulator.feed(QString(), result.size());
|
2016-05-06 17:33:48 +00:00
|
|
|
tagAccumulator.finish();
|
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
if (outTagsChanged) {
|
|
|
|
*outTagsChanged = tagAccumulator.changed();
|
2016-04-30 17:04:14 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FlatTextarea::hasText() const {
|
|
|
|
QTextDocument *doc(document());
|
|
|
|
QTextBlock from = doc->begin(), till = doc->end();
|
|
|
|
|
|
|
|
if (from == till) return false;
|
|
|
|
|
|
|
|
for (QTextBlock::Iterator iter = from.begin(); !iter.atEnd(); ++iter) {
|
|
|
|
QTextFragment fragment(iter.fragment());
|
|
|
|
if (!fragment.isValid()) continue;
|
|
|
|
if (!fragment.text().isEmpty()) return true;
|
|
|
|
}
|
|
|
|
return (from.next() != till);
|
|
|
|
}
|
|
|
|
|
2014-11-18 12:40:43 +00:00
|
|
|
bool FlatTextarea::isUndoAvailable() const {
|
|
|
|
return _undoAvailable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FlatTextarea::isRedoAvailable() const {
|
|
|
|
return _redoAvailable;
|
|
|
|
}
|
|
|
|
|
2015-04-06 22:15:29 +00:00
|
|
|
void FlatTextarea::parseLinks() { // some code is duplicated in text.cpp!
|
|
|
|
LinkRanges newLinks;
|
|
|
|
|
|
|
|
QString text(toPlainText());
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
if (!_links.isEmpty()) {
|
|
|
|
_links.clear();
|
|
|
|
emit linksChanged();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
initLinkSets();
|
|
|
|
|
|
|
|
int32 len = text.size();
|
|
|
|
const QChar *start = text.unicode(), *end = start + text.size();
|
|
|
|
for (int32 offset = 0, matchOffset = offset; offset < len;) {
|
|
|
|
QRegularExpressionMatch m = reDomain().match(text, matchOffset);
|
|
|
|
if (!m.hasMatch()) break;
|
|
|
|
|
|
|
|
int32 domainOffset = m.capturedStart();
|
|
|
|
|
|
|
|
QString protocol = m.captured(1).toLower();
|
|
|
|
QString topDomain = m.captured(3).toLower();
|
|
|
|
|
|
|
|
bool isProtocolValid = protocol.isEmpty() || validProtocols().contains(hashCrc32(protocol.constData(), protocol.size() * sizeof(QChar)));
|
|
|
|
bool isTopDomainValid = !protocol.isEmpty() || validTopDomains().contains(hashCrc32(topDomain.constData(), topDomain.size() * sizeof(QChar)));
|
|
|
|
|
|
|
|
if (protocol.isEmpty() && domainOffset > offset + 1 && *(start + domainOffset - 1) == QChar('@')) {
|
|
|
|
QString forMailName = text.mid(offset, domainOffset - offset - 1);
|
|
|
|
QRegularExpressionMatch mMailName = reMailName().match(forMailName);
|
|
|
|
if (mMailName.hasMatch()) {
|
|
|
|
offset = matchOffset = m.capturedEnd();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isProtocolValid || !isTopDomainValid) {
|
|
|
|
offset = matchOffset = m.capturedEnd();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStack<const QChar*> parenth;
|
|
|
|
const QChar *domainEnd = start + m.capturedEnd(), *p = domainEnd;
|
|
|
|
for (; p < end; ++p) {
|
|
|
|
QChar ch(*p);
|
|
|
|
if (chIsLinkEnd(ch)) break; // link finished
|
|
|
|
if (chIsAlmostLinkEnd(ch)) {
|
|
|
|
const QChar *endTest = p + 1;
|
|
|
|
while (endTest < end && chIsAlmostLinkEnd(*endTest)) {
|
|
|
|
++endTest;
|
|
|
|
}
|
|
|
|
if (endTest >= end || chIsLinkEnd(*endTest)) {
|
|
|
|
break; // link finished at p
|
|
|
|
}
|
|
|
|
p = endTest;
|
|
|
|
ch = *p;
|
|
|
|
}
|
|
|
|
if (ch == '(' || ch == '[' || ch == '{' || ch == '<') {
|
|
|
|
parenth.push(p);
|
|
|
|
} else if (ch == ')' || ch == ']' || ch == '}' || ch == '>') {
|
|
|
|
if (parenth.isEmpty()) break;
|
|
|
|
const QChar *q = parenth.pop(), open(*q);
|
|
|
|
if ((ch == ')' && open != '(') || (ch == ']' && open != '[') || (ch == '}' && open != '{') || (ch == '>' && open != '<')) {
|
|
|
|
p = q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p > domainEnd) { // check, that domain ended
|
|
|
|
if (domainEnd->unicode() != '/' && domainEnd->unicode() != '?') {
|
|
|
|
matchOffset = domainEnd - start;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-05-10 13:39:42 +00:00
|
|
|
newLinks.push_back({ domainOffset - 1, static_cast<int>(p - start - domainOffset + 2) });
|
2015-04-06 22:15:29 +00:00
|
|
|
offset = matchOffset = p - start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newLinks != _links) {
|
|
|
|
_links = newLinks;
|
|
|
|
emit linksChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FlatTextarea::linksList() const {
|
|
|
|
QStringList result;
|
|
|
|
if (!_links.isEmpty()) {
|
|
|
|
QString text(toPlainText());
|
2016-05-04 20:38:37 +00:00
|
|
|
for_const (auto &link, _links) {
|
|
|
|
result.push_back(text.mid(link.start + 1, link.length - 2));
|
2015-04-06 22:15:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::insertFromMimeData(const QMimeData *source) {
|
2016-05-05 16:04:17 +00:00
|
|
|
auto mime = tagsMimeType();
|
2016-05-04 16:46:24 +00:00
|
|
|
auto text = source->text();
|
2016-04-30 17:04:14 +00:00
|
|
|
if (source->hasFormat(mime)) {
|
|
|
|
auto tagsData = source->data(mime);
|
2016-05-04 16:46:24 +00:00
|
|
|
_insertedTags = deserializeTagsList(tagsData, text.size());
|
2016-05-05 16:04:17 +00:00
|
|
|
_insertedTagsAreFromMime = true;
|
2016-04-30 17:04:14 +00:00
|
|
|
} else {
|
2016-05-04 16:46:24 +00:00
|
|
|
_insertedTags.clear();
|
2016-04-30 17:04:14 +00:00
|
|
|
}
|
2016-05-06 17:33:48 +00:00
|
|
|
auto cursor = textCursor();
|
|
|
|
_realInsertPosition = qMin(cursor.position(), cursor.anchor());
|
2016-05-04 16:46:24 +00:00
|
|
|
_realCharsAdded = text.size();
|
2015-04-06 22:15:29 +00:00
|
|
|
QTextEdit::insertFromMimeData(source);
|
2016-05-04 16:46:24 +00:00
|
|
|
if (!_inDrop) {
|
|
|
|
emit spacedReturnedPasted();
|
|
|
|
_insertedTags.clear();
|
|
|
|
_realInsertPosition = -1;
|
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void FlatTextarea::insertEmoji(EmojiPtr emoji, QTextCursor c) {
|
|
|
|
QTextImageFormat imageFormat;
|
2015-07-13 10:45:09 +00:00
|
|
|
int32 ew = ESize + st::emojiPadding * cIntRetinaFactor() * 2, eh = _st.font->height * cIntRetinaFactor();
|
|
|
|
imageFormat.setWidth(ew / cIntRetinaFactor());
|
|
|
|
imageFormat.setHeight(eh / cIntRetinaFactor());
|
|
|
|
imageFormat.setName(qsl("emoji://e.") + QString::number(emojiKey(emoji), 16));
|
2014-05-30 08:53:19 +00:00
|
|
|
imageFormat.setVerticalAlignment(QTextCharFormat::AlignBaseline);
|
2016-05-04 16:46:24 +00:00
|
|
|
if (c.charFormat().isAnchor()) {
|
|
|
|
imageFormat.setAnchor(true);
|
|
|
|
imageFormat.setAnchorName(c.charFormat().anchorName());
|
|
|
|
imageFormat.setForeground(st::defaultTextStyle.linkFg);
|
|
|
|
}
|
2015-07-13 10:45:09 +00:00
|
|
|
static QString objectReplacement(QChar::ObjectReplacementCharacter);
|
|
|
|
c.insertText(objectReplacement, imageFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FlatTextarea::loadResource(int type, const QUrl &name) {
|
|
|
|
QString imageName = name.toDisplayString();
|
|
|
|
if (imageName.startsWith(qstr("emoji://e."))) {
|
|
|
|
if (EmojiPtr emoji = emojiFromUrl(imageName)) {
|
|
|
|
return QVariant(App::emojiSingle(emoji, _st.font->height));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 13:04:08 +00:00
|
|
|
void FlatTextarea::checkContentHeight() {
|
|
|
|
if (heightAutoupdated()) {
|
|
|
|
emit resized();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Optimization: with null page size document does not re-layout
|
|
|
|
// on each insertText / mergeCharFormat.
|
|
|
|
void prepareFormattingOptimization(QTextDocument *document) {
|
|
|
|
if (!document->pageSize().isNull()) {
|
|
|
|
document->setPageSize(QSizeF(0, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeTags(QTextDocument *document, int from, int end) {
|
2016-05-06 17:33:48 +00:00
|
|
|
QTextCursor c(document->docHandle(), 0);
|
|
|
|
c.setPosition(from);
|
2016-05-04 16:46:24 +00:00
|
|
|
c.setPosition(end, QTextCursor::KeepAnchor);
|
|
|
|
|
|
|
|
QTextCharFormat format;
|
|
|
|
format.setAnchor(false);
|
|
|
|
format.setAnchorName(QString());
|
|
|
|
format.setForeground(st::black);
|
|
|
|
c.mergeCharFormat(format);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the position of the first inserted tag or "changedEnd" value if none found.
|
2016-05-04 20:38:37 +00:00
|
|
|
int processInsertedTags(QTextDocument *document, int changedPosition, int changedEnd, const FlatTextarea::TagList &tags, FlatTextarea::TagMimeProcessor *processor) {
|
2016-05-04 16:46:24 +00:00
|
|
|
int firstTagStart = changedEnd;
|
|
|
|
int applyNoTagFrom = changedEnd;
|
|
|
|
for_const (auto &tag, tags) {
|
|
|
|
int tagFrom = changedPosition + tag.offset;
|
|
|
|
int tagTo = tagFrom + tag.length;
|
|
|
|
accumulate_max(tagFrom, changedPosition);
|
|
|
|
accumulate_min(tagTo, changedEnd);
|
2016-05-04 20:38:37 +00:00
|
|
|
auto tagId = processor ? processor->tagFromMimeTag(tag.id) : tag.id;
|
|
|
|
if (tagTo > tagFrom && !tagId.isEmpty()) {
|
2016-05-04 16:46:24 +00:00
|
|
|
accumulate_min(firstTagStart, tagFrom);
|
|
|
|
|
|
|
|
prepareFormattingOptimization(document);
|
|
|
|
|
|
|
|
if (applyNoTagFrom < tagFrom) {
|
|
|
|
removeTags(document, applyNoTagFrom, tagFrom);
|
|
|
|
}
|
2016-05-06 17:33:48 +00:00
|
|
|
QTextCursor c(document->docHandle(), 0);
|
|
|
|
c.setPosition(tagFrom);
|
2016-05-04 16:46:24 +00:00
|
|
|
c.setPosition(tagTo, QTextCursor::KeepAnchor);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
QTextCharFormat format;
|
|
|
|
format.setAnchor(true);
|
2016-05-04 20:38:37 +00:00
|
|
|
format.setAnchorName(tagId + '/' + QString::number(rand_value<uint32>()));
|
2016-05-04 16:46:24 +00:00
|
|
|
format.setForeground(st::defaultTextStyle.linkFg);
|
|
|
|
c.mergeCharFormat(format);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
applyNoTagFrom = tagTo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (applyNoTagFrom < changedEnd) {
|
|
|
|
removeTags(document, applyNoTagFrom, changedEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstTagStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When inserting a part of text inside a tag we need to have
|
|
|
|
// a way to know if the insertion replaced the end of the tag
|
|
|
|
// or it was strictly inside (in the middle) of the tag.
|
|
|
|
bool wasInsertTillTheEndOfTag(QTextBlock block, QTextBlock::iterator fragmentIt, int insertionEnd) {
|
|
|
|
auto insertTagName = fragmentIt.fragment().charFormat().anchorName();
|
|
|
|
while (true) {
|
|
|
|
for (; !fragmentIt.atEnd(); ++fragmentIt) {
|
|
|
|
auto fragment = fragmentIt.fragment();
|
|
|
|
bool fragmentOutsideInsertion = (fragment.position() >= insertionEnd);
|
|
|
|
if (fragmentOutsideInsertion) {
|
|
|
|
return (fragment.charFormat().anchorName() != insertTagName);
|
|
|
|
}
|
|
|
|
int fragmentEnd = fragment.position() + fragment.length();
|
|
|
|
bool notFullFragmentInserted = (fragmentEnd > insertionEnd);
|
|
|
|
if (notFullFragmentInserted) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (block.isValid()) {
|
|
|
|
fragmentIt = block.begin();
|
|
|
|
block = block.next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Insertion goes till the end of the text => not strictly inside a tag.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FormattingAction {
|
|
|
|
enum class Type {
|
|
|
|
Invalid,
|
|
|
|
InsertEmoji,
|
|
|
|
TildeFont,
|
|
|
|
RemoveTag,
|
|
|
|
};
|
|
|
|
Type type = Type::Invalid;
|
|
|
|
EmojiPtr emoji = nullptr;
|
|
|
|
bool isTilde = false;
|
|
|
|
int intervalStart = 0;
|
|
|
|
int intervalEnd = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
void FlatTextarea::processFormatting(int insertPosition, int insertEnd) {
|
2016-05-04 16:46:24 +00:00
|
|
|
// Tilde formatting.
|
|
|
|
auto regularFont = qsl("Open Sans"), semiboldFont = qsl("Open Sans Semibold");
|
|
|
|
bool tildeFormatting = !cRetina() && (font().pixelSize() == 13) && (font().family() == regularFont);
|
|
|
|
bool isTildeFragment = false;
|
|
|
|
|
|
|
|
// First tag handling (the one we inserted text to).
|
|
|
|
bool startTagFound = false;
|
|
|
|
bool breakTagOnNotLetter = false;
|
|
|
|
|
|
|
|
auto doc = document();
|
|
|
|
|
|
|
|
// Apply inserted tags.
|
2016-05-05 16:04:17 +00:00
|
|
|
auto insertedTagsProcessor = _insertedTagsAreFromMime ? _tagMimeProcessor.get() : nullptr;
|
|
|
|
int breakTagOnNotLetterTill = processInsertedTags(doc, insertPosition, insertEnd,
|
|
|
|
_insertedTags, insertedTagsProcessor);
|
2016-05-04 16:46:24 +00:00
|
|
|
using ActionType = FormattingAction::Type;
|
2014-05-30 08:53:19 +00:00
|
|
|
while (true) {
|
2016-05-04 16:46:24 +00:00
|
|
|
FormattingAction action;
|
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
auto fromBlock = doc->findBlock(insertPosition);
|
|
|
|
auto tillBlock = doc->findBlock(insertEnd);
|
2016-05-04 16:46:24 +00:00
|
|
|
if (tillBlock.isValid()) tillBlock = tillBlock.next();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
for (auto block = fromBlock; block != tillBlock; block = block.next()) {
|
|
|
|
for (auto fragmentIt = block.begin(); !fragmentIt.atEnd(); ++fragmentIt) {
|
|
|
|
auto fragment = fragmentIt.fragment();
|
|
|
|
t_assert(fragment.isValid());
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
int fragmentPosition = fragment.position();
|
2016-05-04 20:38:37 +00:00
|
|
|
if (insertPosition >= fragmentPosition + fragment.length()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-04 20:38:37 +00:00
|
|
|
int changedPositionInFragment = insertPosition - fragmentPosition; // Can be negative.
|
|
|
|
int changedEndInFragment = insertEnd - fragmentPosition;
|
2016-05-04 16:46:24 +00:00
|
|
|
if (changedEndInFragment <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2015-11-04 23:46:01 +00:00
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
auto charFormat = fragment.charFormat();
|
|
|
|
if (tildeFormatting) {
|
|
|
|
isTildeFragment = (charFormat.fontFamily() == semiboldFont);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto fragmentText = fragment.text();
|
|
|
|
auto *textStart = fragmentText.constData();
|
|
|
|
auto *textEnd = textStart + fragmentText.size();
|
|
|
|
|
|
|
|
if (!startTagFound) {
|
|
|
|
startTagFound = true;
|
|
|
|
auto tagName = charFormat.anchorName();
|
|
|
|
if (!tagName.isEmpty()) {
|
2016-05-04 20:38:37 +00:00
|
|
|
breakTagOnNotLetter = wasInsertTillTheEndOfTag(block, fragmentIt, insertEnd);
|
2016-05-04 16:46:24 +00:00
|
|
|
}
|
2015-11-04 23:46:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
auto *ch = textStart + qMax(changedPositionInFragment, 0);
|
|
|
|
for (; ch < textEnd; ++ch) {
|
|
|
|
int emojiLength = 0;
|
|
|
|
if (auto emoji = emojiFromText(ch, textEnd, &emojiLength)) {
|
|
|
|
// Replace emoji if no current action is prepared.
|
|
|
|
if (action.type == ActionType::Invalid) {
|
|
|
|
action.type = ActionType::InsertEmoji;
|
|
|
|
action.emoji = emoji;
|
|
|
|
action.intervalStart = fragmentPosition + (ch - textStart);
|
|
|
|
action.intervalEnd = action.intervalStart + emojiLength;
|
2015-11-04 23:46:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
if (breakTagOnNotLetter && !ch->isLetter()) {
|
|
|
|
// Remove tag name till the end if no current action is prepared.
|
|
|
|
if (action.type != ActionType::Invalid) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
breakTagOnNotLetter = false;
|
|
|
|
if (fragmentPosition + (ch - textStart) < breakTagOnNotLetterTill) {
|
|
|
|
action.type = ActionType::RemoveTag;
|
|
|
|
action.intervalStart = fragmentPosition + (ch - textStart);
|
|
|
|
action.intervalEnd = breakTagOnNotLetterTill;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tildeFormatting) { // Tilde symbol fix in OpenSans.
|
2015-10-28 03:43:42 +00:00
|
|
|
bool tilde = (ch->unicode() == '~');
|
2016-05-04 16:46:24 +00:00
|
|
|
if ((tilde && !isTildeFragment) || (!tilde && isTildeFragment)) {
|
|
|
|
if (action.type == ActionType::Invalid) {
|
|
|
|
action.type = ActionType::TildeFont;
|
|
|
|
action.intervalStart = fragmentPosition + (ch - textStart);
|
|
|
|
action.intervalEnd = action.intervalStart + 1;
|
|
|
|
action.isTilde = tilde;
|
2015-11-04 23:46:01 +00:00
|
|
|
} else {
|
2016-05-04 16:46:24 +00:00
|
|
|
++action.intervalEnd;
|
2015-10-28 03:43:42 +00:00
|
|
|
}
|
2016-05-04 16:46:24 +00:00
|
|
|
} else if (action.type == ActionType::TildeFont) {
|
2015-10-28 03:43:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
if (ch + 1 < textEnd && ch->isHighSurrogate() && (ch + 1)->isLowSurrogate()) {
|
2015-10-28 03:43:42 +00:00
|
|
|
++ch;
|
2016-05-04 16:46:24 +00:00
|
|
|
++fragmentPosition;
|
2015-10-28 03:43:42 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-05-04 16:46:24 +00:00
|
|
|
if (action.type != ActionType::Invalid) break;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-05-04 16:46:24 +00:00
|
|
|
if (action.type != ActionType::Invalid) break;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-05-04 16:46:24 +00:00
|
|
|
if (action.type != ActionType::Invalid) {
|
|
|
|
prepareFormattingOptimization(doc);
|
|
|
|
|
2016-05-06 17:33:48 +00:00
|
|
|
QTextCursor c(doc->docHandle(), 0);
|
|
|
|
c.setPosition(action.intervalStart);
|
2016-05-04 16:46:24 +00:00
|
|
|
c.setPosition(action.intervalEnd, QTextCursor::KeepAnchor);
|
|
|
|
if (action.type == ActionType::InsertEmoji) {
|
|
|
|
insertEmoji(action.emoji, c);
|
2016-05-04 20:38:37 +00:00
|
|
|
insertPosition = action.intervalStart + 1;
|
2016-05-04 16:46:24 +00:00
|
|
|
} else if (action.type == ActionType::RemoveTag) {
|
2015-11-04 23:46:01 +00:00
|
|
|
QTextCharFormat format;
|
2016-05-04 16:46:24 +00:00
|
|
|
format.setAnchor(false);
|
|
|
|
format.setAnchorName(QString());
|
|
|
|
format.setForeground(st::black);
|
2015-11-04 23:46:01 +00:00
|
|
|
c.mergeCharFormat(format);
|
2016-05-04 16:46:24 +00:00
|
|
|
} else if (action.type == ActionType::TildeFont) {
|
|
|
|
QTextCharFormat format;
|
|
|
|
format.setFontFamily(action.isTilde ? semiboldFont : regularFont);
|
|
|
|
c.mergeCharFormat(format);
|
2016-05-04 20:38:37 +00:00
|
|
|
insertPosition = action.intervalEnd;
|
2015-07-13 10:45:09 +00:00
|
|
|
}
|
2015-12-31 15:27:21 +00:00
|
|
|
} else {
|
2014-05-30 08:53:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::onDocumentContentsChange(int position, int charsRemoved, int charsAdded) {
|
2015-10-06 19:49:23 +00:00
|
|
|
if (_correcting) return;
|
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
int insertPosition = (_realInsertPosition >= 0) ? _realInsertPosition : position;
|
|
|
|
int insertLength = (_realInsertPosition >= 0) ? _realCharsAdded : charsAdded;
|
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
int removePosition = position;
|
|
|
|
int removeLength = charsRemoved;
|
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
QTextCursor(document()->docHandle(), 0).joinPreviousEditBlock();
|
2015-07-13 10:45:09 +00:00
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
_correcting = true;
|
2015-09-16 21:15:13 +00:00
|
|
|
if (_maxLength >= 0) {
|
|
|
|
QTextCursor c(document()->docHandle(), 0);
|
|
|
|
c.movePosition(QTextCursor::End);
|
|
|
|
int32 fullSize = c.position(), toRemove = fullSize - _maxLength;
|
|
|
|
if (toRemove > 0) {
|
2016-05-04 16:46:24 +00:00
|
|
|
if (toRemove > insertLength) {
|
|
|
|
if (insertLength) {
|
|
|
|
c.setPosition(insertPosition);
|
|
|
|
c.setPosition((insertPosition + insertLength), QTextCursor::KeepAnchor);
|
2015-09-16 21:15:13 +00:00
|
|
|
c.removeSelectedText();
|
|
|
|
}
|
2016-05-04 16:46:24 +00:00
|
|
|
c.setPosition(fullSize - (toRemove - insertLength));
|
2015-09-16 21:15:13 +00:00
|
|
|
c.setPosition(fullSize, QTextCursor::KeepAnchor);
|
|
|
|
c.removeSelectedText();
|
|
|
|
} else {
|
2016-05-04 16:46:24 +00:00
|
|
|
c.setPosition(insertPosition + (insertLength - toRemove));
|
|
|
|
c.setPosition(insertPosition + insertLength, QTextCursor::KeepAnchor);
|
2015-09-16 21:15:13 +00:00
|
|
|
c.removeSelectedText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
_correcting = false;
|
2015-09-16 21:15:13 +00:00
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
if (insertPosition == removePosition) {
|
|
|
|
if (!_links.isEmpty()) {
|
|
|
|
bool changed = false;
|
|
|
|
for (auto i = _links.begin(); i != _links.end();) {
|
|
|
|
if (i->start + i->length <= insertPosition) {
|
|
|
|
++i;
|
|
|
|
} else if (i->start >= removePosition + removeLength) {
|
|
|
|
i->start += insertLength - removeLength;
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
i = _links.erase(i);
|
|
|
|
changed = true;
|
|
|
|
}
|
2015-04-06 22:15:29 +00:00
|
|
|
}
|
2016-05-04 20:38:37 +00:00
|
|
|
if (changed) emit linksChanged();
|
2015-04-06 22:15:29 +00:00
|
|
|
}
|
2016-05-04 20:38:37 +00:00
|
|
|
} else {
|
|
|
|
parseLinks();
|
2015-04-06 22:15:29 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
if (document()->availableRedoSteps() > 0) {
|
|
|
|
QTextCursor(document()->docHandle(), 0).endEditBlock();
|
|
|
|
return;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
if (insertLength <= 0) {
|
2015-10-06 19:49:23 +00:00
|
|
|
QTextCursor(document()->docHandle(), 0).endEditBlock();
|
|
|
|
return;
|
|
|
|
}
|
2015-09-16 21:15:13 +00:00
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
_correcting = true;
|
2016-05-04 16:46:24 +00:00
|
|
|
auto pageSize = document()->pageSize();
|
|
|
|
processFormatting(insertPosition, insertPosition + insertLength);
|
|
|
|
if (document()->pageSize() != pageSize) {
|
|
|
|
document()->setPageSize(pageSize);
|
2015-07-13 11:08:17 +00:00
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
_correcting = false;
|
|
|
|
|
|
|
|
QTextCursor(document()->docHandle(), 0).endEditBlock();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::onDocumentContentsChanged() {
|
2015-10-06 19:49:23 +00:00
|
|
|
if (_correcting) return;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
auto tagsChanged = false;
|
2016-05-05 16:04:17 +00:00
|
|
|
auto curText = getTextPart(0, -1, &_lastTextWithTags.tags, &tagsChanged);
|
2016-04-30 17:04:14 +00:00
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
_correcting = true;
|
2016-05-05 16:04:17 +00:00
|
|
|
correctValue(_lastTextWithTags.text, curText, _lastTextWithTags.tags);
|
2015-10-06 19:49:23 +00:00
|
|
|
_correcting = false;
|
2016-04-30 17:04:14 +00:00
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
bool textOrTagsChanged = tagsChanged || (_lastTextWithTags.text != curText);
|
2016-04-30 17:04:14 +00:00
|
|
|
if (textOrTagsChanged) {
|
2016-05-05 16:04:17 +00:00
|
|
|
_lastTextWithTags.text = curText;
|
2014-05-30 08:53:19 +00:00
|
|
|
emit changed();
|
2015-09-16 13:04:08 +00:00
|
|
|
checkContentHeight();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
updatePlaceholder();
|
2014-11-18 12:40:43 +00:00
|
|
|
if (App::wnd()) App::wnd()->updateGlobalMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::onUndoAvailable(bool avail) {
|
|
|
|
_undoAvailable = avail;
|
|
|
|
if (App::wnd()) App::wnd()->updateGlobalMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::onRedoAvailable(bool avail) {
|
|
|
|
_redoAvailable = avail;
|
|
|
|
if (App::wnd()) App::wnd()->updateGlobalMenu();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
void FlatTextarea::step_appearance(float64 ms, bool timer) {
|
2014-05-30 08:53:19 +00:00
|
|
|
float dt = ms / _st.phDuration;
|
|
|
|
if (dt >= 1) {
|
2015-12-08 12:33:37 +00:00
|
|
|
_a_appearance.stop();
|
2014-05-30 08:53:19 +00:00
|
|
|
a_phLeft.finish();
|
|
|
|
a_phAlpha.finish();
|
|
|
|
a_phColor.finish();
|
|
|
|
a_phLeft = anim::ivalue(a_phLeft.current());
|
|
|
|
a_phAlpha = anim::fvalue(a_phAlpha.current());
|
|
|
|
a_phColor = anim::cvalue(a_phColor.current());
|
|
|
|
} else {
|
|
|
|
a_phLeft.update(dt, _st.phLeftFunc);
|
|
|
|
a_phAlpha.update(dt, _st.phAlphaFunc);
|
|
|
|
a_phColor.update(dt, _st.phColorFunc);
|
|
|
|
}
|
2015-12-08 12:33:37 +00:00
|
|
|
if (timer) update();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 15:27:21 +00:00
|
|
|
void FlatTextarea::setPlaceholder(const QString &ph, int32 afterSymbols) {
|
2015-09-21 20:57:42 +00:00
|
|
|
_ph = ph;
|
2015-12-31 15:27:21 +00:00
|
|
|
if (_phAfter != afterSymbols) {
|
|
|
|
_phAfter = afterSymbols;
|
|
|
|
updatePlaceholder();
|
|
|
|
}
|
2016-05-05 16:04:17 +00:00
|
|
|
int skipWidth = 0;
|
|
|
|
if (_phAfter) {
|
|
|
|
skipWidth = _st.font->width(getTextWithTags().text.mid(0, _phAfter));
|
|
|
|
}
|
|
|
|
_phelided = _st.font->elided(_ph, width() - _st.textMrg.left() - _st.textMrg.right() - _st.phPos.x() - 1 - skipWidth);
|
2015-09-21 20:57:42 +00:00
|
|
|
if (_phVisible) update();
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void FlatTextarea::updatePlaceholder() {
|
2016-05-05 16:04:17 +00:00
|
|
|
bool vis = (getTextWithTags().text.size() <= _phAfter);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (vis == _phVisible) return;
|
|
|
|
|
|
|
|
a_phLeft.start(vis ? 0 : _st.phShift);
|
|
|
|
a_phAlpha.start(vis ? 1 : 0);
|
2015-12-08 12:33:37 +00:00
|
|
|
_a_appearance.start();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
_phVisible = vis;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData *FlatTextarea::createMimeDataFromSelection() const {
|
|
|
|
QMimeData *result = new QMimeData();
|
|
|
|
QTextCursor c(textCursor());
|
|
|
|
int32 start = c.selectionStart(), end = c.selectionEnd();
|
|
|
|
if (end > start) {
|
2016-04-30 17:04:14 +00:00
|
|
|
TagList tags;
|
2016-05-05 16:04:17 +00:00
|
|
|
result->setText(getTextPart(start, end, &tags));
|
2016-04-30 17:04:14 +00:00
|
|
|
if (!tags.isEmpty()) {
|
2016-05-04 20:38:37 +00:00
|
|
|
if (_tagMimeProcessor) {
|
|
|
|
for (auto &tag : tags) {
|
|
|
|
tag.id = _tagMimeProcessor->mimeTagFromTag(tag.id);
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 16:04:17 +00:00
|
|
|
result->setData(tagsMimeType(), serializeTagsList(tags));
|
2016-04-30 17:04:14 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:02:50 +00:00
|
|
|
void FlatTextarea::setSubmitSettings(SubmitSettings settings) {
|
|
|
|
_submitSettings = settings;
|
2015-09-16 13:04:08 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void FlatTextarea::keyPressEvent(QKeyEvent *e) {
|
|
|
|
bool shift = e->modifiers().testFlag(Qt::ShiftModifier);
|
2015-12-03 18:16:34 +00:00
|
|
|
bool macmeta = (cPlatform() == dbipMac || cPlatform() == dbipMacOld) && e->modifiers().testFlag(Qt::ControlModifier) && !e->modifiers().testFlag(Qt::MetaModifier) && !e->modifiers().testFlag(Qt::AltModifier);
|
2016-04-09 11:02:50 +00:00
|
|
|
bool ctrl = e->modifiers().testFlag(Qt::ControlModifier) || e->modifiers().testFlag(Qt::MetaModifier);
|
|
|
|
bool enterSubmit = (ctrl && shift);
|
|
|
|
if (ctrl && _submitSettings != SubmitSettings::None && _submitSettings != SubmitSettings::Enter) {
|
|
|
|
enterSubmit = true;
|
|
|
|
}
|
2016-04-09 18:45:55 +00:00
|
|
|
if (!ctrl && !shift && _submitSettings != SubmitSettings::None && _submitSettings != SubmitSettings::CtrlEnter) {
|
2016-04-09 11:02:50 +00:00
|
|
|
enterSubmit = true;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
bool enter = (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return);
|
|
|
|
|
2015-05-08 14:36:05 +00:00
|
|
|
if (macmeta && e->key() == Qt::Key_Backspace) {
|
|
|
|
QTextCursor tc(textCursor()), start(tc);
|
|
|
|
start.movePosition(QTextCursor::StartOfLine);
|
|
|
|
tc.setPosition(start.position(), QTextCursor::KeepAnchor);
|
|
|
|
tc.removeSelectedText();
|
2016-04-09 11:02:50 +00:00
|
|
|
} else if (enter && enterSubmit) {
|
2014-10-17 12:57:14 +00:00
|
|
|
emit submitted(ctrl && shift);
|
2014-05-30 08:53:19 +00:00
|
|
|
} else if (e->key() == Qt::Key_Escape) {
|
|
|
|
emit cancelled();
|
2014-07-18 11:12:18 +00:00
|
|
|
} else if (e->key() == Qt::Key_Tab || (ctrl && e->key() == Qt::Key_Backtab)) {
|
2014-07-18 10:37:34 +00:00
|
|
|
if (ctrl) {
|
|
|
|
e->ignore();
|
|
|
|
} else {
|
|
|
|
emit tabbed();
|
|
|
|
}
|
2015-08-04 15:01:47 +00:00
|
|
|
} else if (e->key() == Qt::Key_Search || e == QKeySequence::Find) {
|
|
|
|
e->ignore();
|
2016-06-28 18:27:23 +00:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
} else if (e->key() == Qt::Key_E && e->modifiers().testFlag(Qt::ControlModifier)) {
|
|
|
|
auto cursor = textCursor();
|
|
|
|
int start = cursor.selectionStart(), end = cursor.selectionEnd();
|
|
|
|
if (end > start) {
|
|
|
|
TagList tags;
|
|
|
|
QApplication::clipboard()->setText(getTextPart(start, end, &tags), QClipboard::FindBuffer);
|
|
|
|
}
|
|
|
|
#endif // Q_OS_MAC
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
|
|
|
QTextCursor tc(textCursor());
|
|
|
|
if (enter && ctrl) {
|
|
|
|
e->setModifiers(e->modifiers() & ~Qt::ControlModifier);
|
|
|
|
}
|
2015-04-06 22:15:29 +00:00
|
|
|
bool spaceOrReturn = false;
|
|
|
|
QString t(e->text());
|
|
|
|
if (!t.isEmpty() && t.size() < 3) {
|
|
|
|
if (t.at(0) == '\n' || t.at(0) == '\r' || t.at(0).isSpace() || t.at(0) == QChar::LineSeparator) {
|
|
|
|
spaceOrReturn = true;
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
QTextEdit::keyPressEvent(e);
|
|
|
|
if (tc == textCursor()) {
|
|
|
|
bool check = false;
|
|
|
|
if (e->key() == Qt::Key_PageUp || e->key() == Qt::Key_Up) {
|
2015-01-28 13:29:01 +00:00
|
|
|
tc.movePosition(QTextCursor::Start, e->modifiers().testFlag(Qt::ShiftModifier) ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor);
|
2014-05-30 08:53:19 +00:00
|
|
|
check = true;
|
|
|
|
} else if (e->key() == Qt::Key_PageDown || e->key() == Qt::Key_Down) {
|
2015-01-28 13:29:01 +00:00
|
|
|
tc.movePosition(QTextCursor::End, e->modifiers().testFlag(Qt::ShiftModifier) ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor);
|
2014-05-30 08:53:19 +00:00
|
|
|
check = true;
|
|
|
|
}
|
|
|
|
if (check) {
|
|
|
|
if (tc == textCursor()) {
|
|
|
|
e->ignore();
|
|
|
|
} else {
|
|
|
|
setTextCursor(tc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-06 22:15:29 +00:00
|
|
|
if (spaceOrReturn) emit spacedReturnedPasted();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::resizeEvent(QResizeEvent *e) {
|
2015-10-03 10:09:09 +00:00
|
|
|
_phelided = _st.font->elided(_ph, width() - _st.textMrg.left() - _st.textMrg.right() - _st.phPos.x() - 1);
|
2014-05-30 08:53:19 +00:00
|
|
|
QTextEdit::resizeEvent(e);
|
2015-09-22 09:58:40 +00:00
|
|
|
checkContentHeight();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlatTextarea::mousePressEvent(QMouseEvent *e) {
|
|
|
|
QTextEdit::mousePressEvent(e);
|
|
|
|
}
|
2015-08-30 14:57:21 +00:00
|
|
|
|
|
|
|
void FlatTextarea::dropEvent(QDropEvent *e) {
|
|
|
|
_inDrop = true;
|
|
|
|
QTextEdit::dropEvent(e);
|
|
|
|
_inDrop = false;
|
2016-05-04 16:46:24 +00:00
|
|
|
_insertedTags.clear();
|
|
|
|
_realInsertPosition = -1;
|
|
|
|
|
2015-08-30 14:57:21 +00:00
|
|
|
emit spacedReturnedPasted();
|
|
|
|
}
|
2015-11-01 18:14:30 +00:00
|
|
|
|
|
|
|
void FlatTextarea::contextMenuEvent(QContextMenuEvent *e) {
|
|
|
|
if (QMenu *menu = createStandardContextMenu()) {
|
|
|
|
(new PopupMenu(menu))->popup(e->globalPos());
|
|
|
|
}
|
|
|
|
}
|