File indexing completed on 2022-10-04 13:56:35

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 
0017 KEduVocKvtmlCompability::KEduVocKvtmlCompability()
0018 {
0019     m_userdefinedTenseCounter = 0;
0020     m_userdefinedTypeCounter = 0;
0021 
0022     initOldTypeLists();
0023     initOldTenses();
0024 }
0025 
0026 
0027 ////////////////// TYPES /////////////////////////////////////////
0028 void KEduVocKvtmlCompability::initOldTypeLists()
0029 {
0030     m_oldMainTypeNames.clear();
0031     m_oldMainTypeNames.insert( QStringLiteral("v"), i18nc( "@item:inlistbox The grammatical type of a word", "Verb" ) );
0032     m_oldMainTypeNames.insert( QStringLiteral("n"), i18nc( "@item:inlistbox The grammatical type of a word", "Noun" ) );
0033     m_oldMainTypeNames.insert( QStringLiteral("nm"), i18nc( "@item:inlistbox The grammatical type of a word", "Name" ) );
0034     m_oldMainTypeNames.insert( QStringLiteral("ar"), i18nc( "@item:inlistbox The grammatical type of a word", "Article" ) );
0035     m_oldMainTypeNames.insert( QStringLiteral("aj"), i18nc( "@item:inlistbox The grammatical type of a word", "Adjective" ) );
0036     m_oldMainTypeNames.insert( QStringLiteral("av"), i18nc( "@item:inlistbox The grammatical type of a word", "Adverb" ) );
0037     m_oldMainTypeNames.insert( QStringLiteral("pr"), i18nc( "@item:inlistbox The grammatical type of a word", "Pronoun" ) );
0038     m_oldMainTypeNames.insert( QStringLiteral("ph"), i18nc( "@item:inlistbox The grammatical type of an entry", "Phrase" ) );
0039     m_oldMainTypeNames.insert( QStringLiteral("num"), i18nc( "@item:inlistbox The grammatical type of a word", "Numeral" ) );
0040     m_oldMainTypeNames.insert( QStringLiteral("con"), i18nc( "@item:inlistbox The grammatical type of a word", "Conjunction" ) );
0041     m_oldMainTypeNames.insert( QStringLiteral("pre"), i18nc( "@item:inlistbox The grammatical type of a word", "Preposition" ) );
0042     m_oldMainTypeNames.insert( QStringLiteral("qu"), i18nc( "@item:inlistbox The grammatical type of an entry", "Question" ) );
0043 
0044 
0045     m_oldSubTypeNames.clear();
0046     m_oldSubTypeNames.insert( QStringLiteral("ord"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Numeral Ordinal  (first, second, third, ...)","Ordinal" ) );
0047     m_oldSubTypeNames.insert( QStringLiteral("crd"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Numeral Cardinal (one, two, three, ...)","Cardinal" ) );
0048     m_oldSubTypeNames.insert( QStringLiteral("def"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Article (the)","Definite" ) );
0049     m_oldSubTypeNames.insert( QStringLiteral("ind"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Article (a)","Indefinite" ) );
0050     m_oldSubTypeNames.insert( QStringLiteral("re"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Verb with regular conjugation","Regular" ) );
0051     m_oldSubTypeNames.insert( QStringLiteral("ir"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Verb with irregular conjugation","Irregular" ) );
0052     m_oldSubTypeNames.insert( QStringLiteral("pos"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Pronoun (my, your, his, her...)", "Possessive" ) );
0053     m_oldSubTypeNames.insert( QStringLiteral("per"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Pronoun (I, you, he...)", "Personal" ) );
0054     m_oldSubTypeNames.insert( QStringLiteral("m"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Male" ) );
0055     m_oldSubTypeNames.insert( QStringLiteral("f"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Female" ) );
0056     m_oldSubTypeNames.insert( QStringLiteral("s"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Neutral" ) );
0057 }
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.count()-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 0;
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 0;
0118 }
0119 
0120 
0121 
0122 
0123 
0124 
0125 /*
0126 if ( type.length() >= 2 && type.left( 1 ) == QM_USER_TYPE ) {
0127                 // they started counting at 1, we need to know which index we are dealing with:
0128                 int selfDefinedTypeIndex = type.right( type.count()-1 ).toInt() -1;
0129 
0130                 // append invented types (do we not trust our own writer?)
0131                 if ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
0132                     while ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
0133                         m_oldSelfDefinedTypes.append( i18n( "User defined word type %1", m_oldSelfDefinedTypes.count() - 1 ) );
0134                     }
0135                 }
0136                 type = m_oldSelfDefinedTypes.value( selfDefinedTypeIndex );
0137             } else {
0138                 type = m_compability.mainTypeFromOldFormat( oldType );
0139                 subType = m_compability.subTypeFromOldFormat( oldType );
0140             } // not user defined - preset types
0141 
0142 
0143 if ( oldType.length() >= 2 && type.left( 1 ) == QM_USER_TYPE ) {
0144             // they started counting at 1
0145             int selfDefinedTypeIndex = oldType.right( type.count()-1 ).toInt() -1;
0146             // append invented types (do we not trust our own writer?)
0147             if ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
0148                 while ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
0149                     m_oldSelfDefinedTypes.append( i18n( "User defined word type %1", m_oldSelfDefinedTypes.count() - 1 ) );
0150                 }
0151             }
0152             type = m_oldSelfDefinedTypes.value( selfDefinedTypeIndex );
0153         } else {
0154             type = m_compability.mainTypeFromOldFormat( oldType );
0155             subType = m_compability.subTypeFromOldFormat( oldType );
0156         } // not user defined - preset types
0157     }
0158 */
0159 
0160 
0161 void KEduVocKvtmlCompability::initOldTenses()
0162 {
0163     m_oldTenses[QStringLiteral("PrSi")] = i18n( "Simple Present" );
0164     m_oldTenses[QStringLiteral("PrPr")] = i18n( "Present Progressive" );
0165     m_oldTenses[QStringLiteral("PrPe")] = i18n( "Present Perfect" );
0166     m_oldTenses[QStringLiteral("PaSi")] = i18n( "Simple Past" );
0167     m_oldTenses[QStringLiteral("PaPr")] = i18n( "Past Progressive" );
0168     m_oldTenses[QStringLiteral("PaPa")] = i18n( "Past Participle" );
0169     m_oldTenses[QStringLiteral("FuSi")] = i18n( "Future" );
0170 }
0171 
0172 
0173 void KEduVocKvtmlCompability::addUserdefinedTense(const QString & tense)
0174 {
0175     m_userdefinedTenseCounter++;
0176     m_oldTenses[KVTML_1_USER_DEFINED + QString::number( m_userdefinedTenseCounter )] = tense;
0177     m_tenses.insert(tense);
0178 
0179     qDebug() << " Add tense: " << KVTML_1_USER_DEFINED + QString::number( m_userdefinedTenseCounter ) << " - " << tense;
0180 }
0181 
0182 
0183 QString KEduVocKvtmlCompability::tenseFromKvtml1(const QString & oldTense)
0184 {
0185     // in case the document got chaged, at least make up something as tense
0186     if (!m_oldTenses.keys().contains(oldTense)) {
0187         m_oldTenses[oldTense] = oldTense;
0188         qDebug() << "Warning, tense " << oldTense << " not found in document!";
0189     }
0190     m_tenses.insert(m_oldTenses.value(oldTense));
0191     return m_oldTenses.value(oldTense);
0192 }
0193 
0194 
0195 QStringList KEduVocKvtmlCompability::documentTenses() const
0196 {
0197     return m_tenses.values();
0198 }
0199 
0200 
0201 QString KEduVocKvtmlCompability::oldTense(const QString & tense)
0202 {
0203 ///@todo writing of the user defined tenses is probably messed up
0204     if ( !m_oldTenses.values().contains(tense) ) {
0205         m_userdefinedTenseCounter++;
0206         m_oldTenses[KVTML_1_USER_DEFINED + QString::number( m_userdefinedTenseCounter )] = tense;
0207     }
0208     return m_oldTenses.key(tense);
0209 }
0210 
0211 void KEduVocKvtmlCompability::setupWordTypes(KEduVocWordType * parent)
0212 {
0213     QStringList wordTypeNames;
0214     wordTypeNames
0215         << i18nc( "The grammatical type of a word", "Verb" ) // 0
0216         << i18nc( "The grammatical type of a word", "Noun" ) // 1
0217         << i18nc( "The grammatical type of a word", "Name" )
0218         << i18nc( "The grammatical type of a word", "Article" ) // 3
0219         << i18nc( "The grammatical type of a word", "Adjective" ) // 4
0220         << i18nc( "The grammatical type of a word", "Adverb" ) // 5
0221         << i18nc( "The grammatical type of a word", "Pronoun" ) // 6
0222         << i18nc( "The grammatical type of an entry", "Phrase" )
0223         << i18nc( "The grammatical type of a word", "Numeral" ) // 8
0224         << i18nc( "The grammatical type of a word", "Conjunction" )
0225         << i18nc( "The grammatical type of a word", "Preposition" )
0226         << i18nc( "The grammatical type of an entry", "Question" );
0227 
0228     foreach (const QString &typeName, wordTypeNames) {
0229         KEduVocWordType* wordType = new KEduVocWordType(typeName, parent);
0230         parent->appendChildContainer(wordType);
0231         m_userdefinedTypeCounter++;
0232     }
0233     static_cast<KEduVocWordType*>(parent->childContainer(4))->setWordType(KEduVocWordFlag::Adjective);
0234     static_cast<KEduVocWordType*>(parent->childContainer(5))->setWordType(KEduVocWordFlag::Adverb);
0235 
0236     KEduVocWordType* numeral = static_cast<KEduVocWordType*>(parent->childContainer(8));
0237     KEduVocWordType* wordType = new KEduVocWordType(
0238         i18nc( "@item:inlistbox A subtype of the grammatical word type: Numeral Ordinal  (first, second, third, ...)","Ordinal" ), numeral);
0239     wordType->setWordType(KEduVocWordFlag::Adjective);
0240     numeral->appendChildContainer(wordType);
0241     wordType = new KEduVocWordType(
0242         i18nc( "@item:inlistbox A subtype of the grammatical word type: Numeral Cardinal (one, two, three, ...)","Cardinal" ), numeral);
0243 
0244     wordType->setWordType(KEduVocWordFlag::Adjective);
0245     numeral->appendChildContainer(wordType);
0246 
0247     KEduVocWordType* article = static_cast<KEduVocWordType*>(parent->childContainer(3));
0248     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Article (the)","Definite" ), article);
0249     wordType->setWordType(KEduVocWordFlag::Article | KEduVocWordFlag::Definite);
0250     article->appendChildContainer(wordType);
0251     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Article (a)","Indefinite" ), article);
0252     wordType->setWordType(KEduVocWordFlag::Article | KEduVocWordFlag::Indefinite);
0253     article->appendChildContainer(wordType);
0254 
0255     KEduVocWordType* verb = static_cast<KEduVocWordType*>(parent->childContainer(0));
0256     verb->setWordType(KEduVocWordFlag::Verb);
0257     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Verb with regular conjugation","Regular" ), verb);
0258     wordType->setWordType(KEduVocWordFlag::Verb | KEduVocWordFlag::Regular);
0259     verb->appendChildContainer(wordType);
0260     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Verb with irregular conjugation","Irregular" ), verb);
0261     verb->appendChildContainer(wordType);
0262     wordType->setWordType(KEduVocWordFlag::Verb | KEduVocWordFlag::Irregular);
0263 
0264     KEduVocWordType* noun = static_cast<KEduVocWordType*>(parent->childContainer(1));
0265     noun->setWordType(KEduVocWordFlag::Noun);
0266     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Male" ), noun);
0267     noun->appendChildContainer(wordType);
0268     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Masculine);
0269     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Female" ), noun);
0270     noun->appendChildContainer(wordType);
0271     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Feminine);
0272     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Neutral" ), noun);
0273     noun->appendChildContainer(wordType);
0274     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Neuter);
0275 
0276 
0277     KEduVocWordType* pronoun = static_cast<KEduVocWordType*>(parent->childContainer(6));
0278     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Pronoun (my, your, his, her...)", "Possessive" ), pronoun);
0279     wordType->setWordType(KEduVocWordFlag::Pronoun);
0280     pronoun->appendChildContainer(wordType);
0281     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Pronoun (I, you, he...)", "Personal" ), pronoun);
0282     wordType->setWordType(KEduVocWordFlag::Pronoun);
0283     pronoun->appendChildContainer(wordType);
0284 }