00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "keduvocwordtype.h"
00025
00026 #include <KLocale>
00027 #include <kdebug.h>
00028
00029 class KEduVocWordType::Private
00030 {
00031 public:
00032 struct subWordType
00033 {
00034 QString m_subTypeName;
00035 QString m_specialType;
00036 bool operator== ( const subWordType& other );
00037 };
00038 struct wordType
00039 {
00040 QString m_typeName;
00041 QString m_specialType;
00042 QList<subWordType> m_subWordTypeList;
00043 bool operator== ( const wordType& other );
00044 };
00045
00047 QList<wordType> m_wordTypeList;
00048
00049 static const QString WORDTYPE_SPECIAL_NOUN;
00050 static const QString WORDTYPE_SPECIAL_NOUN_MALE;
00051 static const QString WORDTYPE_SPECIAL_NOUN_FEMALE;
00052 static const QString WORDTYPE_SPECIAL_NOUN_NEUTRAL;
00053
00054 static const QString WORDTYPE_SPECIAL_VERB;
00055 static const QString WORDTYPE_SPECIAL_VERB_REGULAR;
00056 static const QString WORDTYPE_SPECIAL_VERB_IRREGULAR;
00057 static const QString WORDTYPE_SPECIAL_ADJECTIVE;
00058 static const QString WORDTYPE_SPECIAL_ADVERB;
00059 };
00060
00061 const QString KEduVocWordType::Private::WORDTYPE_SPECIAL_NOUN =
00062 QString( i18nc( "@item:inlistbox The grammatical type of a word", "Noun" ) );
00063 const QString KEduVocWordType::Private::WORDTYPE_SPECIAL_NOUN_MALE =
00064 QString( i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Male" ) );
00065 const QString KEduVocWordType::Private::WORDTYPE_SPECIAL_NOUN_FEMALE =
00066 QString( i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Female" ) );
00067 const QString KEduVocWordType::Private::WORDTYPE_SPECIAL_NOUN_NEUTRAL =
00068 QString( i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Neutral" ) );
00069
00070 const QString KEduVocWordType::Private::WORDTYPE_SPECIAL_VERB =
00071 QString( i18nc( "@item:inlistbox The grammatical type of a word", "Verb" ) );
00072 const QString KEduVocWordType::Private::WORDTYPE_SPECIAL_ADJECTIVE =
00073 QString( i18nc( "@item:inlistbox The grammatical type of a word", "Adjective" ) );
00074 const QString KEduVocWordType::Private::WORDTYPE_SPECIAL_ADVERB =
00075 QString( i18nc( "@item:inlistbox The grammatical type of a word", "Adverb" ) );
00076
00077
00078 bool KEduVocWordType::Private::wordType::operator ==(const wordType & other)
00079 {
00080 return m_typeName == other.m_typeName &&
00081 m_specialType == other.m_specialType &&
00082 m_subWordTypeList == other.m_subWordTypeList;
00083 }
00084
00085 bool KEduVocWordType::Private::subWordType::operator ==(const subWordType & other)
00086 {
00087 return m_subTypeName == other.m_subTypeName &&
00088 m_specialType == other.m_specialType;
00089 }
00090
00091 bool KEduVocWordType::operator ==(const KEduVocWordType & other)
00092 {
00093 return d->m_wordTypeList == other.d->m_wordTypeList;
00094 }
00095
00096 KEduVocWordType::KEduVocWordType()
00097 : d( new Private )
00098 {}
00099
00100 KEduVocWordType::~KEduVocWordType()
00101 {
00102 delete d;
00103 }
00104
00105 KEduVocWordType & KEduVocWordType::operator = ( const KEduVocWordType & other )
00106 {
00107 d->m_wordTypeList = other.d->m_wordTypeList;
00108 return *this;
00109 }
00110
00111 KEduVocWordType::KEduVocWordType( const KEduVocWordType & other )
00112 : d( new Private )
00113 {
00114 d->m_wordTypeList = other.d->m_wordTypeList;
00115 }
00116
00117
00118 void KEduVocWordType::createDefaultWordTypes()
00119 {
00120
00121 QString noun = i18nc( "@item:inlistbox The grammatical type of a word", "Noun" );
00122 addType( noun , d->WORDTYPE_SPECIAL_NOUN );
00123
00124 addSubType( noun, i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Male" ),
00125 d->WORDTYPE_SPECIAL_NOUN_MALE );
00126
00127 addSubType( noun, i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Female" ),
00128 d->WORDTYPE_SPECIAL_NOUN_FEMALE );
00129
00130 addSubType( noun, i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Neutral" ),
00131 d->WORDTYPE_SPECIAL_NOUN_NEUTRAL );
00132
00133
00134 QString verb = i18nc( "@item:inlistbox The grammatical type of a word", "Verb" );
00135
00136 addType( verb, d->WORDTYPE_SPECIAL_VERB );
00137
00138 addSubType( verb,
00139 i18nc( "@item:inlistbox A subtype of the grammatical word type: Verb with regular conjugation","Regular" ) );
00140
00141 addSubType( verb,
00142 i18nc( "@item:inlistbox A subtype of the grammatical word type: Verb with irregular conjugation","Irregular" ) );
00143
00144 addType( i18nc( "@item:inlistbox The grammatical type of a word", "Adjective" ), d->WORDTYPE_SPECIAL_ADJECTIVE );
00145 addType( i18nc( "@item:inlistbox The grammatical type of a word", "Adverb" ), d->WORDTYPE_SPECIAL_ADVERB );
00146
00147 addType( i18nc( "@item:inlistbox The grammatical type of an entry", "Question" ) );
00148 addType( i18nc( "@item:inlistbox The grammatical type of a word", "Name" ) );
00149 addType( i18nc( "@item:inlistbox The grammatical type of a word", "Article" ) );
00150 addSubType( i18nc( "@item:inlistbox The grammatical type of a word", "Article" ),
00151 i18nc( "@item:inlistbox A subtype of the grammatical word type: Article (the)","Definite" ) );
00152 addSubType( i18nc( "@item:inlistbox The grammatical type of a word", "Article" ),
00153 i18nc( "@item:inlistbox A subtype of the grammatical word type: Article (a)","Indefinite" ) );
00154
00155 addType( i18nc( "@item:inlistbox The grammatical type of a word", "Pronoun" ) );
00156 addSubType( i18nc( "@item:inlistbox The grammatical type of a word", "Pronoun" ),
00157 i18nc( "@item:inlistbox A subtype of the grammatical word type: Pronoun (my, your, his, her...)", "Possessive" ) );
00158 addSubType( i18nc( "@item:inlistbox The grammatical type of a word", "Pronoun" ),
00159 i18nc( "@item:inlistbox A subtype of the grammatical word type: Pronoun (I, you, he...)", "Personal" ) );
00160
00161 addType( i18nc( "@item:inlistbox The grammatical type of an entry", "Phrase" ) );
00162 addType( i18nc( "@item:inlistbox The grammatical type of a word", "Numeral" ) );
00163 addSubType( i18nc( "@item:inlistbox The grammatical type of a word", "Numeral" ),
00164 i18nc( "@item:inlistbox A subtype of the grammatical word type: Numeral Ordinal (one, two, three, ...)","Ordinal" ) );
00165 addSubType( i18nc( "@item:inlistbox The grammatical type of a word", "Numeral" ),
00166 i18nc( "@item:inlistbox A subtype of the grammatical word type: Numeral Cardinal (first, second, third, ...)","Cardinal" ) );
00167
00168 addType( i18nc( "@item:inlistbox The grammatical type of a word", "Conjunction" ) );
00169 addType( i18nc( "@item:inlistbox The grammatical type of a word", "Preposition" ) );
00170 }
00171
00172 QString KEduVocWordType::mainTypeName( int index ) const
00173 {
00174 return d->m_wordTypeList[index].m_typeName;
00175 }
00176
00177 int KEduVocWordType::mainTypeIndex( const QString& name ) const
00178 {
00179 for ( int i=0; i < d->m_wordTypeList.count(); i++ ) {
00180 if ( d->m_wordTypeList.value( i ).m_typeName == name ) {
00181 return i;
00182 }
00183 }
00184 return -1;
00185 }
00186
00187 QStringList KEduVocWordType::typeNameList() const
00188 {
00189 QStringList list;
00190 foreach( Private::wordType wt, d->m_wordTypeList ) {
00191 list.append( wt.m_typeName );
00192 }
00193 return list;
00194 }
00195
00196 QStringList KEduVocWordType::subTypeNameList( const QString & mainType ) const
00197 {
00198 int mainIndex = mainTypeIndex( mainType );
00199 kDebug() << "Get subtypes for " << mainType << " = " << mainIndex;
00200 QStringList list;
00201 foreach( Private::subWordType wt, d->m_wordTypeList.value( mainIndex ).m_subWordTypeList ) {
00202 list.append( wt.m_subTypeName );
00203 }
00204 return list;
00205 }
00206
00207
00208 void KEduVocWordType::addType( const QString & typeName, const QString & specialType )
00209 {
00210 if ( typeName.isEmpty() ) {
00211 kDebug() << "Attempting to add empty type. When opening old kvtml documents this is ok.";
00212 return;
00213 }
00214 if ( typeNameList().contains( typeName ) ) {
00215 kDebug() << "Attempting to add type \"" << typeName << "\" twice.";
00216 return;
00217 }
00218 d->m_wordTypeList.append( Private::wordType() );
00219 d->m_wordTypeList[d->m_wordTypeList.count()-1].m_typeName = typeName;
00220 d->m_wordTypeList[d->m_wordTypeList.count()-1].m_specialType = specialType;
00221 }
00222
00223 void KEduVocWordType::addSubType( const QString & mainType, const QString & typeName, const QString & specialType )
00224 {
00225 int mt = mainTypeIndex( mainType );
00226 if ( subTypeNameList(mainType).contains( typeName ) ) {
00227 kDebug() << "Attempting to add type \"" << typeName << "\" twice.";
00228 return;
00229 }
00230 d->m_wordTypeList[mt].m_subWordTypeList.append( Private::subWordType() );
00231
00232 d->m_wordTypeList[mt].m_subWordTypeList[d->m_wordTypeList[mt].m_subWordTypeList.count()-1].m_subTypeName = typeName;
00233 d->m_wordTypeList[mt].m_subWordTypeList[d->m_wordTypeList[mt].m_subWordTypeList.count()-1].m_specialType = specialType;
00234 }
00235
00236 void KEduVocWordType::renameType( const QString & oldTypeName, const QString & newTypeName )
00237 {
00238 int index = mainTypeIndex( oldTypeName );
00239 d->m_wordTypeList[index].m_typeName= newTypeName;
00240 }
00241
00242 void KEduVocWordType::renameSubType( const QString & mainTypeName, const QString & oldTypeName, const QString & newTypeName )
00243 {
00244 kDebug() << "Rename subtype: " << mainTypeName << oldTypeName << newTypeName;
00245 int mainIndex = mainTypeIndex( mainTypeName );
00246 if ( mainIndex < 0 ) {
00247 kDebug() << "Renaming of subtype faild - parent not found";
00248 return;
00249 }
00250 int subIndex = subTypeIndex( mainTypeName, oldTypeName );
00251 if ( subIndex < 0 ) {
00252 kDebug() << "Renaming of subtype faild - old subtype not found";
00253 return;
00254 }
00255
00256 d->m_wordTypeList[mainIndex].m_subWordTypeList[subIndex].m_subTypeName= newTypeName;
00257 }
00258
00259 bool KEduVocWordType::removeType( const QString & typeName )
00260 {
00261
00262 int index = mainTypeIndex( typeName );
00263 if ( index < 0 ) {
00264 return false;
00265 }
00266 d->m_wordTypeList.removeAt( index );
00267 return true;
00268 }
00269
00270 bool KEduVocWordType::removeSubType( const QString & mainTypeName, const QString & typeName )
00271 {
00272 int mainIndex = mainTypeIndex( mainTypeName );
00273 if ( mainIndex < 0 ) {
00274 return false;
00275 }
00276 int subIndex = subTypeIndex( mainTypeName, typeName );
00277 if ( subIndex < 0 ) {
00278 return false;
00279 }
00280 d->m_wordTypeList[mainIndex].m_subWordTypeList.removeAt( subIndex );
00281 return true;
00282 }
00283
00284 int KEduVocWordType::subTypeIndex( const QString & mainTypeName, const QString & subTypeName ) const
00285 {
00286 int main = mainTypeIndex( mainTypeName );
00287 if ( main < 0 ) {
00288 kDebug() << "Main word type not found (" << mainTypeName << ")";
00289 return -1;
00290 }
00291 for ( int i=0; i < d->m_wordTypeList[main].m_subWordTypeList.count(); i++ ) {
00292 if ( d->m_wordTypeList[main].m_subWordTypeList.value( i ).m_subTypeName == subTypeName ) {
00293 return i;
00294 }
00295 }
00296 return -1;
00297 }
00298
00299 QString KEduVocWordType::specialType( const QString & typeName )
00300 {
00301 int index = mainTypeIndex( typeName );
00302 if ( index >= 0 ) {
00303 return d->m_wordTypeList[index].m_specialType;
00304 }
00305 return QString();
00306 }
00307
00308 QString KEduVocWordType::specialSubType( const QString & mainTypeName, const QString & subTypeName )
00309 {
00310 int mainIndex = mainTypeIndex( mainTypeName );
00311 if ( mainIndex >= 0 ) {
00312 int subIndex = subTypeIndex( mainTypeName, subTypeName );
00313 if ( subIndex >= 0 ) {
00314 return d->m_wordTypeList[mainIndex].m_subWordTypeList[subIndex].m_specialType;
00315 }
00316 }
00317 return QString();
00318 }
00319
00320
00321 QString KEduVocWordType::specialTypeNoun() const
00322 {
00323 return d->WORDTYPE_SPECIAL_NOUN;
00324 }
00325 QString KEduVocWordType::specialTypeNounMale() const
00326 {
00327 return d->WORDTYPE_SPECIAL_NOUN_MALE;
00328 }
00329 QString KEduVocWordType::specialTypeNounFemale() const
00330 {
00331 return d->WORDTYPE_SPECIAL_NOUN_FEMALE;
00332 }
00333 QString KEduVocWordType::specialTypeNounNeutral() const
00334 {
00335 return d->WORDTYPE_SPECIAL_NOUN_NEUTRAL;
00336 }
00337
00338
00339 QString KEduVocWordType::specialTypeVerb() const
00340 {
00341 return d->WORDTYPE_SPECIAL_VERB;
00342 }
00343
00344 QString KEduVocWordType::specialTypeAdjective() const
00345 {
00346 return d->WORDTYPE_SPECIAL_ADJECTIVE;
00347 }
00348
00349 QString KEduVocWordType::specialTypeAdverb() const
00350 {
00351 return d->WORDTYPE_SPECIAL_ADVERB;
00352 }
00353
00354 void KEduVocWordType::setSpecialType(const QString & typeName, const QString & newSpecialType)
00355 {
00356 int mainIndex = mainTypeIndex( typeName );
00357 if ( mainIndex >= 0 ) {
00358 d->m_wordTypeList[mainIndex].m_specialType = newSpecialType;
00359 }
00360 }
00361
00362 void KEduVocWordType::setSpecialSubType(const QString & mainTypeName, const QString & subTypeName, const QString & newSpecialType)
00363 {
00364 int mainIndex = mainTypeIndex( mainTypeName );
00365 if ( mainIndex >= 0 ) {
00366 int subIndex = subTypeIndex( mainTypeName, subTypeName );
00367 if ( subIndex >= 0 ) {
00368 d->m_wordTypeList[mainIndex].m_subWordTypeList[subIndex].m_specialType = newSpecialType;
00369 }
00370 }
00371 }
00372
00373 void KEduVocWordType::clear()
00374 {
00375 d->m_wordTypeList.clear();
00376 }
00377