Warning, file /education/parley/src/settings/documentproperties.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     enter document title and author
0003     SPDX-FileCopyrightText: 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
0004     SPDX-FileCopyrightText: 2005 Peter Hedlund <peter.hedlund@kdemail.net>
0005     SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "documentproperties.h"
0010 
0011 #include "parleydocument.h"
0012 #include "parleymainwindow.h"
0013 
0014 #include <KEduVocDocument>
0015 #include <QLineEdit>
0016 
0017 #include <QLabel>
0018 #include <QTextEdit>
0019 
0020 DocumentProperties::DocumentProperties(KEduVocDocument *doc, bool languageSetup, QWidget *parent)
0021     : QWidget(parent)
0022     , m_doc(doc)
0023     , m_showLanguages(languageSetup)
0024 {
0025     setupUi(this);
0026 
0027     titleLineEdit->setText(doc->title());
0028     authorLineEdit->setText(doc->author());
0029     contactLineEdit->setText(doc->authorContact());
0030     licenseComboBox->setEditText(doc->license());
0031     commentTextEdit->setText(doc->documentComment());
0032     categoryComboBox->setEditText(doc->category());
0033 
0034     if (languageSetup) {
0035         prepareLanguageSelection();
0036     } else {
0037         languageGroupBox->setVisible(false);
0038     }
0039 }
0040 
0041 void DocumentProperties::prepareLanguageSelection()
0042 {
0043     QStringList languageNames;
0044 
0045     // Create a key map, key: language-country pair, value: locale
0046     QMap<QString, QString> languageCodeMap = DocumentProperties::localeLangsMap();
0047 
0048     QMap<QString, QString>::const_iterator lcMapIter;
0049 
0050     for (lcMapIter = languageCodeMap.constBegin(); lcMapIter != languageCodeMap.constEnd(); ++lcMapIter) {
0051         firstLanguageComboBox->addItem(lcMapIter.key(), lcMapIter.value());
0052         secondLanguageComboBox->addItem(lcMapIter.key(), lcMapIter.value());
0053         languageNames.append(lcMapIter.key());
0054     }
0055 
0056     languageNames.sort();
0057 
0058     firstLanguageComboBox->completionObject()->insertItems(languageNames);
0059     firstLanguageComboBox->completionObject()->setIgnoreCase(true);
0060     secondLanguageComboBox->completionObject()->insertItems(languageNames);
0061     secondLanguageComboBox->completionObject()->setIgnoreCase(true);
0062 
0063     languageGroupBox->setVisible(true);
0064 }
0065 
0066 void DocumentProperties::accept()
0067 {
0068     m_doc->setTitle(titleLineEdit->text());
0069     m_doc->setAuthor(authorLineEdit->text());
0070     m_doc->setAuthorContact(contactLineEdit->text());
0071     m_doc->setLicense(licenseComboBox->currentText());
0072     m_doc->setDocumentComment(commentTextEdit->toPlainText());
0073     m_doc->setCategory(categoryComboBox->currentText());
0074 
0075     if (m_showLanguages) {
0076         acceptLanguageConfiguration();
0077     }
0078 }
0079 
0080 void DocumentProperties::acceptLanguageConfiguration()
0081 {
0082     QString firstLanguage = firstLanguageComboBox->currentText();
0083     QString firstLocale = firstLanguageComboBox->currentData().toString();
0084     QString secondLanguage = secondLanguageComboBox->currentText();
0085     QString secondLocale = secondLanguageComboBox->currentData().toString();
0086     m_doc->identifier(0).setLocale(firstLocale);
0087     m_doc->identifier(0).setName(firstLanguage);
0088     m_doc->identifier(1).setLocale(secondLocale);
0089     m_doc->identifier(1).setName(secondLanguage);
0090 }
0091 
0092 QMap<QString, QString> DocumentProperties::localeLangsMap()
0093 {
0094     // Get a list of locales
0095     QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);
0096 
0097     QMap<QString, QString> languageCodeMap;
0098 
0099     for (const QLocale &myLocale : qAsConst(allLocales)) {
0100         if (!myLocale.nativeLanguageName().isEmpty() && !myLocale.nativeCountryName().isEmpty()) {
0101             languageCodeMap[myLocale.nativeLanguageName() + " (" + myLocale.nativeCountryName() + ')'] = myLocale.name();
0102         }
0103     }
0104     return languageCodeMap;
0105 }