xref: /glogg/src/quickfindpattern.cpp (revision 481c483c640a40639a6f0e381fe628dbccb7bd99)
1 /*
2  * Copyright (C) 2010 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 // This file implements QuickFindPattern.
21 // This class implements part of the Quick Find mechanism, it only stores the
22 // current search pattern, once it has been confirmed (return pressed),
23 // it can be asked to return the matches in a specific string.
24 
25 #include "quickfindpattern.h"
26 
27 #include "persistentinfo.h"
28 #include "configuration.h"
29 
QuickFindPattern()30 QuickFindPattern::QuickFindPattern() : QObject(), regexp_()
31 {
32     active_ = false;
33 }
34 
35 #include <iostream>
36 
changeSearchPattern(const QString & pattern)37 void QuickFindPattern::changeSearchPattern( const QString& pattern )
38 {
39     // Determine the type of regexp depending on the config
40     QString searchPattern;
41     switch ( Persistent<Configuration>( "settings" )->quickfindRegexpType() ) {
42         case FixedString:
43             searchPattern = QRegularExpression::escape(pattern);
44             break;
45         default:
46             searchPattern = pattern;
47             break;
48     }
49 
50     regexp_.setPattern( searchPattern );
51 
52     if ( regexp_.isValid() && ( ! searchPattern.isEmpty() ) )
53         active_ = true;
54     else
55         active_ = false;
56 
57     emit patternUpdated();
58 }
59 
changeSearchPattern(const QString & pattern,bool ignoreCase)60 void QuickFindPattern::changeSearchPattern( const QString& pattern, bool ignoreCase )
61 {
62     QRegularExpression::PatternOptions options =
63             QRegularExpression::UseUnicodePropertiesOption
64             | QRegularExpression::OptimizeOnFirstUsageOption;
65 
66     if ( ignoreCase )
67         options |= QRegularExpression::CaseInsensitiveOption;
68 
69     regexp_.setPatternOptions(options);
70     changeSearchPattern( pattern );
71 }
72 
matchLine(const QString & line,QList<QuickFindMatch> & matches) const73 bool QuickFindPattern::matchLine( const QString& line,
74         QList<QuickFindMatch>& matches ) const
75 {
76     matches.clear();
77 
78     if ( active_ ) {
79         QRegularExpressionMatchIterator matchIterator = regexp_.globalMatch(line);
80 
81         while( matchIterator.hasNext() ) {
82             QRegularExpressionMatch match = matchIterator.next();
83             matches << QuickFindMatch ( match.capturedStart(), match.capturedLength() );
84         }
85     }
86 
87     return ( matches.count() > 0 );
88 }
89 
isLineMatching(const QString & line,int column) const90 bool QuickFindPattern::isLineMatching( const QString& line, int column ) const
91 {
92     if ( ! active_ )
93         return false;
94 
95     QRegularExpressionMatch match = regexp_.match( line, column );
96     if ( match.hasMatch() ) {
97         lastMatchStart_ = match.capturedStart();
98         lastMatchEnd_ = match.capturedEnd() - 1;
99         return true;
100     }
101     else {
102         return false;
103     }
104 }
105 
isLineMatchingBackward(const QString & line,int column) const106 bool QuickFindPattern::isLineMatchingBackward(
107         const QString& line, int column ) const
108 {
109     if ( ! active_ )
110         return false;
111 
112     QRegularExpressionMatchIterator matches = regexp_.globalMatch(line);
113     QRegularExpressionMatch lastMatch;
114     while ( matches.hasNext() ) {
115         QRegularExpressionMatch nextMatch = matches.peekNext();
116         if ( column >= 0 && nextMatch.capturedEnd() >= column ) {
117             break;
118         }
119 
120         lastMatch = matches.next();
121     }
122 
123     if ( lastMatch.hasMatch() ) {
124         lastMatchStart_ = lastMatch.capturedStart();
125         lastMatchEnd_ = lastMatch.capturedEnd() - 1;
126         return true;
127     }
128     else {
129         return false;
130     }
131 }
132 
getLastMatch(int * start_col,int * end_col) const133 void QuickFindPattern::getLastMatch( int* start_col, int* end_col ) const
134 {
135     *start_col = lastMatchStart_;
136     *end_col   = lastMatchEnd_;
137 }
138