Implement QRegularExpression overload for QPlainTextEdit::find

This patch adds the missing overload for QRegularExpression to match the
QRegExp one for QPlainTextEdit.

[ChangeLog][QtWidgets][QPlainTextEdit] Added QRegularExpression find()
method overload.

Change-Id: Id156971d3fa0372712bfa8b72a55550942a767e0
Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
Reviewed-by: David Faure <david.faure@kdab.com>
bb10
Samuel Gaist 2018-08-19 18:06:53 +02:00
parent d348363673
commit 5271b36672
3 changed files with 69 additions and 0 deletions

View File

@ -2923,6 +2923,27 @@ bool QPlainTextEdit::find(const QRegExp &exp, QTextDocument::FindFlags options)
}
#endif
/*!
\fn bool QTextEdit::find(const QRegularExpression &exp, QTextDocument::FindFlags options)
\since 5.13
\overload
Finds the next occurrence, matching the regular expression, \a exp, using the given
\a options. The QTextDocument::FindCaseSensitively option is ignored for this overload,
use QRegularExpression::CaseInsensitiveOption instead.
Returns \c true if a match was found and changes the cursor to select the match;
otherwise returns \c false.
*/
#if QT_CONFIG(regularexpression)
bool QPlainTextEdit::find(const QRegularExpression &exp, QTextDocument::FindFlags options)
{
Q_D(QPlainTextEdit);
return d->control->find(exp, options);
}
#endif
/*!
\fn void QPlainTextEdit::copyAvailable(bool yes)

View File

@ -60,6 +60,7 @@ class QMenu;
class QPlainTextEditPrivate;
class QMimeData;
class QPagedPaintDevice;
class QRegularExpression;
class Q_WIDGETS_EXPORT QPlainTextEdit : public QAbstractScrollArea
{
@ -149,6 +150,9 @@ public:
#ifndef QT_NO_REGEXP
bool find(const QRegExp &exp, QTextDocument::FindFlags options = QTextDocument::FindFlags());
#endif
#if QT_CONFIG(regularexpression)
bool find(const QRegularExpression &exp, QTextDocument::FindFlags options = QTextDocument::FindFlags());
#endif
inline QString toPlainText() const
{ return document()->toPlainText(); }

View File

@ -139,6 +139,11 @@ private slots:
void findWithRegExp();
void findBackwardWithRegExp();
void findWithRegExpReturnsFalseIfNoMoreResults();
#endif
#if QT_CONFIG(regularexpression)
void findWithRegularExpression();
void findBackwardWithRegularExpression();
void findWithRegularExpressionReturnsFalseIfNoMoreResults();
#endif
void layoutAfterMultiLineRemove();
void undoCommandRemovesAndReinsertsBlock();
@ -1582,6 +1587,45 @@ void tst_QPlainTextEdit::findWithRegExpReturnsFalseIfNoMoreResults()
}
#endif
#if QT_CONFIG(regularexpression)
void tst_QPlainTextEdit::findWithRegularExpression()
{
ed->setPlainText(QStringLiteral("arbitrary text"));
QRegularExpression rx("\\w{2}xt");
bool found = ed->find(rx);
QVERIFY(found);
QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("text"));
}
void tst_QPlainTextEdit::findBackwardWithRegularExpression()
{
ed->setPlainText(QStringLiteral("arbitrary text"));
QTextCursor cursor = ed->textCursor();
cursor.movePosition(QTextCursor::End);
ed->setTextCursor(cursor);
QRegularExpression rx("a\\w*t");
bool found = ed->find(rx, QTextDocument::FindBackward);
QVERIFY(found);
QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("arbit"));
}
void tst_QPlainTextEdit::findWithRegularExpressionReturnsFalseIfNoMoreResults()
{
ed->setPlainText(QStringLiteral("arbitrary text"));
QRegularExpression rx("t.xt");
ed->find(rx);
bool found = ed->find(rx);
QVERIFY(!found);
QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("text"));
}
#endif
void tst_QPlainTextEdit::layoutAfterMultiLineRemove()
{
ed->setVisible(true); // The widget must be visible to reproduce this bug.