xref: /glogg/src/crawlerwidget.h (revision 1b5e406e86542ff35197f3abc6319ad4422b1413)
1 /*
2  * Copyright (C) 2009, 2010, 2011, 2013 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 "quickfindwidget.h"
38 #include "quickfindmux.h"
39 #include "viewinterface.h"
40 
41 class InfoLine;
42 class QuickFindPattern;
43 class QuickFindWidget;
44 class SavedSearches;
45 class Overview;
46 class QStandardItemModel;
47 class OverviewWidget;
48 
49 // Implements the central widget of the application.
50 // It includes both windows, the search line, the info
51 // lines and various buttons.
52 class CrawlerWidget : public QSplitter,
53     public QuickFindMuxSelectorInterface, public ViewInterface
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     // Implementation on the mux selector interface
73     // (for dispatching QuickFind to the right widget)
74     virtual SearchableWidgetInterface* getActiveSearchable() const;
75 
76   public slots:
77     // Stop the asynchoronous loading of the file if one is in progress
78     // The file is identified by the view attached to it.
79     void stopLoading();
80     // Reload the displayed file
81     void reload();
82 
83   protected:
84     void keyPressEvent( QKeyEvent* keyEvent );
85 
86     // Implementation of the ViewInterface functions
87     virtual void doSetData(
88             std::shared_ptr<LogData> log_data,
89             std::shared_ptr<LogFilteredData> filtered_data );
90     virtual void doSetSavedSearches(
91             std::shared_ptr<SavedSearches> saved_searches );
92 
93   signals:
94     // Sent to signal the client load has progressed,
95     // passing the completion percentage.
96     void loadingProgressed( int progress );
97     // Sent to the client when the loading has finished
98     // weither succesfull or not.
99     void loadingFinished( bool success );
100     // Sent when follow mode is enabled/disabled
101     void followSet( bool checked );
102     // Sent up to the MainWindow to disable the follow mode
103     void followDisabled();
104     // Sent up when the current line number is updated
105     void updateLineNumber( int line );
106 
107   private slots:
108     // Instructs the widget to start a search using the current search line.
109     void startNewSearch();
110     // Stop the currently ongoing search (if one exists)
111     void stopSearch();
112     // Instructs the widget to reconfigure itself because Config() has changed.
113     void applyConfiguration();
114     // Called when new data must be displayed in the filtered window.
115     void updateFilteredView( int nbMatches, int progress );
116     // Called when a new line has been selected in the filtered view,
117     // to instruct the main view to jump to the matching line.
118     void jumpToMatchingLine( int filteredLineNb );
119     // Mark a line that has been clicked on the main (top) view.
120     void markLineFromMain( qint64 line );
121     // Mark a line that has been clicked on the filtered (bottom) view.
122     void markLineFromFiltered( qint64 line );
123 
124     void loadingFinishedHandler( bool success );
125     // Manages the info lines to inform the user the file has changed.
126     void fileChangedHandler( LogData::MonitoredFileStatus );
127 
128     void hideQuickFindBar();
129 
130     // Instructs the widget to change the pattern in the QuickFind widget
131     // and confirm it.
132     void changeQFPattern( const QString& newPattern );
133 
134     void searchForward();
135     void searchBackward();
136 
137     // Called when the checkbox for search auto-refresh is changed
138     void searchRefreshChangedHandler( int state );
139 
140     // Called when the text on the search line is modified
141     void searchTextChangeHandler();
142 
143     // Called when the user change the visibility combobox
144     void changeFilteredViewVisibility( int index );
145 
146     // Called when the user add the string to the search
147     void addToSearch( const QString& string );
148 
149     // Called when a match is hovered on in the filtered view
150     void mouseHoveredOverMatch( qint64 line );
151 
152   private:
153     // State machine holding the state of the search, used to allow/disallow
154     // auto-refresh and inform the user via the info line.
155     class SearchState {
156       public:
157         enum State {
158             NoSearch,
159             Static,
160             Autorefreshing,
161             FileTruncated,
162         };
163 
164         SearchState() { state_ = NoSearch; autoRefreshRequested_ = false; }
165 
166         // Reset the state (no search active)
167         void resetState();
168         // The user changed auto-refresh request
169         void setAutorefresh( bool refresh );
170         // The file has been truncated (stops auto-refresh)
171         void truncateFile();
172         // The expression has been changed (stops auto-refresh)
173         void changeExpression();
174         // The search has been stopped (stops auto-refresh)
175         void stopSearch();
176         // The search has been started (enable auto-refresh)
177         void startSearch();
178 
179         // Get the state in order to display the proper message
180         State getState() const { return state_; }
181         // Is auto-refresh allowed
182         bool isAutorefreshAllowed() const
183             { return ( state_ == Autorefreshing ); }
184 
185       private:
186         State state_;
187         bool autoRefreshRequested_;
188     };
189 
190     // Private functions
191     void setup();
192     void replaceCurrentSearch( const QString& searchText );
193     void updateSearchCombo();
194     AbstractLogView* activeView() const;
195     void printSearchInfoMessage( int nbMatches = 0 );
196 
197     // Palette for error notification (yellow background)
198     static const QPalette errorPalette;
199 
200     LogMainView*    logMainView;
201     QWidget*        bottomWindow;
202     QLabel*         searchLabel;
203     QComboBox*      searchLineEdit;
204     QToolButton*    searchButton;
205     QToolButton*    stopButton;
206     FilteredView*   filteredView;
207     QComboBox*      visibilityBox;
208     InfoLine*       searchInfoLine;
209     QCheckBox*      ignoreCaseCheck;
210     QCheckBox*      searchRefreshCheck;
211     QuickFindWidget* quickFindWidget_;
212     OverviewWidget* overviewWidget_;
213 
214     QVBoxLayout*    bottomMainLayout;
215     QHBoxLayout*    searchLineLayout;
216     QHBoxLayout*    searchInfoLineLayout;
217 
218     // Default palette to be remembered
219     QPalette        searchInfoLineDefaultPalette;
220 
221     std::shared_ptr<SavedSearches> savedSearches_;
222 
223     QuickFindMux*   quickFindMux_;
224 
225     LogData*        logData_;
226     LogFilteredData* logFilteredData_;
227 
228     qint64          logFileSize_;
229 
230     QWidget*        qfSavedFocus_;
231 
232     // Search state (for auto-refresh and truncation)
233     SearchState     searchState_;
234 
235     // Matches overview
236     Overview*       overview_;
237 
238     // Model for the visibility selector
239     QStandardItemModel* visibilityModel_;
240 };
241 
242 #endif
243