File indexing completed on 2025-01-19 04:46:53

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "openurlwithconfiguredialog.h"
0008 #include "openurlwithconfigurewidget.h"
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 #include <KSharedConfig>
0012 #include <KWindowConfig>
0013 #include <QDialogButtonBox>
0014 #include <QVBoxLayout>
0015 #include <QWindow>
0016 namespace
0017 {
0018 static const char myOpenUrlWithConfigureConfigGroupName[] = "OpenUrlWithConfigureDialog";
0019 }
0020 
0021 OpenUrlWithConfigureDialog::OpenUrlWithConfigureDialog(QWidget *parent)
0022     : QDialog(parent)
0023     , mOpenUrlWithWidget(new OpenUrlWithConfigureWidget(this))
0024 {
0025     setWindowTitle(i18nc("@title:window", "Configure Open Url With Plugin"));
0026     auto mainLayout = new QVBoxLayout(this);
0027     mainLayout->setObjectName(QLatin1StringView("mainlayout"));
0028 
0029     mOpenUrlWithWidget->setObjectName(QLatin1StringView("mOpenUrlWithWidget"));
0030     mainLayout->addWidget(mOpenUrlWithWidget);
0031 
0032     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0033     buttonBox->setObjectName(QLatin1StringView("buttonBox"));
0034     mainLayout->addWidget(buttonBox);
0035 
0036     connect(buttonBox, &QDialogButtonBox::accepted, this, &OpenUrlWithConfigureDialog::slotAccepted);
0037     connect(buttonBox, &QDialogButtonBox::rejected, this, &OpenUrlWithConfigureDialog::reject);
0038     mOpenUrlWithWidget->loadSettings();
0039     readConfig();
0040 }
0041 
0042 OpenUrlWithConfigureDialog::~OpenUrlWithConfigureDialog()
0043 {
0044     writeConfig();
0045 }
0046 
0047 void OpenUrlWithConfigureDialog::slotAccepted()
0048 {
0049     mOpenUrlWithWidget->writeSettings();
0050     accept();
0051 }
0052 
0053 void OpenUrlWithConfigureDialog::readConfig()
0054 {
0055     create(); // ensure a window is created
0056     windowHandle()->resize(QSize(300, 200));
0057     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myOpenUrlWithConfigureConfigGroupName));
0058     KWindowConfig::restoreWindowSize(windowHandle(), group);
0059     resize(windowHandle()->size()); // workaround for QTBUG-40584
0060 }
0061 
0062 void OpenUrlWithConfigureDialog::writeConfig()
0063 {
0064     KConfigGroup grp(KSharedConfig::openStateConfig(), QLatin1StringView(myOpenUrlWithConfigureConfigGroupName));
0065     KWindowConfig::saveWindowSize(windowHandle(), grp);
0066     grp.sync();
0067 }
0068 
0069 #include "moc_openurlwithconfiguredialog.cpp"