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