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:
parent
17678bee89
commit
f40c28f915
@ -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)
|
||||
|
||||
|
@ -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(); }
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user