xref: /glogg/src/mainwindow.h (revision 02531d6a363303ac93258a8d3dc43388e1d08ba8)
1 /*
2  * Copyright (C) 2009, 2010, 2011, 2013, 2014 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef MAINWINDOW_H
21 #define MAINWINDOW_H
22 
23 #include <memory>
24 #include <QMainWindow>
25 
26 #include "session.h"
27 #include "crawlerwidget.h"
28 #include "infoline.h"
29 #include "signalmux.h"
30 #include "tabbedcrawlerwidget.h"
31 #include "quickfindwidget.h"
32 #include "quickfindmux.h"
33 #ifdef GLOGG_SUPPORTS_VERSION_CHECKING
34 #include "versionchecker.h"
35 #endif
36 
37 class QAction;
38 class QActionGroup;
39 class Session;
40 class RecentFiles;
41 class MenuActionToolTipBehavior;
42 class ExternalCommunicator;
43 
44 // Main window of the application, creates menus, toolbar and
45 // the CrawlerWidget
46 class MainWindow : public QMainWindow
47 {
48     Q_OBJECT
49 
50   public:
51     // Constructor
52     // The ownership of the session is transferred to us
53     MainWindow( std::unique_ptr<Session> session,
54             std::shared_ptr<ExternalCommunicator> external_communicator );
55 
56     // Re-install the geometry stored in config file
57     // (should be done before 'Widget::show()')
58     void reloadGeometry();
59     // Re-load the files from the previous session
60     void reloadSession();
61     // Loads the initial file (parameter passed or from config file)
62     void loadInitialFile( QString fileName );
63     // Starts the lower priority activities the MW controls such as
64     // version checking etc...
65     void startBackgroundTasks();
66 
67   protected:
68     void closeEvent( QCloseEvent* event );
69     // Drag and drop support
70     void dragEnterEvent( QDragEnterEvent* event );
71     void dropEvent( QDropEvent* event );
72     void keyPressEvent( QKeyEvent* keyEvent );
73 
74   private slots:
75     void open();
76     void openRecentFile();
77     void closeTab();
78     void closeAll();
79     void selectAll();
80     void copy();
81     void find();
82     void filters();
83     void options();
84     void about();
85     void aboutQt();
86     void encodingChanged( QAction* action );
87 
88     // Change the view settings
89     void toggleOverviewVisibility( bool isVisible );
90     void toggleMainLineNumbersVisibility( bool isVisible );
91     void toggleFilteredLineNumbersVisibility( bool isVisible );
92 
93     // Change the follow mode checkbox and send the followSet signal down
94     void changeFollowMode( bool follow );
95 
96     // Update the line number displayed in the status bar.
97     // Must be passed as the internal (starts at 0) line number.
98     void lineNumberHandler( int line );
99 
100     // Instructs the widget to update the loading progress gauge
101     void updateLoadingProgress( int progress );
102     // Instructs the widget to display the 'normal' status bar,
103     // without the progress gauge and with file info
104     // or an error recovery when loading is finished
105     void handleLoadingFinished( LoadingStatus status );
106 
107     // Save the new state as default setting when a crawler
108     // is changing their view options.
109     void handleSearchRefreshChanged( int state );
110     void handleIgnoreCaseChanged( int state );
111 
112     // Close the tab with the passed index
113     void closeTab( int index );
114     // Setup the tab with current index for view
115     void currentTabChanged( int index );
116 
117     // Instructs the widget to change the pattern in the QuickFind widget
118     // and confirm it.
119     void changeQFPattern( const QString& newPattern );
120 
121     // Load a file in a new tab (non-interactive)
122     // (for use from e.g. IPC)
123     void loadFileNonInteractive( const QString& file_name );
124 
125     // Notify the user a new version is available
126     void newVersionNotification( const QString& new_version );
127 
128   signals:
129     // Is emitted when new settings must be used
130     void optionsChanged();
131     // Is emitted when the 'follow' option is enabled/disabled
132     void followSet( bool checked );
133     // Is emitted before the QuickFind box is activated,
134     // to allow crawlers to get search in the right view.
135     void enteringQuickFind();
136     // Emitted when the quickfind bar is closed.
137     void exitingQuickFind();
138 
139   private:
140     void createActions();
141     void createMenus();
142     void createContextMenu();
143     void createToolBars();
144     void createStatusBar();
145     void createRecentFileToolTipTimer();
146     void readSettings();
147     void writeSettings();
148     bool loadFile( const QString& fileName );
149     void updateTitleBar( const QString& file_name );
150     void updateRecentFileActions();
151     QString strippedName( const QString& fullFileName ) const;
152     CrawlerWidget* currentCrawlerWidget() const;
153     void displayQuickFindBar( QuickFindMux::QFDirection direction );
154     void updateMenuBarFromDocument( const CrawlerWidget* crawler );
155     void updateInfoLine();
156 
157     std::unique_ptr<Session> session_;
158     std::shared_ptr<ExternalCommunicator> externalCommunicator_;
159     std::shared_ptr<RecentFiles> recentFiles_;
160     QString loadingFileName;
161 
162     // Encoding
163     struct EncodingList {
164         const char* name;
165     };
166 
167     static const EncodingList encoding_list[];
168 
169     enum { MaxRecentFiles = 5 };
170     QAction *recentFileActions[MaxRecentFiles];
171     MenuActionToolTipBehavior *recentFileActionBehaviors[MaxRecentFiles];
172     QAction *separatorAction;
173 
174     QMenu *fileMenu;
175     QMenu *editMenu;
176     QMenu *viewMenu;
177     QMenu *toolsMenu;
178     QMenu *encodingMenu;
179     QMenu *helpMenu;
180 
181     InfoLine *infoLine;
182     QLabel* lineNbField;
183     QToolBar *toolBar;
184 
185     QAction *openAction;
186     QAction *closeAction;
187     QAction *closeAllAction;
188     QAction *exitAction;
189     QAction *copyAction;
190     QAction *selectAllAction;
191     QAction *findAction;
192     QAction *overviewVisibleAction;
193     QAction *lineNumbersVisibleInMainAction;
194     QAction *lineNumbersVisibleInFilteredAction;
195     QAction *followAction;
196     QAction *reloadAction;
197     QAction *stopAction;
198     QAction *filtersAction;
199     QAction *optionsAction;
200     QAction *aboutAction;
201     QAction *aboutQtAction;
202     QActionGroup *encodingGroup;
203     QAction *encodingAction[CrawlerWidget::ENCODING_MAX];
204 
205     QIcon mainIcon_;
206 
207     // Multiplex signals to any of the CrawlerWidgets
208     SignalMux signalMux_;
209 
210     // QuickFind widget
211     QuickFindWidget quickFindWidget_;
212 
213     // Multiplex signals to/from the QuickFindWidget
214     QuickFindMux quickFindMux_;
215 
216     // The main widget
217     TabbedCrawlerWidget mainTabWidget_;
218 
219     // Version checker
220 #ifdef GLOGG_SUPPORTS_VERSION_CHECKING
221     VersionChecker versionChecker_;
222 #endif
223 };
224 
225 #endif
226