xref: /glogg/src/quickfindpattern.cpp (revision bb02e0acf44ddb4e4f83d6127a1e488789162922)
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 void QuickFindPattern::changeSearchPattern( const QString& pattern )
36 {
37     // Determine the type of regexp depending on the config
38     QRegExp::PatternSyntax syntax;
39     switch ( Persistent<Configuration>( "settings" ).quickfindRegexpType() ) {
40         case Wildcard:
41             syntax = QRegExp::Wildcard;
42             break;
43         case FixedString:
44             syntax = QRegExp::FixedString;
45             break;
46         default:
47             syntax = QRegExp::RegExp2;
48             break;
49     }
50 
51     regexp_.setPattern( pattern );
52     regexp_.setPatternSyntax( syntax );
53 
54     if ( regexp_.isValid() && ( ! regexp_.isEmpty() ) )
55         active_ = true;
56     else
57         active_ = false;
58 
59     emit patternUpdated();
60 }
61 
62 void QuickFindPattern::changeSearchPattern( const QString& pattern, bool ignoreCase )
63 {
64     regexp_.setCaseSensitivity(
65             ignoreCase ? Qt::CaseInsensitive : Qt::CaseSensitive );
66     changeSearchPattern( pattern );
67 }
68 
69 bool QuickFindPattern::matchLine( const QString& line,
70         QList<QuickFindMatch>& matches ) const
71 {
72     matches.clear();
73 
74     if ( active_ ) {
75         int pos = 0;
76         while ( ( pos = regexp_.indexIn( line, pos ) ) != -1 ) {
77             int length = regexp_.matchedLength();
78             matches << QuickFindMatch( pos, length );
79             pos += length;
80         }
81     }
82 
83     return ( matches.count() > 0 );
84 }
85 
86 bool QuickFindPattern::isLineMatching( const QString& line, int column ) const
87 {
88     int pos = 0;
89 
90     if ( ! active_ )
91         return false;
92     if ( ( pos = regexp_.indexIn( line, column ) ) != -1 ) {
93         lastMatchStart_ = pos;
94         lastMatchEnd_   = pos + regexp_.matchedLength() - 1;
95 
96         return true;
97     }
98     else
99         return false;
100 }
101 
102 bool QuickFindPattern::isLineMatchingBackward(
103         const QString& line, int column ) const
104 {
105     int pos = 0;
106 
107     if ( ! active_ )
108         return false;
109     if ( ( pos = regexp_.lastIndexIn( line, column ) ) != -1 ) {
110         lastMatchStart_ = pos;
111         lastMatchEnd_   = pos + regexp_.matchedLength() - 1;
112 
113         return true;
114     }
115     else
116         return false;
117 }
118 
119 void QuickFindPattern::getLastMatch( int* start_col, int* end_col ) const
120 {
121     *start_col = lastMatchStart_;
122     *end_col   = lastMatchEnd_;
123 }
124