File indexing completed on 2024-04-14 14:35:59

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 DIALOGSTATEWATCHER_H
0032 #define DIALOGSTATEWATCHER_H
0033 
0034 #include <qobject.h>
0035 #include "libdialogutil_export.h"
0036 
0037 class QDialog;
0038 class QEvent;
0039 class QAbstractButton;
0040 class DialogStateSaver;
0041 
0042 
0043 /**
0044  * @short Monitor a dialog box to save and restore the size and state.
0045  *
0046  * This class takes care of saving and restoring a dialog's size in the
0047  * application config file.  All that is necessary is to create a
0048  * DialogStateWatcher object in the dialog's constructor, passing the
0049  * dialog as a parameter.  If the dialog is a subclass of DialogBase
0050  * then a watcher will be created automatically.
0051  *
0052  * The watcher uses a DialogStateSaver to do the actual saving and
0053  * restoring.  If required, a custom saver can be subclassed from that
0054  * to save additional information (e.g. the column states of a list view).
0055  *
0056  * @author Jonathan Marten
0057  **/
0058 
0059 class LIBDIALOGUTIL_EXPORT DialogStateWatcher : public QObject
0060 {
0061     Q_OBJECT
0062 
0063 public:
0064     /**
0065      * Constructor.
0066      *
0067      * @param pnt the parent dialog
0068      **/
0069     explicit DialogStateWatcher(QDialog *pnt);
0070 
0071     /**
0072      * Destructor.
0073      **/
0074     ~DialogStateWatcher() override = default;
0075 
0076     /**
0077      * Set a state saver for the dialog being watched.
0078      *
0079      * This may be a subclass of a DialogStateSaver, reimplemented in
0080      * order to save special dialog settings (e.g. the column states of
0081      * a list view).  If this is not set then a plain DialogStateSaver
0082      * will be created and used internally.  If a nullptr state saver is
0083      * set explicitly using this function, then no state restoring or
0084      * saving will be done.
0085      *
0086      * The watcher does not take ownership of the state saver, therefore
0087      * it is the caller's responsibility to delete it when it is no
0088      * longer required.
0089      *
0090      * @param saver the state saver
0091      *
0092      * @note The saver should be set before the dialog is shown for
0093      * the first time.
0094      * @see DialogStateSaver
0095      **/
0096     void setStateSaver(DialogStateSaver *saver);
0097 
0098     /**
0099      * Access the state saver used by the watcher.
0100      *
0101      * This may be the default one, or that set by @c setStateSaver().
0102      *
0103      * @return the state saver
0104      **/
0105     DialogStateSaver *stateSaver() const        { return (mStateSaver); }
0106 
0107     /**
0108      * Sets a button to save the state of the dialog when it is used.
0109      *
0110      * Normally the dialog state will be saved when the parent dialog is accepted.
0111      * This means when any button with the @c QDialogButtonBox::AcceptRole is
0112      * clicked: that is, @c QDialogButtonBox::Ok and some others.  Notably, it
0113      * does not include a @c QDialogButtonBox::Close button which is used where
0114      * there is no difference between closing and cancelling.  This means that the
0115      * dialog state will not normally be saved when that button is used.
0116      *
0117      * If a button is specified here, the state will be saved when that button is
0118      * used, in addition to any button with the @c QDialogButtonBox::AcceptRole.
0119      * Additional buttons may be specified multiple times, and they will all
0120      * save the state.
0121      *
0122      * @param but The button to activate the saving
0123      */
0124     void setSaveOnButton(QAbstractButton *but);
0125 
0126 protected:
0127     /**
0128      * @reimp
0129      **/
0130     bool eventFilter(QObject *obj, QEvent *ev) override;
0131 
0132 private slots:
0133     void restoreConfigInternal();
0134     void saveConfigInternal() const;
0135 
0136 private:
0137     QDialog *mParent;
0138     DialogStateSaver *mStateSaver;
0139     bool mHaveOwnSaver;
0140 };
0141 
0142 #endif                          // DIALOGSTATEWATCHER_H