File indexing completed on 2024-04-28 16:00:58

0001 /*
0002    SPDX-FileCopyrightText: 2014-2023 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "texttospeechconfigdialog.h"
0008 #include "texttospeechconfigwidget.h"
0009 #include <KLocalizedString>
0010 
0011 #include <KConfigGroup>
0012 #include <KSharedConfig>
0013 #include <KWindowConfig>
0014 #include <QDialogButtonBox>
0015 #include <QPushButton>
0016 #include <QVBoxLayout>
0017 #include <QWindow>
0018 namespace
0019 {
0020 static const char myTextToSpeechConfigDialogConfigGroupName[] = "TextToSpeechConfigDialog";
0021 }
0022 using namespace TextEditTextToSpeech;
0023 
0024 TextToSpeechConfigDialog::TextToSpeechConfigDialog(QWidget *parent)
0025     : QDialog(parent)
0026     , mTextToSpeechConfigWidget(new TextToSpeechConfigWidget(parent))
0027 {
0028     setWindowTitle(i18nc("@title:window", "Configure Text-To-Speech"));
0029     auto mainLayout = new QVBoxLayout(this);
0030     mainLayout->addWidget(mTextToSpeechConfigWidget);
0031 
0032     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults, this);
0033     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0034     okButton->setDefault(true);
0035     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0036     connect(buttonBox, &QDialogButtonBox::accepted, this, &TextToSpeechConfigDialog::slotAccepted);
0037     connect(buttonBox, &QDialogButtonBox::rejected, this, &TextToSpeechConfigDialog::reject);
0038     connect(buttonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &TextToSpeechConfigDialog::slotRestoreDefaults);
0039     mainLayout->addWidget(buttonBox);
0040     readConfig();
0041 }
0042 
0043 TextToSpeechConfigDialog::~TextToSpeechConfigDialog()
0044 {
0045     writeConfig();
0046 }
0047 
0048 void TextToSpeechConfigDialog::slotRestoreDefaults()
0049 {
0050     mTextToSpeechConfigWidget->restoreDefaults();
0051 }
0052 
0053 void TextToSpeechConfigDialog::readConfig()
0054 {
0055     create(); // ensure a window is created
0056     windowHandle()->resize(QSize(300, 200));
0057     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1String(myTextToSpeechConfigDialogConfigGroupName));
0058     KWindowConfig::restoreWindowSize(windowHandle(), group);
0059     resize(windowHandle()->size()); // workaround for QTBUG-40584
0060     mTextToSpeechConfigWidget->initializeSettings();
0061 }
0062 
0063 void TextToSpeechConfigDialog::writeConfig()
0064 {
0065     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1String(myTextToSpeechConfigDialogConfigGroupName));
0066     KWindowConfig::saveWindowSize(windowHandle(), group);
0067 }
0068 
0069 void TextToSpeechConfigDialog::slotAccepted()
0070 {
0071     mTextToSpeechConfigWidget->writeConfig();
0072     accept();
0073 }
0074 
0075 #include "moc_texttospeechconfigdialog.cpp"