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 #include "dialogstatesaver.h" 0032 0033 #include <qdialog.h> 0034 #include <qwindow.h> 0035 #include <qscreen.h> 0036 0037 #include <kconfiggroup.h> 0038 #include <ksharedconfig.h> 0039 0040 #include "libdialogutil_logging.h" 0041 0042 0043 static bool sSaveSettings = true; 0044 0045 0046 DialogStateSaver::DialogStateSaver(QDialog *pnt) 0047 { 0048 Q_ASSERT(pnt!=nullptr); 0049 mParent = pnt; 0050 } 0051 0052 0053 static KConfigGroup configGroupFor(QWidget *window) 0054 { 0055 QString objName = window->objectName(); 0056 if (objName.isEmpty()) 0057 { 0058 objName = window->metaObject()->className(); 0059 qCWarning(LIBDIALOGUTIL_LOG) << "object name not set, using class name" << objName; 0060 } 0061 else qCDebug(LIBDIALOGUTIL_LOG) << "for" << objName << "which is a" << window->metaObject()->className(); 0062 0063 return (KSharedConfig::openConfig(QString(), KConfig::NoCascade)->group(objName)); 0064 } 0065 0066 0067 void DialogStateSaver::restoreConfig() 0068 { 0069 if (!sSaveSettings) return; // settings not to be restored 0070 0071 const KConfigGroup grp = configGroupFor(mParent); 0072 this->restoreConfig(mParent, grp); 0073 } 0074 0075 0076 void DialogStateSaver::restoreConfig(QDialog *dialog, const KConfigGroup &grp) 0077 { 0078 restoreWindowState(dialog, grp); 0079 } 0080 0081 0082 void DialogStateSaver::restoreWindowState(QWidget *widget) 0083 { 0084 const KConfigGroup grp = configGroupFor(widget); 0085 restoreWindowState(widget, grp); 0086 } 0087 0088 0089 void DialogStateSaver::restoreWindowState(QWidget *widget, const KConfigGroup &grp) 0090 { 0091 // Ensure that the widget's window() - that is, either the widget itself 0092 // or its nearest ancestor widget that is or could be top level- is a 0093 // native window, so that windowHandle() below will return a valid QWindow. 0094 const WId wid = widget->window()->winId(); 0095 const QRect desk = widget->window()->windowHandle()->screen()->geometry(); 0096 const QSize sizeDefault = widget->sizeHint(); 0097 0098 // originally from KDE4 KDialog::restoreDialogSize() 0099 qCDebug(LIBDIALOGUTIL_LOG) << "from" << grp.name() << "in" << grp.config()->name(); 0100 const int width = grp.readEntry(QString::fromLatin1("Width %1").arg(desk.width()), sizeDefault.width()); 0101 const int height = grp.readEntry(QString::fromLatin1("Height %1").arg(desk.height()), sizeDefault.height()); 0102 widget->resize(width, height); 0103 } 0104 0105 0106 void DialogStateSaver::saveConfig() const 0107 { 0108 if (!sSaveSettings) return; // settings not to be saved 0109 0110 KConfigGroup grp = configGroupFor(mParent); 0111 this->saveConfig(mParent, grp); 0112 grp.sync(); 0113 } 0114 0115 0116 void DialogStateSaver::saveConfig(QDialog *dialog, KConfigGroup &grp) const 0117 { 0118 saveWindowState(dialog, grp); 0119 } 0120 0121 0122 void DialogStateSaver::saveWindowState(QWidget *widget) 0123 { 0124 KConfigGroup grp = configGroupFor(widget); 0125 saveWindowState(widget, grp); 0126 } 0127 0128 0129 void DialogStateSaver::saveWindowState(QWidget *widget, KConfigGroup &grp) 0130 { 0131 const WId wid = widget->window()->winId(); 0132 const QRect desk = widget->window()->windowHandle()->screen()->geometry(); 0133 const QSize sizeToSave = widget->size(); 0134 0135 // originally from KDE4 KDialog::saveDialogSize() 0136 qCDebug(LIBDIALOGUTIL_LOG) << "to" << grp.name() << "in" << grp.config()->name(); 0137 grp.writeEntry(QString::fromLatin1("Width %1").arg(desk.width()), sizeToSave.width()); 0138 grp.writeEntry( QString::fromLatin1("Height %1").arg(desk.height()), sizeToSave.height()); 0139 grp.sync(); 0140 } 0141 0142 0143 void DialogStateSaver::setSaveSettingsDefault(bool on) 0144 { 0145 sSaveSettings = on; 0146 }