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

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "knotesimpleconfigdialog.h"
0008 #include "knotedisplayconfigwidget.h"
0009 #include "knoteeditorconfigwidget.h"
0010 #include <config-knotes.h>
0011 
0012 #include "attributes/notedisplayattribute.h"
0013 
0014 #include <KConfigGroup>
0015 #include <KLocalizedString>
0016 #include <KSharedConfig>
0017 
0018 #include <KWindowConfig>
0019 #include <QDialogButtonBox>
0020 #include <QPushButton>
0021 #include <QTabWidget>
0022 #include <QVBoxLayout>
0023 #include <QWindow>
0024 
0025 KNoteSimpleConfigDialog::KNoteSimpleConfigDialog(const QString &title, QWidget *parent)
0026     : QDialog(parent)
0027     , mTabWidget(new QTabWidget(this))
0028     , mEditorConfigWidget(new KNoteEditorConfigWidget(this))
0029     , mDisplayConfigWidget(new KNoteDisplayConfigWidget(true, this))
0030 {
0031     auto mainLayout = new QVBoxLayout(this);
0032 
0033     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0034     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0035     okButton->setDefault(true);
0036     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0037     connect(buttonBox, &QDialogButtonBox::accepted, this, &KNoteSimpleConfigDialog::accept);
0038     connect(buttonBox, &QDialogButtonBox::rejected, this, &KNoteSimpleConfigDialog::reject);
0039 
0040     mTabWidget->addTab(mEditorConfigWidget, i18n("Editor Settings"));
0041 
0042     mTabWidget->addTab(mDisplayConfigWidget, i18n("Display Settings"));
0043 
0044     mainLayout->addWidget(mTabWidget);
0045     mainLayout->addWidget(buttonBox);
0046 
0047     readConfig();
0048 }
0049 
0050 KNoteSimpleConfigDialog::~KNoteSimpleConfigDialog()
0051 {
0052     writeConfig();
0053 }
0054 
0055 void KNoteSimpleConfigDialog::load(Akonadi::Item &item, bool isRichText)
0056 {
0057     auto attr = item.attribute<NoteShared::NoteDisplayAttribute>();
0058     mEditorConfigWidget->load(attr, isRichText);
0059     mDisplayConfigWidget->load(attr);
0060 }
0061 
0062 void KNoteSimpleConfigDialog::slotUpdateCaption(const QString &name)
0063 {
0064     setWindowTitle(name);
0065 }
0066 
0067 void KNoteSimpleConfigDialog::save(Akonadi::Item &item, bool &isRichText)
0068 {
0069     auto attr = item.attribute<NoteShared::NoteDisplayAttribute>(Akonadi::Item::AddIfMissing);
0070     mEditorConfigWidget->save(attr, isRichText);
0071     mDisplayConfigWidget->save(attr);
0072 }
0073 
0074 namespace
0075 {
0076 static const char myKNoteSimpleConfigDialogName[] = "KNoteSimpleConfigDialog";
0077 }
0078 void KNoteSimpleConfigDialog::readConfig()
0079 {
0080     create(); // ensure a window is created
0081     windowHandle()->resize(QSize(600, 400));
0082     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myKNoteSimpleConfigDialogName));
0083     KWindowConfig::restoreWindowSize(windowHandle(), group);
0084     resize(windowHandle()->size()); // workaround for QTBUG-40584
0085 }
0086 
0087 void KNoteSimpleConfigDialog::writeConfig()
0088 {
0089     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myKNoteSimpleConfigDialogName));
0090     KWindowConfig::saveWindowSize(windowHandle(), group);
0091     group.sync();
0092 }
0093 
0094 #include "moc_knotesimpleconfigdialog.cpp"