xref: /glogg/src/quickfindpattern.cpp (revision 7cbb2ca4497ec8f4a20fe2586dec49657d3308cb)
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 
30 QuickFindPattern::QuickFindPattern() : QObject(), regexp_()
31 {
32     active_ = false;
33 }
34 
35 #include <iostream>
36 
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 
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 
73 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 
90 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 
106 bool QuickFindPattern::isLineMatchingBackward(
107         const QString& line, int column ) const
108 {
109     int pos = 0;
110 
111     if ( ! active_ )
112         return false;
113 
114     QRegularExpressionMatchIterator matches = regexp_.globalMatch(line);
115     QRegularExpressionMatch lastMatch;
116     while ( matches.hasNext() ) {
117         QRegularExpressionMatch nextMatch = matches.peekNext();
118         if ( column >= 0 && nextMatch.capturedEnd() >= column ) {
119             break;
120         }
121 
122         lastMatch = matches.next();
123     }
124 
125     if ( lastMatch.hasMatch() ) {
126         lastMatchStart_ = lastMatch.capturedStart();
127         lastMatchEnd_ = lastMatch.capturedEnd() - 1;
128         return true;
129     }
130     else {
131         return false;
132     }
133 }
134 
135 void QuickFindPattern::getLastMatch( int* start_col, int* end_col ) const
136 {
137     *start_col = lastMatchStart_;
138     *end_col   = lastMatchEnd_;
139 }
140