File indexing completed on 2024-04-21 15:12:07

0001 /************************************************************************
0002  *                                  *
0003  *  This file is part of Kooka, a scanning/OCR application using    *
0004  *  Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>.  *
0005  *                                  *
0006  *  Copyright (C) 2016 Jonathan Marten <jjm@keelhaul.me.uk>     *
0007  *                                  *
0008  *  Kooka is free software; you can redistribute it and/or modify it    *
0009  *  under the terms of the GNU Library General Public License as    *
0010  *  published by the Free Software Foundation and appearing in the  *
0011  *  file COPYING included in the packaging of this file;  either    *
0012  *  version 2 of the License, or (at your option) any later version.    *
0013  *                                  *
0014  *  As a special exception, permission is given to link this program    *
0015  *  with any version of the KADMOS OCR/ICR engine (a product of     *
0016  *  reRecognition GmbH, Kreuzlingen), and distribute the resulting  *
0017  *  executable without including the source code for KADMOS in the  *
0018  *  source distribution.                        *
0019  *                                  *
0020  *  This program is distributed in the hope that it will be useful, *
0021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *
0022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
0023  *  GNU General Public License for more details.            *
0024  *                                  *
0025  *  You should have received a copy of the GNU General Public       *
0026  *  License along with this program;  see the file COPYING.  If     *
0027  *  not, see <http://www.gnu.org/licenses/>.                *
0028  *                                  *
0029  ************************************************************************/
0030 
0031 #ifndef DIALOGSTATESAVER_H
0032 #define DIALOGSTATESAVER_H
0033 
0034 #include "libdialogutil_export.h"
0035 
0036 class QDialog;
0037 class QWidget;
0038 class KConfigGroup;
0039 
0040 
0041 /**
0042  * @short Save and restore the size and state of a dialog box.
0043  *
0044  * This class manages saving and restoring a dialog's size in the
0045  * application config file.  All that is necessary is to create a
0046  * DialogStateWatcher object in the dialog's constructor, passing the
0047  * dialog as a parameter.  If the dialog is a subclass of DialogBase
0048  * then a watcher and saver will be created automatically.
0049  *
0050  * The saver can be subclassed if necessary in order to save additional
0051  * information (e.g. the column states of a list view).  Since it does
0052  * not inherit QObject, the dialog itself can be the DialogStateSaver
0053  * to do its own saving and restoring, having access to its own internal
0054  * state.
0055  *
0056  * @author Jonathan Marten
0057  **/
0058 
0059 class LIBDIALOGUTIL_EXPORT DialogStateSaver
0060 {
0061 public:
0062     /**
0063      * Constructor.
0064      *
0065      * @param pnt the parent dialog
0066      **/
0067     explicit DialogStateSaver(QDialog *pnt);
0068 
0069     /**
0070      * Destructor.
0071      **/
0072     virtual ~DialogStateSaver() = default;
0073 
0074     /**
0075      * Set the default option of whether the size of dialog boxes
0076      * is saved when accepted and restored when shown.  This is an
0077      * application-wide setting which takes effect immediately.
0078      * The default is @c true.
0079      *
0080      * @param on Whether the size is to be saved/restored
0081      *
0082      * @note The setting is saved in the application's default configuration
0083      * file (as used by @c KSharedConfig::openConfig()) in a section named
0084      * by the dialog's object name.  If no object name is set then the
0085      * dialog's class name is used.
0086      *
0087      * @see KSharedConfig
0088      * @see QObject::objectName()
0089      **/
0090     static void setSaveSettingsDefault(bool on);
0091 
0092     /**
0093      * Save the parent dialog size to the application config file.
0094      *
0095      * This is called by the dialog state watcher and should not need
0096      * to be called explicitly.  It simply calls the virtual method
0097      * of the same name, which may be reimplemented in a subclass in order
0098      * to save other settings (e.g. the column states of a list view).
0099      **/
0100     void saveConfig() const;
0101 
0102     /**
0103      * Restore the dialog size from the application config file.
0104      *
0105      * This is called by the dialog state watcher and should not need
0106      * to be called explicitly.  It simply calls the virtual method
0107      * of the same name, which may be reimplemented in a subclass in order
0108      * to restore other settings (e.g. the column states of a list view).
0109      **/
0110     void restoreConfig();
0111 
0112     /**
0113      * Save the state of a window.
0114      *
0115      * The window need not be a dialog, therefore this can be used for
0116      * saving the state of any window.  The state is saved to a group
0117      * named as appropriate for the window.
0118      *
0119      * @param window window to save the state of
0120      **/
0121     static void saveWindowState(QWidget *widget);
0122 
0123     /**
0124      * Save the state of a window.
0125      *
0126      * The window need not be a dialog, therefore this can be used for
0127      * saving the state of any window.  The state is saved to the
0128      * specified group.
0129      *
0130      * @param window window to save the state of
0131      * @param grp group to save the configuration to
0132      **/
0133     static void saveWindowState(QWidget *widget, KConfigGroup &grp);
0134 
0135     /**
0136      * Restore the state of a window.
0137      *
0138      * The window need not be a dialog, therefore this can be used for
0139      * restoring the state of any window.  The state is restore from a group
0140      * named as appropriate for the window.
0141      *
0142      * @param window window to restore the state of
0143      **/
0144     static void restoreWindowState(QWidget *widget);
0145 
0146     /**
0147      * Restore the state of a window.
0148      *
0149      * The window need not be a dialog, therefore this can be used for
0150      * restoring the state of any window.  The state is restored from
0151      * the specified group.
0152      *
0153      * @param window window to restore the state of
0154      * @param grp group to restore the configuration from
0155      **/
0156     static void restoreWindowState(QWidget *widget, const KConfigGroup &grp);
0157 
0158 protected:
0159     /**
0160      * Save the dialog size to the application config file.
0161      *
0162      * This may be reimplemented in a subclass if necessary, in order
0163      * to save other settings (e.g. the column states of a list view).
0164      * Call the base class implementation to save the dialog size.
0165      *
0166      * @param dialog dialog to save the state of
0167      * @param grp group to save the configuration to
0168      **/
0169     virtual void saveConfig(QDialog *dialog, KConfigGroup &grp) const;
0170 
0171     /**
0172      * Restore the dialog size from the application config file.
0173      *
0174      * This may be reimplemented in a subclass if necessary, in order
0175      * to restore other settings (e.g. the column states of a list view).
0176      * Call the base class implementation to restore the dialog size.
0177      *
0178      * @param dialog dialog to restore the state of
0179      * @param grp group to restore the configuration from
0180      **/
0181     virtual void restoreConfig(QDialog *dialog, const KConfigGroup &grp);
0182 
0183 private:
0184     QDialog *mParent;
0185 };
0186 
0187 #endif                          // DIALOGSTATESAVER_H