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  * @short Top level main window
44  *
45  * Top level widget that provides toolbars, a status line and a frame.
46  *
47  * It should be used as a top level (parent-less) widget.
48  * It manages the geometry for all its children, including your
49  * main widget.
50  *
51  * Normally, you will inherit from KMainWindow,
52  * then construct (or use some existing) widget as
53  * your main view. You can set only one main view.
54  *
55  * You can add as many toolbars as you like. There can be only one menubar
56  * and only one statusbar.
57  *
58  * The toolbars, menubar, and statusbar can be created by the
59  * KMainWindow and - unlike the old KMainWindow - may, but do not
60  * have to, be deleted by you. KMainWindow will handle that internally.
61  *
62  * Height and width can be operated independently from each other. Simply
63  * define the minimum/maximum height/width of your main widget and
64  * KMainWindow will take this into account. For fixed size windows set
65  * your main widget to a fixed size.
66  *
67  * Fixed aspect ratios (heightForWidth()) and fixed width widgets are
68  * not supported.
69  *
70  * KMainWindow will set icon, mini icon and caption, which it gets
71  * from KApplication. It provides full session management, and
72  * will save its position, geometry and positions of toolbars and
73  * menubar on logout. If you want to save additional data, reimplement
74  * saveProperties() and (to read them again on next login)
75  * readProperties(). To save special data about your data, reimplement
76  * saveGlobalProperties(). To warn user that application or
77  * windows have unsaved data on close or logout, reimplement
78  * queryClose().
79  *
80  * You have to implement session restoring also in your main() function.
81  * There are also kRestoreMainWindows convenience functions which
82  * can do this for you and restore all your windows on next login.
83  *
84  * Note that KMainWindow uses KGlobal::ref() and KGlobal::deref() so that closing
85  * the last mainwindow will quit the application unless there is still something
86  * that holds a ref in KGlobal - like a KIO job, or a systray icon.
87  *
88  * @author Reginald Stadlbauer ([email protected]) Stephan Kulow ([email protected]), Matthias Ettrich ([email protected]), Chris Schlaeger ([email protected]), Sven Radej
89  * ([email protected]). Maintained by David Faure ([email protected])
90  */
91 
92 class KXMLGUI_EXPORT KMainWindow : public QMainWindow
93 {
94  friend class KMWSessionManager;
95  friend class DockResizeListener;
96  Q_OBJECT
97  Q_PROPERTY(bool hasMenuBar READ hasMenuBar)
98  Q_PROPERTY(bool autoSaveSettings READ autoSaveSettings)
99  Q_PROPERTY(QString autoSaveGroup READ autoSaveGroup)
100 
101 public:
102  /**
103  * Construct a main window.
104  *
105  * @param parent The widget parent. This is usually 0 but it may also be the window
106  * group leader. In that case, the KMainWindow becomes sort of a
107  * secondary window.
108  *
109  * @param f Specify the window flags. The default is none.
110  *
111  * Note that a KMainWindow per-default is created with the
112  * Qt::WA_DeleteOnClose attribute set, i.e. it is automatically destroyed
113  * when the window is closed. If you do not want this behavior, call
114  * \code
115  * window->setAttribute(Qt::WA_DeleteOnClose, false);
116  * \endcode
117  *
118  * KMainWindows must be created on the heap with 'new', like:
119  * \code
120  * KMainWindow *kmw = new KMainWindow(...);
121  * kmw->setObjectName(...);
122  * \endcode
123  *
124  * Since the KDE Frameworks 5.16 release, KMainWindow will also enter information regarding
125  * the application's translators by default, using KAboutData::setTranslator(). This only occurs
126  * if no translators are already assigned in KAboutData (see KAboutData::setTranslator() for
127  * details -- the auto-assignment here uses the same translated strings as specified for that
128  * function).
129  *
130  * IMPORTANT: For session management and window management to work
131  * properly, all main windows in the application should have a
132  * different name. If you don't do it, KMainWindow will create
133  * a unique name, but it's recommended to explicitly pass a window name that will
134  * also describe the type of the window. If there can be several windows of the same
135  * type, append '#' (hash) to the name, and KMainWindow will replace it with numbers to make
136  * the names unique. For example, for a mail client which has one main window showing
137  * the mails and folders, and which can also have one or more windows for composing
138  * mails, the name for the folders window should be e.g. "mainwindow" and
139  * for the composer windows "composer#".
140  *
141  */
142  explicit KMainWindow(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
143 
144  /**
145  * \brief Destructor.
146  *
147  * Will also destroy the toolbars, and menubar if
148  * needed.
149  */
150  ~KMainWindow() override;
151 
152 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
153  /**
154  * Retrieve the standard help menu.
155  *
156  * It contains entries for the
157  * help system (activated by F1), an optional "What's This?" entry
158  * (activated by Shift F1), an application specific dialog box,
159  * and an "About KDE" dialog box.
160  *
161  * Example (adding a standard help menu to your application):
162  * \code
163  * QMenu *help = helpMenu( <myTextString> );
164  * menuBar()->addMenu( help );
165  * \endcode
166  *
167  * @param aboutAppText The string that is used in the application
168  * specific dialog box. If you leave this string empty the
169  * information in the global KAboutData of the
170  * application will be used to make a standard dialog box.
171  *
172  * @param showWhatsThis Set this to false if you do not want to include
173  * the "What's This" menu entry.
174  *
175  * @return A standard help menu.
176  * @deprecated Since 5.0, use KHelpMenu directly
177  */
178  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KHelpMenu directly")
179  QMenu *helpMenu(const QString &aboutAppText = QString(), bool showWhatsThis = true);
180 #endif
181 
182 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
183  /**
184  * Returns the help menu. Creates a standard help menu if none exists yet.
185  *
186  * It contains entries for the
187  * help system (activated by F1), an optional "What's This?" entry
188  * (activated by Shift F1), an application specific dialog box,
189  * and an "About KDE" dialog box. You must create the application
190  * specific dialog box yourself. When the "About application"
191  * menu entry is activated, a signal will trigger the
192  * showAboutApplication slot. See showAboutApplication for more
193  * information.
194  *
195  * Example (adding a help menu to your application):
196  * \code
197  * menuBar()->addMenu( customHelpMenu() );
198  * \endcode
199  *
200  * @param showWhatsThis Set this to @c false if you do not want to include
201  * the "What's This" menu entry.
202  *
203  * @return A standard help menu.
204  * @deprecated Since 5.0, use XMLGUI instead, or KHelpMenu directly
205  */
206  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use XMLGUI or KHelpMenu")
207  QMenu *customHelpMenu(bool showWhatsThis = true);
208 #endif
209 
210  /**
211  * If the session did contain so high a number, @c true is returned,
212  * else @c false.
213  * @see restore()
214  **/
215  static bool canBeRestored(int number);
216 
217  /**
218  * Returns the className() of the @p number of the toplevel window which
219  * should be restored.
220  *
221  * This is only useful if your application uses
222  * different kinds of toplevel windows.
223  */
224  static const QString classNameOfToplevel(int number);
225 
226  /**
227  * Try to restore the toplevel widget as defined by @p number (1..X).
228  *
229  * You should call canBeRestored() first.
230  *
231  * If the session did not contain so high a number, the configuration
232  * is not changed and @c false returned.
233  *
234  * That means clients could simply do the following:
235  * \code
236  * if (qApp->isSessionRestored()){
237  * int n = 1;
238  * while (KMainWindow::canBeRestored(n)){
239  * (new childMW)->restore(n);
240  * n++;
241  * }
242  * } else {
243  * // create default application as usual
244  * }
245  * \endcode
246  * Note that if @p show is @c true (default), QWidget::show() is called
247  * implicitly in restore.
248  *
249  * With this you can easily restore all toplevel windows of your
250  * application.
251  *
252  * If your application uses different kinds of toplevel
253  * windows, then you can use KMainWindow::classNameOfToplevel(n)
254  * to determine the exact type before calling the childMW
255  * constructor in the example from above.
256  *
257  * @note You don't need to deal with this function. Use the
258  * kRestoreMainWindows() convenience template function instead!
259  * @see kRestoreMainWindows()
260  * @see readProperties()
261  * @see canBeRestored()
262  */
263  bool restore(int number, bool show = true);
264 
265  /**
266  * Returns true, if there is a menubar
267  */
268  bool hasMenuBar();
269 
270  /**
271  * List of members of KMainWindow class.
272  */
273  static QList<KMainWindow *> memberList();
274 
275  /**
276  * Returns a pointer to the toolbar with the specified name.
277  * This refers to toolbars created dynamically from the XML UI
278  * framework. If the toolbar does not exist one will be created.
279  *
280  * @param name The internal name of the toolbar. If no name is
281  * specified "mainToolBar" is assumed.
282  *
283  * @return A pointer to the toolbar
284  **/
285  KToolBar *toolBar(const QString &name = QString());
286 
287  /**
288  * @return A list of all toolbars for this window
289  */
290  QList<KToolBar *> toolBars() const;
291 
292  /**
293  * Call this to enable "auto-save" of toolbar/menubar/statusbar settings
294  * (and optionally window size).
295  * If the *bars were moved around/shown/hidden when the window is closed,
296  * saveMainWindowSettings( KConfigGroup(KSharedConfig::openConfig(), groupName) ) will be called.
297  *
298  * @param groupName a name that identifies this "type of window".
299  * You can have several types of window in the same application.
300  *
301  * @param saveWindowSize set it to true to include the window size
302  * when saving.
303  *
304  * Typically, you will call setAutoSaveSettings() in your
305  * KMainWindow-inherited class constructor, and it will take care
306  * of restoring and saving automatically. Make sure you call this
307  * _after all_ your *bars have been created.
308  *
309  * To make sure that KMainWindow properly obtains the default
310  * size of the window you should do the following:
311  * - Remove hard coded resize() calls in the constructor or main, they
312  * should be removed in favor of letting the automatic resizing
313  * determine the default window size. Hard coded window sizes will
314  * be wrong for users that have big fonts, use different styles,
315  * long/small translations, large toolbars, and other factors.
316  * - Put the setAutoSaveSettings() ( or setupGUI() ) call after all widgets
317  * have been created and placed inside the main window (i.e. for 99% of
318  * apps setCentralWidget())
319  * - Widgets that inherit from QWidget (like game boards) should overload
320  * "virtual QSize sizeHint() const;" to specify a default size rather
321  * than letting QWidget::adjust use the default size of 0x0.
322  */
323  void setAutoSaveSettings(const QString &groupName = QStringLiteral("MainWindow"), bool saveWindowSize = true);
324 
325  /**
326  * Overload that lets you specify a KConfigGroup.
327  * This allows the settings to be saved into another file than KSharedConfig::openConfig().
328  * @since 4.1
329  */
330  void setAutoSaveSettings(const KConfigGroup &group, bool saveWindowSize = true);
331 
332  /**
333  * Disable the auto-save-settings feature.
334  * You don't normally need to call this, ever.
335  */
336  void resetAutoSaveSettings();
337 
338  /**
339  * @return the current autosave setting, i.e. true if setAutoSaveSettings() was called,
340  * false by default or if resetAutoSaveSettings() was called.
341  */
342  bool autoSaveSettings() const;
343 
344  /**
345  * @return the group used for setting-autosaving.
346  * Only meaningful if setAutoSaveSettings(const QString&, bool) was called.
347  * This can be useful for forcing a save or an apply, e.g. before and after
348  * using KEditToolBar.
349  *
350  * @note You should rather use saveAutoSaveSettings() for saving or autoSaveConfigGroup() for loading.
351  * This method doesn't make sense if setAutoSaveSettings(const KConfigGroup&, bool) was called.
352  */
353  QString autoSaveGroup() const;
354 
355  /**
356  * @return the group used for setting-autosaving.
357  * Only meaningful if setAutoSaveSettings() was called.
358  * This can be useful for forcing an apply, e.g. after using KEditToolBar.
359  * @since 4.1
360  */
361  KConfigGroup autoSaveConfigGroup() const;
362 
363  /**
364  * Set the config group name for state config returned by @p stateConfigGroup
365  * If this method is called the window size and window state are stored in the resulting KConfigGroup.
366  * @note If this is used in combination with @p setAutoSaveSettings, you should call this method first
367  *
368  * @see KSharedConfig::openStateConfig
369  *
370  * @since 5.88
371  */
372  void setStateConfigGroup(const QString &configGroup);
373 
374  /*
375  * Returns the KConfigGroup used to store state data like window sizes or window state.
376  * The resulting group in invalid if @p setStateConfig is not called explicitly.
377  * @since 5.88
378  */
379  KConfigGroup stateConfigGroup() const;
380 
381  /**
382  * Read settings for statusbar, menubar and toolbar from their respective
383  * groups in the config file and apply them.
384  *
385  * @param config Config group to read the settings from.
386  *
387  * KF5 porting note: the unused bool argument was removed, make sure to remove it from your
388  * reimplementations too! And add a override for good measure.
389  */
390  virtual void applyMainWindowSettings(const KConfigGroup &config);
391 
392  /**
393  * Save settings for statusbar, menubar and toolbar to their respective
394  * groups in the config group @p config.
395  *
396  * @param config Config group to save the settings to.
397  */
398  void saveMainWindowSettings(KConfigGroup &config);
399 
400  /**
401  * Returns the path under which this window's D-Bus object is exported.
402  * @since 4.0.1
403  */
404  QString dbusName() const;
405 
406 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
407  /**
408  * @returns Always @c false
409  * @deprecated since 5.0, the functionality got removed
410  **/
411  KXMLGUI_DEPRECATED_VERSION(5, 0, "Remove usage, is a no-op now")
412  bool initialGeometrySet() const
413  {
414  return false;
415  }
416 #endif
417 
418 public Q_SLOTS:
419  /**
420  * Makes a KDE compliant caption (window title).
421  *
422  * @param caption Your caption. @em Do @em not include the application name
423  * in this string. It will be added automatically according to the KDE
424  * standard.
425  */
426  virtual void setCaption(const QString &caption);
427  /**
428  * Makes a KDE compliant caption.
429  *
430  * @param caption Your caption. @em Do @em not include the application name
431  * in this string. It will be added automatically according to the KDE
432  * standard.
433  * @param modified Specify whether the document is modified. This displays
434  * an additional sign in the title bar, usually "**".
435  */
436  virtual void setCaption(const QString &caption, bool modified);
437 
438  /**
439  * Make a plain caption without any modifications.
440  *
441  * @param caption Your caption. This is the string that will be
442  * displayed in the window title.
443  */
444  virtual void setPlainCaption(const QString &caption);
445 
446  /**
447  * Open the help page for the application.
448  *
449  * The application name is
450  * used as a key to determine what to display and the system will attempt
451  * to open <appName>/index.html.
452  *
453  * This method is intended for use by a help button in the toolbar or
454  * components outside the regular help menu. Use helpMenu() when you
455  * want to provide access to the help system from the help menu.
456  *
457  * Example (adding a help button to the first toolbar):
458  *
459  * \code
460  * toolBar(0)->addAction(QIcon::fromTheme("help-contents"), i18n("Help"),
461  * this, SLOT(appHelpActivated()));
462  * \endcode
463  *
464  */
465  void appHelpActivated();
466 
467  /**
468  * Tell the main window that it should save its settings when being closed.
469  * This is part of the auto-save-settings feature.
470  * For everything related to toolbars this happens automatically,
471  * but you have to call setSettingsDirty() in the slot that toggles
472  * the visibility of the statusbar.
473  */
474  void setSettingsDirty();
475 
476 protected:
477  /**
478  * Reimplemented to catch QEvent::Polish in order to adjust the object name
479  * if needed, once all constructor code for the main window has run.
480  * Also reimplemented to catch when a QDockWidget is added or removed.
481  */
482  bool event(QEvent *event) override;
483 
484  /**
485  * Reimplemented to autosave settings and call queryClose().
486  *
487  * We recommend that you reimplement queryClose() rather than closeEvent().
488  * If you do it anyway, ensure to call the base implementation to keep
489  * the feature of auto-saving window settings working.
490  */
491  void closeEvent(QCloseEvent *) override;
492 
493  /**
494  Called before the window is closed, either by the user or indirectly by
495  the session manager.
496 
497  The purpose of this function is to prepare the window in a way that it is
498  safe to close it, i.e. without the user losing some data.
499 
500  Default implementation returns true. Returning @c false will cancel
501  the closing, and, if KApplication::sessionSaving() is true, it will also
502  cancel KDE logout.
503 
504  Reimplement this function to prevent the user from losing data.
505  Example:
506  \code
507  switch ( KMessageBox::warningYesNoCancel( this,
508  i18n("Save changes to document foo?")) ) {
509  case KMessageBox::Yes :
510  // save document here. If saving fails, return false;
511  return true;
512  case KMessageBox::No :
513  return true;
514  default: // cancel
515  return false;
516  \endcode
517 
518  Note that you should probably @em not actually close the document from
519  within this method, as it may be called by the session manager before the
520  session is saved. If the document is closed before the session save occurs,
521  its location might not be properly saved. In addition, the session shutdown
522  may be canceled, in which case the document should remain open.
523 
524  @see KApplication::sessionSaving()
525  */
526  virtual bool queryClose();
527 
528  /**
529  * Save your instance-specific properties. The function is
530  * invoked when the session manager requests your application
531  * to save its state.
532  *
533  * Please reimplement these function in childclasses.
534  *
535  * Note: No user interaction is allowed
536  * in this function!
537  *
538  */
539  virtual void saveProperties(KConfigGroup &)
540  {
541  }
542 
543  /**
544  * Read your instance-specific properties.
545  *
546  * Is called indirectly by restore().
547  */
548  virtual void readProperties(const KConfigGroup &)
549  {
550  }
551 
552  /**
553  * Save your application-wide properties. The function is
554  * invoked when the session manager requests your application
555  * to save its state.
556  *
557  * This function is similar to saveProperties() but is only called for
558  * the very first main window, regardless how many main window are open.
559 
560  * Override it if you need to save other data about your documents on
561  * session end. sessionConfig is a config to which that data should be
562  * saved. Normally, you don't need this function. But if you want to save
563  * data about your documents that are not in opened windows you might need
564  * it.
565  *
566  * Default implementation does nothing.
567  */
568  virtual void saveGlobalProperties(KConfig *sessionConfig);
569 
570  /**
571  * The counterpart of saveGlobalProperties().
572  *
573  * Read the application-specific properties in again.
574  */
575  virtual void readGlobalProperties(KConfig *sessionConfig);
576  void savePropertiesInternal(KConfig *, int);
577  bool readPropertiesInternal(KConfig *, int);
578 
579  /**
580  * For inherited classes
581  */
582  bool settingsDirty() const;
583 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
584  /**
585  * For inherited classes
586  * @deprecated Since 5.0, use KWindowConfig::saveWindowSize
587  */
588  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KWindowConfig::saveWindowSize(...)")
589  void saveWindowSize(KConfigGroup &config) const;
590 #endif
591 
592 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
593  /**
594  * For inherited classes
595  * @deprecated Since 5.0, use KWindowConfig::restoreWindowSize
596  */
597  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KWindowConfig::restoreWindowSize(...)")
598  void restoreWindowSize(const KConfigGroup &config);
599 #endif
600 
601 protected Q_SLOTS:
602 #if KXMLGUI_BUILD_DEPRECATED_SINCE(5, 0)
603  /**
604  * This slot does nothing.
605  *
606  * It must be reimplemented if you want
607  * to use a custom About Application dialog box. This slot is
608  * connected to the About Application entry in the menu returned
609  * by customHelpMenu.
610  *
611  * Example:
612  * \code
613  *
614  * void MyMainLevel::setupInterface()
615  * {
616  * ..
617  * menuBar()->addMenu( customHelpMenu() );
618  * ..
619  * }
620  *
621  * void MyMainLevel::showAboutApplication()
622  * {
623  * <activate your custom dialog>
624  * }
625  * \endcode
626  * @deprecated Since 5.0, use KHelpMenu
627  */
628  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KHelpMenu")
629  virtual void showAboutApplication()
630  {
631  }
632 #endif
633 
634  /**
635  * This slot should only be called in case you reimplement closeEvent() and
636  * if you are using the "auto-save" feature. In all other cases,
637  * setSettingsDirty() should be called instead to benefit from the delayed
638  * saving.
639  *
640  * @see setAutoSaveSettings
641  * @see setSettingsDirty
642  *
643  * Example:
644  * \code
645  *
646  * void MyMainWindow::closeEvent( QCloseEvent *e )
647  * {
648  * // Save settings if auto-save is enabled, and settings have changed
649  * if ( settingsDirty() && autoSaveSettings() )
650  * saveAutoSaveSettings();
651  * ..
652  * }
653  * \endcode
654  */
655  void saveAutoSaveSettings();
656 
657 protected:
658  KMainWindow(KMainWindowPrivate &dd, QWidget *parent, Qt::WindowFlags f);
659 
660  std::unique_ptr<KMainWindowPrivate> const k_ptr;
661  // KF6 TODO: change k_ptr to d_ptr, use normal Q_DECLARE_PRIVATE
662 
663 private:
664  Q_DECLARE_PRIVATE_D(k_ptr, KMainWindow)
665 
666  Q_PRIVATE_SLOT(d_func(), void _k_slotSettingsChanged(int))
667  Q_PRIVATE_SLOT(d_func(), void _k_slotSaveAutoSaveSize())
668  Q_PRIVATE_SLOT(d_func(), void _k_slotSaveAutoSavePosition())
669 };
670 
671 /**
672  * @defgroup KXMLGUI_Session KXMLGUI Session Macros and Functions
673  *
674  * @{
675  */
676 
677 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
678 /**
679  * @def RESTORE
680  * Restores the last session.
681  *
682  * @deprecated since 5.0, use kRestoreMainWindows() instead
683  **/
684 #define RESTORE(type) \
685  { \
686  int n = 1; \
687  while (KMainWindow::canBeRestored(n)) { \
688  (new type)->restore(n); \
689  n++; \
690  } \
691  }
692 #endif
693 
694 /**
695  * @def KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS
696  * Returns the maximal number of arguments that are actually
697  * supported by kRestoreMainWindows().
698  **/
699 #define KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS 3
700 
701 /**
702  * Restores the last session. (To be used in your main function).
703  *
704  * These functions work also if you have more than one kind of toplevel
705  * widget (each derived from KMainWindow, of course).
706  *
707  * Imagine you have three kinds of toplevel widgets: the classes @c childMW1,
708  * @c childMW2 and @c childMW3. Then you can just do:
709  *
710  * \code
711  * int main(int argc, char *argv[])
712  * {
713  * // [...]
714  * if (qApp->isSessionRestored())
715  * kRestoreMainWindows<childMW1, childMW2, childMW3>();
716  * else {
717  * // create default application as usual
718  * }
719  * // [...]
720  * }
721  * \endcode
722  *
723  * kRestoreMainWindows<>() will create (on the heap) as many instances
724  * of your main windows as have existed in the last session and
725  * call KMainWindow::restore() with the correct arguments. Note that
726  * also QWidget::show() is called implicitly.
727  *
728  * Currently, these functions are provided for up to three
729  * template arguments. If you need more, tell us. To help you in
730  * deciding whether or not you can use kRestoreMainWindows, a
731  * define #KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS is provided.
732  *
733  * @tparam T toplevel widget class
734  *
735  * @see KMainWindow::restore()
736  * @see KMainWindow::classNameOfToplevel()
737  **/
738 template<typename T>
739 inline void kRestoreMainWindows()
740 {
741  for (int n = 1; KMainWindow::canBeRestored(n); ++n) {
742  const QString className = KMainWindow::classNameOfToplevel(n);
743  if (className == QLatin1String(T::staticMetaObject.className())) {
744  (new T)->restore(n);
745  }
746  }
747 }
748 
749 /**
750  * Restores the last session.
751  * Overloaded method for usage with multiple different toplevel widget classes.
752  *
753  * @tparam T0 one toplevel widget class
754  * @tparam T1 explicit other toplevel widget class for disambiguation from base template
755  * @tparam Tn Parameter pack to take 0..n further KMainWindows
756  */
757 template<typename T0, typename T1, typename... Tn>
758 inline void kRestoreMainWindows()
759 {
760  kRestoreMainWindows<T0>();
761  kRestoreMainWindows<T1, Tn...>();
762 }
763 /** @} */
764 
765 #endif
virtual bool event(QEvent *event) override
Q_PROPERTY(...)
Q_SLOTSQ_SLOTS
static const QString classNameOfToplevel(int number)
Returns the className() of the number of the toplevel window which should be restored.
virtual void readProperties(const KConfigGroup &)
Read your instance-specific properties.
Definition: kmainwindow.h:548
Floatable toolbar with auto resize.
Definition: ktoolbar.h:60
void kRestoreMainWindows()
Restores the last session.
Definition: kmainwindow.h:739
typedef WindowFlags
static bool canBeRestored(int number)
If the session did contain so high a number, true is returned, else false.
Top level main window.
Definition: kmainwindow.h:92
virtual void closeEvent(QCloseEvent *event)
virtual void saveProperties(KConfigGroup &)
Save your instance-specific properties.
Definition: kmainwindow.h:539
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 26 2022 03:55:17 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.