1 /* 2 * Copyright (C) 2009, 2010, 2011, 2013, 2014, 2015 Nicolas Bonnefon 3 * and other contributors 4 * 5 * This file is part of glogg. 6 * 7 * glogg is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * glogg is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with glogg. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #ifndef CRAWLERWIDGET_H 22 #define CRAWLERWIDGET_H 23 24 #include <QSplitter> 25 #include <QComboBox> 26 #include <QPushButton> 27 #include <QCheckBox> 28 #include <QToolButton> 29 #include <QVBoxLayout> 30 #include <QHBoxLayout> 31 #include <QLabel> 32 33 #include "logmainview.h" 34 #include "filteredview.h" 35 #include "data/logdata.h" 36 #include "data/logfiltereddata.h" 37 #include "viewinterface.h" 38 #include "signalmux.h" 39 #include "overview.h" 40 #include "loadingstatus.h" 41 42 class InfoLine; 43 class QuickFindPattern; 44 class SavedSearches; 45 class QStandardItemModel; 46 class OverviewWidget; 47 48 // Implements the central widget of the application. 49 // It includes both windows, the search line, the info 50 // lines and various buttons. 51 class CrawlerWidget : public QSplitter, 52 public QuickFindMuxSelectorInterface, public ViewInterface, 53 public MuxableDocumentInterface 54 { 55 Q_OBJECT 56 57 public: 58 CrawlerWidget( QWidget *parent=0 ); 59 60 // Get the line number of the first line displayed. 61 int getTopLine() const; 62 // Get the selected text as a string (from the main window) 63 QString getSelectedText() const; 64 65 // Display the QFB at the bottom, remembering where the focus was 66 void displayQuickFindBar( QuickFindMux::QFDirection direction ); 67 68 // Instructs the widget to select all the text in the window the user 69 // is interacting with 70 void selectAll(); 71 72 public slots: 73 // Stop the asynchoronous loading of the file if one is in progress 74 // The file is identified by the view attached to it. 75 void stopLoading(); 76 // Reload the displayed file 77 void reload(); 78 79 protected: 80 // Implementation of the ViewInterface functions 81 virtual void doSetData( 82 std::shared_ptr<LogData> log_data, 83 std::shared_ptr<LogFilteredData> filtered_data ); 84 virtual void doSetQuickFindPattern( 85 std::shared_ptr<QuickFindPattern> qfp ); 86 virtual void doSetSavedSearches( 87 std::shared_ptr<SavedSearches> saved_searches ); 88 virtual void doSetViewContext( const char* view_context ); 89 virtual std::shared_ptr<const ViewContextInterface> 90 doGetViewContext( void ) const; 91 92 // Implementation of the mux selector interface 93 // (for dispatching QuickFind to the right widget) 94 virtual SearchableWidgetInterface* doGetActiveSearchable() const; 95 virtual std::vector<QObject*> doGetAllSearchables() const; 96 97 // Implementation of the MuxableDocumentInterface 98 virtual void doSendAllStateSignals(); 99 100 signals: 101 // Sent to signal the client load has progressed, 102 // passing the completion percentage. 103 void loadingProgressed( int progress ); 104 // Sent to the client when the loading has finished 105 // weither succesfull or not. 106 void loadingFinished( LoadingStatus status ); 107 // Sent when follow mode is enabled/disabled 108 void followSet( bool checked ); 109 // Sent up to the MainWindow to enable/disable the follow mode 110 void followModeChanged( bool follow ); 111 // Sent up when the current line number is updated 112 void updateLineNumber( int line ); 113 114 // "auto-refresh" check has been changed 115 void searchRefreshChanged( int state ); 116 // "ignore case" check has been changed 117 void ignoreCaseChanged( int state ); 118 119 // Sent when the data status (whether new not seen data are 120 // available) has changed 121 void dataStatusChanged( DataStatus status ); 122 123 private slots: 124 // Instructs the widget to start a search using the current search line. 125 void startNewSearch(); 126 // Stop the currently ongoing search (if one exists) 127 void stopSearch(); 128 // Instructs the widget to reconfigure itself because Config() has changed. 129 void applyConfiguration(); 130 // QuickFind is being entered, save the focus for incremental qf. 131 void enteringQuickFind(); 132 // QuickFind is being closed. 133 void exitingQuickFind(); 134 // Called when new data must be displayed in the filtered window. 135 void updateFilteredView( int nbMatches, int progress ); 136 // Called when a new line has been selected in the filtered view, 137 // to instruct the main view to jump to the matching line. 138 void jumpToMatchingLine( int filteredLineNb ); 139 // Called when the main view is on a new line number 140 void updateLineNumberHandler( int line ); 141 // Mark a line that has been clicked on the main (top) view. 142 void markLineFromMain( qint64 line ); 143 // Mark a line that has been clicked on the filtered (bottom) view. 144 void markLineFromFiltered( qint64 line ); 145 146 void loadingFinishedHandler( LoadingStatus status ); 147 // Manages the info lines to inform the user the file has changed. 148 void fileChangedHandler( LogData::MonitoredFileStatus ); 149 150 void searchForward(); 151 void searchBackward(); 152 153 // Called when the checkbox for search auto-refresh is changed 154 void searchRefreshChangedHandler( int state ); 155 156 // Called when the text on the search line is modified 157 void searchTextChangeHandler(); 158 159 // Called when the user change the visibility combobox 160 void changeFilteredViewVisibility( int index ); 161 162 // Called when the user add the string to the search 163 void addToSearch( const QString& string ); 164 165 // Called when a match is hovered on in the filtered view 166 void mouseHoveredOverMatch( qint64 line ); 167 168 // Called when there was activity in the views 169 void activityDetected(); 170 171 private: 172 // State machine holding the state of the search, used to allow/disallow 173 // auto-refresh and inform the user via the info line. 174 class SearchState { 175 public: 176 enum State { 177 NoSearch, 178 Static, 179 Autorefreshing, 180 FileTruncated, 181 TruncatedAutorefreshing, 182 }; 183 184 SearchState() { state_ = NoSearch; autoRefreshRequested_ = false; } 185 186 // Reset the state (no search active) 187 void resetState(); 188 // The user changed auto-refresh request 189 void setAutorefresh( bool refresh ); 190 // The file has been truncated (stops auto-refresh) 191 void truncateFile(); 192 // The expression has been changed (stops auto-refresh) 193 void changeExpression(); 194 // The search has been stopped (stops auto-refresh) 195 void stopSearch(); 196 // The search has been started (enable auto-refresh) 197 void startSearch(); 198 199 // Get the state in order to display the proper message 200 State getState() const { return state_; } 201 // Is auto-refresh allowed 202 bool isAutorefreshAllowed() const 203 { return ( state_ == Autorefreshing || state_ == TruncatedAutorefreshing ); } 204 bool isFileTruncated() const 205 { return ( state_ == FileTruncated || state_ == TruncatedAutorefreshing ); } 206 207 private: 208 State state_; 209 bool autoRefreshRequested_; 210 }; 211 212 // Private functions 213 void setup(); 214 void replaceCurrentSearch( const QString& searchText ); 215 void updateSearchCombo(); 216 AbstractLogView* activeView() const; 217 void printSearchInfoMessage( int nbMatches = 0 ); 218 void changeDataStatus( DataStatus status ); 219 220 // Palette for error notification (yellow background) 221 static const QPalette errorPalette; 222 223 LogMainView* logMainView; 224 QWidget* bottomWindow; 225 QLabel* searchLabel; 226 QComboBox* searchLineEdit; 227 QToolButton* searchButton; 228 QToolButton* stopButton; 229 FilteredView* filteredView; 230 QComboBox* visibilityBox; 231 InfoLine* searchInfoLine; 232 QCheckBox* ignoreCaseCheck; 233 QCheckBox* searchRefreshCheck; 234 OverviewWidget* overviewWidget_; 235 236 QVBoxLayout* bottomMainLayout; 237 QHBoxLayout* searchLineLayout; 238 QHBoxLayout* searchInfoLineLayout; 239 240 // Default palette to be remembered 241 QPalette searchInfoLineDefaultPalette; 242 243 std::shared_ptr<SavedSearches> savedSearches_; 244 245 // Reference to the QuickFind Pattern (not owned) 246 std::shared_ptr<QuickFindPattern> quickFindPattern_; 247 248 LogData* logData_; 249 LogFilteredData* logFilteredData_; 250 251 qint64 logFileSize_; 252 253 QWidget* qfSavedFocus_; 254 255 // Search state (for auto-refresh and truncation) 256 SearchState searchState_; 257 258 // Matches overview 259 Overview overview_; 260 261 // Model for the visibility selector 262 QStandardItemModel* visibilityModel_; 263 264 // Last main line number received 265 qint64 currentLineNumber_; 266 267 // Are we loading something? 268 // Set to false when we receive a completion message from the LogData 269 bool loadingInProgress_; 270 271 // Is it not the first time we are loading something? 272 bool firstLoadDone_; 273 274 // the current dataStatus (whether we have new, not seen, data) 275 DataStatus dataStatus_; 276 }; 277 278 #endif 279