File indexing completed on 2022-11-22 13:01:09

0001 /*
0002     SPDX-FileCopyrightText: 2008 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "languageproperties.h"
0007 
0008 #include "languagepropertiespage.h"
0009 #include "languagesettings.h"
0010 
0011 #include <KEduVocDocument>
0012 
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 #include <QDialogButtonBox>
0016 #include <QLabel>
0017 #include <QToolTip>
0018 
0019 LanguageProperties::LanguageProperties(KEduVocDocument *doc, QWidget *parent)
0020     : KPageDialog(parent)
0021     , m_doc(doc)
0022 {
0023     setWindowTitle(i18nc("@title:window", "Edit Languages"));
0024     setFaceType(List);
0025     setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0026 
0027     QAbstractButton *addLangButton(buttonBox()->addButton(i18n("Add language"), QDialogButtonBox::ActionRole));
0028     addLangButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0029 
0030     QAbstractButton *removeLangButton(buttonBox()->addButton(i18n("Remove language"), QDialogButtonBox::ActionRole));
0031     removeLangButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
0032 
0033     connect(addLangButton, &QAbstractButton::clicked, this, &LanguageProperties::slotAppendIdentifier);
0034     connect(removeLangButton, &QAbstractButton::clicked, this, &LanguageProperties::slotDeleteIdentifier);
0035 
0036     for (int i = 0; i < m_doc->identifierCount(); i++) {
0037         createPage(i);
0038     }
0039 }
0040 
0041 KPageWidgetItem *LanguageProperties::createPage(int i)
0042 {
0043     LanguagePropertiesPage *page = new LanguagePropertiesPage(m_doc, i);
0044 
0045     QString name = i18n("New Language");
0046 
0047     // check if this language already exists in the doc
0048     if (m_doc->identifierCount() <= i) {
0049         m_doc->appendIdentifier();
0050     }
0051     if (m_doc->identifierCount() > i) {
0052         name = m_doc->identifier(i).name();
0053         LanguageSettings currentSettings(m_doc->identifier(i).locale());
0054         currentSettings.load();
0055     }
0056 
0057     KPageWidgetItem *editPage = new KPageWidgetItem(page, name);
0058     editPage->setHeader(i18nc("Edit language properties", "Properties for %1", name));
0059 
0060     m_pages.append(editPage);
0061     addPage(editPage);
0062 
0063     editPage->setIcon(QIcon::fromTheme(QStringLiteral("set-language")));
0064 
0065     connect(page->identifierNameLineEdit, &QLineEdit::textChanged, this, &LanguageProperties::pageNameChanged);
0066     connect(this, &LanguageProperties::accepted, page, &LanguagePropertiesPage::accept);
0067 
0068     return editPage;
0069 }
0070 
0071 void LanguageProperties::accept()
0072 {
0073     KEduVocDocument *doc = m_doc;
0074     int deleted = 0;
0075 
0076     for (int index = 0; index < m_pages.count(); index++) {
0077         if (m_pages.value(index)->isEnabled()) {
0078             if (index - deleted >= doc->identifierCount()) {
0079                 // add a language
0080                 doc->appendIdentifier();
0081             }
0082             LanguagePropertiesPage *page = static_cast<LanguagePropertiesPage *>(m_pages.value(index)->widget());
0083             page->setLanguageIdentifierIndex(index - deleted);
0084         } else {
0085             // page is disabled, delete the language
0086             if (index < doc->identifierCount()) {
0087                 if (KMessageBox::warningYesNo(this, i18n("Really delete language: %1?", doc->identifier(index - deleted).name()), i18n("Remove Language"))
0088                     == KMessageBox::Yes) {
0089                     doc->removeIdentifier(index - deleted);
0090                     deleted++;
0091                 }
0092             }
0093         }
0094     }
0095 
0096     KPageDialog::accept();
0097 }
0098 
0099 void LanguageProperties::slotAppendIdentifier()
0100 {
0101     // if a page that was "removed" is selected, simply enable it again to not delete it.
0102     if (currentPage() && !currentPage()->isEnabled()) {
0103         currentPage()->setEnabled(true);
0104         return;
0105     }
0106 
0107     int i = m_pages.count();
0108     KPageWidgetItem *newPage = createPage(i);
0109     setCurrentPage(newPage);
0110 }
0111 
0112 void LanguageProperties::slotDeleteIdentifier()
0113 {
0114     currentPage()->setEnabled(false);
0115 }
0116 
0117 void LanguageProperties::pageNameChanged(const QString &newName)
0118 {
0119     currentPage()->setName(newName);
0120 }