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