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 DIALOGBASE_H
0032 #define DIALOGBASE_H
0033 
0034 #include <qdialog.h>
0035 #include <qdialogbuttonbox.h>
0036 #include "libdialogutil_export.h"
0037 
0038 class QShowEvent;
0039 class QSpacerItem;
0040 class KGuiItem;
0041 class KConfigGroup;
0042 class DialogStateWatcher;
0043 class DialogStateSaver;
0044 
0045 
0046 /**
0047  * @short A wrapper for QDialog incorporating some convenience functions.
0048  *
0049  * This is a lightweight wrapper around QDialog, incorporating some useful
0050  * functions which used to be provided by KDialog in KDE4.  These are:
0051  *
0052  * - Managing the button box and providing access to its buttons
0053  * - Managing the top level layout
0054  * - Saving and restoring the dialog size
0055  *
0056  * @author Jonathan Marten
0057  **/
0058 
0059 class LIBDIALOGUTIL_EXPORT DialogBase : public QDialog
0060 {
0061     Q_OBJECT
0062 
0063 public:
0064     /**
0065      * Destructor.
0066      *
0067      **/
0068     virtual ~DialogBase() = default;
0069 
0070     /**
0071      * Retrieve the main widget.
0072      *
0073      * @return the main widget
0074      **/
0075     QWidget *mainWidget() const             { return (mMainWidget); }
0076 
0077     /**
0078      * Set a state saver for the dialog.
0079      *
0080      * This may be a subclass of a DialogStateSaver, reimplemented in
0081      * order to save special dialog settings (e.g. the column states of
0082      * a list view).  If this is not set then a plain DialogStateSaver
0083      * will be created and used internally.  If a nullptr state saver is
0084      * set explicitly using this function, then no state restoring or
0085      * saving will be done.
0086      *
0087      * @param saver the state saver
0088      *
0089      * @note The saver should be set before the dialog is shown for
0090      * the first time.
0091      * @see DialogStateSaver
0092      **/
0093     void setStateSaver(DialogStateSaver *saver);
0094 
0095     /**
0096      * Access the state saver used by the dialog.
0097      *
0098      * This may be the default one, or that set by @c setStateSaver().
0099      *
0100      * @return the state saver
0101      **/
0102     DialogStateSaver *stateSaver() const;
0103 
0104     /**
0105      * Access the state watcher used by the dialog.
0106      *
0107      * This is created and used internally.
0108      *
0109      * @return the state watcher
0110      **/
0111     DialogStateWatcher *stateWatcher() const        { return (mStateWatcher); }
0112 
0113     /**
0114      * Get a vertical spacing suitable for use within the dialog layout.
0115      *
0116      * @return The spacing hint
0117      **/
0118     static int verticalSpacing();
0119 
0120     /**
0121      * Get a horizontal spacing suitable for use within the dialog layout.
0122      *
0123      * @return The spacing hint
0124      **/
0125     static int horizontalSpacing();
0126 
0127     /**
0128      * Create a spacer item suitable for use within a vertical layout.
0129      *
0130      * @return The spacer item
0131      **/
0132     static QSpacerItem *verticalSpacerItem();
0133 
0134     /**
0135      * Create a spacer item suitable for use within a horizontal layout.
0136      *
0137      * @return The spacer item
0138      **/
0139     static QSpacerItem *horizontalSpacerItem();
0140 
0141     /**
0142      * Access the dialog's button box.
0143      *
0144      * @return the button box
0145      **/
0146     QDialogButtonBox *buttonBox() const         { return (mButtonBox); }
0147 
0148     /**
0149      * Set the standard buttons to be displayed within the button box.
0150      *
0151      * @param buttons The buttons required
0152      *
0153      * @note This can be called at any time and the buttons will change
0154      * accordingly.  However, the buttons will be regenerated which means
0155      * that any special button text or icons, or any signal connections from
0156      * them, will be lost.
0157      **/
0158     void setButtons(QDialogButtonBox::StandardButtons buttons);
0159 
0160     /**
0161      * Set the enable state of a button.
0162      *
0163      * @param button The button to set
0164      * @param state The enable state for the button
0165      **/
0166     void setButtonEnabled(QDialogButtonBox::StandardButton button, bool state = true);
0167 
0168     /**
0169      * Set the text of a button.
0170      *
0171      * @param button The button to set
0172      * @param state The new text for the button
0173      *
0174      * @note This can be called at any time, and the button will change
0175      * accordingly.
0176      **/
0177     void setButtonText(QDialogButtonBox::StandardButton button, const QString &text);
0178 
0179     /**
0180      * Set the icon of a button.
0181      *
0182      * @param button The button to set
0183      * @param state The new icon for the button
0184      *
0185      * @note This can be called at any time, and the button will change
0186      * accordingly.
0187      **/
0188     void setButtonIcon(QDialogButtonBox::StandardButton button, const QIcon &icon);
0189 
0190     /**
0191      * Set up a button from a @c KGuiItem.
0192      *
0193      * @param button The button to set
0194      * @param guiItem The @c KGuiItem for the button
0195      *
0196      * @note This can be called at any time, and the button will change
0197      * accordingly.
0198      **/
0199     void setButtonGuiItem(QDialogButtonBox::StandardButton button, const KGuiItem &guiItem);
0200 
0201 protected:
0202     /**
0203      * Constructor.
0204      *
0205      * @param pnt Parent widget
0206      **/
0207     explicit DialogBase(QWidget *pnt = nullptr);
0208 
0209     /**
0210      * Set the main widget to be displayed within the dialog.
0211      *
0212      * @param w The widget
0213      **/
0214     void setMainWidget(QWidget *w)          { mMainWidget = w; }
0215 
0216     /**
0217      * @reimp
0218      **/
0219     void showEvent(QShowEvent *ev) override;
0220 
0221 private:
0222     QDialogButtonBox *mButtonBox;
0223     QWidget *mMainWidget;
0224     DialogStateWatcher *mStateWatcher;
0225 };
0226 
0227 #endif                          // DIALOGBASE_H