File indexing completed on 2024-04-28 07:36:30

0001 /*
0002  * C++ Implementation: keduvockvtml1compability_p
0003  * SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "keduvockvtmlcompability.h"
0007 #include "keduvocwordtype.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include <QDebug>
0012 
0013 const QString KEduVocKvtmlCompability::KVTML_1_USER_DEFINED = QStringLiteral("#");
0014 const QString KEduVocKvtmlCompability::KVTML_1_SEPERATOR = QStringLiteral(":");
0015 
0016 KEduVocKvtmlCompability::KEduVocKvtmlCompability()
0017 {
0018     m_userdefinedTenseCounter = 0;
0019     m_userdefinedTypeCounter = 0;
0020 
0021     initOldTypeLists();
0022     initOldTenses();
0023 }
0024 
0025 ////////////////// TYPES /////////////////////////////////////////
0026 void KEduVocKvtmlCompability::initOldTypeLists()
0027 {
0028     m_oldMainTypeNames.clear();
0029     m_oldMainTypeNames.insert(QStringLiteral("v"), i18nc("@item:inlistbox The grammatical type of a word", "Verb"));
0030     m_oldMainTypeNames.insert(QStringLiteral("n"), i18nc("@item:inlistbox The grammatical type of a word", "Noun"));
0031     m_oldMainTypeNames.insert(QStringLiteral("nm"), i18nc("@item:inlistbox The grammatical type of a word", "Name"));
0032     m_oldMainTypeNames.insert(QStringLiteral("ar"), i18nc("@item:inlistbox The grammatical type of a word", "Article"));
0033     m_oldMainTypeNames.insert(QStringLiteral("aj"), i18nc("@item:inlistbox The grammatical type of a word", "Adjective"));
0034     m_oldMainTypeNames.insert(QStringLiteral("av"), i18nc("@item:inlistbox The grammatical type of a word", "Adverb"));
0035     m_oldMainTypeNames.insert(QStringLiteral("pr"), i18nc("@item:inlistbox The grammatical type of a word", "Pronoun"));
0036     m_oldMainTypeNames.insert(QStringLiteral("ph"), i18nc("@item:inlistbox The grammatical type of an entry", "Phrase"));
0037     m_oldMainTypeNames.insert(QStringLiteral("num"), i18nc("@item:inlistbox The grammatical type of a word", "Numeral"));
0038     m_oldMainTypeNames.insert(QStringLiteral("con"), i18nc("@item:inlistbox The grammatical type of a word", "Conjunction"));
0039     m_oldMainTypeNames.insert(QStringLiteral("pre"), i18nc("@item:inlistbox The grammatical type of a word", "Preposition"));
0040     m_oldMainTypeNames.insert(QStringLiteral("qu"), i18nc("@item:inlistbox The grammatical type of an entry", "Question"));
0041 
0042     m_oldSubTypeNames.clear();
0043     m_oldSubTypeNames.insert(QStringLiteral("ord"),
0044                              i18nc("@item:inlistbox A subtype of the grammatical word type: Numeral Ordinal  (first, second, third, ...)", "Ordinal"));
0045     m_oldSubTypeNames.insert(QStringLiteral("crd"),
0046                              i18nc("@item:inlistbox A subtype of the grammatical word type: Numeral Cardinal (one, two, three, ...)", "Cardinal"));
0047     m_oldSubTypeNames.insert(QStringLiteral("def"), i18nc("@item:inlistbox A subtype of the grammatical word type: Article (the)", "Definite"));
0048     m_oldSubTypeNames.insert(QStringLiteral("ind"), i18nc("@item:inlistbox A subtype of the grammatical word type: Article (a)", "Indefinite"));
0049     m_oldSubTypeNames.insert(QStringLiteral("re"), i18nc("@item:inlistbox A subtype of the grammatical word type: Verb with regular conjugation", "Regular"));
0050     m_oldSubTypeNames.insert(QStringLiteral("ir"),
0051                              i18nc("@item:inlistbox A subtype of the grammatical word type: Verb with irregular conjugation", "Irregular"));
0052     m_oldSubTypeNames.insert(QStringLiteral("pos"),
0053                              i18nc("@item:inlistbox A subtype of the grammatical word type: Pronoun (my, your, his, her...)", "Possessive"));
0054     m_oldSubTypeNames.insert(QStringLiteral("per"), i18nc("@item:inlistbox A subtype of the grammatical word type: Pronoun (I, you, he...)", "Personal"));
0055     m_oldSubTypeNames.insert(QStringLiteral("m"), i18nc("@item:inlistbox A subtype of the grammatical word type: Noun", "Male"));
0056     m_oldSubTypeNames.insert(QStringLiteral("f"), i18nc("@item:inlistbox A subtype of the grammatical word type: Noun", "Female"));
0057     m_oldSubTypeNames.insert(QStringLiteral("s"), i18nc("@item:inlistbox A subtype of the grammatical word type: Noun", "Neutral"));
0058 }
0059 
0060 KEduVocWordType *KEduVocKvtmlCompability::typeFromOldFormat(KEduVocWordType *parent, const QString &typeSubtypeString) const
0061 {
0062     // check if it's user defined
0063     if (typeSubtypeString.length() >= 2 && typeSubtypeString.left(1) == QM_USER_TYPE) {
0064         // they started counting at 1, we need to know which index we are dealing with:
0065 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0066         int selfDefinedTypeIndex = typeSubtypeString.rightRef(typeSubtypeString.count() - 1).toInt() - 1;
0067 #else
0068         int selfDefinedTypeIndex = QStringView(typeSubtypeString).right(typeSubtypeString.length() - 1).toInt() - 1;
0069 #endif
0070         return static_cast<KEduVocWordType *>(parent->childContainer(selfDefinedTypeIndex));
0071     }
0072 
0073     // assume the parent is set up to contain the old types correctly
0074     QString mainType;
0075     QString subType;
0076     int i;
0077 
0078     if ((i = typeSubtypeString.indexOf(KVTML_1_SEPERATOR)) >= 0) {
0079         mainType = typeSubtypeString.left(i);
0080         subType = typeSubtypeString.right(i + 1);
0081     } else {
0082         mainType = typeSubtypeString;
0083     }
0084 
0085     // convert from pre-0.5 versions (I guess we can just leave that in here.
0086     // I seriously doubt that any such documents exist...
0087     if (mainType == QLatin1Char('1')) {
0088         mainType = QM_VERB;
0089     } else if (mainType == QLatin1Char('2')) {
0090         mainType = QM_NOUN;
0091     } else if (mainType == QLatin1Char('3')) {
0092         mainType = QM_NAME;
0093     }
0094 
0095     QString typeName = m_oldMainTypeNames.value(mainType);
0096     if (typeName.isEmpty()) {
0097         qDebug() << "Unknown old maintype: " << typeSubtypeString;
0098         return nullptr;
0099     }
0100 
0101     QString subTypeName = m_oldSubTypeNames.value(subType);
0102 
0103     foreach (KEduVocContainer *wordType, parent->childContainers()) {
0104         if (wordType->name() == typeName) {
0105             if (subType.isEmpty()) {
0106                 return static_cast<KEduVocWordType *>(wordType);
0107             } else {
0108                 foreach (KEduVocContainer *subWordType, wordType->childContainers()) {
0109                     if (subWordType->name() == subTypeName) {
0110                         return static_cast<KEduVocWordType *>(subWordType);
0111                     }
0112                 }
0113             }
0114         }
0115     }
0116 
0117     return nullptr;
0118 }
0119 
0120 /*
0121 if ( type.length() >= 2 && type.left( 1 ) == QM_USER_TYPE ) {
0122                 // they started counting at 1, we need to know which index we are dealing with:
0123                 int selfDefinedTypeIndex = type.right( type.count()-1 ).toInt() -1;
0124 
0125                 // append invented types (do we not trust our own writer?)
0126                 if ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
0127                     while ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
0128                         m_oldSelfDefinedTypes.append( i18n( "User defined word type %1", m_oldSelfDefinedTypes.count() - 1 ) );
0129                     }
0130                 }
0131                 type = m_oldSelfDefinedTypes.value( selfDefinedTypeIndex );
0132             } else {
0133                 type = m_compability.mainTypeFromOldFormat( oldType );
0134                 subType = m_compability.subTypeFromOldFormat( oldType );
0135             } // not user defined - preset types
0136 
0137 
0138 if ( oldType.length() >= 2 && type.left( 1 ) == QM_USER_TYPE ) {
0139             // they started counting at 1
0140             int selfDefinedTypeIndex = oldType.right( type.count()-1 ).toInt() -1;
0141             // append invented types (do we not trust our own writer?)
0142             if ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
0143                 while ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
0144                     m_oldSelfDefinedTypes.append( i18n( "User defined word type %1", m_oldSelfDefinedTypes.count() - 1 ) );
0145                 }
0146             }
0147             type = m_oldSelfDefinedTypes.value( selfDefinedTypeIndex );
0148         } else {
0149             type = m_compability.mainTypeFromOldFormat( oldType );
0150             subType = m_compability.subTypeFromOldFormat( oldType );
0151         } // not user defined - preset types
0152     }
0153 */
0154 
0155 void KEduVocKvtmlCompability::initOldTenses()
0156 {
0157     m_oldTenses[QStringLiteral("PrSi")] = i18n("Simple Present");
0158     m_oldTenses[QStringLiteral("PrPr")] = i18n("Present Progressive");
0159     m_oldTenses[QStringLiteral("PrPe")] = i18n("Present Perfect");
0160     m_oldTenses[QStringLiteral("PaSi")] = i18n("Simple Past");
0161     m_oldTenses[QStringLiteral("PaPr")] = i18n("Past Progressive");
0162     m_oldTenses[QStringLiteral("PaPa")] = i18n("Past Participle");
0163     m_oldTenses[QStringLiteral("FuSi")] = i18n("Future");
0164 }
0165 
0166 void KEduVocKvtmlCompability::addUserdefinedTense(const QString &tense)
0167 {
0168     m_userdefinedTenseCounter++;
0169     m_oldTenses[KVTML_1_USER_DEFINED + QString::number(m_userdefinedTenseCounter)] = tense;
0170     m_tenses.insert(tense);
0171 
0172     qDebug() << " Add tense: " << KVTML_1_USER_DEFINED + QString::number(m_userdefinedTenseCounter) << " - " << tense;
0173 }
0174 
0175 QString KEduVocKvtmlCompability::tenseFromKvtml1(const QString &oldTense)
0176 {
0177     // in case the document got chaged, at least make up something as tense
0178     if (!m_oldTenses.keys().contains(oldTense)) {
0179         m_oldTenses[oldTense] = oldTense;
0180         qDebug() << "Warning, tense " << oldTense << " not found in document!";
0181     }
0182     m_tenses.insert(m_oldTenses.value(oldTense));
0183     return m_oldTenses.value(oldTense);
0184 }
0185 
0186 QStringList KEduVocKvtmlCompability::documentTenses() const
0187 {
0188     return m_tenses.values();
0189 }
0190 
0191 QString KEduVocKvtmlCompability::oldTense(const QString &tense)
0192 {
0193     ///@todo writing of the user defined tenses is probably messed up
0194     if (!m_oldTenses.values().contains(tense)) {
0195         m_userdefinedTenseCounter++;
0196         m_oldTenses[KVTML_1_USER_DEFINED + QString::number(m_userdefinedTenseCounter)] = tense;
0197     }
0198     return m_oldTenses.key(tense);
0199 }
0200 
0201 void KEduVocKvtmlCompability::setupWordTypes(KEduVocWordType *parent)
0202 {
0203     QStringList wordTypeNames;
0204     wordTypeNames << i18nc("The grammatical type of a word", "Verb") // 0
0205                   << i18nc("The grammatical type of a word", "Noun") // 1
0206                   << i18nc("The grammatical type of a word", "Name") << i18nc("The grammatical type of a word", "Article") // 3
0207                   << i18nc("The grammatical type of a word", "Adjective") // 4
0208                   << i18nc("The grammatical type of a word", "Adverb") // 5
0209                   << i18nc("The grammatical type of a word", "Pronoun") // 6
0210                   << i18nc("The grammatical type of an entry", "Phrase") << i18nc("The grammatical type of a word", "Numeral") // 8
0211                   << i18nc("The grammatical type of a word", "Conjunction") << i18nc("The grammatical type of a word", "Preposition")
0212                   << i18nc("The grammatical type of an entry", "Question");
0213 
0214     foreach (const QString &typeName, wordTypeNames) {
0215         KEduVocWordType *wordType = new KEduVocWordType(typeName, parent);
0216         parent->appendChildContainer(wordType);
0217         m_userdefinedTypeCounter++;
0218     }
0219     static_cast<KEduVocWordType *>(parent->childContainer(4))->setWordType(KEduVocWordFlag::Adjective);
0220     static_cast<KEduVocWordType *>(parent->childContainer(5))->setWordType(KEduVocWordFlag::Adverb);
0221 
0222     KEduVocWordType *numeral = static_cast<KEduVocWordType *>(parent->childContainer(8));
0223     KEduVocWordType *wordType =
0224         new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Numeral Ordinal  (first, second, third, ...)", "Ordinal"), numeral);
0225     wordType->setWordType(KEduVocWordFlag::Adjective);
0226     numeral->appendChildContainer(wordType);
0227     wordType =
0228         new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Numeral Cardinal (one, two, three, ...)", "Cardinal"), numeral);
0229 
0230     wordType->setWordType(KEduVocWordFlag::Adjective);
0231     numeral->appendChildContainer(wordType);
0232 
0233     KEduVocWordType *article = static_cast<KEduVocWordType *>(parent->childContainer(3));
0234     wordType = new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Article (the)", "Definite"), article);
0235     wordType->setWordType(KEduVocWordFlag::Article | KEduVocWordFlag::Definite);
0236     article->appendChildContainer(wordType);
0237     wordType = new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Article (a)", "Indefinite"), article);
0238     wordType->setWordType(KEduVocWordFlag::Article | KEduVocWordFlag::Indefinite);
0239     article->appendChildContainer(wordType);
0240 
0241     KEduVocWordType *verb = static_cast<KEduVocWordType *>(parent->childContainer(0));
0242     verb->setWordType(KEduVocWordFlag::Verb);
0243     wordType = new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Verb with regular conjugation", "Regular"), verb);
0244     wordType->setWordType(KEduVocWordFlag::Verb | KEduVocWordFlag::Regular);
0245     verb->appendChildContainer(wordType);
0246     wordType = new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Verb with irregular conjugation", "Irregular"), verb);
0247     verb->appendChildContainer(wordType);
0248     wordType->setWordType(KEduVocWordFlag::Verb | KEduVocWordFlag::Irregular);
0249 
0250     KEduVocWordType *noun = static_cast<KEduVocWordType *>(parent->childContainer(1));
0251     noun->setWordType(KEduVocWordFlag::Noun);
0252     wordType = new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Noun", "Male"), noun);
0253     noun->appendChildContainer(wordType);
0254     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Masculine);
0255     wordType = new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Noun", "Female"), noun);
0256     noun->appendChildContainer(wordType);
0257     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Feminine);
0258     wordType = new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Noun", "Neutral"), noun);
0259     noun->appendChildContainer(wordType);
0260     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Neuter);
0261 
0262     KEduVocWordType *pronoun = static_cast<KEduVocWordType *>(parent->childContainer(6));
0263     wordType = new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Pronoun (my, your, his, her...)", "Possessive"), pronoun);
0264     wordType->setWordType(KEduVocWordFlag::Pronoun);
0265     pronoun->appendChildContainer(wordType);
0266     wordType = new KEduVocWordType(i18nc("@item:inlistbox A subtype of the grammatical word type: Pronoun (I, you, he...)", "Personal"), pronoun);
0267     wordType->setWordType(KEduVocWordFlag::Pronoun);
0268     pronoun->appendChildContainer(wordType);
0269 }