xref: /glogg/src/mainwindow.h (revision 5fa2539161053b807e2c1723310745da4a032802) !
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-load the files from the previous session
57     void reloadSession();
58     // Loads the initial file (parameter passed or from config file)
59     void loadInitialFile( QString fileName );
60     // Starts the lower priority activities the MW controls such as
61     // version checking etc...
62     void startBackgroundTasks();
63 
64   protected:
65     void closeEvent( QCloseEvent* event );
66     // Drag and drop support
67     void dragEnterEvent( QDragEnterEvent* event );
68     void dropEvent( QDropEvent* event );
69     void keyPressEvent( QKeyEvent* keyEvent );
70 
71   private slots:
72     void open();
73     void openRecentFile();
74     void closeTab();
75     void closeAll();
76     void selectAll();
77     void copy();
78     void find();
79     void filters();
80     void options();
81     void about();
82     void aboutQt();
83     void encodingChanged( QAction* action );
84 
85     // Change the view settings
86     void toggleOverviewVisibility( bool isVisible );
87     void toggleMainLineNumbersVisibility( bool isVisible );
88     void toggleFilteredLineNumbersVisibility( bool isVisible );
89 
90     // Change the follow mode checkbox and send the followSet signal down
91     void changeFollowMode( bool follow );
92 
93     // Update the line number displayed in the status bar.
94     // Must be passed as the internal (starts at 0) line number.
95     void lineNumberHandler( int line );
96 
97     // Instructs the widget to update the loading progress gauge
98     void updateLoadingProgress( int progress );
99     // Instructs the widget to display the 'normal' status bar,
100     // without the progress gauge and with file info
101     // or an error recovery when loading is finished
102     void handleLoadingFinished( LoadingStatus status );
103 
104     // Save the new state as default setting when a crawler
105     // is changing their view options.
106     void handleSearchRefreshChanged( int state );
107     void handleIgnoreCaseChanged( int state );
108 
109     // Close the tab with the passed index
110     void closeTab( int index );
111     // Setup the tab with current index for view
112     void currentTabChanged( int index );
113 
114     // Instructs the widget to change the pattern in the QuickFind widget
115     // and confirm it.
116     void changeQFPattern( const QString& newPattern );
117 
118     // Load a file in a new tab (non-interactive)
119     // (for use from e.g. IPC)
120     void loadFileNonInteractive( const QString& file_name );
121 
122     // Notify the user a new version is available
123     void newVersionNotification( const QString& new_version );
124 
125   signals:
126     // Is emitted when new settings must be used
127     void optionsChanged();
128     // Is emitted when the 'follow' option is enabled/disabled
129     void followSet( bool checked );
130     // Is emitted before the QuickFind box is activated,
131     // to allow crawlers to get search in the right view.
132     void enteringQuickFind();
133     // Emitted when the quickfind bar is closed.
134     void exitingQuickFind();
135 
136   private:
137     void createActions();
138     void createMenus();
139     void createContextMenu();
140     void createToolBars();
141     void createStatusBar();
142     void createRecentFileToolTipTimer();
143     void readSettings();
144     void writeSettings();
145     bool loadFile( const QString& fileName );
146     void updateTitleBar( const QString& file_name );
147     void updateRecentFileActions();
148     QString strippedName( const QString& fullFileName ) const;
149     CrawlerWidget* currentCrawlerWidget() const;
150     void displayQuickFindBar( QuickFindMux::QFDirection direction );
151     void updateMenuBarFromDocument( const CrawlerWidget* crawler );
152     void updateInfoLine();
153 
154     std::unique_ptr<Session> session_;
155     std::shared_ptr<ExternalCommunicator> externalCommunicator_;
156     std::shared_ptr<RecentFiles> recentFiles_;
157     QString loadingFileName;
158 
159     // Encoding
160     struct EncodingList {
161         const char* name;
162     };
163 
164     static const EncodingList encoding_list[];
165 
166     enum { MaxRecentFiles = 5 };
167     QAction *recentFileActions[MaxRecentFiles];
168     MenuActionToolTipBehavior *recentFileActionBehaviors[MaxRecentFiles];
169     QAction *separatorAction;
170 
171     QMenu *fileMenu;
172     QMenu *editMenu;
173     QMenu *viewMenu;
174     QMenu *toolsMenu;
175     QMenu *encodingMenu;
176     QMenu *helpMenu;
177 
178     InfoLine *infoLine;
179     QLabel* lineNbField;
180     QToolBar *toolBar;
181 
182     QAction *openAction;
183     QAction *closeAction;
184     QAction *closeAllAction;
185     QAction *exitAction;
186     QAction *copyAction;
187     QAction *selectAllAction;
188     QAction *findAction;
189     QAction *overviewVisibleAction;
190     QAction *lineNumbersVisibleInMainAction;
191     QAction *lineNumbersVisibleInFilteredAction;
192     QAction *followAction;
193     QAction *reloadAction;
194     QAction *stopAction;
195     QAction *filtersAction;
196     QAction *optionsAction;
197     QAction *aboutAction;
198     QAction *aboutQtAction;
199     QActionGroup *encodingGroup;
200     QAction *encodingAction[CrawlerWidget::ENCODING_MAX];
201 
202     QIcon mainIcon_;
203 
204     // Multiplex signals to any of the CrawlerWidgets
205     SignalMux signalMux_;
206 
207     // QuickFind widget
208     QuickFindWidget quickFindWidget_;
209 
210     // Multiplex signals to/from the QuickFindWidget
211     QuickFindMux quickFindMux_;
212 
213     // The main widget
214     TabbedCrawlerWidget mainTabWidget_;
215 
216     // Version checker
217 #ifdef GLOGG_SUPPORTS_VERSION_CHECKING
218     VersionChecker versionChecker_;
219 #endif
220 };
221 
222 #endif
223