Commit 34d402ad authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Add option for respecting diacritics

parent a913f881
Pipeline #142223 passed with stages
in 2 minutes and 28 seconds
......@@ -16,15 +16,29 @@
#include <QTextEdit>
using namespace KPIMTextEdit;
QTextDocument::FindFlags FindUtils::convertTextEditFindFlags(TextEditFindBarBase::FindFlags textEditFlags)
{
QTextDocument::FindFlags flags;
if (textEditFlags & TextEditFindBarBase::FindBackward) {
flags |= QTextDocument::FindBackward;
}
if (textEditFlags & TextEditFindBarBase::FindCaseSensitively) {
flags |= QTextDocument::FindCaseSensitively;
}
if (textEditFlags & TextEditFindBarBase::FindWholeWords) {
flags |= QTextDocument::FindWholeWords;
}
return flags;
}
int FindUtils::replaceAll(QTextDocument *document, const TextFindWidget *findWidget, const TextReplaceWidget *replaceWidget)
{
QTextCursor c(document);
c.beginEditBlock();
int count = 0;
// Ignoring FindBackward when replacing all
const QTextDocument::FindFlags flags = FindUtils::convertTextEditFindFlags(findWidget->searchOptions()) & ~QTextDocument::FindBackward;
while (!c.isNull()) {
// Ignoring FindBackward when replacing all
QTextDocument::FindFlags flags = findWidget->searchOptions() & ~QTextDocument::FindBackward;
if (findWidget->isRegularExpression()) {
c = document->find(findWidget->searchRegularExpression(), c, flags);
} else {
......
......@@ -6,6 +6,8 @@
#pragma once
#include "kpimtextedit/texteditfindbarbase.h"
#include <QChar>
#include <QStringView>
#include <QTextDocument>
......@@ -31,6 +33,7 @@ Q_REQUIRED_RESULT QString normalize(QStringView str);
Q_REQUIRED_RESULT QChar normalize(QChar c);
Q_REQUIRED_RESULT bool find(QPlainTextEdit *view, const QString &searchText, QTextDocument::FindFlags searchOptions);
Q_REQUIRED_RESULT bool find(QTextEdit *view, const QString &searchText, QTextDocument::FindFlags searchOptions);
Q_REQUIRED_RESULT QTextDocument::FindFlags convertTextEditFindFlags(TextEditFindBarBase::FindFlags textEditFlags);
}
} // namespace KPIMTextEdit
......@@ -145,9 +145,9 @@ bool TextEditFindBarBase::searchText(bool backward, bool isAutoSearch)
{
mLastSearchStr = mFindWidget->searchText();
mLastSearchRegExp = mFindWidget->searchRegularExpression();
QTextDocument::FindFlags searchOptions = mFindWidget->searchOptions();
TextEditFindBarBase::FindFlags searchOptions = mFindWidget->searchOptions();
if (backward) {
searchOptions |= QTextDocument::FindBackward;
searchOptions |= TextEditFindBarBase::FindBackward;
}
if (isAutoSearch) {
......@@ -181,7 +181,7 @@ void TextEditFindBarBase::findPrev()
void TextEditFindBarBase::slotUpdateSearchOptions()
{
const QTextDocument::FindFlags searchOptions = mFindWidget->searchOptions();
const TextEditFindBarBase::FindFlags searchOptions = mFindWidget->searchOptions();
mLastSearchStr = mFindWidget->searchText();
mLastSearchRegExp = mFindWidget->searchRegularExpression();
if (mFindWidget->isRegularExpression()) {
......
......@@ -23,6 +23,14 @@ class KPIMTEXTEDIT_EXPORT TextEditFindBarBase : public QWidget
Q_OBJECT
public:
enum FindFlag {
FindBackward = 0x00001,
FindCaseSensitively = 0x00002,
FindWholeWords = 0x00004,
FindRespectDiacritics = 0x00008,
};
Q_DECLARE_FLAGS(FindFlags, FindFlag)
explicit TextEditFindBarBase(QWidget *parent = nullptr);
~TextEditFindBarBase() override;
......@@ -42,8 +50,8 @@ Q_SIGNALS:
protected:
Q_REQUIRED_RESULT virtual bool viewIsReadOnly() const = 0;
Q_REQUIRED_RESULT virtual bool documentIsEmpty() const = 0;
Q_REQUIRED_RESULT virtual bool searchInDocument(const QString &text, QTextDocument::FindFlags searchOptions) = 0;
Q_REQUIRED_RESULT virtual bool searchInDocument(const QRegularExpression &regExp, QTextDocument::FindFlags searchOptions) = 0;
virtual bool searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions) = 0;
  • @mlaurent Was the Q_REQUIRED_RESULT removed on purpose from here?

  • Yep as in TextEditFindBarBase::slotUpdateSearchOptions we don't verify result it's normal to not use result.

Please register or sign in to reply
virtual bool searchInDocument(const QRegularExpression &regExp, TextEditFindBarBase::FindFlags searchOptions) = 0;
virtual void autoSearchMoveCursor() = 0;
bool event(QEvent *e) override;
......
......@@ -67,6 +67,7 @@ TextFindWidget::TextFindWidget(QWidget *parent)
mSearch->setToolTip(i18n("Text to search for"));
mSearch->setClearButtonEnabled(true);
mSearch->setObjectName(QStringLiteral("mSearch"));
label->setBuddy(mSearch);
lay->addWidget(mSearch);
......@@ -74,15 +75,18 @@ TextFindWidget::TextFindWidget(QWidget *parent)
mFindNextBtn->setToolTip(i18n("Jump to next match"));
lay->addWidget(mFindNextBtn);
mFindNextBtn->setEnabled(false);
mFindNextBtn->setObjectName(QStringLiteral("mFindNextBtn"));
mFindPrevBtn = new QPushButton(QIcon::fromTheme(QStringLiteral("go-up-search")), i18nc("Find and go to the previous search match", "Previous"), this);
mFindPrevBtn->setToolTip(i18n("Jump to previous match"));
lay->addWidget(mFindPrevBtn);
mFindPrevBtn->setEnabled(false);
mFindPrevBtn->setObjectName(QStringLiteral("mFindPrevBtn"));
auto optionsBtn = new QPushButton(this);
optionsBtn->setText(i18n("Options"));
optionsBtn->setToolTip(i18n("Modify search behavior"));
optionsBtn->setObjectName(QStringLiteral("optionsBtn"));
auto optionsMenu = new QMenu(optionsBtn);
mCaseSensitiveAct = optionsMenu->addAction(i18n("Case sensitive"));
mCaseSensitiveAct->setCheckable(true);
......@@ -93,6 +97,9 @@ TextFindWidget::TextFindWidget(QWidget *parent)
mRegExpAct = optionsMenu->addAction(i18n("Regular Expression"));
mRegExpAct->setCheckable(true);
mRespectDiacriticAct = optionsMenu->addAction(i18n("Respect Diacritic and Accents"));
mRespectDiacriticAct->setCheckable(true);
optionsBtn->setMenu(optionsMenu);
lay->addWidget(optionsBtn);
......@@ -100,6 +107,7 @@ TextFindWidget::TextFindWidget(QWidget *parent)
connect(mFindPrevBtn, &QPushButton::clicked, this, &TextFindWidget::findPrev);
connect(mCaseSensitiveAct, &QAction::toggled, this, &TextFindWidget::updateSearchOptions);
connect(mWholeWordAct, &QAction::toggled, this, &TextFindWidget::updateSearchOptions);
connect(mRespectDiacriticAct, &QAction::toggled, this, &TextFindWidget::updateSearchOptions);
connect(mRegExpAct, &QAction::toggled, this, &TextFindWidget::updateSearchOptions);
connect(mSearch, &QLineEdit::textChanged, this, &TextFindWidget::slotAutoSearch);
connect(mSearch, &QLineEdit::returnPressed, this, &TextFindWidget::findNext);
......@@ -171,14 +179,17 @@ QRegularExpression TextFindWidget::searchRegularExpression() const
return reg;
}
QTextDocument::FindFlags TextFindWidget::searchOptions() const
TextEditFindBarBase::FindFlags TextFindWidget::searchOptions() const
{
QTextDocument::FindFlags opt = {};
TextEditFindBarBase::FindFlags opt = {};
if (mCaseSensitiveAct->isChecked()) {
opt |= QTextDocument::FindCaseSensitively;
opt |= TextEditFindBarBase::FindCaseSensitively;
}
if (mWholeWordAct->isChecked()) {
opt |= QTextDocument::FindWholeWords;
opt |= TextEditFindBarBase::FindWholeWords;
}
if (mRespectDiacriticAct->isChecked()) {
opt |= TextEditFindBarBase::FindRespectDiacritics;
}
return opt;
}
......@@ -6,6 +6,8 @@
#pragma once
#include "kpimtextedit_private_export.h"
#include "texteditfindbarbase.h"
#include <QTextDocument>
#include <QWidget>
......@@ -14,14 +16,14 @@ class QLineEdit;
class QPushButton;
namespace KPIMTextEdit
{
class TextFindWidget : public QWidget
class KPIMTEXTEDIT_TESTS_EXPORT TextFindWidget : public QWidget
{
Q_OBJECT
public:
explicit TextFindWidget(QWidget *parent = nullptr);
~TextFindWidget() override;
Q_REQUIRED_RESULT QTextDocument::FindFlags searchOptions() const;
Q_REQUIRED_RESULT TextEditFindBarBase::FindFlags searchOptions() const;
QLineEdit *searchLineEdit() const;
......@@ -48,12 +50,13 @@ private:
QAction *mCaseSensitiveAct = nullptr;
QAction *mWholeWordAct = nullptr;
QAction *mRegExpAct = nullptr;
QAction *mRespectDiacriticAct = nullptr;
QPushButton *mFindPrevBtn = nullptr;
QPushButton *mFindNextBtn = nullptr;
};
class TextReplaceWidget : public QWidget
class KPIMTEXTEDIT_TESTS_EXPORT TextReplaceWidget : public QWidget
{
Q_OBJECT
public:
......
......@@ -51,20 +51,21 @@ bool PlainTextEditFindBar::documentIsEmpty() const
return d->mView->document()->isEmpty();
}
bool PlainTextEditFindBar::searchInDocument(const QString &text, QTextDocument::FindFlags searchOptions)
bool PlainTextEditFindBar::searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions)
{
#ifdef SEARCH_DIACRITIC_WORD
const bool found = FindUtils::find(d->mView, text, searchOptions);
#else
const bool found = d->mView->find(text, searchOptions);
#endif
bool found = false;
if (searchOptions & TextEditFindBarBase::FindRespectDiacritics) {
found = d->mView->find(text, FindUtils::convertTextEditFindFlags(searchOptions));
} else {
found = FindUtils::find(d->mView, text, FindUtils::convertTextEditFindFlags(searchOptions));
}
mFindWidget->setFoundMatch(found);
return found;
}
bool PlainTextEditFindBar::searchInDocument(const QRegularExpression &regExp, QTextDocument::FindFlags searchOptions)
bool PlainTextEditFindBar::searchInDocument(const QRegularExpression &regExp, TextEditFindBarBase::FindFlags searchOptions)
{
const bool found = d->mView->find(regExp, searchOptions);
const bool found = d->mView->find(regExp, FindUtils::convertTextEditFindFlags(searchOptions));
mFindWidget->setFoundMatch(found);
return found;
}
......
......@@ -29,8 +29,8 @@ public:
protected:
Q_REQUIRED_RESULT bool viewIsReadOnly() const override;
Q_REQUIRED_RESULT bool documentIsEmpty() const override;
Q_REQUIRED_RESULT bool searchInDocument(const QString &text, QTextDocument::FindFlags searchOptions) override;
Q_REQUIRED_RESULT bool searchInDocument(const QRegularExpression &regExp, QTextDocument::FindFlags searchOptions) override;
Q_REQUIRED_RESULT bool searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions) override;
Q_REQUIRED_RESULT bool searchInDocument(const QRegularExpression &regExp, TextEditFindBarBase::FindFlags searchOptions) override;
void autoSearchMoveCursor() override;
public Q_SLOTS:
......
......@@ -51,20 +51,21 @@ bool RichTextEditFindBar::documentIsEmpty() const
return d->mView->document()->isEmpty();
}
bool RichTextEditFindBar::searchInDocument(const QString &text, QTextDocument::FindFlags searchOptions)
bool RichTextEditFindBar::searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions)
{
#ifdef SEARCH_DIACRITIC_WORD
const bool found = FindUtils::find(d->mView, text, searchOptions);
#else
const bool found = d->mView->find(text, searchOptions);
#endif
bool found = false;
if (searchOptions & TextEditFindBarBase::FindRespectDiacritics) {
found = d->mView->find(text, FindUtils::convertTextEditFindFlags(searchOptions));
} else {
found = FindUtils::find(d->mView, text, FindUtils::convertTextEditFindFlags(searchOptions));
}
mFindWidget->setFoundMatch(found);
return found;
}
bool RichTextEditFindBar::searchInDocument(const QRegularExpression &regExp, QTextDocument::FindFlags searchOptions)
bool RichTextEditFindBar::searchInDocument(const QRegularExpression &regExp, TextEditFindBarBase::FindFlags searchOptions)
{
const bool found = d->mView->find(regExp, searchOptions);
const bool found = d->mView->find(regExp, FindUtils::convertTextEditFindFlags(searchOptions));
mFindWidget->setFoundMatch(found);
return found;
}
......
......@@ -28,8 +28,8 @@ public:
protected:
Q_REQUIRED_RESULT bool viewIsReadOnly() const override;
Q_REQUIRED_RESULT bool documentIsEmpty() const override;
Q_REQUIRED_RESULT bool searchInDocument(const QString &text, QTextDocument::FindFlags searchOptions) override;
Q_REQUIRED_RESULT bool searchInDocument(const QRegularExpression &regExp, QTextDocument::FindFlags searchOptions) override;
Q_REQUIRED_RESULT bool searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions) override;
Q_REQUIRED_RESULT bool searchInDocument(const QRegularExpression &regExp, TextEditFindBarBase::FindFlags searchOptions) override;
void autoSearchMoveCursor() override;
public Q_SLOTS:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment