Implement QRegularExpression overload for QTextEdit::find
This patch adds the missing overload for QRegularExpression to match the QRegExp one for QTextEdit. [ChangeLog][QtWidgets][QTextEdit] Added QRegularExpression find() method overload. Change-Id: Ic7be224dcc59fc8d832daddd1999a713b7f04253 Reviewed-by: Luca Beldi <v.ronin@yahoo.it> Reviewed-by: David Faure <david.faure@kdab.com>bb10
parent
0c82994623
commit
d348363673
|
|
@ -2533,6 +2533,27 @@ bool QTextEdit::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 QTextEdit::find(const QRegularExpression &exp, QTextDocument::FindFlags options)
|
||||
{
|
||||
Q_D(QTextEdit);
|
||||
return d->control->find(exp, options);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*!
|
||||
\fn void QTextEdit::copyAvailable(bool yes)
|
||||
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@ class QMenu;
|
|||
class QTextEditPrivate;
|
||||
class QMimeData;
|
||||
class QPagedPaintDevice;
|
||||
class QRegularExpression;
|
||||
|
||||
class Q_WIDGETS_EXPORT QTextEdit : public QAbstractScrollArea
|
||||
{
|
||||
|
|
@ -165,6 +166,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
|
||||
|
||||
QString toPlainText() const;
|
||||
#ifndef QT_NO_TEXTHTMLPARSER
|
||||
|
|
|
|||
|
|
@ -3094,6 +3094,19 @@ bool QWidgetTextControl::find(const QRegExp &exp, QTextDocument::FindFlags optio
|
|||
}
|
||||
#endif
|
||||
|
||||
#if QT_CONFIG(regularexpression)
|
||||
bool QWidgetTextControl::find(const QRegularExpression &exp, QTextDocument::FindFlags options)
|
||||
{
|
||||
Q_D(QWidgetTextControl);
|
||||
QTextCursor search = d->doc->find(exp, d->cursor, options);
|
||||
if (search.isNull())
|
||||
return false;
|
||||
|
||||
setTextCursor(search);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
QString QWidgetTextControl::toPlainText() const
|
||||
{
|
||||
return document()->toPlainText();
|
||||
|
|
|
|||
|
|
@ -80,6 +80,7 @@ class QMenu;
|
|||
class QWidgetTextControlPrivate;
|
||||
class QAbstractScrollArea;
|
||||
class QEvent;
|
||||
class QRegularExpression;
|
||||
class QTimerEvent;
|
||||
|
||||
class Q_WIDGETS_EXPORT QWidgetTextControl : public QInputControl
|
||||
|
|
@ -119,6 +120,9 @@ public:
|
|||
#ifndef QT_NO_REGEXP
|
||||
bool find(const QRegExp &exp, QTextDocument::FindFlags options = 0);
|
||||
#endif
|
||||
#if QT_CONFIG(regularexpression)
|
||||
bool find(const QRegularExpression &exp, QTextDocument::FindFlags options = 0);
|
||||
#endif
|
||||
|
||||
QString toPlainText() const;
|
||||
#ifndef QT_NO_TEXTHTMLPARSER
|
||||
|
|
|
|||
|
|
@ -199,6 +199,12 @@ private slots:
|
|||
void findWithRegExpReturnsFalseIfNoMoreResults();
|
||||
#endif
|
||||
|
||||
#if QT_CONFIG(regularexpression)
|
||||
void findWithRegularExpression();
|
||||
void findBackwardWithRegularExpression();
|
||||
void findWithRegularExpressionReturnsFalseIfNoMoreResults();
|
||||
#endif
|
||||
|
||||
#if QT_CONFIG(wheelevent)
|
||||
void wheelEvent();
|
||||
#endif
|
||||
|
|
@ -2572,6 +2578,45 @@ void tst_QTextEdit::findWithRegExpReturnsFalseIfNoMoreResults()
|
|||
}
|
||||
#endif
|
||||
|
||||
#if QT_CONFIG(regularexpression)
|
||||
void tst_QTextEdit::findWithRegularExpression()
|
||||
{
|
||||
ed->setHtml(QStringLiteral("arbitrary te<span style=\"color:#ff0000\">xt</span>"));
|
||||
QRegularExpression rx("\\w{2}xt");
|
||||
|
||||
bool found = ed->find(rx);
|
||||
|
||||
QVERIFY(found);
|
||||
QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("text"));
|
||||
}
|
||||
|
||||
void tst_QTextEdit::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_QTextEdit::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
|
||||
|
||||
#if QT_CONFIG(wheelevent)
|
||||
|
||||
class TextEdit : public QTextEdit
|
||||
|
|
|
|||
Loading…
Reference in New Issue