xref: /glogg/src/quickfindpattern.h (revision 4fb0346e73d7caa82d42531c8c8681b5eb607728)
1 /*
2  * Copyright (C) 2010, 2013 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef QUICKFINDPATTERN_H
21 #define QUICKFINDPATTERN_H
22 
23 #include <QObject>
24 #include <QString>
25 #include <QRegularExpression>
26 #include <QList>
27 
28 // Represents a match result for QuickFind
29 class QuickFindMatch
30 {
31   public:
32     // Construct a match (must be initialised)
QuickFindMatch(int start_column,int length)33     QuickFindMatch( int start_column, int length )
34     { startColumn_ = start_column; length_ = length; }
35 
36     // Accessor functions
startColumn()37     int startColumn() const { return startColumn_; }
length()38     int length() const { return length_; }
39 
40   private:
41     int startColumn_;
42     int length_;
43 };
44 
45 // Represents a search pattern for QuickFind (without its results)
46 class QuickFindPattern : public QObject
47 {
48   Q_OBJECT
49 
50   public:
51     // Construct an empty search
52     QuickFindPattern();
53 
54     // Set the search to a new pattern, using the current
55     // case status
56     void changeSearchPattern( const QString& pattern );
57 
58     // Set the search to a new pattern, as well as the case status
59     void changeSearchPattern( const QString& pattern, bool ignoreCase );
60 
61     // Returns whether the search is active (i.e. valid and non empty regexp)
isActive()62     bool isActive() const { return active_; }
63 
64     // Return the text of the regex
getPattern()65     QString getPattern() const { return regexp_.pattern(); }
66 
67     // Returns whether the passed line match the quick find search.
68     // If so, it populate the passed list with the list of matches
69     // within this particular line.
70     bool matchLine( const QString& line,
71             QList<QuickFindMatch>& matches ) const;
72 
73     // Returns whether there is a match in the passed line, starting at
74     // the passed column.
75     // Results are stored internally.
76     bool isLineMatching( const QString& line, int column = 0 ) const;
77 
78     // Same as isLineMatching but search backward
79     bool isLineMatchingBackward( const QString& line, int column = -1 ) const;
80 
81     // Must be called when isLineMatching returns 'true', returns
82     // the position of the first match found.
83     void getLastMatch( int* start_col, int* end_col ) const;
84 
85   signals:
86     // Sent when the pattern is changed
87     void patternUpdated();
88 
89   private:
90     bool active_;
91     QRegularExpression regexp_;
92 
93     mutable int lastMatchStart_;
94     mutable int lastMatchEnd_;
95 };
96 
97 #endif
98