File indexing completed on 2024-06-16 04:50:35

0001 /*
0002     SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org>
0003     SPDX-FileCopyrightText: 2008 Omat Holding B.V. <info@omat.nl>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "agenttypedialog.h"
0009 #include "agentfilterproxymodel.h"
0010 
0011 #include <KConfig>
0012 #include <QVBoxLayout>
0013 
0014 #include <KConfigGroup>
0015 #include <KLocalizedString>
0016 #include <KSharedConfig>
0017 #include <QLineEdit>
0018 
0019 #include <QDialogButtonBox>
0020 #include <QPushButton>
0021 
0022 using namespace Akonadi;
0023 namespace
0024 {
0025 static const char myAgentTypeDialogGroupName[] = "AgentTypeDialog";
0026 }
0027 class Akonadi::AgentTypeDialogPrivate
0028 {
0029 public:
0030     explicit AgentTypeDialogPrivate(AgentTypeDialog *qq)
0031         : q(qq)
0032     {
0033     }
0034     void readConfig();
0035     void writeConfig() const;
0036     void slotSearchAgentType(const QString &str);
0037     AgentTypeWidget *Widget = nullptr;
0038     AgentType agentType;
0039     AgentTypeDialog *const q;
0040 };
0041 
0042 void AgentTypeDialogPrivate::writeConfig() const
0043 {
0044     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAgentTypeDialogGroupName));
0045     group.writeEntry("Size", q->size());
0046 }
0047 
0048 void AgentTypeDialogPrivate::readConfig()
0049 {
0050     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAgentTypeDialogGroupName));
0051     const QSize sizeDialog = group.readEntry("Size", QSize(460, 320));
0052     if (sizeDialog.isValid()) {
0053         q->resize(sizeDialog);
0054     }
0055 }
0056 
0057 void AgentTypeDialogPrivate::slotSearchAgentType(const QString &str)
0058 {
0059     Widget->agentFilterProxyModel()->setFilterRegularExpression(str);
0060 }
0061 
0062 AgentTypeDialog::AgentTypeDialog(QWidget *parent)
0063     : QDialog(parent)
0064     , d(new AgentTypeDialogPrivate(this))
0065 {
0066     setWindowTitle(i18nc("@title:window", "Configure Account"));
0067     auto layout = new QVBoxLayout(this);
0068 
0069     d->Widget = new Akonadi::AgentTypeWidget(this);
0070     connect(d->Widget, &AgentTypeWidget::activated, this, &AgentTypeDialog::accept);
0071 
0072     auto searchLine = new QLineEdit(this);
0073     layout->addWidget(searchLine);
0074     searchLine->setClearButtonEnabled(true);
0075     connect(searchLine, &QLineEdit::textChanged, this, [this](const QString &str) {
0076         d->slotSearchAgentType(str);
0077     });
0078 
0079     layout->addWidget(d->Widget);
0080 
0081     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0082     connect(buttonBox, &QDialogButtonBox::accepted, this, &AgentTypeDialog::accept);
0083     connect(buttonBox, &QDialogButtonBox::rejected, this, &AgentTypeDialog::reject);
0084     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0085     okButton->setDefault(true);
0086     okButton->setShortcut(Qt::CTRL | Qt::Key_Return); // NOLINT(bugprone-suspicious-enum-usage)
0087     layout->addWidget(buttonBox);
0088     d->readConfig();
0089 
0090     searchLine->setFocus();
0091 }
0092 
0093 AgentTypeDialog::~AgentTypeDialog()
0094 {
0095     d->writeConfig();
0096 }
0097 
0098 void AgentTypeDialog::done(int result)
0099 {
0100     if (result == Accepted) {
0101         d->agentType = d->Widget->currentAgentType();
0102     } else {
0103         d->agentType = AgentType();
0104     }
0105 
0106     QDialog::done(result);
0107 }
0108 
0109 AgentType AgentTypeDialog::agentType() const
0110 {
0111     return d->agentType;
0112 }
0113 
0114 AgentFilterProxyModel *AgentTypeDialog::agentFilterProxyModel() const
0115 {
0116     return d->Widget->agentFilterProxyModel();
0117 }
0118 
0119 #include "moc_agenttypedialog.cpp"