File indexing completed on 2024-12-22 04:28:12

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "richtextbrowserfindbar.h"
0008 #include "widgets/findutils.h"
0009 #include "widgets/textfindreplacewidget.h"
0010 
0011 #include <KLocalizedString>
0012 #include <TextUtils/ConvertText>
0013 
0014 #include <QLineEdit>
0015 #include <QRegularExpression>
0016 #include <QTextBrowser>
0017 
0018 using namespace TextCustomEditor;
0019 class Q_DECL_HIDDEN TextCustomEditor::RichTextBrowserFindBarPrivate
0020 {
0021 public:
0022     RichTextBrowserFindBarPrivate(QTextBrowser *view)
0023         : mView(view)
0024     {
0025     }
0026 
0027     QTextBrowser *const mView;
0028 };
0029 
0030 RichTextBrowserFindBar::RichTextBrowserFindBar(QTextBrowser *view, QWidget *parent)
0031     : TextEditFindBarBase(parent)
0032     , d(new TextCustomEditor::RichTextBrowserFindBarPrivate(view))
0033 {
0034 }
0035 
0036 RichTextBrowserFindBar::~RichTextBrowserFindBar() = default;
0037 
0038 void RichTextBrowserFindBar::slotSearchText(bool backward, bool isAutoSearch)
0039 {
0040     d->mView->moveCursor(QTextCursor::Start);
0041     searchText(backward, isAutoSearch);
0042 }
0043 
0044 bool RichTextBrowserFindBar::viewIsReadOnly() const
0045 {
0046     return true;
0047 }
0048 
0049 bool RichTextBrowserFindBar::documentIsEmpty() const
0050 {
0051     return d->mView->document()->isEmpty();
0052 }
0053 
0054 bool RichTextBrowserFindBar::searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions)
0055 {
0056     bool found = false;
0057     if (searchOptions & TextEditFindBarBase::FindRespectDiacritics) {
0058         found = d->mView->find(text, FindUtils::convertTextEditFindFlags(searchOptions));
0059     } else {
0060         found = FindUtils::find(d->mView, text, FindUtils::convertTextEditFindFlags(searchOptions));
0061     }
0062     mFindWidget->setFoundMatch(found);
0063     return found;
0064 }
0065 
0066 bool RichTextBrowserFindBar::searchInDocument(const QRegularExpression &regExp, TextEditFindBarBase::FindFlags searchOptions)
0067 {
0068     const bool found = d->mView->find(regExp, FindUtils::convertTextEditFindFlags(searchOptions));
0069     mFindWidget->setFoundMatch(found);
0070     return found;
0071 }
0072 
0073 void RichTextBrowserFindBar::autoSearchMoveCursor()
0074 {
0075     QTextCursor cursor = d->mView->textCursor();
0076     cursor.setPosition(cursor.selectionStart());
0077     d->mView->setTextCursor(cursor);
0078 }
0079 
0080 void RichTextBrowserFindBar::slotReplaceText()
0081 {
0082     // Nothing as readonly
0083 }
0084 
0085 void RichTextBrowserFindBar::slotReplaceAllText()
0086 {
0087     // Nothing as readonly
0088 }
0089 
0090 #include "moc_richtextbrowserfindbar.cpp"