xref: /glogg/src/crawlerwidget.h (revision 209000a64813870d3d6a4fc0d1af7898cc8c37ca)
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     Encoding encodingSetting() const;
73 
74     // Returns whether follow is enabled in this crawler
75     bool isFollowEnabled() const;
76 
77     // Get the text description of the encoding effectively used,
78     // suitable to display to the user.
79     QString encodingText() const;
80 
81   public slots:
82     // Stop the asynchoronous loading of the file if one is in progress
83     // The file is identified by the view attached to it.
84     void stopLoading();
85     // Reload the displayed file
86     void reload();
87     // Set the encoding
88     void setEncoding( Encoding encoding );
89 
90   protected:
91     // Implementation of the ViewInterface functions
92     virtual void doSetData(
93             std::shared_ptr<LogData> log_data,
94             std::shared_ptr<LogFilteredData> filtered_data );
95     virtual void doSetQuickFindPattern(
96             std::shared_ptr<QuickFindPattern> qfp );
97     virtual void doSetSavedSearches(
98             std::shared_ptr<SavedSearches> saved_searches );
99     virtual void doSetViewContext( const char* view_context );
100     virtual std::shared_ptr<const ViewContextInterface>
101         doGetViewContext( void ) const;
102 
103     // Implementation of the mux selector interface
104     // (for dispatching QuickFind to the right widget)
105     virtual SearchableWidgetInterface* doGetActiveSearchable() const;
106     virtual std::vector<QObject*> doGetAllSearchables() const;
107 
108     // Implementation of the MuxableDocumentInterface
109     virtual void doSendAllStateSignals();
110 
111     virtual void keyPressEvent( QKeyEvent* keyEvent );
112 
113   signals:
114     // Sent to signal the client load has progressed,
115     // passing the completion percentage.
116     void loadingProgressed( int progress );
117     // Sent to the client when the loading has finished
118     // weither succesfull or not.
119     void loadingFinished( LoadingStatus status );
120     // Sent when follow mode is enabled/disabled
121     void followSet( bool checked );
122     // Sent up to the MainWindow to enable/disable the follow mode
123     void followModeChanged( bool follow );
124     // Sent up when the current line number is updated
125     void updateLineNumber( int line );
126 
127     // "auto-refresh" check has been changed
128     void searchRefreshChanged( int state );
129     // "ignore case" check has been changed
130     void ignoreCaseChanged( int state );
131 
132     // Sent when the data status (whether new not seen data are
133     // available) has changed
134     void dataStatusChanged( DataStatus status );
135 
136   private slots:
137     // Instructs the widget to start a search using the current search line.
138     void startNewSearch();
139     // Stop the currently ongoing search (if one exists)
140     void stopSearch();
141     // Instructs the widget to reconfigure itself because Config() has changed.
142     void applyConfiguration();
143     // QuickFind is being entered, save the focus for incremental qf.
144     void enteringQuickFind();
145     // QuickFind is being closed.
146     void exitingQuickFind();
147     // Called when new data must be displayed in the filtered window.
148     void updateFilteredView( int nbMatches, int progress );
149     // Called when a new line has been selected in the filtered view,
150     // to instruct the main view to jump to the matching line.
151     void jumpToMatchingLine( int filteredLineNb );
152     // Called when the main view is on a new line number
153     void updateLineNumberHandler( int line );
154     // Mark a line that has been clicked on the main (top) view.
155     void markLineFromMain( qint64 line );
156     // Mark a line that has been clicked on the filtered (bottom) view.
157     void markLineFromFiltered( qint64 line );
158 
159     void loadingFinishedHandler( LoadingStatus status );
160     // Manages the info lines to inform the user the file has changed.
161     void fileChangedHandler( LogData::MonitoredFileStatus );
162 
163     void searchForward();
164     void searchBackward();
165 
166     // Called when the checkbox for search auto-refresh is changed
167     void searchRefreshChangedHandler( int state );
168 
169     // Called when the text on the search line is modified
170     void searchTextChangeHandler();
171 
172     // Called when the user change the visibility combobox
173     void changeFilteredViewVisibility( int index );
174 
175     // Called when the user add the string to the search
176     void addToSearch( const QString& string );
177 
178     // Called when a match is hovered on in the filtered view
179     void mouseHoveredOverMatch( qint64 line );
180 
181     // Called when there was activity in the views
182     void activityDetected();
183 
184   private:
185     // State machine holding the state of the search, used to allow/disallow
186     // auto-refresh and inform the user via the info line.
187     class SearchState {
188       public:
189         enum State {
190             NoSearch,
191             Static,
192             Autorefreshing,
193             FileTruncated,
194             TruncatedAutorefreshing,
195         };
196 
197         SearchState() { state_ = NoSearch; autoRefreshRequested_ = false; }
198 
199         // Reset the state (no search active)
200         void resetState();
201         // The user changed auto-refresh request
202         void setAutorefresh( bool refresh );
203         // The file has been truncated (stops auto-refresh)
204         void truncateFile();
205         // The expression has been changed (stops auto-refresh)
206         void changeExpression();
207         // The search has been stopped (stops auto-refresh)
208         void stopSearch();
209         // The search has been started (enable auto-refresh)
210         void startSearch();
211 
212         // Get the state in order to display the proper message
213         State getState() const { return state_; }
214         // Is auto-refresh allowed
215         bool isAutorefreshAllowed() const
216         { return ( state_ == Autorefreshing || state_ == TruncatedAutorefreshing ); }
217         bool isFileTruncated() const
218         { return ( state_ == FileTruncated || state_ == TruncatedAutorefreshing ); }
219 
220       private:
221         State state_;
222         bool autoRefreshRequested_;
223     };
224 
225     // Private functions
226     void setup();
227     void replaceCurrentSearch( const QString& searchText );
228     void updateSearchCombo();
229     AbstractLogView* activeView() const;
230     void printSearchInfoMessage( int nbMatches = 0 );
231     void changeDataStatus( DataStatus status );
232     void updateEncoding();
233     void changeTopViewSize( int32_t delta );
234 
235     // Palette for error notification (yellow background)
236     static const QPalette errorPalette;
237 
238     LogMainView*    logMainView;
239     QWidget*        bottomWindow;
240     QLabel*         searchLabel;
241     QComboBox*      searchLineEdit;
242     QToolButton*    searchButton;
243     QToolButton*    stopButton;
244     FilteredView*   filteredView;
245     QComboBox*      visibilityBox;
246     InfoLine*       searchInfoLine;
247     QCheckBox*      ignoreCaseCheck;
248     QCheckBox*      searchRefreshCheck;
249     OverviewWidget* overviewWidget_;
250 
251     QVBoxLayout*    bottomMainLayout;
252     QHBoxLayout*    searchLineLayout;
253     QHBoxLayout*    searchInfoLineLayout;
254 
255     // Default palette to be remembered
256     QPalette        searchInfoLineDefaultPalette;
257 
258     std::shared_ptr<SavedSearches> savedSearches_;
259 
260     // Reference to the QuickFind Pattern (not owned)
261     std::shared_ptr<QuickFindPattern> quickFindPattern_;
262 
263     LogData*        logData_;
264     LogFilteredData* logFilteredData_;
265 
266     qint64          logFileSize_;
267 
268     QWidget*        qfSavedFocus_;
269 
270     // Search state (for auto-refresh and truncation)
271     SearchState     searchState_;
272 
273     // Matches overview
274     Overview        overview_;
275 
276     // Model for the visibility selector
277     QStandardItemModel* visibilityModel_;
278 
279     // Last main line number received
280     qint64 currentLineNumber_;
281 
282     // Are we loading something?
283     // Set to false when we receive a completion message from the LogData
284     bool            loadingInProgress_;
285 
286     // Is it not the first time we are loading something?
287     bool            firstLoadDone_;
288 
289     // Current number of matches
290     int             nbMatches_;
291 
292     // the current dataStatus (whether we have new, not seen, data)
293     DataStatus      dataStatus_;
294 
295     // Current encoding setting;
296     Encoding        encodingSetting_ = Encoding::ENCODING_AUTO;
297     QString         encoding_text_;
298 };
299 
300 #endif
301