File indexing completed on 2024-12-08 06:26:54

0001 /***************************************************************************
0002  *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
0003  *             (C) 2015 by Jeremy Whiting <jpwhiting@kde.org>              *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0019  ***************************************************************************/
0020 
0021 #include "optionsdialog.h"
0022 
0023 #include <QDialog>
0024 #include <QDialogButtonBox>
0025 #include <QIcon>
0026 #include <QPushButton>
0027 #include <QTabWidget>
0028 
0029 #include <KConfigGroup>
0030 #include <KLocalizedString>
0031 #include <KSharedConfig>
0032 
0033 #include "speech.h"
0034 #include "texttospeechconfigurationwidget.h"
0035 #include "wordcompletion/wordcompletionwidget.h"
0036 
0037 PreferencesWidget::PreferencesWidget(QWidget *parent, const QString &name)
0038     : QWidget(parent)
0039 {
0040     setObjectName(name);
0041     setupUi(this);
0042     speakCombo->setCurrentIndex(1);
0043     speak = false;
0044 
0045     closeCombo->setCurrentIndex(2);
0046     save = 2;
0047 }
0048 
0049 PreferencesWidget::~PreferencesWidget()
0050 {
0051 }
0052 
0053 void PreferencesWidget::cancel()
0054 {
0055     if (speak)
0056         speakCombo->setCurrentIndex(0);
0057     else
0058         speakCombo->setCurrentIndex(1);
0059     closeCombo->setCurrentIndex(save);
0060 }
0061 
0062 void PreferencesWidget::ok()
0063 {
0064     speak = speakCombo->currentIndex() == 0;
0065     save = closeCombo->currentIndex();
0066 }
0067 
0068 void PreferencesWidget::readOptions()
0069 {
0070     KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("Preferences"));
0071     if (cg.hasKey("AutomaticSpeak"))
0072         if (cg.readEntry("AutomaticSpeak") == QLatin1String("Yes"))
0073             speak = true;
0074         else
0075             speak = false;
0076     else
0077         speak = false;
0078 
0079     KConfigGroup cg2(KSharedConfig::openConfig(), QStringLiteral("Notification Messages"));
0080     if (cg2.hasKey("AutomaticSave"))
0081         if (cg2.readEntry("AutomaticSave") == QLatin1String("Yes"))
0082             save = 0;
0083         else
0084             save = 1;
0085     else
0086         save = 2;
0087 
0088     if (speak)
0089         speakCombo->setCurrentIndex(0);
0090     else
0091         speakCombo->setCurrentIndex(1);
0092     closeCombo->setCurrentIndex(save);
0093 }
0094 
0095 void PreferencesWidget::saveOptions()
0096 {
0097     KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("Preferences"));
0098     if (speak)
0099         cg.writeEntry("AutomaticSpeak", "Yes");
0100     else
0101         cg.writeEntry("AutomaticSpeak", "No");
0102 
0103     KConfigGroup cg2(KSharedConfig::openConfig(), QStringLiteral("Notification Messages"));
0104     if (save == 0)
0105         cg2.writeEntry("AutomaticSave", "Yes");
0106     else if (save == 1)
0107         cg2.writeEntry("AutomaticSave", "No");
0108     else
0109         cg2.deleteEntry("AutomaticSave");
0110 }
0111 
0112 bool PreferencesWidget::isSpeakImmediately()
0113 {
0114     return speak;
0115 }
0116 
0117 /***************************************************************************/
0118 
0119 OptionsDialog::OptionsDialog(QWidget *parent)
0120     : KPageDialog(parent)
0121 {
0122     setWindowTitle(i18n("Configuration"));
0123     setFaceType(KPageDialog::List);
0124     // setHelp(QLatin1String("config-dialog"));
0125     setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Apply);
0126 
0127     // addGridPage (1, Qt::Horizontal, i18n("General Options"), QString(), iconGeneral);
0128 
0129     tabCtl = new QTabWidget();
0130     tabCtl->setObjectName(QStringLiteral("general"));
0131 
0132     behaviourWidget = new PreferencesWidget(tabCtl, QStringLiteral("prefPage"));
0133     tabCtl->addTab(behaviourWidget, i18n("&Preferences"));
0134 
0135     commandWidget = new TextToSpeechConfigurationWidget(tabCtl, QStringLiteral("ttsTab"));
0136     tabCtl->addTab(commandWidget, i18n("&Text-to-Speech"));
0137 
0138     KPageWidgetItem *pageGeneral = new KPageWidgetItem(tabCtl, i18n("General Options"));
0139     pageGeneral->setHeader(i18n("General Options"));
0140     pageGeneral->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
0141     addPage(pageGeneral);
0142 
0143     completionWidget = new WordCompletionWidget(nullptr, "Word Completion widget");
0144     KPageWidgetItem *pageCompletion = new KPageWidgetItem(completionWidget, i18n("Word Completion"));
0145     pageCompletion->setHeader(i18n("Word Completion"));
0146     pageCompletion->setIcon(QIcon::fromTheme(QStringLiteral("keyboard")));
0147     addPage(pageCompletion);
0148     connect(button(QDialogButtonBox::Ok), &QAbstractButton::clicked, this, &OptionsDialog::slotOk);
0149     connect(button(QDialogButtonBox::Cancel), &QAbstractButton::clicked, this, &OptionsDialog::slotCancel);
0150     connect(button(QDialogButtonBox::Apply), &QAbstractButton::clicked, this, &OptionsDialog::slotApply);
0151 }
0152 
0153 OptionsDialog::~OptionsDialog()
0154 {
0155 }
0156 
0157 void OptionsDialog::slotCancel()
0158 {
0159     //   QDialog::slotCancel();
0160     commandWidget->cancel();
0161     behaviourWidget->cancel();
0162     completionWidget->load();
0163 }
0164 
0165 void OptionsDialog::slotOk()
0166 {
0167     //   QDialog::slotOk();
0168     commandWidget->ok();
0169     behaviourWidget->ok();
0170     completionWidget->save();
0171     Q_EMIT configurationChanged();
0172 }
0173 
0174 void OptionsDialog::slotApply()
0175 {
0176     //   QDialog::slotApply();
0177     commandWidget->ok();
0178     behaviourWidget->ok();
0179     completionWidget->save();
0180     Q_EMIT configurationChanged();
0181 }
0182 
0183 TextToSpeechSystem *OptionsDialog::getTTSSystem() const
0184 {
0185     return commandWidget->getTTSSystem();
0186 }
0187 
0188 void OptionsDialog::readOptions()
0189 {
0190     commandWidget->readOptions(QStringLiteral("TTS System"));
0191     behaviourWidget->readOptions();
0192 }
0193 
0194 void OptionsDialog::saveOptions()
0195 {
0196     commandWidget->saveOptions(QStringLiteral("TTS System"));
0197     behaviourWidget->saveOptions();
0198     KSharedConfig::openConfig()->sync();
0199 }
0200 
0201 bool OptionsDialog::isSpeakImmediately()
0202 {
0203     return behaviourWidget->isSpeakImmediately();
0204 }