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 QUICKFIND_H 21bb02e0acSNicolas Bonnefon #define QUICKFIND_H 22bb02e0acSNicolas Bonnefon 23bb02e0acSNicolas Bonnefon #include <QObject> 24bb02e0acSNicolas Bonnefon #include <QPoint> 25bb02e0acSNicolas Bonnefon #include <QTime> 26bb02e0acSNicolas Bonnefon 27bb02e0acSNicolas Bonnefon #include "utils.h" 28bb02e0acSNicolas Bonnefon #include "qfnotifications.h" 29bb02e0acSNicolas Bonnefon #include "selection.h" 30bb02e0acSNicolas Bonnefon 31bb02e0acSNicolas Bonnefon class QuickFindPattern; 32bb02e0acSNicolas Bonnefon class AbstractLogData; 33bb02e0acSNicolas Bonnefon class Portion; 34bb02e0acSNicolas Bonnefon 35bb02e0acSNicolas Bonnefon // Handle "long processing" notifications to the UI. 36bb02e0acSNicolas Bonnefon // reset() shall be called at the beginning of the search 37bb02e0acSNicolas Bonnefon // and then ping() should be called periodically during the processing. 38bb02e0acSNicolas Bonnefon // The notify() signal should be forwarded to the UI. 39bb02e0acSNicolas Bonnefon class SearchingNotifier : public QObject 40bb02e0acSNicolas Bonnefon { 41bb02e0acSNicolas Bonnefon Q_OBJECT 42bb02e0acSNicolas Bonnefon 43bb02e0acSNicolas Bonnefon public: SearchingNotifier()44bb02e0acSNicolas Bonnefon SearchingNotifier() {}; 45bb02e0acSNicolas Bonnefon 46bb02e0acSNicolas Bonnefon // Reset internal timers at the beiginning of the processing 47bb02e0acSNicolas Bonnefon void reset(); 48bb02e0acSNicolas Bonnefon // Shall be called frequently during processing, send the notification 49bb02e0acSNicolas Bonnefon // and call the event loop when appropriate. 50bb02e0acSNicolas Bonnefon // Pass the current line number and total number of line so that 51bb02e0acSNicolas Bonnefon // a progress percentage is calculated and displayed. 52bb02e0acSNicolas Bonnefon // (line shall be negative if ging in reverse) ping(qint64 line,qint64 nb_lines)53bb02e0acSNicolas Bonnefon inline void ping( qint64 line, qint64 nb_lines ) { 54*e85b29ccSNicolas Bonnefon if ( startTime_.msecsTo( QTime::currentTime() ) > 200 ) 55bb02e0acSNicolas Bonnefon sendNotification( line, nb_lines ); 56bb02e0acSNicolas Bonnefon } 57bb02e0acSNicolas Bonnefon 58bb02e0acSNicolas Bonnefon signals: 59bb02e0acSNicolas Bonnefon // Sent when the UI shall display a message to the user. 60bb02e0acSNicolas Bonnefon void notify( const QFNotification& message ); 61bb02e0acSNicolas Bonnefon 62bb02e0acSNicolas Bonnefon private: 63bb02e0acSNicolas Bonnefon void sendNotification( qint64 current_line, qint64 nb_lines ); 64bb02e0acSNicolas Bonnefon 65bb02e0acSNicolas Bonnefon QTime startTime_; 66bb02e0acSNicolas Bonnefon int dotToDisplay_; 67bb02e0acSNicolas Bonnefon }; 68bb02e0acSNicolas Bonnefon 69bb02e0acSNicolas Bonnefon // Represents a search made with Quick Find (without its results) 70bb02e0acSNicolas Bonnefon // it keeps a pointer to a set of data and to a QuickFindPattern which 71bb02e0acSNicolas Bonnefon // are used for the searches. (the caller retains ownership of both). 72bb02e0acSNicolas Bonnefon class QuickFind : public QObject 73bb02e0acSNicolas Bonnefon { 74bb02e0acSNicolas Bonnefon Q_OBJECT 75bb02e0acSNicolas Bonnefon 76bb02e0acSNicolas Bonnefon public: 77bb02e0acSNicolas Bonnefon // Construct a search 78bb02e0acSNicolas Bonnefon QuickFind( const AbstractLogData* const logData, Selection* selection, 79bb02e0acSNicolas Bonnefon const QuickFindPattern* const quickFindPattern ); 80bb02e0acSNicolas Bonnefon 81bb02e0acSNicolas Bonnefon // Set the starting point that will be used by the next search 82bb02e0acSNicolas Bonnefon void setSearchStartPoint( QPoint startPoint ); 83bb02e0acSNicolas Bonnefon 84bb02e0acSNicolas Bonnefon // Used for incremental searches 85bb02e0acSNicolas Bonnefon // Return the first occurence of the passed pattern from the starting 86bb02e0acSNicolas Bonnefon // point. These searches don't use the QFP and don't change the 87bb02e0acSNicolas Bonnefon // starting point. 88bb02e0acSNicolas Bonnefon // TODO Update comment 89bb02e0acSNicolas Bonnefon qint64 incrementallySearchForward(); 90bb02e0acSNicolas Bonnefon qint64 incrementallySearchBackward(); 91bb02e0acSNicolas Bonnefon 92bb02e0acSNicolas Bonnefon // Stop the currently ongoing incremental search, leave the selection 93bb02e0acSNicolas Bonnefon // where it is if a match has been found, restore the old one 94bb02e0acSNicolas Bonnefon // if not. Also throw away the start point associated with 95bb02e0acSNicolas Bonnefon // the search. 96bb02e0acSNicolas Bonnefon void incrementalSearchStop(); 97bb02e0acSNicolas Bonnefon 98bb02e0acSNicolas Bonnefon // Throw away the current search and restore the initial 99bb02e0acSNicolas Bonnefon // position/selection 100bb02e0acSNicolas Bonnefon void incrementalSearchAbort(); 101bb02e0acSNicolas Bonnefon 102bb02e0acSNicolas Bonnefon // Used for 'repeated' (n/N) QF searches using the current direction 103bb02e0acSNicolas Bonnefon // Return the line of the first occurence of the QFP and 104bb02e0acSNicolas Bonnefon // update the selection. It returns -1 if nothing is found. 105bb02e0acSNicolas Bonnefon /* 106bb02e0acSNicolas Bonnefon int searchNext(); 107bb02e0acSNicolas Bonnefon int searchPrevious(); 108bb02e0acSNicolas Bonnefon */ 109bb02e0acSNicolas Bonnefon 110bb02e0acSNicolas Bonnefon // Idem but ignore the direction and always search in the 111bb02e0acSNicolas Bonnefon // specified direction 112bb02e0acSNicolas Bonnefon qint64 searchForward(); 113bb02e0acSNicolas Bonnefon qint64 searchBackward(); 114bb02e0acSNicolas Bonnefon 115bb02e0acSNicolas Bonnefon // Make the object forget the 'no more match' flag. 116bb02e0acSNicolas Bonnefon void resetLimits(); 117bb02e0acSNicolas Bonnefon 118bb02e0acSNicolas Bonnefon signals: 119bb02e0acSNicolas Bonnefon // Sent when the UI shall display a message to the user. 120bb02e0acSNicolas Bonnefon void notify( const QFNotification& message ); 121bb02e0acSNicolas Bonnefon // Sent when the UI shall clear the notification. 122bb02e0acSNicolas Bonnefon void clearNotification(); 123bb02e0acSNicolas Bonnefon 124bb02e0acSNicolas Bonnefon private: 125bb02e0acSNicolas Bonnefon enum QFDirection { 126bb02e0acSNicolas Bonnefon None, 127bb02e0acSNicolas Bonnefon Forward, 128bb02e0acSNicolas Bonnefon Backward, 129bb02e0acSNicolas Bonnefon }; 130bb02e0acSNicolas Bonnefon 131*e85b29ccSNicolas Bonnefon enum class SearchState { 132*e85b29ccSNicolas Bonnefon Idle, 133*e85b29ccSNicolas Bonnefon OnGoing, 134*e85b29ccSNicolas Bonnefon RestartNeeded, 135*e85b29ccSNicolas Bonnefon }; 136*e85b29ccSNicolas Bonnefon 137bb02e0acSNicolas Bonnefon class LastMatchPosition { 138bb02e0acSNicolas Bonnefon public: LastMatchPosition()139bb02e0acSNicolas Bonnefon LastMatchPosition() : line_( -1 ), column_( -1 ) {} 140bb02e0acSNicolas Bonnefon void set( int line, int column ); 141bb02e0acSNicolas Bonnefon void set( const FilePosition& position ); reset()142bb02e0acSNicolas Bonnefon void reset() { line_ = -1; column_ = -1; } 143bb02e0acSNicolas Bonnefon // Does the passed position come after the recorded one 144bb02e0acSNicolas Bonnefon bool isLater( int line, int column ) const; 145bb02e0acSNicolas Bonnefon bool isLater( const FilePosition& position ) const; 146bb02e0acSNicolas Bonnefon // Does the passed position come before the recorded one 147bb02e0acSNicolas Bonnefon bool isSooner( int line, int column ) const; 148bb02e0acSNicolas Bonnefon bool isSooner( const FilePosition& position ) const; 149bb02e0acSNicolas Bonnefon 150bb02e0acSNicolas Bonnefon private: 151bb02e0acSNicolas Bonnefon int line_; 152bb02e0acSNicolas Bonnefon int column_; 153bb02e0acSNicolas Bonnefon }; 154bb02e0acSNicolas Bonnefon 155bb02e0acSNicolas Bonnefon class IncrementalSearchStatus { 156bb02e0acSNicolas Bonnefon public: 157bb02e0acSNicolas Bonnefon /* Constructors */ IncrementalSearchStatus()158bb02e0acSNicolas Bonnefon IncrementalSearchStatus() : 159bb02e0acSNicolas Bonnefon ongoing_( None ), position_(), initialSelection_() {} IncrementalSearchStatus(QFDirection direction,const FilePosition & position,const Selection & initial_selection)160bb02e0acSNicolas Bonnefon IncrementalSearchStatus( 161bb02e0acSNicolas Bonnefon QFDirection direction, 162bb02e0acSNicolas Bonnefon const FilePosition& position, 163bb02e0acSNicolas Bonnefon const Selection& initial_selection ) : 164bb02e0acSNicolas Bonnefon ongoing_( direction ), 165bb02e0acSNicolas Bonnefon position_( position ), 166bb02e0acSNicolas Bonnefon initialSelection_( initial_selection ) {} 167bb02e0acSNicolas Bonnefon isOngoing()168bb02e0acSNicolas Bonnefon bool isOngoing() const { return ( ongoing_ != None ); } direction()169bb02e0acSNicolas Bonnefon QFDirection direction() const { return ongoing_; } position()170bb02e0acSNicolas Bonnefon FilePosition position() const { return position_; } initialSelection()171bb02e0acSNicolas Bonnefon Selection initialSelection() const { return initialSelection_; } 172bb02e0acSNicolas Bonnefon private: 173bb02e0acSNicolas Bonnefon QFDirection ongoing_; 174bb02e0acSNicolas Bonnefon FilePosition position_; 175bb02e0acSNicolas Bonnefon Selection initialSelection_; 176bb02e0acSNicolas Bonnefon }; 177bb02e0acSNicolas Bonnefon 178bb02e0acSNicolas Bonnefon // Pointers to external objects 179bb02e0acSNicolas Bonnefon const AbstractLogData* const logData_; 180bb02e0acSNicolas Bonnefon Selection* selection_; 181bb02e0acSNicolas Bonnefon const QuickFindPattern* const quickFindPattern_; 182bb02e0acSNicolas Bonnefon 183bb02e0acSNicolas Bonnefon // Owned objects 184bb02e0acSNicolas Bonnefon 185bb02e0acSNicolas Bonnefon // Position of the last match in the file 186bb02e0acSNicolas Bonnefon // (to avoid searching multiple times where there is no result) 187bb02e0acSNicolas Bonnefon LastMatchPosition lastMatch_; 188bb02e0acSNicolas Bonnefon LastMatchPosition firstMatch_; 189bb02e0acSNicolas Bonnefon 190bb02e0acSNicolas Bonnefon SearchingNotifier searchingNotifier_; 191bb02e0acSNicolas Bonnefon 192bb02e0acSNicolas Bonnefon // Incremental search status 193bb02e0acSNicolas Bonnefon IncrementalSearchStatus incrementalSearchStatus_; 194bb02e0acSNicolas Bonnefon 195*e85b29ccSNicolas Bonnefon // Flag to request stopping the search 196*e85b29ccSNicolas Bonnefon // (e.g. on key press) 197*e85b29ccSNicolas Bonnefon SearchState searchState_; 198*e85b29ccSNicolas Bonnefon 199bb02e0acSNicolas Bonnefon // Private functions 200bb02e0acSNicolas Bonnefon qint64 doSearchForward( const FilePosition &start_position ); 201bb02e0acSNicolas Bonnefon qint64 doSearchBackward( const FilePosition &start_position ); 202bb02e0acSNicolas Bonnefon }; 203bb02e0acSNicolas Bonnefon 204bb02e0acSNicolas Bonnefon #endif 205