parley
lesson.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "lesson.h"
00016 #include "expression.h"
00017
00018 #include <keduvocexpression.h>
00019
00020 #include <KDebug>
00021
00022 namespace Scripting
00023 {
00024 Lesson::Lesson ( KEduVocLesson * lesson )
00025 : Container ( lesson ), m_lesson ( lesson )
00026 {
00027 }
00028
00029 Lesson::Lesson ( KEduVocContainer * container )
00030 {
00031 m_lesson = static_cast<KEduVocLesson*> ( container );
00032 m_container = m_lesson;
00033 }
00034
00035 Lesson::Lesson ( const QString& name )
00036 {
00037 m_lesson = new KEduVocLesson ( name );
00038 m_container = m_lesson;
00039 }
00040
00041
00042 Lesson::~Lesson()
00043 {
00044 }
00045
00046 QList<KEduVocLesson*> flattenLessons ( KEduVocLesson * rootLesson )
00047 {
00048 QList<KEduVocLesson*> lessonsList;
00049 foreach ( KEduVocContainer * child, rootLesson->childContainers() )
00050 {
00051 lessonsList << static_cast<KEduVocLesson*> ( child );
00052 lessonsList += flattenLessons ( static_cast<KEduVocLesson*> ( child ) );
00053 }
00054 return lessonsList;
00055 }
00056
00057
00058 QVariantList Lesson::childLessons ( bool recursive )
00059 {
00060 if ( recursive )
00061 return toVariantList<KEduVocLesson,Lesson> ( flattenLessons ( m_lesson ) );
00062 return toVariantList<KEduVocContainer,Lesson> ( m_lesson->childContainers() );
00063 }
00064
00065 QVariantList Lesson::entries ( bool recursive ) const
00066 {
00067 return toVariantList<KEduVocExpression,Expression> ( m_lesson->entries ( boolToEnum ( recursive ) ) );
00068 }
00069
00070 void Lesson::setEntries ( QVariantList entries )
00071 {
00072 clearEntries();
00073
00074 foreach ( const QVariant &ventry, entries )
00075 {
00076 QObject * obj = qvariant_cast<QObject*> ( ventry );
00077 Expression * entry = dynamic_cast<Expression*> ( obj );
00078 if ( entry )
00079 m_lesson->appendEntry ( entry->kEduVocExpression() );
00080
00081 }
00082 }
00083
00084 QObject * Lesson::entry ( int row, bool recursive )
00085 {
00086 return new Expression ( m_lesson->entry ( row, boolToEnum ( recursive ) ) );
00087 }
00088
00089 int Lesson::entryCount ( bool recursive )
00090 {
00091 return m_lesson->entryCount ( boolToEnum ( recursive ) );
00092 }
00093
00094 void Lesson::appendEntry ( Expression * entry )
00095 {
00096 m_lesson->appendEntry ( entry->kEduVocExpression() );
00097 }
00098
00099 void Lesson::insertEntry ( int index, Expression * entry )
00100 {
00101 m_lesson->insertEntry ( index,entry->kEduVocExpression() );
00102 }
00103
00104 void Lesson::removeEntry ( QObject * entry )
00105 {
00107 Expression * e = dynamic_cast<Expression*> ( entry );
00108 if ( e )
00109 {
00110 m_lesson->removeEntry ( e->kEduVocExpression() );
00111 }
00112 else
00113 {
00114 kDebug() << "The entry given does not exist";
00115 }
00116 }
00117
00118 void Lesson::clearEntries()
00119 {
00120 int N = m_lesson->entryCount ( KEduVocLesson::NotRecursive );
00121 for ( int i = 0; i < N; i++ )
00122 m_lesson->removeEntry( m_lesson->entry ( 0,KEduVocLesson::NotRecursive ) );
00123 }
00124
00125
00126 QObject* Lesson::newEntry()
00127 {
00128 return new Expression();
00129 }
00130
00131
00132
00133
00134
00135
00136 QObject* Lesson::newEntry ( QStringList translations )
00137 {
00138 return new Expression ( translations );
00139 }
00140
00141 void Lesson::appendNewEntry ( QStringList translations )
00142 {
00143 KEduVocExpression * expr = new KEduVocExpression ( translations );
00144 m_lesson->appendEntry ( expr );
00145 }
00146
00147 QObject * Lesson::findChildLesson ( const QString& name )
00148 {
00149 KEduVocContainer * container = findContainer ( name );
00150 if ( container )
00151 return new Lesson ( container );
00152 kDebug() << "not found";
00153 return 0;
00154 }
00155
00156 }