1*bb02e0acSNicolas Bonnefon /* 2*bb02e0acSNicolas Bonnefon * Copyright (C) 2010, 2013 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 #ifndef QUICKFINDPATTERN_H 21*bb02e0acSNicolas Bonnefon #define QUICKFINDPATTERN_H 22*bb02e0acSNicolas Bonnefon 23*bb02e0acSNicolas Bonnefon #include <QObject> 24*bb02e0acSNicolas Bonnefon #include <QString> 25*bb02e0acSNicolas Bonnefon #include <QRegExp> 26*bb02e0acSNicolas Bonnefon #include <QList> 27*bb02e0acSNicolas Bonnefon 28*bb02e0acSNicolas Bonnefon // Represents a match result for QuickFind 29*bb02e0acSNicolas Bonnefon class QuickFindMatch 30*bb02e0acSNicolas Bonnefon { 31*bb02e0acSNicolas Bonnefon public: 32*bb02e0acSNicolas Bonnefon // Construct a match (must be initialised) 33*bb02e0acSNicolas Bonnefon QuickFindMatch( int start_column, int length ) 34*bb02e0acSNicolas Bonnefon { startColumn_ = start_column; length_ = length; } 35*bb02e0acSNicolas Bonnefon 36*bb02e0acSNicolas Bonnefon // Accessor functions 37*bb02e0acSNicolas Bonnefon int startColumn() const { return startColumn_; } 38*bb02e0acSNicolas Bonnefon int length() const { return length_; } 39*bb02e0acSNicolas Bonnefon 40*bb02e0acSNicolas Bonnefon private: 41*bb02e0acSNicolas Bonnefon int startColumn_; 42*bb02e0acSNicolas Bonnefon int length_; 43*bb02e0acSNicolas Bonnefon }; 44*bb02e0acSNicolas Bonnefon 45*bb02e0acSNicolas Bonnefon // Represents a search pattern for QuickFind (without its results) 46*bb02e0acSNicolas Bonnefon class QuickFindPattern : public QObject 47*bb02e0acSNicolas Bonnefon { 48*bb02e0acSNicolas Bonnefon Q_OBJECT 49*bb02e0acSNicolas Bonnefon 50*bb02e0acSNicolas Bonnefon public: 51*bb02e0acSNicolas Bonnefon // Construct an empty search 52*bb02e0acSNicolas Bonnefon QuickFindPattern(); 53*bb02e0acSNicolas Bonnefon 54*bb02e0acSNicolas Bonnefon // Set the search to a new pattern, using the current 55*bb02e0acSNicolas Bonnefon // case status 56*bb02e0acSNicolas Bonnefon void changeSearchPattern( const QString& pattern ); 57*bb02e0acSNicolas Bonnefon 58*bb02e0acSNicolas Bonnefon // Set the search to a new pattern, as well as the case status 59*bb02e0acSNicolas Bonnefon void changeSearchPattern( const QString& pattern, bool ignoreCase ); 60*bb02e0acSNicolas Bonnefon 61*bb02e0acSNicolas Bonnefon // Returns whether the search is active (i.e. valid and non empty regexp) 62*bb02e0acSNicolas Bonnefon bool isActive() const { return active_; } 63*bb02e0acSNicolas Bonnefon 64*bb02e0acSNicolas Bonnefon // Return the text of the regex 65*bb02e0acSNicolas Bonnefon QString getPattern() const { return regexp_.pattern(); } 66*bb02e0acSNicolas Bonnefon 67*bb02e0acSNicolas Bonnefon // Returns whether the passed line match the quick find search. 68*bb02e0acSNicolas Bonnefon // If so, it populate the passed list with the list of matches 69*bb02e0acSNicolas Bonnefon // within this particular line. 70*bb02e0acSNicolas Bonnefon bool matchLine( const QString& line, 71*bb02e0acSNicolas Bonnefon QList<QuickFindMatch>& matches ) const; 72*bb02e0acSNicolas Bonnefon 73*bb02e0acSNicolas Bonnefon // Returns whether there is a match in the passed line, starting at 74*bb02e0acSNicolas Bonnefon // the passed column. 75*bb02e0acSNicolas Bonnefon // Results are stored internally. 76*bb02e0acSNicolas Bonnefon bool isLineMatching( const QString& line, int column = 0 ) const; 77*bb02e0acSNicolas Bonnefon 78*bb02e0acSNicolas Bonnefon // Same as isLineMatching but search backward 79*bb02e0acSNicolas Bonnefon bool isLineMatchingBackward( const QString& line, int column = -1 ) const; 80*bb02e0acSNicolas Bonnefon 81*bb02e0acSNicolas Bonnefon // Must be called when isLineMatching returns 'true', returns 82*bb02e0acSNicolas Bonnefon // the position of the first match found. 83*bb02e0acSNicolas Bonnefon void getLastMatch( int* start_col, int* end_col ) const; 84*bb02e0acSNicolas Bonnefon 85*bb02e0acSNicolas Bonnefon signals: 86*bb02e0acSNicolas Bonnefon // Sent when the pattern is changed 87*bb02e0acSNicolas Bonnefon void patternUpdated(); 88*bb02e0acSNicolas Bonnefon 89*bb02e0acSNicolas Bonnefon private: 90*bb02e0acSNicolas Bonnefon bool active_; 91*bb02e0acSNicolas Bonnefon QRegExp regexp_; 92*bb02e0acSNicolas Bonnefon 93*bb02e0acSNicolas Bonnefon mutable int lastMatchStart_; 94*bb02e0acSNicolas Bonnefon mutable int lastMatchEnd_; 95*bb02e0acSNicolas Bonnefon }; 96*bb02e0acSNicolas Bonnefon 97*bb02e0acSNicolas Bonnefon #endif 98