spellchecking.cpp 4.68 KB
Newer Older
1 2 3
/*

Copyright 2008 Albert Astals Cid <aacid@kde.org>
4
Copyright 2020 Benjamin Port <benjamin.port@enioka.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License or (at your option) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
by the membership of KDE e.V.), which shall act as a proxy
defined in Section 14 of version 3 of the license.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "spellchecking.h"

#include <QBoxLayout>
27 28 29 30 31 32 33
#include <QSet>

#include <KConfigDialogManager>
#include <Sonnet/ConfigView>
#include <Sonnet/Settings>
#include <KPluginFactory>
#include "spellcheckingskeleton.h"
34 35 36

K_PLUGIN_FACTORY(SpellFactory, registerPlugin<SonnetSpellCheckingModule>();)

37 38 39 40 41 42 43 44 45 46 47 48 49 50
SonnetSpellCheckingModule::SonnetSpellCheckingModule(QWidget *parent, const QVariantList &)
    : KCModule(parent)
{
    QBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    m_skeleton = new SpellCheckingSkeleton(this);
    m_configWidget = new Sonnet::ConfigView(this);
    m_configWidget->setNoBackendFoundVisible(m_skeleton->clients().isEmpty());
    layout->addWidget(m_configWidget);
    m_managedConfig = addConfig(m_skeleton, m_configWidget);
    connect(m_configWidget, &Sonnet::ConfigView::configChanged, this, &SonnetSpellCheckingModule::stateChanged);
}

void SonnetSpellCheckingModule::stateChanged()
51
{
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    bool unmanagedChangeState = false;
    bool unmanagedDefaultState = true;

    QStringList refIgnoreList(m_skeleton->ignoreList());
    QStringList currentIgnoreList(m_configWidget->ignoreList());
    QStringList defaultIgnoreList(Sonnet::Settings::defaultIgnoreList());
    QSet<QString> refIgnoreSet(refIgnoreList.begin(), refIgnoreList.end());
    QSet<QString> currentIgnoreSet(currentIgnoreList.begin(), currentIgnoreList.end());
    QSet<QString> defaultIgnoreSet(defaultIgnoreList.begin(), defaultIgnoreList.end());
    unmanagedChangeState |= currentIgnoreSet != refIgnoreSet;
    unmanagedDefaultState &= currentIgnoreSet == defaultIgnoreSet;

    QStringList refPreferredLanguagesList(m_skeleton->preferredLanguages());
    QStringList currentPreferredLanguagesList(m_configWidget->preferredLanguages());
    QStringList defaultPreferredLanguagesList(Sonnet::Settings::defaultPreferredLanguages());
    QSet<QString> refPreferredLanguages(refPreferredLanguagesList.begin(), refPreferredLanguagesList.end());
    QSet<QString> currentPreferredLanguages(currentPreferredLanguagesList.begin(), currentPreferredLanguagesList.end());
    QSet<QString> defaultPreferredLanguages(defaultPreferredLanguagesList.begin(), defaultPreferredLanguagesList.end());
    unmanagedChangeState |= currentPreferredLanguages != refPreferredLanguages;
    unmanagedDefaultState &= currentPreferredLanguages == defaultPreferredLanguages;

    unmanagedChangeState |= m_skeleton->defaultLanguage() != m_configWidget->language();
    unmanagedDefaultState &= m_configWidget->language() == Sonnet::Settings::defaultDefaultLanguage();

    unmanagedWidgetDefaultState(unmanagedDefaultState);
    unmanagedWidgetChangeState(unmanagedChangeState);
78 79 80 81 82 83
}

SonnetSpellCheckingModule::~SonnetSpellCheckingModule()
{
}

84 85 86 87 88 89 90 91 92
void SonnetSpellCheckingModule::load()
{
    KCModule::load();
    // Set unmanaged widget value
    m_configWidget->setIgnoreList(m_skeleton->ignoreList());
    m_configWidget->setPreferredLanguages(m_skeleton->preferredLanguages());
    m_configWidget->setLanguage(m_skeleton->defaultLanguage());
}

93 94
void SonnetSpellCheckingModule::save()
{
95 96 97 98 99 100 101 102 103
    m_skeleton->setIgnoreList(m_configWidget->ignoreList());
    m_skeleton->setPreferredLanguages(m_configWidget->preferredLanguages());
    m_skeleton->setDefaultLanguage(m_configWidget->language());

    // with addConfig, save on skeleton will be trigger only if one managed widget changed
    if (!m_managedConfig->hasChanged()) {
        m_skeleton->save();
    }
    KCModule::save();
104 105 106 107
}

void SonnetSpellCheckingModule::defaults()
{
108 109 110 111 112
    KCModule::defaults();
    // set default value for unmanaged widgets
    m_configWidget->setIgnoreList(Sonnet::Settings::defaultIgnoreList());
    m_configWidget->setPreferredLanguages(Sonnet::Settings::defaultPreferredLanguages());
    m_configWidget->setLanguage(Sonnet::Settings::defaultDefaultLanguage());
113 114 115
}

#include "spellchecking.moc"