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