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  *
367  * @see KSharedConfig::openStateConfig
368  *
369  * @since 5.88
370  */
371  void setStateConfigGroup(const QString &configGroup);
372 
373  /*
374  * Returns the KConfigGroup used to store state data like window sizes or window state.
375  * The resulting group in invalid if @p setStateConfig is not called explicitly.
376  * @since 5.88
377  */
378  KConfigGroup stateConfigGroup() const;
379 
380  /**
381  * Read settings for statusbar, menubar and toolbar from their respective
382  * groups in the config file and apply them.
383  *
384  * @param config Config group to read the settings from.
385  *
386  * KF5 porting note: the unused bool argument was removed, make sure to remove it from your
387  * reimplementations too! And add a override for good measure.
388  */
389  virtual void applyMainWindowSettings(const KConfigGroup &config);
390 
391  /**
392  * Save settings for statusbar, menubar and toolbar to their respective
393  * groups in the config group @p config.
394  *
395  * @param config Config group to save the settings to.
396  */
397  void saveMainWindowSettings(KConfigGroup &config);
398 
399  /**
400  * Returns the path under which this window's D-Bus object is exported.
401  * @since 4.0.1
402  */
403  QString dbusName() const;
404 
405 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
406  /**
407  * @returns Always @c false
408  * @deprecated since 5.0, the functionality got removed
409  **/
410  KXMLGUI_DEPRECATED_VERSION(5, 0, "Remove usage, is a no-op now")
411  bool initialGeometrySet() const
412  {
413  return false;
414  }
415 #endif
416 
417 public Q_SLOTS:
418  /**
419  * Makes a KDE compliant caption (window title).
420  *
421  * @param caption Your caption. @em Do @em not include the application name
422  * in this string. It will be added automatically according to the KDE
423  * standard.
424  */
425  virtual void setCaption(const QString &caption);
426  /**
427  * Makes a KDE compliant caption.
428  *
429  * @param caption Your caption. @em Do @em not include the application name
430  * in this string. It will be added automatically according to the KDE
431  * standard.
432  * @param modified Specify whether the document is modified. This displays
433  * an additional sign in the title bar, usually "**".
434  */
435  virtual void setCaption(const QString &caption, bool modified);
436 
437  /**
438  * Make a plain caption without any modifications.
439  *
440  * @param caption Your caption. This is the string that will be
441  * displayed in the window title.
442  */
443  virtual void setPlainCaption(const QString &caption);
444 
445  /**
446  * Open the help page for the application.
447  *
448  * The application name is
449  * used as a key to determine what to display and the system will attempt
450  * to open <appName>/index.html.
451  *
452  * This method is intended for use by a help button in the toolbar or
453  * components outside the regular help menu. Use helpMenu() when you
454  * want to provide access to the help system from the help menu.
455  *
456  * Example (adding a help button to the first toolbar):
457  *
458  * \code
459  * toolBar(0)->addAction(QIcon::fromTheme("help-contents"), i18n("Help"),
460  * this, SLOT(appHelpActivated()));
461  * \endcode
462  *
463  */
464  void appHelpActivated();
465 
466  /**
467  * Tell the main window that it should save its settings when being closed.
468  * This is part of the auto-save-settings feature.
469  * For everything related to toolbars this happens automatically,
470  * but you have to call setSettingsDirty() in the slot that toggles
471  * the visibility of the statusbar.
472  */
473  void setSettingsDirty();
474 
475 protected:
476  /**
477  * Reimplemented to catch QEvent::Polish in order to adjust the object name
478  * if needed, once all constructor code for the main window has run.
479  * Also reimplemented to catch when a QDockWidget is added or removed.
480  */
481  bool event(QEvent *event) override;
482 
483  /**
484  * Reimplemented to autosave settings and call queryClose().
485  *
486  * We recommend that you reimplement queryClose() rather than closeEvent().
487  * If you do it anyway, ensure to call the base implementation to keep
488  * the feature of auto-saving window settings working.
489  */
490  void closeEvent(QCloseEvent *) override;
491 
492  /**
493  Called before the window is closed, either by the user or indirectly by
494  the session manager.
495 
496  The purpose of this function is to prepare the window in a way that it is
497  safe to close it, i.e. without the user losing some data.
498 
499  Default implementation returns true. Returning @c false will cancel
500  the closing, and, if KApplication::sessionSaving() is true, it will also
501  cancel KDE logout.
502 
503  Reimplement this function to prevent the user from losing data.
504  Example:
505  \code
506  switch ( KMessageBox::warningYesNoCancel( this,
507  i18n("Save changes to document foo?")) ) {
508  case KMessageBox::Yes :
509  // save document here. If saving fails, return false;
510  return true;
511  case KMessageBox::No :
512  return true;
513  default: // cancel
514  return false;
515  \endcode
516 
517  Note that you should probably @em not actually close the document from
518  within this method, as it may be called by the session manager before the
519  session is saved. If the document is closed before the session save occurs,
520  its location might not be properly saved. In addition, the session shutdown
521  may be canceled, in which case the document should remain open.
522 
523  @see KApplication::sessionSaving()
524  */
525  virtual bool queryClose();
526 
527  /**
528  * Save your instance-specific properties. The function is
529  * invoked when the session manager requests your application
530  * to save its state.
531  *
532  * Please reimplement these function in childclasses.
533  *
534  * Note: No user interaction is allowed
535  * in this function!
536  *
537  */
538  virtual void saveProperties(KConfigGroup &)
539  {
540  }
541 
542  /**
543  * Read your instance-specific properties.
544  *
545  * Is called indirectly by restore().
546  */
547  virtual void readProperties(const KConfigGroup &)
548  {
549  }
550 
551  /**
552  * Save your application-wide properties. The function is
553  * invoked when the session manager requests your application
554  * to save its state.
555  *
556  * This function is similar to saveProperties() but is only called for
557  * the very first main window, regardless how many main window are open.
558 
559  * Override it if you need to save other data about your documents on
560  * session end. sessionConfig is a config to which that data should be
561  * saved. Normally, you don't need this function. But if you want to save
562  * data about your documents that are not in opened windows you might need
563  * it.
564  *
565  * Default implementation does nothing.
566  */
567  virtual void saveGlobalProperties(KConfig *sessionConfig);
568 
569  /**
570  * The counterpart of saveGlobalProperties().
571  *
572  * Read the application-specific properties in again.
573  */
574  virtual void readGlobalProperties(KConfig *sessionConfig);
575  void savePropertiesInternal(KConfig *, int);
576  bool readPropertiesInternal(KConfig *, int);
577 
578  /**
579  * For inherited classes
580  */
581  bool settingsDirty() const;
582 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
583  /**
584  * For inherited classes
585  * @deprecated Since 5.0, use KWindowConfig::saveWindowSize
586  */
587  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KWindowConfig::saveWindowSize(...)")
588  void saveWindowSize(KConfigGroup &config) const;
589 #endif
590 
591 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
592  /**
593  * For inherited classes
594  * @deprecated Since 5.0, use KWindowConfig::restoreWindowSize
595  */
596  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KWindowConfig::restoreWindowSize(...)")
597  void restoreWindowSize(const KConfigGroup &config);
598 #endif
599 
600 protected Q_SLOTS:
601 #if KXMLGUI_BUILD_DEPRECATED_SINCE(5, 0)
602  /**
603  * This slot does nothing.
604  *
605  * It must be reimplemented if you want
606  * to use a custom About Application dialog box. This slot is
607  * connected to the About Application entry in the menu returned
608  * by customHelpMenu.
609  *
610  * Example:
611  * \code
612  *
613  * void MyMainLevel::setupInterface()
614  * {
615  * ..
616  * menuBar()->addMenu( customHelpMenu() );
617  * ..
618  * }
619  *
620  * void MyMainLevel::showAboutApplication()
621  * {
622  * <activate your custom dialog>
623  * }
624  * \endcode
625  * @deprecated Since 5.0, use KHelpMenu
626  */
627  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KHelpMenu")
628  virtual void showAboutApplication()
629  {
630  }
631 #endif
632 
633  /**
634  * This slot should only be called in case you reimplement closeEvent() and
635  * if you are using the "auto-save" feature. In all other cases,
636  * setSettingsDirty() should be called instead to benefit from the delayed
637  * saving.
638  *
639  * @see setAutoSaveSettings
640  * @see setSettingsDirty
641  *
642  * Example:
643  * \code
644  *
645  * void MyMainWindow::closeEvent( QCloseEvent *e )
646  * {
647  * // Save settings if auto-save is enabled, and settings have changed
648  * if ( settingsDirty() && autoSaveSettings() )
649  * saveAutoSaveSettings();
650  * ..
651  * }
652  * \endcode
653  */
654  void saveAutoSaveSettings();
655 
656 protected:
657  KMainWindow(KMainWindowPrivate &dd, QWidget *parent, Qt::WindowFlags f);
658 
659  std::unique_ptr<KMainWindowPrivate> const k_ptr;
660  // KF6 TODO: change k_ptr to d_ptr, use normal Q_DECLARE_PRIVATE
661 
662 private:
663  Q_DECLARE_PRIVATE_D(k_ptr, KMainWindow)
664 
665  Q_PRIVATE_SLOT(d_func(), void _k_slotSettingsChanged(int))
666  Q_PRIVATE_SLOT(d_func(), void _k_slotSaveAutoSaveSize())
667  Q_PRIVATE_SLOT(d_func(), void _k_slotSaveAutoSavePosition())
668 };
669 
670 /**
671  * @defgroup KXMLGUI_Session KXMLGUI Session Macros and Functions
672  *
673  * @{
674  */
675 
676 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
677 /**
678  * @def RESTORE
679  * Restores the last session.
680  *
681  * @deprecated since 5.0, use kRestoreMainWindows() instead
682  **/
683 #define RESTORE(type) \
684  { \
685  int n = 1; \
686  while (KMainWindow::canBeRestored(n)) { \
687  (new type)->restore(n); \
688  n++; \
689  } \
690  }
691 #endif
692 
693 /**
694  * @def KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS
695  * Returns the maximal number of arguments that are actually
696  * supported by kRestoreMainWindows().
697  **/
698 #define KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS 3
699 
700 /**
701  * Restores the last session. (To be used in your main function).
702  *
703  * These functions work also if you have more than one kind of toplevel
704  * widget (each derived from KMainWindow, of course).
705  *
706  * Imagine you have three kinds of toplevel widgets: the classes @c childMW1,
707  * @c childMW2 and @c childMW3. Then you can just do:
708  *
709  * \code
710  * int main(int argc, char *argv[])
711  * {
712  * // [...]
713  * if (qApp->isSessionRestored())
714  * kRestoreMainWindows<childMW1, childMW2, childMW3>();
715  * else {
716  * // create default application as usual
717  * }
718  * // [...]
719  * }
720  * \endcode
721  *
722  * kRestoreMainWindows<>() will create (on the heap) as many instances
723  * of your main windows as have existed in the last session and
724  * call KMainWindow::restore() with the correct arguments. Note that
725  * also QWidget::show() is called implicitly.
726  *
727  * Currently, these functions are provided for up to three
728  * template arguments. If you need more, tell us. To help you in
729  * deciding whether or not you can use kRestoreMainWindows, a
730  * define #KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS is provided.
731  *
732  * @tparam T toplevel widget class
733  *
734  * @see KMainWindow::restore()
735  * @see KMainWindow::classNameOfToplevel()
736  **/
737 template<typename T>
738 inline void kRestoreMainWindows()
739 {
740  for (int n = 1; KMainWindow::canBeRestored(n); ++n) {
741  const QString className = KMainWindow::classNameOfToplevel(n);
742  if (className == QLatin1String(T::staticMetaObject.className())) {
743  (new T)->restore(n);
744  }
745  }
746 }
747 
748 /**
749  * Restores the last session.
750  * Overloaded method for usage with multiple different toplevel widget classes.
751  *
752  * @tparam T0 one toplevel widget class
753  * @tparam T1 explicit other toplevel widget class for disambiguation from base template
754  * @tparam Tn Parameter pack to take 0..n further KMainWindows
755  */
756 template<typename T0, typename T1, typename... Tn>
757 inline void kRestoreMainWindows()
758 {
759  kRestoreMainWindows<T0>();
760  kRestoreMainWindows<T1, Tn...>();
761 }
762 /** @} */
763 
764 #endif
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:547
Top level main window.
Definition: kmainwindow.h:92
virtual bool event(QEvent *event) override
static bool canBeRestored(int number)
If the session did contain so high a number, true is returned, else false.
Q_PROPERTY(...)
void kRestoreMainWindows()
Restores the last session.
Definition: kmainwindow.h:738
Floatable toolbar with auto resize.
Definition: ktoolbar.h:60
virtual void closeEvent(QCloseEvent *event)
typedef WindowFlags
virtual void saveProperties(KConfigGroup &)
Save your instance-specific properties.
Definition: kmainwindow.h:538
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:52:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.