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