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