File indexing completed on 2025-01-05 05:23:37

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009, 2023 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #ifndef KASTEN_REPLACETOOL_HPP
0010 #define KASTEN_REPLACETOOL_HPP
0011 
0012 // libfinddialog
0013 #include <finddirection.hpp>
0014 // Kasten core
0015 #include <Kasten/AbstractTool>
0016 // Okteta core
0017 #include <Okteta/Address>
0018 
0019 namespace Okteta {
0020 class AbstractByteArrayModel;
0021 }
0022 
0023 namespace Kasten {
0024 
0025 class ByteArrayView;
0026 class ReplaceJob;
0027 
0028 // TODO: is queryAgent needed, or should the tool better be a state machine? same with search tool
0029 class ReplaceTool : public AbstractTool
0030 {
0031     Q_OBJECT
0032 
0033 private:
0034     static inline constexpr char ConfigGroupId[] = "ReplaceTool";
0035 
0036     static inline constexpr char CaseSensitivityConfigKey[] = "CaseSensitivity";
0037     static inline constexpr char PromptConfigKey[] = "Prompt";
0038 
0039     static inline constexpr Qt::CaseSensitivity DefaultCaseSensitivity = Qt::CaseInsensitive;
0040     static inline constexpr bool DefaultDoPrompt = false;
0041 
0042 public:
0043     ReplaceTool();
0044     ~ReplaceTool() override;
0045 
0046 public: // AbstractTool API
0047 //     virtual AbstractModel* targetModel() const;
0048     QString title() const override;
0049 
0050     void setTargetModel(AbstractModel* model) override;
0051 
0052 public: // status
0053     QByteArray searchData() const;
0054     QByteArray replaceData() const;
0055     Qt::CaseSensitivity caseSensitivity() const;
0056     bool isDoPrompt() const;
0057     bool hasSelectedData() const;
0058     QString charCodingName() const;
0059 
0060     bool isApplyable() const; // candidate for AbstractTool API
0061 
0062 public: // actions
0063     void replace(FindDirection direction, bool fromCursor, bool inSelection);
0064 
0065 public:
0066     /// @param userQueryAgent expected to implement If::ReplaceUserQueryable
0067     void setUserQueryAgent(QObject* userQueryAgent);
0068 
0069 public Q_SLOTS: // settings
0070     void setSearchData(const QByteArray& searchData);
0071     void setReplaceData(const QByteArray& replaceData);
0072     void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity);
0073     void setDoPrompt(bool doPrompt);
0074 
0075 Q_SIGNALS:
0076     void isApplyableChanged(bool isApplyable);    // candidate for AbstractTool API
0077     void finished(bool previousFound, int noOfReplacements);
0078 
0079 private:
0080     void doReplace(FindDirection direction, Okteta::Address startIndex);
0081 
0082 private Q_SLOTS:
0083     void onReadOnlyChanged(bool isReadOnly);
0084     void onJobFinished(bool previousFound, int noOfReplacements);
0085 
0086 private: // settings
0087     QByteArray mSearchData;
0088     QByteArray mReplaceData;
0089     Qt::CaseSensitivity mCaseSensitivity;
0090     bool mDoPrompt : 1;
0091 
0092 private: // status
0093     ReplaceJob* mReplaceJob = nullptr;
0094 
0095 private:
0096     // expected to implement If::ReplaceUserQueryable
0097     QObject* mUserQueryAgent = nullptr;
0098 
0099 private: // target
0100     ByteArrayView* mByteArrayView = nullptr;
0101     Okteta::AbstractByteArrayModel* mByteArrayModel = nullptr;
0102 };
0103 
0104 inline QByteArray ReplaceTool::searchData() const { return mSearchData; }
0105 inline QByteArray ReplaceTool::replaceData() const { return mReplaceData; }
0106 inline Qt::CaseSensitivity ReplaceTool::caseSensitivity() const { return mCaseSensitivity; }
0107 inline bool ReplaceTool::isDoPrompt() const { return mDoPrompt; }
0108 
0109 }
0110 
0111 #endif