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