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