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 35*7cbb2ca4SNicolas Bonnefon #include <iostream> 36*7cbb2ca4SNicolas Bonnefon 37bb02e0acSNicolas Bonnefon void QuickFindPattern::changeSearchPattern( const QString& pattern ) 38bb02e0acSNicolas Bonnefon { 39bb02e0acSNicolas Bonnefon // Determine the type of regexp depending on the config 404fb0346eSAnton Filimonov QString searchPattern; 4111582726SNicolas Bonnefon switch ( Persistent<Configuration>( "settings" )->quickfindRegexpType() ) { 42bb02e0acSNicolas Bonnefon case FixedString: 434fb0346eSAnton Filimonov searchPattern = QRegularExpression::escape(pattern); 44bb02e0acSNicolas Bonnefon break; 45bb02e0acSNicolas Bonnefon default: 464fb0346eSAnton Filimonov searchPattern = pattern; 47bb02e0acSNicolas Bonnefon break; 48bb02e0acSNicolas Bonnefon } 49bb02e0acSNicolas Bonnefon 50*7cbb2ca4SNicolas Bonnefon regexp_.setPattern( searchPattern ); 51bb02e0acSNicolas Bonnefon 524fb0346eSAnton Filimonov if ( regexp_.isValid() && ( ! searchPattern.isEmpty() ) ) 53bb02e0acSNicolas Bonnefon active_ = true; 54bb02e0acSNicolas Bonnefon else 55bb02e0acSNicolas Bonnefon active_ = false; 56bb02e0acSNicolas Bonnefon 57bb02e0acSNicolas Bonnefon emit patternUpdated(); 58bb02e0acSNicolas Bonnefon } 59bb02e0acSNicolas Bonnefon 60bb02e0acSNicolas Bonnefon void QuickFindPattern::changeSearchPattern( const QString& pattern, bool ignoreCase ) 61bb02e0acSNicolas Bonnefon { 624fb0346eSAnton Filimonov QRegularExpression::PatternOptions options = 634fb0346eSAnton Filimonov QRegularExpression::UseUnicodePropertiesOption 644fb0346eSAnton Filimonov | QRegularExpression::OptimizeOnFirstUsageOption; 654fb0346eSAnton Filimonov 664fb0346eSAnton Filimonov if ( ignoreCase ) 674fb0346eSAnton Filimonov options |= QRegularExpression::CaseInsensitiveOption; 684fb0346eSAnton Filimonov 694fb0346eSAnton Filimonov regexp_.setPatternOptions(options); 70bb02e0acSNicolas Bonnefon changeSearchPattern( pattern ); 71bb02e0acSNicolas Bonnefon } 72bb02e0acSNicolas Bonnefon 73bb02e0acSNicolas Bonnefon bool QuickFindPattern::matchLine( const QString& line, 74bb02e0acSNicolas Bonnefon QList<QuickFindMatch>& matches ) const 75bb02e0acSNicolas Bonnefon { 76bb02e0acSNicolas Bonnefon matches.clear(); 77bb02e0acSNicolas Bonnefon 78bb02e0acSNicolas Bonnefon if ( active_ ) { 794fb0346eSAnton Filimonov QRegularExpressionMatchIterator matchIterator = regexp_.globalMatch(line); 804fb0346eSAnton Filimonov 814fb0346eSAnton Filimonov while( matchIterator.hasNext() ) { 824fb0346eSAnton Filimonov QRegularExpressionMatch match = matchIterator.next(); 834fb0346eSAnton Filimonov matches << QuickFindMatch ( match.capturedStart(), match.capturedLength() ); 84bb02e0acSNicolas Bonnefon } 85bb02e0acSNicolas Bonnefon } 86bb02e0acSNicolas Bonnefon 87bb02e0acSNicolas Bonnefon return ( matches.count() > 0 ); 88bb02e0acSNicolas Bonnefon } 89bb02e0acSNicolas Bonnefon 90bb02e0acSNicolas Bonnefon bool QuickFindPattern::isLineMatching( const QString& line, int column ) const 91bb02e0acSNicolas Bonnefon { 92bb02e0acSNicolas Bonnefon if ( ! active_ ) 93bb02e0acSNicolas Bonnefon return false; 94bb02e0acSNicolas Bonnefon 954fb0346eSAnton Filimonov QRegularExpressionMatch match = regexp_.match( line, column ); 964fb0346eSAnton Filimonov if ( match.hasMatch() ) { 974fb0346eSAnton Filimonov lastMatchStart_ = match.capturedStart(); 984fb0346eSAnton Filimonov lastMatchEnd_ = match.capturedEnd() - 1; 99bb02e0acSNicolas Bonnefon return true; 100bb02e0acSNicolas Bonnefon } 1014fb0346eSAnton Filimonov else { 102bb02e0acSNicolas Bonnefon return false; 103bb02e0acSNicolas Bonnefon } 1044fb0346eSAnton Filimonov } 105bb02e0acSNicolas Bonnefon 106bb02e0acSNicolas Bonnefon bool QuickFindPattern::isLineMatchingBackward( 107bb02e0acSNicolas Bonnefon const QString& line, int column ) const 108bb02e0acSNicolas Bonnefon { 109bb02e0acSNicolas Bonnefon int pos = 0; 110bb02e0acSNicolas Bonnefon 111bb02e0acSNicolas Bonnefon if ( ! active_ ) 112bb02e0acSNicolas Bonnefon return false; 113bb02e0acSNicolas Bonnefon 1144fb0346eSAnton Filimonov QRegularExpressionMatchIterator matches = regexp_.globalMatch(line); 1154fb0346eSAnton Filimonov QRegularExpressionMatch lastMatch; 1164fb0346eSAnton Filimonov while ( matches.hasNext() ) { 1174fb0346eSAnton Filimonov QRegularExpressionMatch nextMatch = matches.peekNext(); 1184fb0346eSAnton Filimonov if ( column >= 0 && nextMatch.capturedEnd() >= column ) { 1194fb0346eSAnton Filimonov break; 1204fb0346eSAnton Filimonov } 1214fb0346eSAnton Filimonov 1224fb0346eSAnton Filimonov lastMatch = matches.next(); 1234fb0346eSAnton Filimonov } 1244fb0346eSAnton Filimonov 1254fb0346eSAnton Filimonov if ( lastMatch.hasMatch() ) { 1264fb0346eSAnton Filimonov lastMatchStart_ = lastMatch.capturedStart(); 1274fb0346eSAnton Filimonov lastMatchEnd_ = lastMatch.capturedEnd() - 1; 128bb02e0acSNicolas Bonnefon return true; 129bb02e0acSNicolas Bonnefon } 1304fb0346eSAnton Filimonov else { 131bb02e0acSNicolas Bonnefon return false; 132bb02e0acSNicolas Bonnefon } 1334fb0346eSAnton Filimonov } 134bb02e0acSNicolas Bonnefon 135bb02e0acSNicolas Bonnefon void QuickFindPattern::getLastMatch( int* start_col, int* end_col ) const 136bb02e0acSNicolas Bonnefon { 137bb02e0acSNicolas Bonnefon *start_col = lastMatchStart_; 138bb02e0acSNicolas Bonnefon *end_col = lastMatchEnd_; 139bb02e0acSNicolas Bonnefon } 140