summaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/searchtextlocator.h
blob: 0cfdb4421efb2d9e7900521fbb4fa98c1768d1e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef SEARCHTEXTLOCATOR_H
#define SEARCHTEXTLOCATOR_H

#include "guiSQLiteStudio_global.h"
#include <QObject>
#include <QTextDocument>

class GUI_API_EXPORT SearchTextLocator : public QObject
{
        Q_OBJECT

    public:
        struct GUI_API_EXPORT Occurrance
        {
            int start;
            int end;
        };

        SearchTextLocator(QTextDocument* document, QObject* parent = nullptr);

        QString getLookupString() const;
        void setLookupString(const QString& value);

        QString getReplaceString() const;
        void setReplaceString(const QString& value);

        bool getCaseSensitive() const;
        void setCaseSensitive(bool value);

        bool getRegularExpression() const;
        void setRegularExpression(bool value);

        bool getSearchBackwards() const;
        void setSearchBackwards(bool value);

        int getStartPosition() const;
        void setStartPosition(int value);

        void reset();

    private:
        QTextDocument::FindFlags getFlags();
        void notFound();
        QTextCursor findInWholeDoc(QTextDocument::FindFlags flags);
        void replaceCurrent();

        QTextDocument* document = nullptr;
        int initialStartPosition;
        int lastMatchStart = -1;
        int lastMatchEnd = -1;
        bool afterDocPositionSwitch = false;

        // Config parameters
        QString lookupString;
        QString replaceString;
        bool caseSensitive = false;
        bool regularExpression = false;
        bool searchBackwards = false;
        int startPosition = 0;

    public slots:
        bool find(QTextDocument::FindFlags flags = 0);
        void findNext();
        void findPrev();
        bool replaceAndFind();
        void replaceAll();
        void cursorMoved();

    signals:
        void found(int start, int end);
        void reachedEnd();
        void replaceAvailable(bool available);
};

#endif // SEARCHTEXTLOCATOR_H