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