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