File indexing completed on 2025-01-05 04:58:20

0001 /** -*- c++ -*-
0002  * completionordereditor.cpp
0003  *
0004  *  SPDX-FileCopyrightText: 2004 David Faure <faure@kde.org>
0005  *  SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
0006  *
0007  *  SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 
0010 #include "completionordereditor.h"
0011 #include "completionorderwidget.h"
0012 #include <KLDAPWidgets/LdapClient>
0013 #include <KLDAPWidgets/LdapClientSearch>
0014 
0015 #include <kldapcore/ldapserver.h>
0016 
0017 #include <KConfigGroup>
0018 #include <KLocalizedString>
0019 #include <KSharedConfig>
0020 #include <KWindowConfig>
0021 #include <QDialogButtonBox>
0022 #include <QPushButton>
0023 #include <QVBoxLayout>
0024 #include <QWindow>
0025 
0026 using namespace PimCommon;
0027 namespace
0028 {
0029 static const char myCompletionOrderEditorGroupName[] = "CompletionOrderEditor";
0030 }
0031 class PimCommon::CompletionOrderEditorPrivate
0032 {
0033 public:
0034     CompletionOrderEditorPrivate() = default;
0035 
0036     CompletionOrderWidget *mCompletionOrderWidget = nullptr;
0037 };
0038 
0039 CompletionOrderEditor::CompletionOrderEditor(KLDAPWidgets::LdapClientSearch *ldapSearch, QWidget *parent)
0040     : QDialog(parent)
0041     , d(new PimCommon::CompletionOrderEditorPrivate)
0042 {
0043     setWindowTitle(i18nc("@title:window", "Edit Completion Order"));
0044     auto mainLayout = new QVBoxLayout(this);
0045 
0046     d->mCompletionOrderWidget = new CompletionOrderWidget(this);
0047     d->mCompletionOrderWidget->setObjectName(QLatin1StringView("completionorderwidget"));
0048 
0049     mainLayout->addWidget(d->mCompletionOrderWidget);
0050 
0051     d->mCompletionOrderWidget->setLdapClientSearch(ldapSearch);
0052 
0053     d->mCompletionOrderWidget->loadCompletionItems();
0054 
0055     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0056     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0057     okButton->setDefault(true);
0058     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0059     connect(buttonBox, &QDialogButtonBox::accepted, this, &CompletionOrderEditor::slotOk);
0060     connect(buttonBox, &QDialogButtonBox::rejected, this, &CompletionOrderEditor::reject);
0061     mainLayout->addWidget(buttonBox);
0062 
0063     readConfig();
0064 }
0065 
0066 CompletionOrderEditor::~CompletionOrderEditor()
0067 {
0068     writeConfig();
0069 }
0070 
0071 void CompletionOrderEditor::readConfig()
0072 {
0073     create(); // ensure a window is created
0074     windowHandle()->resize(QSize(600, 400));
0075     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myCompletionOrderEditorGroupName));
0076     KWindowConfig::restoreWindowSize(windowHandle(), group);
0077     resize(windowHandle()->size()); // workaround for QTBUG-40584
0078 }
0079 
0080 void CompletionOrderEditor::writeConfig()
0081 {
0082     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myCompletionOrderEditorGroupName));
0083     KWindowConfig::saveWindowSize(windowHandle(), group);
0084 }
0085 
0086 void CompletionOrderEditor::slotOk()
0087 {
0088     d->mCompletionOrderWidget->save();
0089     accept();
0090 }
0091 
0092 #include "moc_completionordereditor.cpp"