File indexing completed on 2024-10-06 09:43:54

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 Reginald Stadlbauer <reggie@kde.org>
0004     SPDX-FileCopyrightText: 1997 Stephan Kulow <coolo@kde.org>
0005     SPDX-FileCopyrightText: 1997-2000 Sven Radej <radej@kde.org>
0006     SPDX-FileCopyrightText: 1997-2000 Matthias Ettrich <ettrich@kde.org>
0007     SPDX-FileCopyrightText: 1999 Chris Schlaeger <cs@kde.org>
0008     SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
0009     SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
0010     SPDX-FileCopyrightText: 2000-2008 David Faure <faure@kde.org>
0011 
0012     SPDX-License-Identifier: LGPL-2.0-only
0013 */
0014 
0015 #ifndef KMAINWINDOW_H
0016 #define KMAINWINDOW_H
0017 
0018 #include <kxmlgui_export.h>
0019 
0020 #include <QMainWindow>
0021 #include <memory>
0022 
0023 class QMenu;
0024 class KConfig;
0025 class KConfigGroup;
0026 class KMWSessionManager;
0027 class KMainWindowPrivate;
0028 class KToolBar;
0029 
0030 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 65)
0031 /**
0032  * @relates KMainWindow
0033  * @deprecated Since 5.65, use Qt::WindowFlags()
0034  */
0035 #define KDE_DEFAULT_WINDOWFLAGS 0
0036 #else
0037 #define KDE_DEFAULT_WINDOWFLAGS KDE_DEFAULT_WINDOWFLAGS_is_deprecated_use_Qt_WindowFlags()
0038 #endif
0039 
0040 /**
0041  * @class KMainWindow kmainwindow.h KMainWindow
0042  *
0043  * @brief KMainWindow represents a top-level main window.
0044  *
0045  * It extends QMainWindow with session management capabilities. For ready-made window functionality and simpler UI management, use KXmlGuiWindow instead.
0046  *
0047  * Define the minimum/maximum height/width of your central widget and KMainWindow will take this into account.
0048  * For fixed size windows set your main widget to a fixed size. Fixed aspect ratios (QWidget::heightForWidth()) and fixed width widgets are not supported.
0049  *
0050  * Use toolBar() to generate a main toolbar "mainToolBar" or refer to a specific toolbar.
0051  * For a simpler way to manage your toolbars, use KXmlGuiWindow::setupGUI() instead.
0052  *
0053  * Use setAutoSaveSettings() to automatically save and restore the window geometry and toolbar/menubar/statusbar state when the application is restarted.
0054  *
0055  * Use kRestoreMainWindows() in your main function to restore your windows when the session is restored.
0056  *
0057  * The window state is saved when the application is exited.
0058  * Reimplement queryClose() to warn the user of unsaved data upon close or exit.
0059  *
0060  * Reimplement saveProperties() / readProperties() or saveGlobalProperties() / readGlobalProperties()
0061  * to save/restore application-specific state during session management.
0062  *
0063  * Note that session saving is automatically called, session restoring is not,
0064  * and so it needs to be implemented in your main() function.
0065  *
0066  * See https://develop.kde.org/docs/use/session-managment for more information on session management.
0067  */
0068 
0069 class KXMLGUI_EXPORT KMainWindow : public QMainWindow
0070 {
0071     friend class KMWSessionManager;
0072     friend class DockResizeListener;
0073     Q_OBJECT
0074     Q_PROPERTY(bool hasMenuBar READ hasMenuBar)
0075     Q_PROPERTY(bool autoSaveSettings READ autoSaveSettings)
0076     Q_PROPERTY(QString autoSaveGroup READ autoSaveGroup)
0077 
0078 public:
0079     /**
0080      * @brief Constructs a main window.
0081      *
0082      * @param parent The parent widget. This is usually @c nullptr, but it may also be
0083      * the window group leader. In that case,
0084      * the KMainWindow becomes a secondary window.
0085      *
0086      * @param flags Specify the window flags. The default is none.
0087      *
0088      * Note that by default a KMainWindow is created with the
0089      * Qt::WA_DeleteOnClose attribute set, i.e. it is automatically destroyed
0090      * when the window is closed. If you do not want this behavior, call
0091      * \code
0092      * window->setAttribute(Qt::WA_DeleteOnClose, false);
0093      * \endcode
0094      *
0095      * KMainWindows must be created on the heap with 'new', like:
0096      * \code
0097      * KMainWindow *kmw = new KMainWindow(...);
0098      * kmw->setObjectName(...);
0099      * \endcode
0100      *
0101      * Since KDE Frameworks 5.16, KMainWindow will enter information regarding
0102      * the application's translators by default, using KAboutData::setTranslator(). This only occurs
0103      * if no translators are already assigned in KAboutData (see KAboutData::setTranslator() for
0104      * details -- the auto-assignment here uses the same translated strings as specified for that
0105      * function).
0106      *
0107      * IMPORTANT: For session management and window management to work
0108      * properly, all main windows in the application should have a
0109      * different name. Otherwise, KMainWindow will create
0110      * a unique name, but it's recommended to explicitly pass a window name that will
0111      * also describe the type of the window. If there can be several windows of the same
0112      * type, append '#' (hash) to the name, and KMainWindow will replace it with numbers to make
0113      * the names unique. For example, for a mail client which has one main window showing
0114      * the mails and folders, and which can also have one or more windows for composing
0115      * mails, the name for the folders window should be e.g. "mainwindow" and
0116      * for the composer windows "composer#".
0117      *
0118      * @see KAboutData
0119      */
0120     explicit KMainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
0121 
0122     /**
0123      * @brief Destructor.
0124      *
0125      * Will also destroy the toolbars and menubar if
0126      * needed.
0127      */
0128     ~KMainWindow() override;
0129 
0130 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
0131     /**
0132      * Retrieve the standard help menu.
0133      *
0134      * It contains entries for the
0135      * help system (activated by F1), an optional "What's This?" entry
0136      * (activated by Shift F1), an application specific dialog box,
0137      * and an "About KDE" dialog box.
0138      *
0139      * Example (adding a standard help menu to your application):
0140      * \code
0141      * QMenu *help = helpMenu( <myTextString> );
0142      * menuBar()->addMenu( help );
0143      * \endcode
0144      *
0145      * @param aboutAppText The string that is used in the application
0146      *        specific dialog box. If you leave this string empty the
0147      *        information in the global KAboutData of the
0148      *        application will be used to make a standard dialog box.
0149      *
0150      * @param showWhatsThis Set this to false if you do not want to include
0151      *        the "What's This" menu entry.
0152      *
0153      * @return A standard help menu.
0154      * @deprecated Since 5.0, use KHelpMenu directly
0155      */
0156     KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KHelpMenu directly")
0157     QMenu *helpMenu(const QString &aboutAppText = QString(), bool showWhatsThis = true);
0158 #endif
0159 
0160 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
0161     /**
0162      * @returns The help menu. Creates a standard help menu if none exists yet.
0163      *
0164      * It contains entries for the
0165      * help system (activated by F1), an optional "What's This?" entry
0166      * (activated by Shift F1), an application specific dialog box,
0167      * and an "About KDE" dialog box. You must create the application
0168      * specific dialog box yourself. When the "About application"
0169      * menu entry is activated, a signal will trigger the
0170      * showAboutApplication slot. See showAboutApplication for more
0171      * information.
0172      *
0173      * Example (adding a help menu to your application):
0174      * \code
0175      * menuBar()->addMenu( customHelpMenu() );
0176      * \endcode
0177      *
0178      * @param showWhatsThis Whether to show the "What's This" menu entry. @c true by default.
0179      *
0180      * @return A standard help menu.
0181      * @deprecated Since 5.0, use XMLGUI instead, or KHelpMenu directly
0182      */
0183     KXMLGUI_DEPRECATED_VERSION(5, 0, "Use XMLGUI or KHelpMenu")
0184     QMenu *customHelpMenu(bool showWhatsThis = true);
0185 #endif
0186 
0187     /**
0188      * @param numberOfInstances The number of KMainWindow instances in the application.
0189      * @returns @c true if the number of KMainWindow instances of the previous session did contain the requested @p numberOfInstances, @c false otherwise.
0190      * @see restore()
0191      **/
0192     static bool canBeRestored(int numberOfInstances);
0193 
0194     /**
0195      * @brief Useful if your application uses
0196      * different kinds of top-level windows.
0197      *
0198      * @returns The class name of the top-level window to be restored
0199      * that corresponds to @p instanceNumber.
0200      *
0201      * @param instanceNumber
0202      * @see restore()
0203      */
0204     static const QString classNameOfToplevel(int instanceNumber);
0205 
0206     /**
0207      * @brief Attempt to restore the top-level widget as defined by @p numberOfInstances (1..X).
0208      *
0209      * You should call canBeRestored() first.
0210      *
0211      * If the session did not contain so high a number, the configuration
0212      * is not changed and @c false is returned.
0213      *
0214      * That means clients could simply do the following:
0215      * \code
0216      * if (qApp->isSessionRestored()){
0217      *   int n = 1;
0218      *   while (KMainWindow::canBeRestored(n)){
0219      *     (new childMW)->restore(n);
0220      *     n++;
0221      *   }
0222      * } else {
0223      *   // create default application as usual
0224      * }
0225      * \endcode
0226      * Note that if @p show is @c true (default), QWidget::show() is called
0227      * implicitly in restore.
0228      *
0229      * With this you can easily restore all top-level windows of your
0230      * application.
0231      *
0232      * If your application uses different kinds of top-level
0233      * windows, then you can use KMainWindow::classNameOfToplevel(n)
0234      * to determine the exact type before calling the childMW
0235      * constructor in the example from above.
0236      *
0237      * @note You don't need to deal with this function. Use the
0238      * kRestoreMainWindows() convenience template function instead!
0239      *
0240      * @param numberOfInstances The number of KMainWindow instances from the last session.
0241      * @param show Whether the KMainWindow instances will be visible by default.
0242      *
0243      * @returns @c true if the session contained
0244      * the same number of instances as the requested number,
0245      * @c false if the session contained less instances than the requested number,
0246      * in which case no configuration is changed.
0247      *
0248      * @see kRestoreMainWindows()
0249      * @see readProperties()
0250      * @see canBeRestored()
0251      */
0252     bool restore(int numberOfInstances, bool show = true);
0253 
0254     /**
0255      * @returns @c true if there is a menubar, @c false otherwise.
0256      */
0257     bool hasMenuBar();
0258 
0259     /**
0260      * @returns The list of members of the KMainWindow class.
0261      */
0262     static QList<KMainWindow *> memberList();
0263 
0264     /**
0265      * @brief This is useful to both call specific toolbars that have been created
0266      * or to generate a default one upon call.
0267      *
0268      * This refers to toolbars created dynamically from the XML UI
0269      * framework via KConfig or appnameui.rc.
0270      *
0271      * If the toolbar does not exist, one will be created.
0272      *
0273      * @param name The internal name of the toolbar. If no name is
0274      *             specified, "mainToolBar" is assumed.
0275      *
0276      * @return A pointer to the toolbar with the specified name.
0277      * @see toolBars()
0278      **/
0279     KToolBar *toolBar(const QString &name = QString());
0280 
0281     /**
0282      * @return A list of all toolbars for this window
0283      */
0284     QList<KToolBar *> toolBars() const;
0285 
0286     /**
0287      * @brief This enables autosave of toolbar/menubar/statusbar settings
0288      * (and optionally window size).
0289      * @param groupName A name that identifies the type of window.
0290      * You can have several types of window in the same application.
0291      * If no @p groupName is specified, the value defaults to "MainWindow".
0292      *
0293      * @param saveWindowSize Whether to include the window size
0294      * when saving. @c true by default.
0295      *
0296      * If the *bars were modified when the window is closed,
0297      * saveMainWindowSettings( KConfigGroup(KSharedConfig::openConfig(), groupName) ) will be called.
0298      *
0299      * Typically, you will call setAutoSaveSettings() in your
0300      * KMainWindow-inherited class constructor, and it will take care
0301      * of restoring and saving automatically.
0302      *
0303      * By default, this generates an
0304      * appnamerc ini file as if using default KConfig constructor or KConfig::SimpleConfig.
0305      *
0306      * Make sure you call this @em after all your *bars have been created.
0307      *
0308      * To make sure that KMainWindow properly obtains the default
0309      * size of the window you should do the following:
0310      * - Remove hardcoded resize() calls in the constructor or main
0311      *   to let the automatic resizing determine the default window size.
0312      *   Hardcoded window sizes will be wrong for users that have big fonts,
0313      *   use different styles, long/small translations, large toolbars, and other factors.
0314      * - Put the setAutoSaveSettings() call after all widgets
0315      *   have been created and placed inside the main window
0316      *   (for most apps this means QMainWindow::setCentralWidget())
0317      * - QWidget-based objects should overload "virtual QSize sizeHint() const;"
0318      *   to specify a default size.
0319      * @see KConfig
0320      * @see KSharedConfig
0321      * @see saveMainWindowSettings()
0322      * @see toolBar()
0323      * @see KXmlGuiWindow::setupGUI()
0324      */
0325     void setAutoSaveSettings(const QString &groupName = QStringLiteral("MainWindow"), bool saveWindowSize = true);
0326 
0327     /**
0328      * This is an overloaded function.
0329      * This allows the settings to be saved into a different file
0330      * that does not correspond to that used for KSharedConfig::openConfig().
0331      * @see setAutoSaveSettings(const QString &groupName, bool saveWindowSize)
0332      * @see KConfig
0333      * @see KSharedConfig
0334      * @since 4.1
0335      */
0336     void setAutoSaveSettings(const KConfigGroup &group, bool saveWindowSize = true);
0337 
0338     /**
0339      * @brief Disables the autosave settings feature.
0340      * You don't normally need to call this, ever.
0341      * @see setAutoSaveSettings()
0342      * @see autoSaveSettings()
0343      */
0344     void resetAutoSaveSettings();
0345 
0346     /**
0347      * @return @c true if setAutoSaveSettings() was called,
0348      * @c false by default or if resetAutoSaveSettings() was called.
0349      * @see setAutoSaveSettings()
0350      * @see resetAutoSaveSettings()
0351      */
0352     bool autoSaveSettings() const;
0353 
0354     /**
0355      * @return The group used for autosaving settings.
0356      *
0357      * Do not mistake this with autoSaveConfigGroup.
0358      *
0359      * Only meaningful if setAutoSaveSettings(const QString&, bool) was called.
0360      *
0361      * Do not use this method if setAutoSaveSettings(const KConfigGroup&, bool) was called.
0362      *
0363      * This can be useful for forcing a save or an apply, e.g. before and after
0364      * using KEditToolBar.
0365      *
0366      * @note Prefer saveAutoSaveSettings() for saving or autoSaveConfigGroup() for loading.
0367      *
0368      * @see autoSaveSettings()
0369      * @see setAutoSaveSettings()
0370      * @see saveAutoSaveSettings()
0371      * @see resetAutoSaveSettings()
0372      * @see autoSaveConfigGroup()
0373      */
0374     QString autoSaveGroup() const;
0375 
0376     /**
0377      * @return The group used for autosaving settings.
0378      *
0379      * Only meaningful if setAutoSaveSettings(const QString&, bool) was called.
0380      *
0381      * Do not use this method if setAutoSaveSettings(const KConfigGroup&, bool) was called.
0382      *
0383      * This can be useful for forcing an apply, e.g. after using KEditToolBar.
0384      *
0385      * @see setAutoSaveSettings()
0386      * @see autoSaveGroup()
0387      * @since 4.1
0388      */
0389     KConfigGroup autoSaveConfigGroup() const;
0390 
0391     /**
0392      * @brief Assigns the config group name for the KConfigGroup returned by stateConfigGroup.
0393      * @param configGroup The config group to be assigned.
0394      * Window size and state are stored in the resulting KConfigGroup when this function is called.
0395      * @note If this is used in combination with setAutoSaveSettings, you should call this method first.
0396      *
0397      * @see KConfigGroup()
0398      * @see KSharedConfig::openStateConfig()
0399      * @see stateConfigGroup()
0400      *
0401      * @since 5.88
0402      */
0403     void setStateConfigGroup(const QString &configGroup);
0404 
0405     /**
0406      * @returns The KConfigGroup used to store state data like window sizes or window state.
0407      *
0408      * The resulting group is invalid if setStateConfig is not called explicitly.
0409      *
0410      * @see KConfigGroup
0411      * @since 5.88
0412      */
0413     KConfigGroup stateConfigGroup() const;
0414 
0415     /**
0416      * @brief Read settings for statusbar, menubar and toolbar from their respective
0417      * groups in the config file and apply them.
0418      *
0419      * @param config Config group to read the settings from.
0420      */
0421     virtual void applyMainWindowSettings(const KConfigGroup &config);
0422 
0423     /**
0424      * @brief Manually save the settings for statusbar, menubar and toolbar to their respective
0425      * groups in the KConfigGroup @p config.
0426      *
0427      * Example:
0428      * \code
0429      * KConfigGroup group(KSharedConfig::openConfig(), "MainWindow");
0430      * saveMainWindowSettings(group);
0431      * \endcode
0432      *
0433      * @param config Config group to save the settings to.
0434      * @see setAutoSaveSettings()
0435      * @see KConfig
0436      * @see KSharedConfig
0437      * @see KConfigGroup
0438      */
0439     void saveMainWindowSettings(KConfigGroup &config);
0440 
0441     /**
0442      * @returns The path for the exported window's D-Bus object.
0443      * @since 4.0.1
0444      */
0445     QString dbusName() const;
0446 
0447 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
0448     /**
0449      * @returns Always @c false
0450      * @deprecated since 5.0, the functionality got removed
0451      **/
0452     KXMLGUI_DEPRECATED_VERSION(5, 0, "Remove usage, is a no-op now")
0453     bool initialGeometrySet() const
0454     {
0455         return false;
0456     }
0457 #endif
0458 
0459 public Q_SLOTS:
0460     /**
0461      * @brief Assigns a KDE compliant caption (window title).
0462      *
0463      * @param caption The string that will be
0464      * displayed in the window title, before the application name.
0465      *
0466      * @note This function does the same as setPlainCaption().
0467      *
0468      * @note Do not include the application name
0469      * in this string. It will be added automatically according to the KDE
0470      * standard.
0471      *
0472      * @see setPlainCaption()
0473      */
0474     virtual void setCaption(const QString &caption);
0475     /**
0476      * @brief Makes a KDE compliant caption.
0477      * @param caption Your caption.
0478      * @param modified Whether the document is modified. This displays
0479      * an additional sign in the title bar, usually "**".
0480      *
0481      * This is an overloaded function.
0482      *
0483      * @note Do not include the application name
0484      * in this string. It will be added automatically according to the KDE
0485      * standard.
0486      */
0487     virtual void setCaption(const QString &caption, bool modified);
0488 
0489     /**
0490      * @brief Make a plain caption without any modifications.
0491      *
0492      * @param caption The string that will be
0493      * displayed in the window title, before the application name.
0494      *
0495      * @note This function does the same as setCaption().
0496      *
0497      * @note Do not include the application name
0498      * in this string. It will be added automatically according to the KDE
0499      * standard.
0500      *
0501      * @see setCaption()
0502      */
0503     virtual void setPlainCaption(const QString &caption);
0504 
0505     /**
0506      * @brief Opens the help page for the application.
0507      *
0508      * The application name is
0509      * used as a key to determine what to display and the system will attempt
0510      * to open \<appName\>/index.html.
0511      *
0512      * This method is intended for use by a help button in the toolbar or
0513      * components outside the regular help menu.
0514      *
0515      * Use helpMenu() when you
0516      * want to provide access to the help system from the help menu.
0517      *
0518      * Example (adding a help button to the first toolbar):
0519      *
0520      * \code
0521      * toolBar()->addAction(QIcon::fromTheme("help-contents"), i18n("Help"),
0522      *                       this, &KMainWindow::appHelpActivated);
0523      * \endcode
0524      *
0525      * @see helpMenu()
0526      * @see toolBar()
0527      */
0528     void appHelpActivated();
0529 
0530     /**
0531      * @brief Tell the main window that it should save its settings when being closed.
0532      *
0533      * This is part of the autosave settings feature.
0534      *
0535      * For everything related to toolbars this happens automatically,
0536      * but you have to call setSettingsDirty() in the slot that toggles
0537      * the visibility of the statusbar.
0538      *
0539      * @see saveAutoSaveSettings()
0540      */
0541     void setSettingsDirty();
0542 
0543 protected:
0544     /**
0545      * Reimplemented to catch QEvent::Polish in order to adjust the object name
0546      * if needed, once all constructor code for the main window has run.
0547      * Also reimplemented to catch when a QDockWidget is added or removed.
0548      */
0549     bool event(QEvent *event) override;
0550 
0551     /**
0552      * Reimplemented to autosave settings and call queryClose().
0553      *
0554      * We recommend that you reimplement queryClose() rather than closeEvent().
0555      * If you do it anyway, ensure to call the base implementation to keep
0556      * the feature of autosaving window settings working.
0557      */
0558     void closeEvent(QCloseEvent *) override;
0559 
0560     /**
0561      * @brief This function is called before the window is closed,
0562      * either by the user or indirectly by the session manager.
0563      *
0564      * This can be used to prompt the user to save unsaved data before the window is closed.
0565      *
0566      * Example:
0567      * \code
0568      * switch ( KMessageBox::warningTwoActionsCancel( this,
0569      *          i18n("Save changes to document foo?"), QString(),
0570      *          KStandardGuiItem::save(), KStandardGuiItem::discard())) ) {
0571      *   case KMessageBox::PrimaryAction :
0572      *     // save document here. If saving fails, return false;
0573      *     return true;
0574      *   case KMessageBox::SecondaryAction :
0575      *     return true;
0576      *   default: // cancel
0577      *     return false;
0578      * \endcode
0579      *
0580      * @note Do @em not close the document from within this method,
0581      * as it may be called by the session manager before the
0582      * session is saved. If the document is closed before the session save occurs,
0583      * its location might not be properly saved. In addition, the session shutdown
0584      * may be canceled, in which case the document should remain open.
0585      *
0586      * @return @c true by default, @c false according to the reimplementation.
0587      * Returning @c false will cancel the closing operation,
0588      * and if KApplication::sessionSaving() is true, it cancels logout.
0589      *
0590      * @see KApplication::sessionSaving()
0591      */
0592     virtual bool queryClose();
0593 
0594     /**
0595      * @brief Saves your instance-specific properties.
0596      *
0597      * The function is
0598      * invoked when the session manager requests your application
0599      * to save its state.
0600      *
0601      * Reimplement this function in child classes.
0602      *
0603      * \code
0604      * void MainWindow::saveProperties(KConfigGroup &config) {
0605      *   config.writeEntry("myKey", "newValue");
0606      *   ...
0607      * }
0608      * \endcode
0609      *
0610      * @note No user interaction is allowed
0611      * in this function!
0612      *
0613      */
0614     virtual void saveProperties(KConfigGroup &)
0615     {
0616     }
0617 
0618     /**
0619      * @brief Reads your instance-specific properties.
0620      *
0621      * This function is called indirectly by restore().
0622      *
0623      * \code
0624      * void MainWindow::readProperties(KConfigGroup &config) {
0625      *   if (config.hasKey("myKey")) {
0626      *     config.readEntry("myKey", "DefaultValue");
0627      *   }
0628      *   ...
0629      * }
0630      * \endcode
0631      *
0632      * @see readGlobalProperties()
0633      */
0634     virtual void readProperties(const KConfigGroup &)
0635     {
0636     }
0637 
0638     /**
0639      * @brief Saves your application-wide properties.
0640      *
0641      * @param sessionConfig A pointer to the KConfig instance
0642      * used to save the session data.
0643      *
0644      * This function is invoked when the session manager
0645      * requests your application to save its state.
0646      * It is similar to saveProperties(), but it is only called for
0647      * the first main window. This is useful to save global state of your application
0648      * that isn't bound to a particular window.
0649      *
0650      * The default implementation does nothing.
0651      *
0652      * @see readGlobalProperties()
0653      * @see saveProperties()
0654      */
0655     virtual void saveGlobalProperties(KConfig *sessionConfig);
0656 
0657     /**
0658      * @brief Reads your application-wide properties.
0659      *
0660      * @param sessionConfig A pointer to the KConfig instance
0661      * used to load the session data.
0662      *
0663      * @see saveGlobalProperties()
0664      * @see readProperties()
0665      *
0666      */
0667     virtual void readGlobalProperties(KConfig *sessionConfig);
0668     void savePropertiesInternal(KConfig *, int);
0669     bool readPropertiesInternal(KConfig *, int);
0670 
0671     /**
0672      * For inherited classes
0673      */
0674     bool settingsDirty() const;
0675 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
0676     /**
0677      * For inherited classes
0678      * @deprecated Since 5.0, use KWindowConfig::saveWindowSize
0679      */
0680     KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KWindowConfig::saveWindowSize(...)")
0681     void saveWindowSize(KConfigGroup &config) const;
0682 #endif
0683 
0684 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
0685     /**
0686      * For inherited classes
0687      * @deprecated Since 5.0, use KWindowConfig::restoreWindowSize
0688      */
0689     KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KWindowConfig::restoreWindowSize(...)")
0690     void restoreWindowSize(const KConfigGroup &config);
0691 #endif
0692 
0693 protected Q_SLOTS:
0694 #if KXMLGUI_BUILD_DEPRECATED_SINCE(5, 0)
0695     /**
0696      * This slot does nothing.
0697      *
0698      * It must be reimplemented if you want
0699      * to use a custom About Application dialog box. This slot is
0700      * connected to the About Application entry in the menu returned
0701      * by customHelpMenu.
0702      *
0703      * Example:
0704      * \code
0705      *
0706      * void MyMainLevel::setupInterface()
0707      * {
0708      *   ..
0709      *   menuBar()->addMenu( customHelpMenu() );
0710      *   ..
0711      * }
0712      *
0713      * void MyMainLevel::showAboutApplication()
0714      * {
0715      *   <activate your custom dialog>
0716      * }
0717      * \endcode
0718      * @deprecated Since 5.0, use KHelpMenu
0719      */
0720     KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KHelpMenu")
0721     virtual void showAboutApplication()
0722     {
0723     }
0724 #endif
0725 
0726     /**
0727      * This slot should only be called in case you reimplement closeEvent() and
0728      * if you are using the autosave feature. In all other cases,
0729      * setSettingsDirty() should be called instead to benefit from the delayed
0730      * saving.
0731      *
0732      * Example:
0733      * \code
0734      *
0735      * void MyMainWindow::closeEvent( QCloseEvent *e )
0736      * {
0737      *   // Save settings if autosave is enabled, and settings have changed
0738      *   if ( settingsDirty() && autoSaveSettings() )
0739      *     saveAutoSaveSettings();
0740      *   ..
0741      * }
0742      * \endcode
0743      *
0744      * @see setAutoSaveSettings()
0745      * @see setSettingsDirty()
0746      */
0747     void saveAutoSaveSettings();
0748 
0749 protected:
0750     KXMLGUI_NO_EXPORT KMainWindow(KMainWindowPrivate &dd, QWidget *parent, Qt::WindowFlags f);
0751 
0752     std::unique_ptr<KMainWindowPrivate> const k_ptr;
0753     // KF6 TODO: change k_ptr to d_ptr, use normal Q_DECLARE_PRIVATE
0754 
0755 private:
0756     Q_DECLARE_PRIVATE_D(k_ptr, KMainWindow)
0757 
0758     Q_PRIVATE_SLOT(d_func(), void _k_slotSettingsChanged(int))
0759     Q_PRIVATE_SLOT(d_func(), void _k_slotSaveAutoSaveSize())
0760     Q_PRIVATE_SLOT(d_func(), void _k_slotSaveAutoSavePosition())
0761 };
0762 
0763 /**
0764  * @defgroup KXMLGUI_Session KXMLGUI Session Macros and Functions
0765  *
0766  * @{
0767  */
0768 
0769 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
0770 /**
0771  * @def RESTORE
0772  * Restores the last session.
0773  *
0774  * @deprecated since 5.0, use kRestoreMainWindows() instead
0775  **/
0776 #define RESTORE(type)                                                                                                                                          \
0777     {                                                                                                                                                          \
0778         int n = 1;                                                                                                                                             \
0779         while (KMainWindow::canBeRestored(n)) {                                                                                                                \
0780             (new type)->restore(n);                                                                                                                            \
0781             n++;                                                                                                                                               \
0782         }                                                                                                                                                      \
0783     }
0784 #endif
0785 
0786 /**
0787  * @def KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS
0788  * Returns the maximal number of arguments that are actually
0789  * supported by kRestoreMainWindows().
0790  **/
0791 #define KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS 3
0792 
0793 /**
0794  * @brief Restores the last session. (To be used in your main function).
0795  *
0796  * These functions work also if you have more than one kind of top-level
0797  * widget (each derived from KMainWindow, of course).
0798  *
0799  * Imagine you have three kinds of top-level widgets: the classes @c childMW1,
0800  * @c childMW2 and @c childMW3. Then you can just do:
0801  *
0802  * \code
0803  * int main(int argc, char *argv[])
0804  * {
0805  *     // [...]
0806  *     if (qApp->isSessionRestored())
0807  *         kRestoreMainWindows<childMW1, childMW2, childMW3>();
0808  *     else {
0809  *       // create default application as usual
0810  *     }
0811  *     // [...]
0812  * }
0813  * \endcode
0814  *
0815  * kRestoreMainWindows<>() will create (on the heap) as many instances
0816  * of your main windows as have existed in the last session and
0817  * call KMainWindow::restore() with the correct arguments. Note that
0818  * also QWidget::show() is called implicitly.
0819  *
0820  * Currently, these functions are provided for up to three
0821  * template arguments. If you need more, tell us. To help you in
0822  * deciding whether or not you can use kRestoreMainWindows, a
0823  * define #KDE_RESTORE_MAIN_WINDOWS_NUM_TEMPLATE_ARGS is provided.
0824  *
0825  * @note Prefer this function over directly calling KMainWindow::restore().
0826  *
0827  * @tparam T Top-level widget class
0828  *
0829  * @see KMainWindow::restore()
0830  * @see KMainWindow::classNameOfToplevel()
0831  **/
0832 template<typename T>
0833 inline void kRestoreMainWindows()
0834 {
0835     for (int n = 1; KMainWindow::canBeRestored(n); ++n) {
0836         const QString className = KMainWindow::classNameOfToplevel(n);
0837         if (className == QLatin1String(T::staticMetaObject.className())) {
0838             (new T)->restore(n);
0839         }
0840     }
0841 }
0842 
0843 /**
0844  * @brief Restores the last session.
0845  * This is an overloaded function.
0846  *
0847  * Use this with multiple different top-level widget classes.
0848  *
0849  * @tparam T0 One top-level widget class
0850  * @tparam T1 Explicit other top-level widget class for disambiguation from base template
0851  * @tparam Tn Parameter pack to take 0..n further KMainWindows
0852  * @see kRestoreMainWindows()
0853  */
0854 template<typename T0, typename T1, typename... Tn>
0855 inline void kRestoreMainWindows()
0856 {
0857     kRestoreMainWindows<T0>();
0858     kRestoreMainWindows<T1, Tn...>();
0859 }
0860 /** @}  */
0861 
0862 #endif