libkdeedu/keduvocdocument
keduvocwordtype.cpp
Go to the documentation of this file.00001 /*************************************************************************** 00002 00003 Copyright 2007 Jeremy Whiting <jeremywhiting@scitools.com> 00004 Copyright 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net> 00005 00006 ***************************************************************************/ 00007 00008 /*************************************************************************** 00009 * * 00010 * This program is free software; you can redistribute it and/or modify * 00011 * it under the terms of the GNU General Public License as published by * 00012 * the Free Software Foundation; either version 2 of the License, or * 00013 * (at your option) any later version. * 00014 * * 00015 ***************************************************************************/ 00016 00017 #include "keduvocwordtype.h" 00018 00019 #include "keduvocexpression.h" 00020 00021 #include <KDebug> 00022 00023 #include <QtCore/QList> 00024 #include <QtCore/QSet> 00025 00026 class KEduVocWordType::Private 00027 { 00028 public: 00029 // bitvector of word type flags 00030 KEduVocWordFlags m_flags; 00031 QList<KEduVocExpression*> m_expressions; 00032 // list of translations 00033 QList<KEduVocTranslation*> m_translations; 00034 }; 00035 00036 KEduVocWordType::KEduVocWordType(const QString& name, KEduVocWordType *parent) 00037 : KEduVocContainer(name, WordType, parent), d( new Private ) 00038 {} 00039 00040 KEduVocWordType::~KEduVocWordType() 00041 { 00042 foreach(KEduVocTranslation* translation, d->m_translations) { 00043 translation->setWordType(0); 00044 } 00045 delete d; 00046 } 00047 00048 QList<KEduVocExpression*> KEduVocWordType::entries(EnumEntriesRecursive recursive) 00049 { 00050 if (recursive == Recursive) { 00051 return entriesRecursive(); 00052 } 00053 00054 return d->m_expressions; 00055 } 00056 00057 int KEduVocWordType::entryCount(EnumEntriesRecursive recursive) 00058 { 00059 if (recursive == Recursive) { 00060 return entriesRecursive().count(); 00061 } 00062 return d->m_expressions.count(); 00063 } 00064 00065 void KEduVocWordType::addTranslation(KEduVocTranslation* translation) 00066 { 00067 // add to expression - if not already there because another translation of the same word is there. 00068 bool found = false; 00069 foreach(int i, translation->entry()->translationIndices()) { 00070 if (translation->entry()->translation(i)->wordType() == this) { 00071 found = true; 00072 break; 00073 } 00074 } 00075 if (!found) { 00076 d->m_expressions.append(translation->entry()); 00077 } 00078 d->m_translations.append( translation ); 00079 invalidateChildLessonEntries(); 00080 } 00081 00082 void KEduVocWordType::removeTranslation(KEduVocTranslation* translation) 00083 { 00084 d->m_translations.removeAt( d->m_translations.indexOf(translation) ); 00085 00086 // no lesson found - this entry is being deleted. remove all its siblings. 00087 if (!translation->entry()->lesson()) { 00088 int index = d->m_expressions.indexOf(translation->entry()); 00089 if (index != -1) { 00090 d->m_expressions.removeAt(index); 00091 } 00092 } 00093 00094 // TODO translation(i)'s d pointer can be null. How should we fix this? 00095 // remove from cache 00096 bool found = false; 00097 foreach(int i, translation->entry()->translationIndices()) { 00098 if (translation->entry()->translation(i)->wordType() && translation->entry()->translation(i)->wordType() == this) { 00099 found = true; 00100 break; 00101 } 00102 } 00103 if (!found) { 00104 d->m_expressions.removeAt(d->m_expressions.indexOf(translation->entry())); 00105 } 00106 00107 invalidateChildLessonEntries(); 00108 } 00109 00110 KEduVocTranslation * KEduVocWordType::translation(int row) 00111 { 00112 00113 return d->m_translations.value(row); 00114 } 00115 00116 KEduVocExpression * KEduVocWordType::entry(int row, EnumEntriesRecursive recursive) 00117 { 00118 if (recursive == Recursive) { 00119 return entriesRecursive().value(row); 00120 } 00121 return entries().value(row); 00122 } 00123 00124 KEduVocWordFlags KEduVocWordType::wordType() const 00125 { 00126 return d->m_flags; 00127 } 00128 00129 void KEduVocWordType::setWordType(KEduVocWordFlags flags) 00130 { 00131 d->m_flags = flags; 00132 } 00133 00134 KEduVocWordType* KEduVocWordType::childOfType(const KEduVocWordFlags& flags) 00135 { 00136 if(d->m_flags == flags) { 00137 return this; 00138 } 00139 foreach(KEduVocContainer* child, childContainers()) { 00140 KEduVocWordType* result = static_cast<KEduVocWordType*>(child)->childOfType(flags); 00141 if(result) { 00142 return result; 00143 } 00144 } 00145 return 0; 00146 } 00147
KDE 4.2 API Reference