Q(Plain)TextEdit: Add find() overload with QRegExp

Add overloads to the find() methods in QPlainTextEdit and QTextEdit
that find the next occurrence matching the passed regular expression.

These are convenience methods that eliminate the need to use the
document() method and the need to handle the QTextCursor return value.

[ChangeLog][QtWidgets][QPlainTextEdit] Added find method overload using QRegExp

[ChangeLog][QtWidgets][QTextEdit] Added find method overload using QRegExp

Change-Id: Ia6139b771e3ae4ca02e4b8ea7fde19e5dc71b9d8
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Christian Loose 2014-01-27 12:27:38 +01:00 committed by The Qt Project
parent 17678bee89
commit f40c28f915
8 changed files with 153 additions and 0 deletions

View File

@ -2798,6 +2798,27 @@ bool QPlainTextEdit::find(const QString &exp, QTextDocument::FindFlags options)
return d->control->find(exp, options);
}
/*!
\fn bool QPlainTextEdit::find(const QRegExp &exp, QTextDocument::FindFlags options)
\since 5.3
\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 QRegExp::caseSensitivity instead.
Returns \c true if a match was found and changes the cursor to select the match;
otherwise returns \c false.
*/
#ifndef QT_NO_REGEXP
bool QPlainTextEdit::find(const QRegExp &exp, QTextDocument::FindFlags options)
{
Q_D(QPlainTextEdit);
return d->control->find(exp, options);
}
#endif
/*!
\fn void QPlainTextEdit::copyAvailable(bool yes)

View File

@ -145,6 +145,9 @@ public:
bool centerOnScroll() const;
bool find(const QString &exp, QTextDocument::FindFlags options = 0);
#ifndef QT_NO_REGEXP
bool find(const QRegExp &exp, QTextDocument::FindFlags options = 0);
#endif
inline QString toPlainText() const
{ return document()->toPlainText(); }

View File

@ -2455,6 +2455,27 @@ bool QTextEdit::find(const QString &exp, QTextDocument::FindFlags options)
return d->control->find(exp, options);
}
/*!
\fn bool QTextEdit::find(const QRegExp &exp, QTextDocument::FindFlags options)
\since 5.3
\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 QRegExp::caseSensitivity instead.
Returns \c true if a match was found and changes the cursor to select the match;
otherwise returns \c false.
*/
#ifndef QT_NO_REGEXP
bool QTextEdit::find(const QRegExp &exp, QTextDocument::FindFlags options)
{
Q_D(QTextEdit);
return d->control->find(exp, options);
}
#endif
/*!
\fn void QTextEdit::copyAvailable(bool yes)

View File

@ -162,6 +162,9 @@ public:
void setWordWrapMode(QTextOption::WrapMode policy);
bool find(const QString &exp, QTextDocument::FindFlags options = 0);
#ifndef QT_NO_REGEXP
bool find(const QRegExp &exp, QTextDocument::FindFlags options = 0);
#endif
QString toPlainText() const;
#ifndef QT_NO_TEXTHTMLPARSER

View File

@ -2958,6 +2958,19 @@ bool QWidgetTextControl::find(const QString &exp, QTextDocument::FindFlags optio
return true;
}
#ifndef QT_NO_REGEXP
bool QWidgetTextControl::find(const QRegExp &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();

View File

@ -110,6 +110,9 @@ public:
QTextCharFormat currentCharFormat() const;
bool find(const QString &exp, QTextDocument::FindFlags options = 0);
#ifndef QT_NO_REGEXP
bool find(const QRegExp &exp, QTextDocument::FindFlags options = 0);
#endif
QString toPlainText() const;
#ifndef QT_NO_TEXTHTMLPARSER

View File

@ -149,6 +149,11 @@ private slots:
void insertAndScrollToBottom();
void inputMethodQueryImHints_data();
void inputMethodQueryImHints();
#ifndef QT_NO_REGEXP
void findWithRegExp();
void findBackwardWithRegExp();
void findWithRegExpReturnsFalseIfNoMoreResults();
#endif
private:
void createSelection();
@ -1523,5 +1528,44 @@ void tst_QPlainTextEdit::inputMethodQueryImHints()
QCOMPARE(static_cast<Qt::InputMethodHints>(value.toInt()), hints);
}
#ifndef QT_NO_REGEXP
void tst_QPlainTextEdit::findWithRegExp()
{
ed->setPlainText(QStringLiteral("arbitrary text"));
QRegExp rx("\\w{2}xt");
bool found = ed->find(rx);
QVERIFY(found == true);
QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("text"));
}
void tst_QPlainTextEdit::findBackwardWithRegExp()
{
ed->setPlainText(QStringLiteral("arbitrary text"));
QTextCursor cursor = ed->textCursor();
cursor.movePosition(QTextCursor::End);
ed->setTextCursor(cursor);
QRegExp rx("a\\w*t");
bool found = ed->find(rx, QTextDocument::FindBackward);
QVERIFY(found == true);
QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("arbit"));
}
void tst_QPlainTextEdit::findWithRegExpReturnsFalseIfNoMoreResults()
{
ed->setPlainText(QStringLiteral("arbitrary text"));
QRegExp rx("t.xt");
ed->find(rx);
bool found = ed->find(rx);
QVERIFY(found == false);
QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("text"));
}
#endif
QTEST_MAIN(tst_QPlainTextEdit)
#include "tst_qplaintextedit.moc"

View File

@ -205,6 +205,12 @@ private slots:
void countTextChangedOnRemove();
#ifndef QT_NO_REGEXP
void findWithRegExp();
void findBackwardWithRegExp();
void findWithRegExpReturnsFalseIfNoMoreResults();
#endif
private:
void createSelection();
int blockCount() const;
@ -2515,5 +2521,44 @@ void tst_QTextEdit::countTextChangedOnRemove()
QCOMPARE(spy.count(), 1);
}
#ifndef QT_NO_REGEXP
void tst_QTextEdit::findWithRegExp()
{
ed->setHtml(QStringLiteral("arbitrary te<span style=\"color:#ff0000\">xt</span>"));
QRegExp rx("\\w{2}xt");
bool found = ed->find(rx);
QVERIFY(found == true);
QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("text"));
}
void tst_QTextEdit::findBackwardWithRegExp()
{
ed->setPlainText(QStringLiteral("arbitrary text"));
QTextCursor cursor = ed->textCursor();
cursor.movePosition(QTextCursor::End);
ed->setTextCursor(cursor);
QRegExp rx("a\\w*t");
bool found = ed->find(rx, QTextDocument::FindBackward);
QVERIFY(found == true);
QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("arbit"));
}
void tst_QTextEdit::findWithRegExpReturnsFalseIfNoMoreResults()
{
ed->setPlainText(QStringLiteral("arbitrary text"));
QRegExp rx("t.xt");
ed->find(rx);
bool found = ed->find(rx);
QVERIFY(found == false);
QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("text"));
}
#endif
QTEST_MAIN(tst_QTextEdit)
#include "tst_qtextedit.moc"