KXmlGui

kxmlguiwindow.h
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2000 Reginald Stadlbauer <reggie@kde.org>
4 SPDX-FileCopyrightText: 1997 Stephan Kulow <coolo@kde.org>
5 SPDX-FileCopyrightText: 1997-2000 Sven Radej <radej@kde.org>
6 SPDX-FileCopyrightText: 1997-2000 Matthias Ettrich <ettrich@kde.org>
7 SPDX-FileCopyrightText: 1999 Chris Schlaeger <cs@kde.org>
8 SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
9 SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
10
11 SPDX-License-Identifier: LGPL-2.0-only
12*/
13
14#ifndef KXMLGUIWINDOW_H
15#define KXMLGUIWINDOW_H
16
17#include "kmainwindow.h"
18#include "kxmlguibuilder.h"
19#include "kxmlguiclient.h"
20
21class KMenu;
22class KXMLGUIFactory;
23class KConfig;
24class KConfigGroup;
25class KToolBar;
26class KXmlGuiWindowPrivate;
27
28/**
29 * @class KXmlGuiWindow kxmlguiwindow.h KXmlGuiWindow
30 *
31 * @brief KMainWindow with convenience functions and integration with XmlGui files.
32 *
33 * This class includes several convenience <action>Enabled() functions
34 * to toggle the presence of functionality in your main window,
35 * including a KCommandBar instance.
36 *
37 * The @ref StandardWindowOptions enum can be used to pass additional options
38 * to describe the main window behavior/appearance.
39 * Use it in conjunction with setupGUI() to load an appnameui.rc file
40 * to manage the main window's actions.
41 *
42 * setCommandBarEnabled() is set by default.
43 *
44 * A minimal example can be created with
45 * QMainWindow::setCentralWidget() and setupGUI():
46 *
47 * @code
48 * MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent) {
49 * textArea = new KTextEdit();
50 * setCentralWidget(textArea);
51 * setupGUI(Default);
52 * }
53 * @endcode
54 *
55 * With this, a ready-made main window with menubar and statusbar is created,
56 * as well as two default menus, Settings and Help.
57 *
58 * Management of QActions is made trivial in combination with
59 * KActionCollection and KStandardAction.
60 *
61 * @code
62 * void MainWindow::setupActions() {
63 * QAction *clearAction = new QAction(this);
64 * clearAction->setText(i18n("&Clear"));
65 * clearAction->setIcon(QIcon::fromTheme("document-new"));
66 * KActionCollection::setDefaultShortcut(clearAction, Qt::CTRL + Qt::Key_W);
67 * actionCollection()->addAction("clear", clearAction);
68 * connect(clearAction, &QAction::triggered, textArea, &KTextEdit::clear);
69 * KStandardAction::quit(qApp, &QCoreApplication::quit, actionCollection());
70 * setupGUI(Default, "texteditorui.rc");
71 * }
72 * @endcode
73 *
74 * See https://develop.kde.org/docs/use/kxmlgui/ for a tutorial
75 * on how to create a simple text editor using KXmlGuiWindow.
76 *
77 * See https://develop.kde.org/docs/use/session-managment for more information on session management.
78 *
79 * @see KMainWindow
80 * @see KActionCollection
81 * @see KStandardAction
82 * @see setupGUI()
83 * @see createGUI()
84 * @see setCommandBarEnabled()
85 */
86
87class KXMLGUI_EXPORT KXmlGuiWindow : public KMainWindow, public KXMLGUIBuilder, virtual public KXMLGUIClient
88{
89 Q_OBJECT
90 Q_PROPERTY(bool hasMenuBar READ hasMenuBar)
91 Q_PROPERTY(bool autoSaveSettings READ autoSaveSettings)
92 Q_PROPERTY(QString autoSaveGroup READ autoSaveGroup)
93 Q_PROPERTY(bool standardToolBarMenuEnabled READ isStandardToolBarMenuEnabled WRITE setStandardToolBarMenuEnabled)
94 Q_PROPERTY(QStringList toolBars READ toolBarNames)
95
96public:
97 /**
98 * @brief Construct a main window.
99 *
100 * Note that by default a KXmlGuiWindow is created with the
101 * Qt::WA_DeleteOnClose attribute set, i.e. it is automatically destroyed
102 * when the window is closed. If you do not want this behavior, call:
103 *
104 * @code
105 * window->setAttribute(Qt::WA_DeleteOnClose, false);
106 * @endcode
107 *
108 * KXmlGuiWindows must be created on the heap with 'new', like:
109 *
110 * @code
111 * KXmlGuiWindow *kmw = new KXmlGuiWindow(...);
112 * kmw->setObjectName(...);
113 * @endcode
114 *
115 * IMPORTANT: For session management and window management to work
116 * properly, all main windows in the application should have a
117 * different name. Otherwise, the base class KMainWindow will create
118 * a unique name, but it's recommended to explicitly pass a window name that will
119 * also describe the type of the window. If there can be several windows of the same
120 * type, append '#' (hash) to the name, and KMainWindow will replace it with numbers to make
121 * the names unique. For example, for a mail client which has one main window showing
122 * the mails and folders, and which can also have one or more windows for composing
123 * mails, the name for the folders window should be e.g. "mainwindow" and
124 * for the composer windows "composer#".
125 *
126 * @param parent The widget parent. This is usually @c nullptr,
127 * but it may also be the window group leader.
128 * In that case, the KXmlGuiWindow becomes a secondary window.
129 *
130 * @param flags Specify the window flags. The default is none.
131 *
132 * @see KMainWindow::KMainWindow
133 */
134 explicit KXmlGuiWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
135
136 /**
137 * @brief Destructor.
138 *
139 * Will also destroy the toolbars and menubar if needed.
140 */
141 ~KXmlGuiWindow() override;
142
143 /**
144 * @brief Creates a standard help menu when calling createGUI()
145 * or setupGUI().
146 *
147 * @param showHelpMenu Whether to create a Help Menu. @c true by default.
148 *
149 * @see isHelpMenuEnabled()
150 */
151 void setHelpMenuEnabled(bool showHelpMenu = true);
152
153 /**
154 * @returns @c true if the help menu is enabled, @c false if setHelpMenuEnabled(false) was set.
155 * @see setHelpMenuEnabled()
156 */
157 bool isHelpMenuEnabled() const;
158
159 virtual KXMLGUIFactory *guiFactory();
160
161 /**
162 * @brief Generates the interface based on a local XML file.
163 *
164 * This is the function that generates UI elements such as the main menu,
165 * toolbar (if any) and statusbar. This is called by setupGUI(Create) as well.
166 *
167 * Typically, in a regular application, you would use setupGUI()
168 * instead, as it sets up the toolbar/shortcut
169 * edit actions, among other things.
170 *
171 * If @p xmlfile is an empty string, this method will try to construct
172 * a local XML filename like appnameui.rc where 'appname' is your app's
173 * name. Typically that app name is what KXMLGUIClient::componentName()
174 * returns. If that file does not exist, then the XML UI code will use only
175 * the global (standard) XML file for its layout purposes.
176 *
177 * @param xmlfile The path (relative or absolute) to the local xmlfile
178 *
179 * @see setupGUI()
180 */
181 void createGUI(const QString &xmlfile = QString());
182
183 /**
184 * @brief Creates a toggle under the 'Settings' menu to show/hide the available toolbars.
185 *
186 * The standard toolbar menu toggles the visibility of one or multiple toolbars.
187 *
188 * If there is only one toolbar configured, a simple 'Show <toolbar name>'
189 * menu item is shown; if more than one toolbar is configured, a "Shown Toolbars"
190 * menu is created instead, with 'Show <toolbar1 name>', 'Show <toolbar2 name>'
191 * ... sub-menu actions.
192 *
193 * If your application uses a non-default XmlGui resource file, then you can
194 * specify the exact position of the menu/menu item by adding a
195 * &lt;Merge name="StandardToolBarMenuHandler" /&gt;
196 * line to the settings menu section of your resource file ( usually appname.rc ).
197 *
198 * @param showToolBarMenu Whether to show the standard toolbar menu. @c false by default.
199 *
200 * @note This function only makes sense before calling createGUI().
201 * Using setupGUI(ToolBar) overrides this function.
202 *
203 * @see createGUI()
204 * @see setupGUI()
205 * @see KToggleBarAction
206 * @see StandardWindowOption
207 * @see KMainWindow::toolBar()
208 * @see KMainWindow::toolBars()
209 * @see QMainWindow::addToolBar()
210 * @see QMainWindow::removeToolBar()
211 * @see createStandardStatusBarAction()
212 */
213 void setStandardToolBarMenuEnabled(bool showToolBarMenu);
214
215 /**
216 * @brief Returns whether setStandardToolBarMenuEnabled() was set.
217 *
218 * @note This function only makes sense if createGUI() was used.
219 * This function returns true only if setStandardToolBarMenuEnabled() was set
220 * and will return false even if @ref StandardWindowOption::ToolBar was used.
221 *
222 * @returns @c true if setStandardToolBarMenuEnabled() was set, @c false otherwise.
223 *
224 * @see createGUI()
225 * @see setupGUI()
226 * @see setStandardToolBarMenuEnabled()
227 * @see StandardWindowOption
228 */
229 bool isStandardToolBarMenuEnabled() const;
230
231 /**
232 * @brief Creates a toggle under the 'Settings' menu to show/hide the statusbar.
233 *
234 * Calling this method will create a statusbar if one doesn't already exist.
235 *
236 * If an application maintains the action on its own (i.e. never calls
237 * this function), a connection needs to be made to let KMainWindow
238 * know when the hidden/shown status of the statusbar has changed.
239 * For example:
240 * @code
241 * connect(action, &QAction::triggered,
242 * kmainwindow, &KMainWindow::setSettingsDirty);
243 * @endcode
244 * Otherwise the status might not be saved by KMainWindow.
245 *
246 * @note This function only makes sense before calling createGUI()
247 * or when using setupGUI() without @ref StandardWindowOption::StatusBar.
248 *
249 * @see createGUI()
250 * @see setupGUI()
251 * @see StandardWindowOption
252 * @see KStandardAction::showStatusbar()
253 * @see setStandardToolBarMenuEnabled()
254 * @see QMainWindow::setStatusBar()
255 * @see QMainWindow::statusBar()
256 */
257 void createStandardStatusBarAction();
258
259 /**
260 * @brief Use these options for the first argument of setupGUI().
261 * @see setupGUI()
262 * @see StandardWindowOption
263 */
265 /**
266 * Adds action(s) to show/hide the toolbar(s) and adds a menu
267 * action to configure the toolbar(s).
268 *
269 * @see setStandardToolBarMenuEnabled()
270 * @see isStandardToolBarMenuEnabled()
271 */
273
274 /**
275 * @brief Adds an action in the 'Settings' menu
276 * to open the configure keyboard shortcuts dialog.
277 */
278 Keys = 2,
279
280 /**
281 * @brief Adds an action to show/hide the statusbar in the 'Settings' menu.
282 * Note that setting this value will create a statusbar
283 * if one doesn't already exist.
284 *
285 * @see createStandardStatusBarAction()
286 */
287 StatusBar = 4,
288
289 /**
290 * @brief Autosaves (and loads) the toolbar/menubar/statusbar settings and
291 * window size using the default name.
292 *
293 * Like KMainWindow::setAutoSaveSettings(), enabling this causes the application
294 * to save state data upon close in a KConfig-managed configuration file.
295 *
296 * Typically you want to let the default window size be determined by
297 * the widgets' size hints. Make sure that setupGUI() is called after
298 * all the widgets are created (including QMainWindow::setCentralWidget())
299 * so that the default size is managed properly.
300 *
301 * @see KMainWindow::setAutoSaveSettings()
302 * @see KConfig
303 */
304 Save = 8,
305
306 /**
307 * @brief Calls createGUI() once ToolBar, Keys and Statusbar have been
308 * taken care of.
309 *
310 * @note When using KParts::MainWindow, remove this flag from
311 * the setupGUI() call, since you'll be using createGUI(part)
312 * instead:
313 *
314 * @code
315 * setupGUI(ToolBar | Keys | StatusBar | Save);
316 * @endcode
317 *
318 * @see createGUI()
319 */
320 Create = 16,
321
322 /**
323 * @brief Sets all of the above options as true.
324 */
325 Default = ToolBar | Keys | StatusBar | Save | Create,
326 };
327 Q_FLAG(StandardWindowOption)
328 /**
329 * @brief Stores a combination of @ref StandardWindowOptions values.
330 *
331 * Use these options for the first argument of setupGUI().
332 * @see setupGUI()
333 * @see StandardWindowOption
334 */
336
337 /**
338 * @brief Configures the current window and its actions in the typical KDE
339 * fashion.
340 *
341 * You can specify which window options/features are going to be set up using
342 * @p options, see the @ref StandardWindowOptions enum for more details.
343 *
344 * @code
345 * MainWindow::MainWindow(QWidget* parent) : KXmlGuiWindow(parent){
346 * textArea = new KTextEdit();
347 * setCentralWidget(textArea);
348 * setupGUI(Default, "appnameui.rc");
349 * }
350 * @endcode
351 *
352 * Use a bitwise OR (|) to select multiple enum choices for setupGUI()
353 * (except when using StandardWindowOption::Default).
354 *
355 * @code
356 * setupGUI(Save | Create, "appnameui.rc");
357 * @endcode
358 *
359 * Typically this function replaces createGUI(),
360 * but it is possible to call setupGUI(Create) together with helper functions
361 * such as setStandardToolBarMenuEnabled() and createStandardStatusBarAction().
362 *
363 * @warning To use createGUI() and setupGUI()
364 * for the same window, you must avoid using
365 * @ref StandardWindowOption::Create. Prefer using only setupGUI().
366 *
367 * @note When @ref StandardWindowOption::Save is used,
368 * this method will restore the state of the application
369 * window (toolbar, dockwindows positions ...etc), so you need to have
370 * added all your actions to your UI before calling this
371 * method.
372 *
373 * @param options A combination of @ref StandardWindowOptions to specify
374 * UI elements to be present in your application window.
375 * @param xmlfile The relative or absolute path to the local xmlfile.
376 * If this is an empty string, the code will look for a local XML file
377 * appnameui.rc, where 'appname' is the name of your app. See the note
378 * about the xmlfile argument in createGUI().
379 * @see StandardWindowOption
380 */
381 void setupGUI(StandardWindowOptions options = Default, const QString &xmlfile = QString());
382
383 /**
384 * @brief This is an overloaded function.
385 *
386 * @param defaultSize A manually specified window size that overrides the saved size.
387 * @param options A combination of @ref StandardWindowOptions to specify
388 * UI elements to be present in your application window.
389 * @param xmlfile The relative or absolute path to the local xmlfile.
390 * @see setupGUI()
391 */
392 void setupGUI(const QSize &defaultSize, StandardWindowOptions options = Default, const QString &xmlfile = QString());
393
394 /**
395 * @returns A pointer to the main window's action responsible for the toolbar's menu.
396 */
397 QAction *toolBarMenuAction();
398
399 /**
400 * @internal for KToolBar
401 */
402 void setupToolbarMenuActions();
403
404 /**
405 * @returns A list of all toolbars for this window in string form
406 * @see KMainWindow::toolBars()
407 * @since 6.10
408 */
409 QStringList toolBarNames() const;
410
411 /**
412 * @internal
413 */
414 void finalizeGUI(bool force);
415 using KXMLGUIBuilder::finalizeGUI;
416
417 // reimplemented for internal reasons
418 void applyMainWindowSettings(const KConfigGroup &config) override;
419
420 /**
421 * @brief Enable a KCommandBar to list and quickly execute actions.
422 *
423 * A KXmlGuiWindow by default automatically creates a KCommandBar,
424 * but it is inaccessible unless createGUI() or setupGUI(Create) is used.
425 *
426 * It provides a HUD-like menu that lists all QActions in your application
427 * and can be activated via Ctrl+Atl+i or via an action in the 'Help' menu.
428 *
429 * If you need more than a global set of QActions listed for your application,
430 * use KCommandBar directly instead.
431 *
432 * @param showCommandBar Whether to show the command bar. @c true by default.
433 *
434 * @since 5.83
435 *
436 * @see KCommandBar
437 * @see KCommandBar::setActions()
438 * @see isCommandBarEnabled()
439 */
440 void setCommandBarEnabled(bool showCommandBar);
441
442 /**
443 * @brief Returns whether a KCommandBar was set.
444 * @returns @c true by default, @c false if setCommandBarEnabled(false) was set.
445 * @since 5.83
446 * @see setCommandBarEnabled()
447 */
448 bool isCommandBarEnabled() const;
449
450public Q_SLOTS:
451 /**
452 * @brief Show a standard configure toolbar dialog.
453 *
454 * This slot can be connected directly to the action to configure the toolbar.
455 *
456 * @code
457 * KStandardAction::configureToolbars(this, &KXmlGuiWindow::configureToolbars, actionCollection);
458 * @endcode
459 */
460 virtual void configureToolbars();
461
462 /**
463 * @brief Applies a state change
464 *
465 * Reimplement this to enable and disable actions as defined in the XmlGui rc file.
466 *
467 * @param newstate The state change to be applied.
468 */
469 virtual void slotStateChanged(const QString &newstate);
470
471 /**
472 * @brief Applies a state change
473 *
474 * Reimplement this to enable and disable actions as defined in the XmlGui rc file.
475 *
476 * This function can "reverse" the state (disable the actions which should be
477 * enabled, and vice-versa) if specified.
478 *
479 * @param newstate The state change to be applied.
480 * @param reverse Whether to reverse @p newstate or not.
481 */
482 void slotStateChanged(const QString &newstate, bool reverse);
483
484 /**
485 * @brief Checks the visual state of a given toolbar. If an invalid name is
486 * given, the method will always return false.
487 *
488 * This does not utilize KMainWindow::toolBar(), as that method would
489 * create a new toolbar in the case of an invalid name, which is not
490 * something that should be freely accessable via the dbus.
491 *
492 * @param name The internal name of the toolbar.
493 *
494 * @returns whether the toolbar with the specified name is currently visible
495 * @see toolBarNames()
496 * @see setToolBarVisible()
497 * @see KMainWindow::toolBar()
498 * @since 6.10
499 */
500 bool isToolBarVisible(const QString &name);
501
502 /**
503 * @brief Sets the visibility of a given toolbar. If an invalid name is
504 * given, nothing happens.
505 *
506 * @param name The internal name of the toolbar.
507 * @param visible The new state of the toolbar's visibility.
508 * @since 6.10
509 */
510 void setToolBarVisible(const QString &name, bool visible);
511
512protected:
513 /**
514 * Reimplemented to catch QEvent::Polish in order to adjust the object name
515 * if needed, once all constructor code for the main window has run.
516 * Also reimplemented to catch when a QDockWidget is added or removed.
517 */
518 bool event(QEvent *event) override;
519
520 /**
521 * @brief Checks if there are actions using the same shortcut.
522 *
523 * This is called automatically from createGUI().
524 *
525 * @since 5.30
526 */
527 void checkAmbiguousShortcuts();
528
529protected Q_SLOTS:
530 /**
531 * @brief Rebuilds the GUI after KEditToolBar changes the toolbar layout.
532 * @see configureToolbars()
533 */
534 virtual void saveNewToolbarConfig();
535
536private:
537 Q_DECLARE_PRIVATE(KXmlGuiWindow)
538};
539
540Q_DECLARE_OPERATORS_FOR_FLAGS(KXmlGuiWindow::StandardWindowOptions)
541
542#endif
KMainWindow represents a top-level main window.
Definition kmainwindow.h:60
Floatable toolbar with auto resize.
Definition ktoolbar.h:68
Implements the creation of the GUI (menubar, menus and toolbars) as requested by the GUI factory.
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document,...
KXMLGUIFactory, together with KXMLGUIClient objects, can be used to create a GUI of container widgets...
KMainWindow with convenience functions and integration with XmlGui files.
StandardWindowOption
Use these options for the first argument of setupGUI().
Q_FLAG(...)
Q_PROPERTY(...)
typedef WindowFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.