File indexing completed on 2024-06-23 04:40:19

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "textcustomeditor_export.h"
0010 #include <QRegularExpression>
0011 #include <QWidget>
0012 namespace TextCustomEditor
0013 {
0014 class TextFindWidget;
0015 class TextReplaceWidget;
0016 /**
0017  * @brief The TextEditFindBarBase class
0018  * @author Laurent Montel <montel@kde.org>
0019  */
0020 class TEXTCUSTOMEDITOR_EXPORT TextEditFindBarBase : public QWidget
0021 {
0022     Q_OBJECT
0023 
0024 public:
0025     enum FindFlag {
0026         FindBackward = 0x00001,
0027         FindCaseSensitively = 0x00002,
0028         FindWholeWords = 0x00004,
0029         FindRespectDiacritics = 0x00008,
0030     };
0031     Q_DECLARE_FLAGS(FindFlags, FindFlag)
0032 
0033     explicit TextEditFindBarBase(QWidget *parent = nullptr);
0034     ~TextEditFindBarBase() override;
0035 
0036     [[nodiscard]] QString text() const;
0037     void setText(const QString &text);
0038 
0039     void focusAndSetCursor();
0040 
0041     void showReplace();
0042     void showFind();
0043     void setHideWhenClose(bool hide);
0044 
0045 Q_SIGNALS:
0046     void displayMessageIndicator(const QString &message);
0047     void hideFindBar();
0048 
0049 protected:
0050     [[nodiscard]] virtual bool viewIsReadOnly() const = 0;
0051     [[nodiscard]] virtual bool documentIsEmpty() const = 0;
0052     virtual bool searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions) = 0;
0053     virtual bool searchInDocument(const QRegularExpression &regExp, TextEditFindBarBase::FindFlags searchOptions) = 0;
0054     virtual void autoSearchMoveCursor() = 0;
0055 
0056     bool event(QEvent *e) override;
0057     void clearSelections();
0058     bool searchText(bool backward, bool isAutoSearch);
0059 
0060     void setFoundMatch(bool match);
0061     void messageInfo(bool backward, bool isAutoSearch, bool found);
0062 
0063 public Q_SLOTS:
0064     void findNext();
0065     void findPrev();
0066     void autoSearch(const QString &str);
0067     virtual void slotSearchText(bool backward = false, bool isAutoSearch = true) = 0;
0068     void closeBar();
0069 
0070 private Q_SLOTS:
0071     TEXTCUSTOMEDITOR_NO_EXPORT void slotClearSearch();
0072     TEXTCUSTOMEDITOR_NO_EXPORT void slotUpdateSearchOptions();
0073     virtual void slotReplaceText() = 0;
0074     virtual void slotReplaceAllText() = 0;
0075 
0076 protected:
0077     QString mLastSearchStr;
0078     QRegularExpression mLastSearchRegExp;
0079     TextFindWidget *const mFindWidget;
0080     TextReplaceWidget *const mReplaceWidget;
0081     bool mHideWhenClose = true;
0082 };
0083 }