]> git.lyx.org Git - features.git/blob - src/frontends/qt/GuiApplication.h
4e0983b134511d9352c89c1a2954ad332871d6d5
[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         /// Return true if current position is RTL of if no document is open and interface if RTL
92         bool rtlContext() const;
93
94         ///
95         Clipboard & clipboard();
96         ///
97         Selection & selection();
98         ///
99         FontLoader & fontLoader();
100
101         ///
102         Toolbars const & toolbars() const;
103         ///
104         Toolbars & toolbars();
105         ///
106         Menus const & menus() const;
107         ///
108         Menus & menus();
109
110         /// \name Methods inherited from QApplication class
111         //@{
112         bool notify(QObject * receiver, QEvent * event) override;
113         void commitData(QSessionManager & sm);
114 #ifdef Q_WS_X11
115         bool x11EventFilter(XEvent * ev) override;
116 #elif defined(QPA_XCB)
117         virtual bool nativeEventFilter(const QByteArray & eventType, void * message,
118                                        long * result) override;
119 #endif
120         //@}
121
122         /// Create the main window with given geometry settings.
123         /// \param geometry_arg: only for Windows platform.
124         /// \param optional id identifier.
125         void createView(QString const & geometry_arg = QString(),
126                 bool autoShow = true, int id = 0);
127         /// FIXME: this method and the one above are quite ugly.
128         void createView(int id);
129         ///
130         GuiView const * currentView() const { return current_view_; }
131         ///
132         GuiView * currentView() { return current_view_; }
133         ///
134         void setCurrentView(GuiView * view) { current_view_ = view; }
135         ///
136         QList<int> viewIds() const;
137
138         /// Clear all session information.
139         void clearSession();
140
141         ///
142         ColorCache & colorCache();
143         ///
144         QAbstractItemModel * languageModel();
145
146         /// return a suitable serif font name.
147         QString const romanFontName();
148
149         /// return a suitable sans serif font name.
150         QString const sansFontName();
151
152         /// return a suitable monospaced font name.
153         QString const typewriterFontName();
154         QFont const typewriterSystemFont();
155
156         ///
157         void unregisterView(GuiView * gv);
158         ///
159         GuiView & view(int id) const;
160
161         /// Current ratio between physical pixels and device-independent pixels
162         double pixelRatio() const;
163
164         /// How to load image files
165         support::search_mode imageSearchMode() const {
166 #if QT_VERSION >= 0x050000
167                 return pixelRatio() > 1 ? support::check_hidpi : support::must_exist;
168 #else
169                 return support::must_exist;
170 #endif
171         }
172
173         /// return true if the key is part of a shortcut
174         bool queryKeySym(KeySymbol const & key, KeyModifier state) const;
175         ///
176         void processKeySym(KeySymbol const & key, KeyModifier state);
177         /// return the status bar state string
178         docstring viewStatusMessage();
179
180         /// \name Methods to process FuncRequests
181         //@{
182         /// process the func request
183         void processFuncRequest(FuncRequest const &);
184         /// add a func request to the queue and process it asynchronously
185         /// \note As a side-effect this will also process the
186         /// func requests that were added to the queue before.
187         void processFuncRequestAsync(FuncRequest const &);
188         /// process the func requests in the queue
189         void processFuncRequestQueue();
190         /// process the func requests in the queue asynchronously
191         void processFuncRequestQueueAsync();
192         /// add a func request to the queue for later processing
193         void addToFuncRequestQueue(FuncRequest const &);
194         //@}
195
196         /// goto a bookmark
197         /// openFile: whether or not open a file if the file is not opened
198         /// switchToBuffer: whether or not switch to buffer if the buffer is
199         ///             not the current buffer
200         void gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer);
201
202         /// Start a long operation with some cancel possibility (button or ESC)
203         void startLongOperation() override;
204         /// This needs to be periodically called to avoid freezing the GUI
205         bool longOperationCancelled() override;
206         /// Stop the long operation mode (i.e., release the GUI)
207         void stopLongOperation() override;
208         /// A started long operation is still in progress ?
209         bool longOperationStarted() override;
210 private Q_SLOTS:
211         ///
212         void execBatchCommands();
213         ///
214         void socketDataReceived(int fd);
215         /// events to be triggered by Private::general_timer_ should go here
216         void handleRegularEvents();
217         ///
218         void onLastWindowClosed();
219         ///
220         void onLocaleChanged();
221         ///
222         void slotProcessFuncRequestQueue() { processFuncRequestQueue(); }
223
224 private:
225         ///
226         void validateCurrentView();
227         ///
228         void updateCurrentView(FuncRequest const & cmd, DispatchResult & dr);
229         ///
230         bool closeAllViews();
231         /// Things that need to be done when the OSes session manager
232         /// requests a log out.
233         bool prepareAllViewsForLogout();
234         /// read the given ui (menu/toolbar) file
235         bool readUIFile(QString const & name, bool include = false);
236         ///
237         enum ReturnValues {
238                 ReadOK,
239                 ReadError,
240                 FormatMismatch
241         };
242         ///
243         ReturnValues readUIFile(support::FileName);
244         ///
245         void setGuiLanguage();
246         ///
247         void reconfigure(std::string const & option);
248
249         /// This GuiView is the one receiving Clipboard and Selection
250         /// events
251         GuiView * current_view_;
252
253         ///
254         struct Private;
255         Private * const d;
256 }; // GuiApplication
257
258 extern GuiApplication * guiApp;
259
260 /// \return the icon file name for the given action.
261 QString iconName(FuncRequest const & f, bool unknown,
262                  QString const & suffix = QString());
263
264 /// \return the pixmap for the given path, name and extension.
265 /// in case of errors a warning is produced and an empty pixmap is returned.
266 QPixmap getPixmap(QString const & path, QString const & name, QString const & ext);
267
268 /// \return an icon for the given action.
269 QIcon getIcon(FuncRequest const & f, bool unknown, bool rtl = false);
270
271 ///
272 GuiApplication * theGuiApp();
273
274 } // namespace frontend
275 } // namespace lyx
276
277 #endif // GUIAPPLICATION_H