KXmlGui

kmainwindow.h
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2000 Reginald Stadlbauer <[email protected]>
4  SPDX-FileCopyrightText: 1997 Stephan Kulow <[email protected]>
5  SPDX-FileCopyrightText: 1997-2000 Sven Radej <[email protected]>
6  SPDX-FileCopyrightText: 1997-2000 Matthias Ettrich <[email protected]>
7  SPDX-FileCopyrightText: 1999 Chris Schlaeger <[email protected]>
8  SPDX-FileCopyrightText: 2002 Joseph Wenninger <[email protected]>
9  SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <[email protected]>
10  SPDX-FileCopyrightText: 2000-2008 David Faure <[email protected]>
11 
12  SPDX-License-Identifier: LGPL-2.0-only
13 */
14 
15 #ifndef KMAINWINDOW_H
16 #define KMAINWINDOW_H
17 
18 #include <kxmlgui_export.h>
19 
20 #include <QMainWindow>
21 #include <memory>
22 
23 class QMenu;
24 class KConfig;
25 class KConfigGroup;
26 class KMWSessionManager;
27 class KMainWindowPrivate;
28 class KToolBar;
29 
30 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 65)
31 /**
32  * @relates KMainWindow
33  * @deprecated Since 5.65, use Qt::WindowFlags()
34  */
35 #define KDE_DEFAULT_WINDOWFLAGS 0
36 #else
37 #define KDE_DEFAULT_WINDOWFLAGS KDE_DEFAULT_WINDOWFLAGS_is_deprecated_use_Qt_WindowFlags()
38 #endif
39 
40 /**
41  * @class KMainWindow kmainwindow.h KMainWindow
42  *
43  * @brief KMainWindow represents a top-level main window.
44  *
45  * It extends QMainWindow with session management capabilities. For ready-made window functionality and simpler UI management, use KXmlGuiWindow instead.
46  *
47  * Define the minimum/maximum height/width of your central widget and KMainWindow will take this into account.
48  * For fixed size windows set your main widget to a fixed size. Fixed aspect ratios (QWidget::heightForWidth()) and fixed width widgets are not supported.
49  *
50  * Use toolBar() to generate a main toolbar "mainToolBar" or refer to a specific toolbar.
51  * For a simpler way to manage your toolbars, use KXmlGuiWindow::setupGUI() instead.
52  *
53  * Use setAutoSaveSettings() to automatically save and restore the window geometry and toolbar/menubar/statusbar state when the application is restarted.
54  *
55  * Use kRestoreMainWindows() in your main function to restore your windows when the session is restored.
56  *
57  * The window state is saved when the application is exited.
58  * Reimplement queryClose() to warn the user of unsaved data upon close or exit.
59  *
60  * Reimplement saveProperties() / readProperties() or saveGlobalProperties() / readGlobalProperties()
61  * to save/restore application-specific state during session management.
62  *
63  * Note that session saving is automatically called, session restoring is not,
64  * and so it needs to be implemented in your main() function.
65  *
66  * See https://develop.kde.org/docs/use/session-managment for more information on session management.
67  */
68 
69 class KXMLGUI_EXPORT KMainWindow : public QMainWindow
70 {
71  friend class KMWSessionManager;
72  friend class DockResizeListener;
73  Q_OBJECT
74  Q_PROPERTY(bool hasMenuBar READ hasMenuBar)
75  Q_PROPERTY(bool autoSaveSettings READ autoSaveSettings)
76  Q_PROPERTY(QString autoSaveGroup READ autoSaveGroup)
77 
78 public:
79  /**
80  * @brief Constructs a main window.
81  *
82  * @param parent The parent widget. This is usually @c nullptr, but it may also be
83  * the window group leader. In that case,
84  * the KMainWindow becomes a secondary window.
85  *
86  * @param flags Specify the window flags. The default is none.
87  *
88  * Note that by default a KMainWindow is created with the
89  * Qt::WA_DeleteOnClose attribute set, i.e. it is automatically destroyed
90  * when the window is closed. If you do not want this behavior, call
91  * \code
92  * window->setAttribute(Qt::WA_DeleteOnClose, false);
93  * \endcode
94  *
95  * KMainWindows must be created on the heap with 'new', like:
96  * \code
97  * KMainWindow *kmw = new KMainWindow(...);
98  * kmw->setObjectName(...);
99  * \endcode
100  *
101  * Since KDE Frameworks 5.16, KMainWindow will enter information regarding
102  * the application's translators by default, using KAboutData::setTranslator(). This only occurs
103  * if no translators are already assigned in KAboutData (see KAboutData::setTranslator() for
104  * details -- the auto-assignment here uses the same translated strings as specified for that
105  * function).
106  *
107  * IMPORTANT: For session management and window management to work
108  * properly, all main windows in the application should have a
109  * different name. Otherwise, KMainWindow will create
110  * a unique name, but it's recommended to explicitly pass a window name that will
111  * also describe the type of the window. If there can be several windows of the same
112  * type, append '#' (hash) to the name, and KMainWindow will replace it with numbers to make
113  * the names unique. For example, for a mail client which has one main window showing
114  * the mails and folders, and which can also have one or more windows for composing
115  * mails, the name for the folders window should be e.g. "mainwindow" and
116  * for the composer windows "composer#".
117  *
118  * @see KAboutData
119  */
120  explicit KMainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
121 
122  /**
123  * @brief Destructor.
124  *
125  * Will also destroy the toolbars and menubar if
126  * needed.
127  */
128  ~KMainWindow() override;
129 
130 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
131  /**
132  * Retrieve the standard help menu.
133  *
134  * It contains entries for the
135  * help system (activated by F1), an optional "What's This?" entry
136  * (activated by Shift F1), an application specific dialog box,
137  * and an "About KDE" dialog box.
138  *
139  * Example (adding a standard help menu to your application):
140  * \code
141  * QMenu *help = helpMenu( <myTextString> );
142  * menuBar()->addMenu( help );
143  * \endcode
144  *
145  * @param aboutAppText The string that is used in the application
146  * specific dialog box. If you leave this string empty the
147  * information in the global KAboutData of the
148  * application will be used to make a standard dialog box.
149  *
150  * @param showWhatsThis Set this to false if you do not want to include
151  * the "What's This" menu entry.
152  *
153  * @return A standard help menu.
154  * @deprecated Since 5.0, use KHelpMenu directly
155  */
156  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KHelpMenu directly")
157  QMenu *helpMenu(const QString &aboutAppText = QString(), bool showWhatsThis = true);
158 #endif
159 
160 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
161  /**
162  * @returns The help menu. Creates a standard help menu if none exists yet.
163  *
164  * It contains entries for the
165  * help system (activated by F1), an optional "What's This?" entry
166  * (activated by Shift F1), an application specific dialog box,
167  * and an "About KDE" dialog box. You must create the application
168  * specific dialog box yourself. When the "About application"
169  * menu entry is activated, a signal will trigger the
170  * showAboutApplication slot. See showAboutApplication for more
171  * information.
172  *
173  * Example (adding a help menu to your application):
174  * \code
175  * menuBar()->addMenu( customHelpMenu() );
176  * \endcode
177  *
178  * @param showWhatsThis Whether to show the "What's This" menu entry. @c true by default.
179  *
180  * @return A standard help menu.
181  * @deprecated Since 5.0, use XMLGUI instead, or KHelpMenu directly
182  */
183  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use XMLGUI or KHelpMenu")
184  QMenu *customHelpMenu(bool showWhatsThis = true);
185 #endif
186 
187  /**
188  * @param numberOfInstances The number of KMainWindow instances in the application.
189  * @returns @c true if the number of KMainWindow instances of the previous session did contain the requested @p numberOfInstances, @c false otherwise.
190  * @see restore()
191  **/
192  static bool canBeRestored(int numberOfInstances);
193 
194  /**
195  * @brief Useful if your application uses
196  * different kinds of top-level windows.
197  *
198  * @returns The class name of the top-level window to be restored
199  * that corresponds to @p instanceNumber.
200  *
201  * @param instanceNumber
202  * @see restore()
203  */
204  static const QString classNameOfToplevel(int instanceNumber);
205 
206  /**
207  * @brief Attempt to restore the top-level widget as defined by @p numberOfInstances (1..X).
208  *
209  * You should call canBeRestored() first.
210  *
211  * If the session did not contain so high a number, the configuration
212  * is not changed and @c false is returned.
213  *
214  * That means clients could simply do the following:
215  * \code
216  * if (qApp->isSessionRestored()){
217  * int n = 1;
218  * while (KMainWindow::canBeRestored(n)){
219  * (new childMW)->restore(n);
220  * n++;
221  * }
222  * } else {
223  * // create default application as usual
224  * }
225  * \endcode
226  * Note that if @p show is @c true (default), QWidget::show() is called
227  * implicitly in restore.
228  *
229  * With this you can easily restore all top-level windows of your
230  * application.
231  *
232  * If your application uses different kinds of top-level
233  * windows, then you can use KMainWindow::classNameOfToplevel(n)
234  * to determine the exact type before calling the childMW
235  * constructor in the example from above.
236  *
237  * @note You don't need to deal with this function. Use the
238  * kRestoreMainWindows() convenience template function instead!
239  *
240  * @param numberOfInstances The number of KMainWindow instances from the last session.
241  * @param show Whether the KMainWindow instances will be visible by default.
242  *
243  * @returns @c true if the session contained
244  * the same number of instances as the requested number,
245  * @c false if the session contained less instances than the requested number,
246  * in which case no configuration is changed.
247  *
248  * @see kRestoreMainWindows()
249  * @see readProperties()
250  * @see canBeRestored()
251  */
252  bool restore(int numberOfInstances, bool show = true);
253 
254  /**
255  * @returns @c true if there is a menubar, @c false otherwise.
256  */
257  bool hasMenuBar();
258 
259  /**
260  * @returns The list of members of the KMainWindow class.
261  */
262  static QList<KMainWindow *> memberList();
263 
264  /**
265  * @brief This is useful to both call specific toolbars that have been created
266  * or to generate a default one upon call.
267  *
268  * This refers to toolbars created dynamically from the XML UI
269  * framework via KConfig or appnameui.rc.
270  *
271  * If the toolbar does not exist, one will be created.
272  *
273  * @param name The internal name of the toolbar. If no name is
274  * specified, "mainToolBar" is assumed.
275  *
276  * @return A pointer to the toolbar with the specified name.
277  * @see toolBars()
278  **/
279  KToolBar *toolBar(const QString &name = QString());
280 
281  /**
282  * @return A list of all toolbars for this window
283  */
284  QList<KToolBar *> toolBars() const;
285 
286  /**
287  * @brief This enables autosave of toolbar/menubar/statusbar settings
288  * (and optionally window size).
289  * @param groupName A name that identifies the type of window.
290  * You can have several types of window in the same application.
291  * If no @p groupName is specified, the value defaults to "MainWindow".
292  *
293  * @param saveWindowSize Whether to include the window size
294  * when saving. @c true by default.
295  *
296  * If the *bars were modified when the window is closed,
297  * saveMainWindowSettings( KConfigGroup(KSharedConfig::openConfig(), groupName) ) will be called.
298  *
299  * Typically, you will call setAutoSaveSettings() in your
300  * KMainWindow-inherited class constructor, and it will take care
301  * of restoring and saving automatically.
302  *
303  * By default, this generates an
304  * appnamerc ini file as if using default KConfig constructor or KConfig::SimpleConfig.
305  *
306  * Make sure you call this @em after all your *bars have been created.
307  *
308  * To make sure that KMainWindow properly obtains the default
309  * size of the window you should do the following:
310  * - Remove hardcoded resize() calls in the constructor or main
311  * to let the automatic resizing determine the default window size.
312  * Hardcoded window sizes will be wrong for users that have big fonts,
313  * use different styles, long/small translations, large toolbars, and other factors.
314  * - Put the setAutoSaveSettings() call after all widgets
315  * have been created and placed inside the main window
316  * (for most apps this means QMainWindow::setCentralWidget())
317  * - QWidget-based objects should overload "virtual QSize sizeHint() const;"
318  * to specify a default size.
319  * @see KConfig
320  * @see KSharedConfig
321  * @see saveMainWindowSettings()
322  * @see toolBar()
323  * @see KXmlGuiWindow::setupGUI()
324  */
325  void setAutoSaveSettings(const QString &groupName = QStringLiteral("MainWindow"), bool saveWindowSize = true);
326 
327  /**
328  * This is an overloaded function.
329  * This allows the settings to be saved into a different file
330  * that does not correspond to that used for KSharedConfig::openConfig().
331  * @see setAutoSaveSettings(const QString &groupName, bool saveWindowSize)
332  * @see KConfig
333  * @see KSharedConfig
334  * @since 4.1
335  */
336  void setAutoSaveSettings(const KConfigGroup &group, bool saveWindowSize = true);
337 
338  /**
339  * @brief Disables the autosave settings feature.
340  * You don't normally need to call this, ever.
341  * @see setAutoSaveSettings()
342  * @see autoSaveSettings()
343  */
344  void resetAutoSaveSettings();
345 
346  /**
347  * @return @c true if setAutoSaveSettings() was called,
348  * @c false by default or if resetAutoSaveSettings() was called.
349  * @see setAutoSaveSettings()
350  * @see resetAutoSaveSettings()
351  */
352  bool autoSaveSettings() const;
353 
354  /**
355  * @return The group used for autosaving settings.
356  *
357  * Do not mistake this with autoSaveConfigGroup.
358  *
359  * Only meaningful if setAutoSaveSettings(const QString&, bool) was called.
360  *
361  * Do not use this method if setAutoSaveSettings(const KConfigGroup&, bool) was called.
362  *
363  * This can be useful for forcing a save or an apply, e.g. before and after
364  * using KEditToolBar.
365  *
366  * @note Prefer saveAutoSaveSettings() for saving or autoSaveConfigGroup() for loading.
367  *
368  * @see autoSaveSettings()
369  * @see setAutoSaveSettings()
370  * @see saveAutoSaveSettings()
371  * @see resetAutoSaveSettings()
372  * @see autoSaveConfigGroup()
373  */
374  QString autoSaveGroup() const;
375 
376  /**
377  * @return The group used for autosaving settings.
378  *
379  * Only meaningful if setAutoSaveSettings(const QString&, bool) was called.
380  *
381  * Do not use this method if setAutoSaveSettings(const KConfigGroup&, bool) was called.
382  *
383  * This can be useful for forcing an apply, e.g. after using KEditToolBar.
384  *
385  * @see setAutoSaveSettings()
386  * @see autoSaveGroup()
387  * @since 4.1
388  */
389  KConfigGroup autoSaveConfigGroup() const;
390 
391  /**
392  * @brief Assigns the config group name for the KConfigGroup returned by stateConfigGroup.
393  * @param configGroup The config group to be assigned.
394  * Window size and state are stored in the resulting KConfigGroup when this function is called.
395  * @note If this is used in combination with setAutoSaveSettings, you should call this method first.
396  *
397  * @see KConfigGroup()
398  * @see KSharedConfig::openStateConfig()
399  * @see stateConfigGroup()
400  *
401  * @since 5.88
402  */
403  void setStateConfigGroup(const QString &configGroup);
404 
405  /**
406  * @returns The KConfigGroup used to store state data like window sizes or window state.
407  *
408  * The resulting group is invalid if setStateConfig is not called explicitly.
409  *
410  * @see KConfigGroup
411  * @since 5.88
412  */
413  KConfigGroup stateConfigGroup() const;
414 
415  /**
416  * @brief Read settings for statusbar, menubar and toolbar from their respective
417  * groups in the config file and apply them.
418  *
419  * @param config Config group to read the settings from.
420  */
421  virtual void applyMainWindowSettings(const KConfigGroup &config);
422 
423  /**
424  * @brief Manually save the settings for statusbar, menubar and toolbar to their respective
425  * groups in the KConfigGroup @p config.
426  *
427  * Example:
428  * \code
429  * KConfigGroup group(KSharedConfig::openConfig(), "MainWindow");
430  * saveMainWindowSettings(group);
431  * \endcode
432  *
433  * @param config Config group to save the settings to.
434  * @see setAutoSaveSettings()
435  * @see KConfig
436  * @see KSharedConfig
437  * @see KConfigGroup
438  */
439  void saveMainWindowSettings(KConfigGroup &config);
440 
441  /**
442  * @returns The path for the exported window's D-Bus object.
443  * @since 4.0.1
444  */
445  QString dbusName() const;
446 
447 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
448  /**
449  * @returns Always @c false
450  * @deprecated since 5.0, the functionality got removed
451  **/
452  KXMLGUI_DEPRECATED_VERSION(5, 0, "Remove usage, is a no-op now")
453  bool initialGeometrySet() const
454  {
455  return false;
456  }
457 #endif
458 
459 public Q_SLOTS:
460  /**
461  * @brief Assigns a KDE compliant caption (window title).
462  *
463  * @param caption The string that will be
464  * displayed in the window title, before the application name.
465  *
466  * @note This function does the same as setPlainCaption().
467  *
468  * @note Do not include the application name
469  * in this string. It will be added automatically according to the KDE
470  * standard.
471  *
472  * @see setPlainCaption()
473  */
474  virtual void setCaption(const QString &caption);
475  /**
476  * @brief Makes a KDE compliant caption.
477  * @param caption Your caption.
478  * @param modified Whether the document is modified. This displays
479  * an additional sign in the title bar, usually "**".
480  *
481  * This is an overloaded function.
482  *
483  * @note Do not include the application name
484  * in this string. It will be added automatically according to the KDE
485  * standard.
486  */
487  virtual void setCaption(const QString &caption, bool modified);
488 
489  /**
490  * @brief Make a plain caption without any modifications.
491  *
492  * @param caption The string that will be
493  * displayed in the window title, before the application name.
494  *
495  * @note This function does the same as setCaption().
496  *
497  * @note Do not include the application name
498  * in this string. It will be added automatically according to the KDE
499  * standard.
500  *
501  * @see setCaption()
502  */
503  virtual void setPlainCaption(const QString &caption);
504 
505  /**
506  * @brief Opens the help page for the application.
507  *
508  * The application name is
509  * used as a key to determine what to display and the system will attempt
510  * to open <appName>/index.html.
511  *
512  * This method is intended for use by a help button in the toolbar or
513  * components outside the regular help menu.
514  *
515  * Use helpMenu() when you
516  * want to provide access to the help system from the help menu.
517  *
518  * Example (adding a help button to the first toolbar):
519  *
520  * \code
521  * toolBar()->addAction(QIcon::fromTheme("help-contents"), i18n("Help"),
522  * this, &KMainWindow::appHelpActivated);
523  * \endcode
524  *
525  * @see helpMenu()
526  * @see toolBar()
527  */
528  void appHelpActivated();
529 
530  /**
531  * @brief Tell the main window that it should save its settings when being closed.
532  *
533  * This is part of the autosave settings feature.
534  *
535  * For everything related to toolbars this happens automatically,
536  * but you have to call setSettingsDirty() in the slot that toggles
537  * the visibility of the statusbar.
538  *
539  * @see saveAutoSaveSettings()
540  */
541  void setSettingsDirty();
542 
543 protected:
544  /**
545  * Reimplemented to catch QEvent::Polish in order to adjust the object name
546  * if needed, once all constructor code for the main window has run.
547  * Also reimplemented to catch when a QDockWidget is added or removed.
548  */
549  bool event(QEvent *event) override;
550 
551  /**
552  * Reimplemented to autosave settings and call queryClose().
553  *
554  * We recommend that you reimplement queryClose() rather than closeEvent().
555  * If you do it anyway, ensure to call the base implementation to keep
556  * the feature of autosaving window settings working.
557  */
558  void closeEvent(QCloseEvent *) override;
559 
560  /**
561  * @brief This function is called before the window is closed,
562  * either by the user or indirectly by the session manager.
563  *
564  * This can be used to prompt the user to save unsaved data before the window is closed.
565  *
566  * Example:
567  * \code
568  * switch ( KMessageBox::warningTwoActionsCancel( this,
569  * i18n("Save changes to document foo?"), QString(),
570  * KStandardGuiItem::save(), KStandardGuiItem::discard())) ) {
571  * case KMessageBox::PrimaryAction :
572  * // save document here. If saving fails, return false;
573  * return true;
574  * case KMessageBox::SecondaryAction :
575  * return true;
576  * default: // cancel
577  * return false;
578  * \endcode
579  *
580  * @note Do @em not close the document from within this method,
581  * as it may be called by the session manager before the
582  * session is saved. If the document is closed before the session save occurs,
583  * its location might not be properly saved. In addition, the session shutdown
584  * may be canceled, in which case the document should remain open.
585  *
586  * @return @c true by default, @c false according to the reimplementation.
587  * Returning @c false will cancel the closing operation,
588  * and if KApplication::sessionSaving() is true, it cancels logout.
589  *
590  * @see KApplication::sessionSaving()
591  */
592  virtual bool queryClose();
593 
594  /**
595  * @brief Saves your instance-specific properties.
596  *
597  * The function is
598  * invoked when the session manager requests your application
599  * to save its state.
600  *
601  * Reimplement this function in child classes.
602  *
603  * \code
604  * void MainWindow::saveProperties(KConfigGroup &config) {
605  * config.writeEntry("myKey", "newValue");
606  * ...
607  * }
608  * \endcode
609  *
610  * @note No user interaction is allowed
611  * in this function!
612  *
613  */
614  virtual void saveProperties(KConfigGroup &)
615  {
616  }
617 
618  /**
619  * @brief Reads your instance-specific properties.
620  *
621  * This function is called indirectly by restore().
622  *
623  * \code
624  * void MainWindow::readProperties(KConfigGroup &config) {
625  * if (config.hasKey("myKey")) {
626  * config.readEntry("myKey", "DefaultValue");
627  * }
628  * ...
629  * }
630  * \endcode
631  *
632  * @see readGlobalProperties()
633  */
634  virtual void readProperties(const KConfigGroup &)
635  {
636  }
637 
638  /**
639  * @brief Saves your application-wide properties.
640  *
641  * @param sessionConfig A pointer to the KConfig instance
642  * used to save the session data.
643  *
644  * This function is invoked when the session manager
645  * requests your application to save its state.
646  * It is similar to saveProperties(), but it is only called for
647  * the first main window. This is useful to save global state of your application
648  * that isn't bound to a particular window.
649  *
650  * The default implementation does nothing.
651  *
652  * @see readGlobalProperties()
653  * @see saveProperties()
654  */
655  virtual void saveGlobalProperties(KConfig *sessionConfig);
656 
657  /**
658  * @brief Reads your application-wide properties.
659  *
660  * @param sessionConfig A pointer to the KConfig instance
661  * used to load the session data.
662  *
663  * @see saveGlobalProperties()
664  * @see readProperties()
665  *
666  */
667  virtual void readGlobalProperties(KConfig *sessionConfig);
668  void savePropertiesInternal(KConfig *, int);
669  bool readPropertiesInternal(KConfig *, int);
670 
671  /**
672  * For inherited classes
673  */
674  bool settingsDirty() const;
675 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
676  /**
677  * For inherited classes
678  * @deprecated Since 5.0, use KWindowConfig::saveWindowSize
679  */
680  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KWindowConfig::saveWindowSize(...)")
681  void saveWindowSize(KConfigGroup &config) const;
682 #endif
683 
684 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
685  /**
686  * For inherited classes
687  * @deprecated Since 5.0, use KWindowConfig::restoreWindowSize
688  */
689  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KWindowConfig::restoreWindowSize(...)")
690  void restoreWindowSize(const KConfigGroup &config);
691 #endif
692 
693 protected Q_SLOTS:
694 #if KXMLGUI_BUILD_DEPRECATED_SINCE(5, 0)
695  /**
696  * This slot does nothing.
697  *
698  * It must be reimplemented if you want
699  * to use a custom About Application dialog box. This slot is
700  * connected to the About Application entry in the menu returned
701  * by customHelpMenu.
702  *
703  * Example:
704  * \code
705  *
706  * void MyMainLevel::setupInterface()
707  * {
708  * ..
709  * menuBar()->addMenu( customHelpMenu() );
710  * ..
711  * }
712  *
713  * void MyMainLevel::showAboutApplication()
714  * {
715  * <activate your custom dialog>
716  * }
717  * \endcode
718  * @deprecated Since 5.0, use KHelpMenu
719  */
720  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KHelpMenu")
721  virtual void showAboutApplication()
722  {
723  }
724 #endif
725 
726  /**
727  * This slot should only be called in case you reimplement closeEvent() and
728  * if you are using the autosave feature. In all other cases,
729  * setSettingsDirty() should be called instead to benefit from the delayed
730  * saving.
731  *
732  * Example:
733  * \code
734  *
735  * void MyMainWindow::closeEvent( QCloseEvent *e )
736  * {
737  * // Save settings if autosave is enabled, and settings have changed
738  * if ( settingsDirty() && autoSaveSettings() )
739  * saveAutoSaveSettings();
740  * ..
741  * }
742  * \endcode
743  *
744  * @see setAutoSaveSettings()
745  * @see setSettingsDirty()
746  */
747  void saveAutoSaveSettings();
748 
749 protected:
750  KXMLGUI_NO_EXPORT KMainWindow(KMainWindowPrivate &dd, QWidget *parent, Qt::WindowFlags f);
751 
752  std::unique_ptr<KMainWindowPrivate> const k_ptr;
753  // KF6 TODO: change k_ptr to d_ptr, use normal Q_DECLARE_PRIVATE
754 
755 private:
756  Q_DECLARE_PRIVATE_D(k_ptr, KMainWindow)
757 
758  Q_PRIVATE_SLOT(d_func(), void _k_slotSettingsChanged(int))
759  Q_PRIVATE_SLOT(d_func(), void _k_slotSaveAutoSaveSize())
760  Q_PRIVATE_SLOT(d_func(), void _k_slotSaveAutoSavePosition())
761 };
762 
763 /**
764  * @defgroup KXMLGUI_Session KXMLGUI Session Macros and Functions
765  *
766  * @{
767  */
768 
769 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
770 /**
771  * @def RESTORE
772  * Restores the last session.
773  *
774  * @deprecated since 5.0, use kRestoreMainWindows() instead
775  **/
776 #define RESTORE(type) \
777  { \
778  int n = 1; \
779  while (KMainWindow::canBeRestored(n)) { \
780  (new type)->restore(n); \
781  n++; \
782  } \
783  }
784 #endif
785 
786 /**
787  * @def KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS
788  * Returns the maximal number of arguments that are actually
789  * supported by kRestoreMainWindows().
790  **/
791 #define KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS 3
792 
793 /**
794  * @brief Restores the last session. (To be used in your main function).
795  *
796  * These functions work also if you have more than one kind of top-level
797  * widget (each derived from KMainWindow, of course).
798  *
799  * Imagine you have three kinds of top-level widgets: the classes @c childMW1,
800  * @c childMW2 and @c childMW3. Then you can just do:
801  *
802  * \code
803  * int main(int argc, char *argv[])
804  * {
805  * // [...]
806  * if (qApp->isSessionRestored())
807  * kRestoreMainWindows<childMW1, childMW2, childMW3>();
808  * else {
809  * // create default application as usual
810  * }
811  * // [...]
812  * }
813  * \endcode
814  *
815  * kRestoreMainWindows<>() will create (on the heap) as many instances
816  * of your main windows as have existed in the last session and
817  * call KMainWindow::restore() with the correct arguments. Note that
818  * also QWidget::show() is called implicitly.
819  *
820  * Currently, these functions are provided for up to three
821  * template arguments. If you need more, tell us. To help you in
822  * deciding whether or not you can use kRestoreMainWindows, a
823  * define #KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS is provided.
824  *
825  * @note Prefer this function over directly calling KMainWindow::restore().
826  *
827  * @tparam T Top-level widget class
828  *
829  * @see KMainWindow::restore()
830  * @see KMainWindow::classNameOfToplevel()
831  **/
832 template<typename T>
833 inline void kRestoreMainWindows()
834 {
835  for (int n = 1; KMainWindow::canBeRestored(n); ++n) {
836  const QString className = KMainWindow::classNameOfToplevel(n);
837  if (className == QLatin1String(T::staticMetaObject.className())) {
838  (new T)->restore(n);
839  }
840  }
841 }
842 
843 /**
844  * @brief Restores the last session.
845  * This is an overloaded function.
846  *
847  * Use this with multiple different top-level widget classes.
848  *
849  * @tparam T0 One top-level widget class
850  * @tparam T1 Explicit other top-level widget class for disambiguation from base template
851  * @tparam Tn Parameter pack to take 0..n further KMainWindows
852  * @see kRestoreMainWindows()
853  */
854 template<typename T0, typename T1, typename... Tn>
855 inline void kRestoreMainWindows()
856 {
857  kRestoreMainWindows<T0>();
858  kRestoreMainWindows<T1, Tn...>();
859 }
860 /** @} */
861 
862 #endif
virtual bool event(QEvent *event) override
Q_PROPERTY(...)
Q_SLOTSQ_SLOTS
virtual void readProperties(const KConfigGroup &)
Reads your instance-specific properties.
Definition: kmainwindow.h:634
Floatable toolbar with auto resize.
Definition: ktoolbar.h:67
void kRestoreMainWindows()
Restores the last session.
Definition: kmainwindow.h:833
static const QString classNameOfToplevel(int instanceNumber)
Useful if your application uses different kinds of top-level windows.
typedef WindowFlags
static bool canBeRestored(int numberOfInstances)
KSharedConfigPtr config()
KMainWindow represents a top-level main window.
Definition: kmainwindow.h:69
virtual void closeEvent(QCloseEvent *event)
virtual void saveProperties(KConfigGroup &)
Saves your instance-specific properties.
Definition: kmainwindow.h:614
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 10 2023 03:57:29 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.