1*bb02e0acSNicolas Bonnefon /* 2*bb02e0acSNicolas Bonnefon * Copyright (C) 2010 Nicolas Bonnefon and other contributors 3*bb02e0acSNicolas Bonnefon * 4*bb02e0acSNicolas Bonnefon * This file is part of glogg. 5*bb02e0acSNicolas Bonnefon * 6*bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7*bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8*bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9*bb02e0acSNicolas Bonnefon * (at your option) any later version. 10*bb02e0acSNicolas Bonnefon * 11*bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12*bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13*bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*bb02e0acSNicolas Bonnefon * GNU General Public License for more details. 15*bb02e0acSNicolas Bonnefon * 16*bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17*bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18*bb02e0acSNicolas Bonnefon */ 19*bb02e0acSNicolas Bonnefon 20*bb02e0acSNicolas Bonnefon // This file implements QuickFindPattern. 21*bb02e0acSNicolas Bonnefon // This class implements part of the Quick Find mechanism, it only stores the 22*bb02e0acSNicolas Bonnefon // current search pattern, once it has been confirmed (return pressed), 23*bb02e0acSNicolas Bonnefon // it can be asked to return the matches in a specific string. 24*bb02e0acSNicolas Bonnefon 25*bb02e0acSNicolas Bonnefon #include "quickfindpattern.h" 26*bb02e0acSNicolas Bonnefon 27*bb02e0acSNicolas Bonnefon #include "persistentinfo.h" 28*bb02e0acSNicolas Bonnefon #include "configuration.h" 29*bb02e0acSNicolas Bonnefon 30*bb02e0acSNicolas Bonnefon QuickFindPattern::QuickFindPattern() : QObject(), regexp_() 31*bb02e0acSNicolas Bonnefon { 32*bb02e0acSNicolas Bonnefon active_ = false; 33*bb02e0acSNicolas Bonnefon } 34*bb02e0acSNicolas Bonnefon 35*bb02e0acSNicolas Bonnefon void QuickFindPattern::changeSearchPattern( const QString& pattern ) 36*bb02e0acSNicolas Bonnefon { 37*bb02e0acSNicolas Bonnefon // Determine the type of regexp depending on the config 38*bb02e0acSNicolas Bonnefon QRegExp::PatternSyntax syntax; 39*bb02e0acSNicolas Bonnefon switch ( Persistent<Configuration>( "settings" ).quickfindRegexpType() ) { 40*bb02e0acSNicolas Bonnefon case Wildcard: 41*bb02e0acSNicolas Bonnefon syntax = QRegExp::Wildcard; 42*bb02e0acSNicolas Bonnefon break; 43*bb02e0acSNicolas Bonnefon case FixedString: 44*bb02e0acSNicolas Bonnefon syntax = QRegExp::FixedString; 45*bb02e0acSNicolas Bonnefon break; 46*bb02e0acSNicolas Bonnefon default: 47*bb02e0acSNicolas Bonnefon syntax = QRegExp::RegExp2; 48*bb02e0acSNicolas Bonnefon break; 49*bb02e0acSNicolas Bonnefon } 50*bb02e0acSNicolas Bonnefon 51*bb02e0acSNicolas Bonnefon regexp_.setPattern( pattern ); 52*bb02e0acSNicolas Bonnefon regexp_.setPatternSyntax( syntax ); 53*bb02e0acSNicolas Bonnefon 54*bb02e0acSNicolas Bonnefon if ( regexp_.isValid() && ( ! regexp_.isEmpty() ) ) 55*bb02e0acSNicolas Bonnefon active_ = true; 56*bb02e0acSNicolas Bonnefon else 57*bb02e0acSNicolas Bonnefon active_ = false; 58*bb02e0acSNicolas Bonnefon 59*bb02e0acSNicolas Bonnefon emit patternUpdated(); 60*bb02e0acSNicolas Bonnefon } 61*bb02e0acSNicolas Bonnefon 62*bb02e0acSNicolas Bonnefon void QuickFindPattern::changeSearchPattern( const QString& pattern, bool ignoreCase ) 63*bb02e0acSNicolas Bonnefon { 64*bb02e0acSNicolas Bonnefon regexp_.setCaseSensitivity( 65*bb02e0acSNicolas Bonnefon ignoreCase ? Qt::CaseInsensitive : Qt::CaseSensitive ); 66*bb02e0acSNicolas Bonnefon changeSearchPattern( pattern ); 67*bb02e0acSNicolas Bonnefon } 68*bb02e0acSNicolas Bonnefon 69*bb02e0acSNicolas Bonnefon bool QuickFindPattern::matchLine( const QString& line, 70*bb02e0acSNicolas Bonnefon QList<QuickFindMatch>& matches ) const 71*bb02e0acSNicolas Bonnefon { 72*bb02e0acSNicolas Bonnefon matches.clear(); 73*bb02e0acSNicolas Bonnefon 74*bb02e0acSNicolas Bonnefon if ( active_ ) { 75*bb02e0acSNicolas Bonnefon int pos = 0; 76*bb02e0acSNicolas Bonnefon while ( ( pos = regexp_.indexIn( line, pos ) ) != -1 ) { 77*bb02e0acSNicolas Bonnefon int length = regexp_.matchedLength(); 78*bb02e0acSNicolas Bonnefon matches << QuickFindMatch( pos, length ); 79*bb02e0acSNicolas Bonnefon pos += length; 80*bb02e0acSNicolas Bonnefon } 81*bb02e0acSNicolas Bonnefon } 82*bb02e0acSNicolas Bonnefon 83*bb02e0acSNicolas Bonnefon return ( matches.count() > 0 ); 84*bb02e0acSNicolas Bonnefon } 85*bb02e0acSNicolas Bonnefon 86*bb02e0acSNicolas Bonnefon bool QuickFindPattern::isLineMatching( const QString& line, int column ) const 87*bb02e0acSNicolas Bonnefon { 88*bb02e0acSNicolas Bonnefon int pos = 0; 89*bb02e0acSNicolas Bonnefon 90*bb02e0acSNicolas Bonnefon if ( ! active_ ) 91*bb02e0acSNicolas Bonnefon return false; 92*bb02e0acSNicolas Bonnefon if ( ( pos = regexp_.indexIn( line, column ) ) != -1 ) { 93*bb02e0acSNicolas Bonnefon lastMatchStart_ = pos; 94*bb02e0acSNicolas Bonnefon lastMatchEnd_ = pos + regexp_.matchedLength() - 1; 95*bb02e0acSNicolas Bonnefon 96*bb02e0acSNicolas Bonnefon return true; 97*bb02e0acSNicolas Bonnefon } 98*bb02e0acSNicolas Bonnefon else 99*bb02e0acSNicolas Bonnefon return false; 100*bb02e0acSNicolas Bonnefon } 101*bb02e0acSNicolas Bonnefon 102*bb02e0acSNicolas Bonnefon bool QuickFindPattern::isLineMatchingBackward( 103*bb02e0acSNicolas Bonnefon const QString& line, int column ) const 104*bb02e0acSNicolas Bonnefon { 105*bb02e0acSNicolas Bonnefon int pos = 0; 106*bb02e0acSNicolas Bonnefon 107*bb02e0acSNicolas Bonnefon if ( ! active_ ) 108*bb02e0acSNicolas Bonnefon return false; 109*bb02e0acSNicolas Bonnefon if ( ( pos = regexp_.lastIndexIn( line, column ) ) != -1 ) { 110*bb02e0acSNicolas Bonnefon lastMatchStart_ = pos; 111*bb02e0acSNicolas Bonnefon lastMatchEnd_ = pos + regexp_.matchedLength() - 1; 112*bb02e0acSNicolas Bonnefon 113*bb02e0acSNicolas Bonnefon return true; 114*bb02e0acSNicolas Bonnefon } 115*bb02e0acSNicolas Bonnefon else 116*bb02e0acSNicolas Bonnefon return false; 117*bb02e0acSNicolas Bonnefon } 118*bb02e0acSNicolas Bonnefon 119*bb02e0acSNicolas Bonnefon void QuickFindPattern::getLastMatch( int* start_col, int* end_col ) const 120*bb02e0acSNicolas Bonnefon { 121*bb02e0acSNicolas Bonnefon *start_col = lastMatchStart_; 122*bb02e0acSNicolas Bonnefon *end_col = lastMatchEnd_; 123*bb02e0acSNicolas Bonnefon } 124