KTextEditor

mainwindow.h
1 /*
2  SPDX-FileCopyrightText: 2013 Christoph Cullmann <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef KTEXTEDITOR_MAINWINDOW_H
8 #define KTEXTEDITOR_MAINWINDOW_H
9 
10 #include <ktexteditor_export.h>
11 
12 #include <QObject>
13 
14 class QEvent;
15 class QIcon;
16 class QUrl;
17 class QWidget;
18 
19 class KXMLGUIFactory;
20 
21 namespace KTextEditor
22 {
23 class Plugin;
24 class Document;
25 class View;
26 
27 /**
28  * \class MainWindow mainwindow.h <KTextEditor/MainWindow>
29  *
30  * This class allows the application that embeds the KTextEditor component to
31  * allow it to access parts of its main window.
32  *
33  * For example the component can get a place to show view bar widgets (e.g. search&replace, goto line, ...).
34  * This is useful to e.g. have one place inside the window to show such stuff even if the application allows
35  * the user to have multiple split views available per window.
36  *
37  * The application must pass a pointer to the MainWindow object to the createView method on view creation
38  * and ensure that this main window stays valid for the complete lifetime of the view.
39  *
40  * It must not reimplement this class but construct an instance and pass a pointer to a QObject that
41  * has the required slots to receive the requests.
42  */
43 class KTEXTEDITOR_EXPORT MainWindow : public QObject
44 {
45  Q_OBJECT
46 
47 public:
48  /**
49  * Construct an MainWindow wrapper object.
50  * The passed parent is both the parent of this QObject and the receiver of all interface
51  * calls via invokeMethod.
52  * @param parent object the calls are relayed to
53  */
54  MainWindow(QObject *parent);
55 
56  /**
57  * Virtual Destructor
58  */
59  ~MainWindow() override;
60 
61  //
62  // Accessors to some window properties and contents
63  //
64 public:
65  /**
66  * Get the toplevel widget.
67  * \return the real main window widget, nullptr if not available
68  */
69  QWidget *window();
70 
71  /**
72  * Accessor to the XMLGUIFactory.
73  * \return the mainwindow's KXMLGUIFactory, nullptr if not available
74  */
75  KXMLGUIFactory *guiFactory();
76 
77  //
78  // Signals related to the main window
79  //
80 Q_SIGNALS:
81  /**
82  * This signal is emitted for every unhandled ShortcutOverride in the window
83  * @param e responsible event
84  */
85  void unhandledShortcutOverride(QEvent *e);
86 
87  //
88  // View access and manipulation interface
89  //
90 public:
91  /**
92  * Get a list of all views for this main window.
93  *
94  * It is beneficial if the list is sorted by most recently used,
95  * as the library will e.g. try to use the most recent used url() by walking over this
96  * list for save and other such things.
97  *
98  * @return all views, might be empty!
99  */
101 
102  /**
103  * Access the active view.
104  * \return active view, nullptr if not available
105  */
106  KTextEditor::View *activeView();
107 
108  /**
109  * Activate the view with the corresponding \p document.
110  * If none exist for this document, create one
111  * \param document the document
112  * \return activated view of this document,
113  * return nullptr if not possible
114  */
115  KTextEditor::View *activateView(KTextEditor::Document *document);
116 
117  /**
118  * Open the document \p url with the given \p encoding.
119  * \param url the document's url
120  * \param encoding the preferred encoding. If encoding is QString() the
121  * encoding will be guessed or the default encoding will be used.
122  * \return a pointer to the created view for the new document, if a document
123  * with this url is already existing, its view will be activated,
124  * return nullptr if not possible
125  */
126  KTextEditor::View *openUrl(const QUrl &url, const QString &encoding = QString());
127 
128  /**
129  * Close selected view
130  * \param view the view
131  * \return true if view was closed
132  */
133  bool closeView(KTextEditor::View *view);
134 
135  /**
136  * Split current view space according to \p orientation
137  * \param orientation in which line split the view
138  */
139  void splitView(Qt::Orientation orientation);
140 
141  /**
142  * Close the split view that contains the given view.
143  * \param view the view.
144  * \return true if the split view was closed.
145  */
146  bool closeSplitView(KTextEditor::View *view);
147 
148  /**
149  * \returns \c true if the given views \p view1 and \p view2 share
150  * the same split view, false otherwise.
151  */
152  bool viewsInSameSplitView(KTextEditor::View *view1, KTextEditor::View *view2);
153 
154  //
155  // Signals related to view handling
156  //
157 Q_SIGNALS:
158  /**
159  * This signal is emitted whenever the active view changes.
160  * @param view new active view
161  */
162  void viewChanged(KTextEditor::View *view);
163 
164  /**
165  * This signal is emitted whenever a new view is created
166  * @param view view that was created
167  */
168  void viewCreated(KTextEditor::View *view);
169 
170  //
171  // Interface to allow view bars to be constructed in a central place per window
172  //
173 public:
174  /**
175  * Try to create a view bar for the given view.
176  * @param view view for which we want an view bar
177  * @return suitable widget that can host view bars widgets or nullptr
178  */
179  QWidget *createViewBar(KTextEditor::View *view);
180 
181  /**
182  * Delete the view bar for the given view.
183  * @param view view for which we want an view bar
184  */
185  void deleteViewBar(KTextEditor::View *view);
186 
187  /**
188  * Add a widget to the view bar.
189  * @param view view for which the view bar is used
190  * @param bar bar widget, shall have the viewBarParent() as parent widget
191  */
192  void addWidgetToViewBar(KTextEditor::View *view, QWidget *bar);
193 
194  /**
195  * Show the view bar for the given view
196  * @param view view for which the view bar is used
197  */
198  void showViewBar(KTextEditor::View *view);
199 
200  /**
201  * Hide the view bar for the given view
202  * @param view view for which the view bar is used
203  */
204  void hideViewBar(KTextEditor::View *view);
205 
206  //
207  // ToolView stuff, here all stuff belong which allows to
208  // add/remove and manipulate the toolview of this main windows
209  //
210 public:
211  /**
212  * Toolview position.
213  * A toolview can only be at one side at a time.
214  */
216  Left = 0, /**< Left side. */
217  Right = 1, /**< Right side. */
218  Top = 2, /**< Top side. */
219  Bottom = 3 /**< Bottom side. */
220  };
221 
222  /**
223  * Create a new toolview with unique \p identifier at side \p pos
224  * with \p icon and caption \p text. Use the returned widget to embedd
225  * your widgets.
226  * \param plugin which owns this tool view
227  * \param identifier unique identifier for this toolview
228  * \param pos position for the toolview, if we are in session restore,
229  * this is only a preference
230  * \param icon icon to use in the sidebar for the toolview
231  * \param text translated text (i18n()) to use in addition to icon
232  * \return created toolview on success, otherwise NULL
233  */
234  QWidget *createToolView(KTextEditor::Plugin *plugin,
235  const QString &identifier,
237  const QIcon &icon,
238  const QString &text);
239 
240  /**
241  * Move the toolview \p widget to position \p pos.
242  * \param widget the toolview to move, where the widget was constructed
243  * by createToolView().
244  * \param pos new position to move widget to
245  * \return \e true on success, otherwise \e false
246  */
248 
249  /**
250  * Show the toolview \p widget.
251  * \param widget the toolview to show, where the widget was constructed
252  * by createToolView().
253  * \return \e true on success, otherwise \e false
254  * \todo add focus parameter: bool showToolView (QWidget *widget, bool giveFocus );
255  */
256  bool showToolView(QWidget *widget);
257 
258  /**
259  * Hide the toolview \p widget.
260  * \param widget the toolview to hide, where the widget was constructed
261  * by createToolView().
262  * \return \e true on success, otherwise \e false
263  */
264  bool hideToolView(QWidget *widget);
265 
266  //
267  // Application plugin accessors
268  //
269 public:
270  /**
271  * Shows the @p plugin's config page. The @p page specifies which
272  * config page will be shown, see KTextEditor::Plugin::configPages().
273  *
274  * \return \e true on success, otherwise \e false
275  * \since 5.63
276  */
277  bool showPluginConfigPage(KTextEditor::Plugin *plugin, int page);
278 
279  /**
280  * Get a plugin view for the plugin with with identifier \p name.
281  * \param name the plugin's name
282  * \return pointer to the plugin view if a plugin with \p name is loaded and has a view for this mainwindow,
283  * otherwise NULL
284  */
285  QObject *pluginView(const QString &name);
286 
287  //
288  // Signals related to application plugins
289  //
290 Q_SIGNALS:
291  /**
292  * This signal is emitted when the view of some Plugin is created for this main window.
293  *
294  * @param name name of plugin
295  * @param pluginView the new plugin view
296  */
297  void pluginViewCreated(const QString &name, QObject *pluginView);
298 
299  /**
300  * This signal is emitted when the view of some Plugin got deleted.
301  *
302  * @warning Do not access the data referenced by the pointer, it is already invalid.
303  * Use the pointer only to remove mappings in hash or maps
304  *
305  * @param name name of plugin
306  * @param pluginView the deleted plugin view
307  */
308  void pluginViewDeleted(const QString &name, QObject *pluginView);
309 
310  //
311  // Custom widget handling
312  //
313 public:
314  /**
315  * Add a widget to the main window.
316  * This is useful to show non-KTextEditor::View widgets in the main window.
317  * The host application should try to manage this like some KTextEditor::View (e.g. as a tab) and provide
318  * the means to close it.
319  * \param widget widget to add
320  * \return success, if false, the plugin needs to take care to show the widget itself, otherwise
321  * the main window will take ownership of the widget
322  * \since 5.98
323  */
324  bool addWidget(QWidget *widget);
325 
326  //
327  // Message output
328  //
329 public:
330  /**
331  * Display a message to the user.
332  * The host application might show this inside a dedicated output view.
333  *
334  * \param message incoming message we shall handle
335  * \return true, if the host application was able to handle the message, else false
336  * \since 5.98
337  *
338  * details of message format:
339  *
340  * message text, will be trimmed before output
341  *
342  * message["text"] = i18n("your cool message")
343  *
344  * the text will be split in lines, all lines beside the first can be collapsed away
345  *
346  * message type, we support at the moment
347  *
348  * message["type"] = "Error"
349  * message["type"] = "Warning"
350  * message["type"] = "Info"
351  * message["type"] = "Log"
352  *
353  * this is take from https://microsoft.github.io/language-server-protocol/specification#window_showMessage MessageType of LSP
354  *
355  * will lead to appropriate icons/... in the output view
356  *
357  * a message should have some category, like Git, LSP, ....
358  *
359  * message["category"] = i18n(...)
360  *
361  * will be used to allow the user to filter for
362  *
363  * one can additionally provide a categoryIcon
364  *
365  * message["categoryIcon"] = QIcon(...)
366  *
367  * the categoryIcon icon QVariant must contain a QIcon, nothing else!
368  *
369  * A string token can be passed to allow to replace messages already send out with new ones.
370  * That is useful for e.g. progress output
371  *
372  * message["token"] = "yourmessagetoken"
373  *
374  */
375  bool showMessage(const QVariantMap &message);
376 
377 private:
378  /**
379  * Private d-pointer class is our best friend ;)
380  */
381  friend class MainWindowPrivate;
382 
383  /**
384  * Private d-pointer
385  */
386  class MainWindowPrivate *const d;
387 };
388 
389 } // namespace KTextEditor
390 
391 #endif
QWidget * widget() override
ToolViewPosition
Toolview position.
Definition: mainwindow.h:215
Orientation
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:146
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:22
QString message
KTextEditor Plugin interface.
Definition: plugin.h:78
A KParts derived class representing a text document.
Definition: document.h:185
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Dec 9 2023 03:50:16 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.