]> git.lyx.org Git - features.git/blob - src/frontends/qt/GuiApplication.h
Unify handling in dispatchVC.
[features.git] / src / frontends / qt / GuiApplication.h
1 /**
2  * \file GuiApplication.h
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef GUIAPPLICATION_H
14 #define GUIAPPLICATION_H
15
16 #include "frontends/Application.h"
17 #include "support/filetools.h"
18
19 #include <QApplication>
20 #include <QList>
21 #ifdef QPA_XCB
22 #include <QAbstractNativeEventFilter>
23 #endif
24
25 class QAbstractItemModel;
26 class QIcon;
27 class QSessionManager;
28 class QFont;
29
30 namespace lyx {
31
32 class BufferView;
33 class ColorCache;
34 class KeySymbol;
35
36 namespace support {
37 class FileName;
38 }
39
40 namespace frontend {
41
42 class GuiView;
43 class GuiWorkArea;
44 class Menus;
45 class SocketNotifier;
46 class Toolbars;
47
48 /// The Qt main application class
49 /**
50 There should be only one instance of this class. No Qt object
51 initialisation should be done before the instantiation of this class.
52 */
53 class GuiApplication : public QApplication, public Application
54 #ifdef QPA_XCB
55                      , public QAbstractNativeEventFilter
56 #endif
57 {
58         Q_OBJECT
59
60 public:
61         GuiApplication(int & argc, char ** argv);
62         ~GuiApplication();
63
64         /// \name Methods inherited from Application class
65         //@{
66         DispatchResult const & dispatch(FuncRequest const &) override;
67         void dispatch(FuncRequest const &, DispatchResult & dr) override;
68         FuncStatus getStatus(FuncRequest const & cmd) const override;
69         void restoreGuiSession() override;
70         Buffer const * updateInset(Inset const * inset) const override;
71         int exec() override;
72         void exit(int status) override;
73         bool event(QEvent * e) override;
74         bool getRgbColor(ColorCode col, RGBColor & rgbcol) override;
75         std::string const hexName(ColorCode col) override;
76         void registerSocketCallback(int fd, SocketCallback func) override;
77         void unregisterSocketCallback(int fd) override;
78         bool searchMenu(FuncRequest const & func, docstring_list & names) const override;
79         bool hasBufferView() const override;
80         std::string inputLanguageCode() const override;
81         void handleKeyFunc(FuncCode action) override;
82         bool unhide(Buffer * buf) override;
83         //@}
84
85         ///
86         bool getStatus(FuncRequest const & cmd, FuncStatus & status) const;
87         ///
88         void hideDialogs(std::string const & name, Inset * inset) const;
89         ///
90         void resetGui();
91
92         ///
93         Clipboard & clipboard();
94         ///
95         Selection & selection();
96         ///
97         FontLoader & fontLoader();
98
99         ///
100         Toolbars const & toolbars() const;
101         ///
102         Toolbars & toolbars();
103         ///
104         Menus const & menus() const;
105         ///
106         Menus & menus();
107
108         /// \name Methods inherited from QApplication class
109         //@{
110         bool notify(QObject * receiver, QEvent * event) override;
111         void commitData(QSessionManager & sm);
112 #ifdef Q_WS_X11
113         bool x11EventFilter(XEvent * ev) override;
114 #elif defined(QPA_XCB)
115         virtual bool nativeEventFilter(const QByteArray & eventType, void * message,
116                                        long * result) override;
117 #endif
118         //@}
119
120         /// Create the main window with given geometry settings.
121         /// \param geometry_arg: only for Windows platform.
122         /// \param optional id identifier.
123         void createView(QString const & geometry_arg = QString(),
124                 bool autoShow = true, int id = 0);
125         /// FIXME: this method and the one above are quite ugly.
126         void createView(int id);
127         ///
128         GuiView const * currentView() const { return current_view_; }
129         ///
130         GuiView * currentView() { return current_view_; }
131         ///
132         void setCurrentView(GuiView * view) { current_view_ = view; }
133         ///
134         QList<int> viewIds() const;
135
136         /// Clear all session information.
137         void clearSession();
138
139         ///
140         ColorCache & colorCache();
141         ///
142         QAbstractItemModel * languageModel();
143
144         /// return a suitable serif font name.
145         QString const romanFontName();
146
147         /// return a suitable sans serif font name.
148         QString const sansFontName();
149
150         /// return a suitable monospaced font name.
151         QString const typewriterFontName();
152         QFont const typewriterSystemFont();
153
154         ///
155         void unregisterView(GuiView * gv);
156         ///
157         GuiView & view(int id) const;
158
159         /// Current ratio between physical pixels and device-independent pixels
160         double pixelRatio() const;
161
162         /// How to load image files
163         support::search_mode imageSearchMode() const {
164 #if QT_VERSION >= 0x050000
165                 return pixelRatio() > 1 ? support::check_hidpi : support::must_exist;
166 #else
167                 return support::must_exist;
168 #endif
169         }
170
171         /// return true if the key is part of a shortcut
172         bool queryKeySym(KeySymbol const & key, KeyModifier state) const;
173         ///
174         void processKeySym(KeySymbol const & key, KeyModifier state);
175         /// return the status bar state string
176         docstring viewStatusMessage();
177
178         /// \name Methods to process FuncRequests
179         //@{
180         /// process the func request
181         void processFuncRequest(FuncRequest const &);
182         /// add a func request to the queue and process it asynchronously
183         /// \note As a side-effect this will also process the
184         /// func requests that were added to the queue before.
185         void processFuncRequestAsync(FuncRequest const &);
186         /// process the func requests in the queue
187         void processFuncRequestQueue();
188         /// process the func requests in the queue asynchronously
189         void processFuncRequestQueueAsync();
190         /// add a func request to the queue for later processing
191         void addToFuncRequestQueue(FuncRequest const &);
192         //@}
193
194         /// goto a bookmark
195         /// openFile: whether or not open a file if the file is not opened
196         /// switchToBuffer: whether or not switch to buffer if the buffer is
197         ///             not the current buffer
198         void gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer);
199
200         /// Start a long operation with some cancel possibility (button or ESC)
201         void startLongOperation() override;
202         /// This needs to be periodically called to avoid freezing the GUI
203         bool longOperationCancelled() override;
204         /// Stop the long operation mode (i.e., release the GUI)
205         void stopLongOperation() override;
206         /// A started long operation is still in progress ?
207         bool longOperationStarted() override;
208 private Q_SLOTS:
209         ///
210         void execBatchCommands();
211         ///
212         void socketDataReceived(int fd);
213         /// events to be triggered by Private::general_timer_ should go here
214         void handleRegularEvents();
215         ///
216         void onLastWindowClosed();
217         ///
218         void onLocaleChanged();
219         ///
220         void slotProcessFuncRequestQueue() { processFuncRequestQueue(); }
221
222 private:
223         ///
224         void validateCurrentView();
225         ///
226         void updateCurrentView(FuncRequest const & cmd, DispatchResult & dr);
227         ///
228         bool closeAllViews();
229         /// Things that need to be done when the OSes session manager
230         /// requests a log out.
231         bool prepareAllViewsForLogout();
232         /// read the given ui (menu/toolbar) file
233         bool readUIFile(QString const & name, bool include = false);
234         ///
235         enum ReturnValues {
236                 ReadOK,
237                 ReadError,
238                 FormatMismatch
239         };
240         ///
241         ReturnValues readUIFile(support::FileName);
242         ///
243         void setGuiLanguage();
244         ///
245         void reconfigure(std::string const & option);
246
247         /// This GuiView is the one receiving Clipboard and Selection
248         /// events
249         GuiView * current_view_;
250
251         ///
252         struct Private;
253         Private * const d;
254 }; // GuiApplication
255
256 extern GuiApplication * guiApp;
257
258 /// \return the icon file name for the given action.
259 QString iconName(FuncRequest const & f, bool unknown);
260
261 /// \return the pixmap for the given path, name and extension.
262 /// in case of errors a warning is produced and an empty pixmap is returned.
263 QPixmap getPixmap(QString const & path, QString const & name, QString const & ext);
264 /// Load the file at \param path and convert it to a pixmap.
265 /// \return true on success otherwise invalidate the pixmap and return false.
266 /// The caller is responsible for error reporting.
267 bool getPixmap(QPixmap & pixmap, QString const & path);
268
269 /// \return an icon for the given action.
270 QIcon getIcon(FuncRequest const & f, bool unknown);
271
272 ///
273 GuiApplication * theGuiApp();
274
275 } // namespace frontend
276 } // namespace lyx
277
278 #endif // GUIAPPLICATION_H