File indexing completed on 2024-03-24 15:48:00

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 #include "dialogstatewatcher.h"
0032 
0033 #include <qdialog.h>
0034 #include <qevent.h>
0035 #include <qapplication.h>
0036 #include <qabstractbutton.h>
0037 
0038 #include "dialogstatesaver.h"
0039 #include "libdialogutil_logging.h"
0040 
0041 
0042 DialogStateWatcher::DialogStateWatcher(QDialog *pnt)
0043     : QObject(pnt)
0044 {
0045     Q_ASSERT(pnt!=nullptr);
0046     mParent = pnt;
0047     mParent->installEventFilter(this);
0048     connect(mParent, &QDialog::accepted, this, &DialogStateWatcher::saveConfigInternal);
0049 
0050     mStateSaver = new DialogStateSaver(mParent);    // use our own as default
0051     mHaveOwnSaver = true;               // note that we created it
0052 }
0053 
0054 
0055 void DialogStateWatcher::setSaveOnButton(QAbstractButton *but)
0056 {
0057     qCDebug(LIBDIALOGUTIL_LOG) << "button" << but->text();
0058     connect(but, &QAbstractButton::clicked, this, &DialogStateWatcher::saveConfigInternal);
0059 }
0060 
0061 
0062 bool DialogStateWatcher::eventFilter(QObject *obj, QEvent *ev)
0063 {
0064     if (obj==mParent && ev->type()==QEvent::Show)   // only interested in show event
0065     {
0066         restoreConfigInternal();            // restore size and config
0067     }
0068     return (false);                 // always pass the event on
0069 }
0070 
0071 
0072 void DialogStateWatcher::restoreConfigInternal()
0073 {
0074     if (mStateSaver==nullptr) return;           // no saver set or provided
0075     mStateSaver->restoreConfig();
0076 }
0077 
0078 
0079 void DialogStateWatcher::saveConfigInternal() const
0080 {
0081     if (mStateSaver==nullptr) return;           // no saver set or provided
0082     mStateSaver->saveConfig();
0083 }
0084 
0085 
0086 void DialogStateWatcher::setStateSaver(DialogStateSaver *saver)
0087 {
0088     // We only delete the existing saver if we created it.
0089     if (mStateSaver!=nullptr && mHaveOwnSaver) delete mStateSaver;
0090 
0091     mStateSaver = saver;                // set the new one
0092     mHaveOwnSaver = false;              // note that it's not ours
0093 }