Replaced Qt slots in InputField and removed Q_OBJECT macro.

This commit is contained in:
23rd 2023-08-31 16:41:50 +03:00
parent dfbf009904
commit 9d2f648a11
2 changed files with 50 additions and 40 deletions

View file

@ -14,6 +14,7 @@
#include "base/invoke_queued.h" #include "base/invoke_queued.h"
#include "base/random.h" #include "base/random.h"
#include "base/platform/base_platform_info.h" #include "base/platform/base_platform_info.h"
#include "base/qt_signal_producer.h"
#include "emoji_suggestions_helper.h" #include "emoji_suggestions_helper.h"
#include "base/qthelp_regex.h" #include "base/qthelp_regex.h"
#include "base/qt/qt_common_adapters.h" #include "base/qt/qt_common_adapters.h"
@ -1210,13 +1211,42 @@ InputField::InputField(
_touchTimer.setCallback([=] { _touchRightButton = true; }); _touchTimer.setCallback([=] { _touchRightButton = true; });
connect(_inner->document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(onDocumentContentsChange(int,int,int))); base::qt_signal_producer(
connect(_inner.get(), SIGNAL(undoAvailable(bool)), this, SLOT(onUndoAvailable(bool))); _inner->document(),
connect(_inner.get(), SIGNAL(redoAvailable(bool)), this, SLOT(onRedoAvailable(bool))); &QTextDocument::contentsChange
connect(_inner.get(), SIGNAL(cursorPositionChanged()), this, SLOT(onCursorPositionChanged())); ) | rpl::start_with_next([=](int position, int removed, int added) {
connect(_inner.get(), &Inner::selectionChanged, [] { documentContentsChanged(position, removed, added);
}, lifetime());
base::qt_signal_producer(
_inner.get(),
&QTextEdit::undoAvailable
) | rpl::start_with_next([=](bool undoAvailable) {
_undoAvailable = undoAvailable;
Integration::Instance().textActionsUpdated(); Integration::Instance().textActionsUpdated();
}); }, lifetime());
base::qt_signal_producer(
_inner.get(),
&QTextEdit::redoAvailable
) | rpl::start_with_next([=](bool redoAvailable) {
_redoAvailable = redoAvailable;
Integration::Instance().textActionsUpdated();
}, lifetime());
base::qt_signal_producer(
_inner.get(),
&QTextEdit::cursorPositionChanged
) | rpl::start_with_next([=] {
auto cursor = textCursor();
if (!cursor.hasSelection() && !cursor.position()) {
cursor.setCharFormat(_defaultCharFormat);
setTextCursor(cursor);
}
}, lifetime());
base::qt_signal_producer(
_inner.get(),
&Inner::selectionChanged
) | rpl::start_with_next([] {
Integration::Instance().textActionsUpdated();
}, lifetime());
const auto bar = _inner->verticalScrollBar(); const auto bar = _inner->verticalScrollBar();
_scrollTop = bar->value(); _scrollTop = bar->value();
@ -1757,12 +1787,16 @@ void InputField::focusInEvent(QFocusEvent *e) {
_borderAnimationStart = (e->reason() == Qt::MouseFocusReason) _borderAnimationStart = (e->reason() == Qt::MouseFocusReason)
? mapFromGlobal(QCursor::pos()).x() ? mapFromGlobal(QCursor::pos()).x()
: (width() / 2); : (width() / 2);
InvokeQueued(this, [=] { if (hasFocus()) onFocusInner(); }); InvokeQueued(this, [=] {
if (hasFocus()) {
focusInner();
}
});
} }
void InputField::mousePressEvent(QMouseEvent *e) { void InputField::mousePressEvent(QMouseEvent *e) {
_borderAnimationStart = e->pos().x(); _borderAnimationStart = e->pos().x();
InvokeQueued(this, [=] { onFocusInner(); }); InvokeQueued(this, [=] { focusInner(); });
} }
void InputField::mousePressEventInner(QMouseEvent *e) { void InputField::mousePressEventInner(QMouseEvent *e) {
@ -1788,7 +1822,7 @@ void InputField::mouseMoveEventInner(QMouseEvent *e) {
_inner->QTextEdit::mouseMoveEvent(e); _inner->QTextEdit::mouseMoveEvent(e);
} }
void InputField::onFocusInner() { void InputField::focusInner() {
auto borderStart = _borderAnimationStart; auto borderStart = _borderAnimationStart;
_inner->setFocus(); _inner->setFocus();
_borderAnimationStart = borderStart; _borderAnimationStart = borderStart;
@ -2271,7 +2305,7 @@ void InputField::processFormatting(int insertPosition, int insertEnd) {
} }
} }
void InputField::onDocumentContentsChange( void InputField::documentContentsChanged(
int position, int position,
int charsRemoved, int charsRemoved,
int charsAdded) { int charsAdded) {
@ -2339,14 +2373,6 @@ void InputField::onDocumentContentsChange(
} }
} }
void InputField::onCursorPositionChanged() {
auto cursor = textCursor();
if (!cursor.hasSelection() && !cursor.position()) {
cursor.setCharFormat(_defaultCharFormat);
setTextCursor(cursor);
}
}
void InputField::chopByMaxLength(int insertPosition, int insertLength) { void InputField::chopByMaxLength(int insertPosition, int insertLength) {
Expects(_correcting); Expects(_correcting);
@ -2445,16 +2471,6 @@ void InputField::highlightMarkdown() {
} }
} }
void InputField::onUndoAvailable(bool avail) {
_undoAvailable = avail;
Integration::Instance().textActionsUpdated();
}
void InputField::onRedoAvailable(bool avail) {
_redoAvailable = avail;
Integration::Instance().textActionsUpdated();
}
void InputField::setDisplayFocused(bool focused) { void InputField::setDisplayFocused(bool focused) {
setFocused(focused); setFocused(focused);
finishAnimating(); finishAnimating();

View file

@ -103,8 +103,6 @@ private:
}; };
class InputField : public RpWidget { class InputField : public RpWidget {
Q_OBJECT
public: public:
enum class Mode { enum class Mode {
SingleLine, SingleLine,
@ -324,16 +322,6 @@ public:
~InputField(); ~InputField();
private Q_SLOTS:
void onDocumentContentsChange(int position, int charsRemoved, int charsAdded);
void onCursorPositionChanged();
void onUndoAvailable(bool avail);
void onRedoAvailable(bool avail);
void onFocusInner();
protected: protected:
void startPlaceholderAnimation(); void startPlaceholderAnimation();
void startBorderAnimation(); void startBorderAnimation();
@ -378,6 +366,12 @@ private:
void insertFromMimeDataInner(const QMimeData *source); void insertFromMimeDataInner(const QMimeData *source);
TextWithTags getTextWithTagsSelected() const; TextWithTags getTextWithTagsSelected() const;
void documentContentsChanged(
int position,
int charsRemoved,
int charsAdded);
void focusInner();
// "start" and "end" are in coordinates of text where emoji are replaced // "start" and "end" are in coordinates of text where emoji are replaced
// by ObjectReplacementCharacter. If "end" = -1 means get text till the end. // by ObjectReplacementCharacter. If "end" = -1 means get text till the end.
QString getTextPart( QString getTextPart(