From 47dca093fde1dcbeb5742943e2271283c2c3d09f Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Tue, 27 Nov 2007 18:01:18 +0000 Subject: [PATCH] Start a container class. Move lesson specific stuff out of there. Next: Start the word type class. svn path=/branches/work/kdeedu_parley/libkdeedu/; revision=742271 --- keduvocdocument/CMakeLists.txt | 1 + keduvocdocument/keduvoccontainer.cpp | 191 ++++++++++++++++++ keduvocdocument/keduvoccontainer.h | 128 ++++++++++++ keduvocdocument/keduvoccsvreader.cpp | 2 +- keduvocdocument/keduvocexpression.h | 1 + keduvocdocument/keduvockvtml2reader.cpp | 6 +- keduvocdocument/keduvockvtml2writer.cpp | 8 +- keduvocdocument/keduvoclesson.cpp | 154 +------------- keduvocdocument/keduvoclesson.h | 79 ++------ keduvocdocument/keduvocpaukerreader.cpp | 2 +- .../tests/keduvocdocumentvalidatortest.cpp | 4 +- 11 files changed, 350 insertions(+), 226 deletions(-) create mode 100644 keduvocdocument/keduvoccontainer.cpp create mode 100644 keduvocdocument/keduvoccontainer.h diff --git a/keduvocdocument/CMakeLists.txt b/keduvocdocument/CMakeLists.txt index 966dc73..46c3050 100644 --- a/keduvocdocument/CMakeLists.txt +++ b/keduvocdocument/CMakeLists.txt @@ -7,6 +7,7 @@ keduvocdocument.cpp keduvocidentifier.cpp keduvocexpression.cpp keduvoctranslation.cpp + keduvoccontainer.cpp keduvoclesson.cpp keduvocgrade.cpp keduvocgrammar.cpp diff --git a/keduvocdocument/keduvoccontainer.cpp b/keduvocdocument/keduvoccontainer.cpp new file mode 100644 index 0000000..5086fb8 --- /dev/null +++ b/keduvocdocument/keduvoccontainer.cpp @@ -0,0 +1,191 @@ +/*************************************************************************** + + Copyright 2007 Jeremy Whiting + Copyright 2007 Frederik Gladhorn + + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "keduvoccontainer.h" + +#include "keduvocexpression.h" + +#include + +/** private class to store information about a lesson */ +class KEduVocContainer::Private +{ +public: + ~Private(); + + // properties for this lesson + QString m_name; + bool m_inPractice; + + // other lessons in the tree + KEduVocContainer *m_parentContainer; + QList m_childContainers; + + EnumContainerType m_type; + + /// Image url + KUrl m_imageUrl; +}; + +KEduVocContainer::Private::~ Private() +{ + qDeleteAll(m_childContainers); +} + +KEduVocContainer::KEduVocContainer(const QString& name, EnumContainerType type, KEduVocContainer *parent) + : d( new Private ) +{ + d->m_parentContainer = parent; + d->m_name = name; + d->m_inPractice = false; + d->m_type = type; +} + +KEduVocContainer::KEduVocContainer( const KEduVocContainer &other ) + : d( new Private ) +{ + d->m_name = other.d->m_name; + d->m_inPractice = other.d->m_inPractice; +} + +KEduVocContainer::~KEduVocContainer() +{ + delete d; +} + +void KEduVocContainer::appendChildContainer(KEduVocContainer * child) +{ + d->m_childContainers.append(child); +} + +KEduVocContainer * KEduVocContainer::childContainer(int row) +{ + return d->m_childContainers.value(row); +} + +int KEduVocContainer::childContainerCount() const +{ + return d->m_childContainers.count(); +} + +int KEduVocContainer::row() const +{ + if (d->m_parentContainer) { + return d->m_parentContainer->d->m_childContainers.indexOf(const_cast(this)); + } + return 0; +} + + +KEduVocContainer& KEduVocContainer::operator= ( const KEduVocContainer &other ) +{ + d->m_name = other.d->m_name; + d->m_inPractice = other.d->m_inPractice; + return *this; +} + +bool KEduVocContainer::operator==(const KEduVocContainer &other) +{ + return d->m_name == other.d->m_name && + d->m_inPractice == other.d->m_inPractice;; +} + +void KEduVocContainer::setName( const QString &name ) +{ + d->m_name = name; +} + +QString KEduVocContainer::name() +{ + return d->m_name; +} + +bool KEduVocContainer::inPractice() +{ + return d->m_inPractice; +} + +void KEduVocContainer::setInPractice(bool inPractice) +{ + d->m_inPractice = inPractice; +} + +void KEduVocContainer::removeTranslation(int translation) +{ + foreach(KEduVocContainer *childContainer, d->m_childContainers) { + childContainer->removeTranslation(translation); + } + + foreach(KEduVocExpression *entry, entries() ) { + entry->removeTranslation( translation ); + } +} + +QList< KEduVocExpression * > KEduVocContainer::entriesRecursive() +{ + QList< KEduVocExpression * > entryList = entries(); + foreach(KEduVocContainer *childContainer, d->m_childContainers) { + foreach(KEduVocExpression *childEntry, childContainer->entriesRecursive()) { + if(!entryList.contains(childEntry)) { + entryList.append(childEntry); + } + } + } + return entryList; +} + +QList< KEduVocContainer * > KEduVocContainer::childContainers() +{ + return d->m_childContainers; +} + +KEduVocContainer * KEduVocContainer::parent() +{ + return d->m_parentContainer; +} + +// KEduVocContainer * KEduVocContainer::childContainer(const QString & name) +// { +// for(int i = 0; im_childContainers.count(); i++){ +// if(d->m_childContainers.value(i)->name() == name) { +// return d->m_childContainers[i]; +// } +// } +// return 0; +// } + +void KEduVocContainer::setContainerType(KEduVocContainer::EnumContainerType type) +{ + d->m_type = type; +} + +KEduVocContainer::EnumContainerType KEduVocContainer::containerType() +{ + return d->m_type; +} + + +KUrl KEduVocContainer::imageUrl() +{ + return d->m_imageUrl; +} + +void KEduVocContainer::setImageUrl(const KUrl &url) +{ + d->m_imageUrl = url; +} + + diff --git a/keduvocdocument/keduvoccontainer.h b/keduvocdocument/keduvoccontainer.h new file mode 100644 index 0000000..377bdcb --- /dev/null +++ b/keduvocdocument/keduvoccontainer.h @@ -0,0 +1,128 @@ +/*************************************************************************** + + Copyright 2007 Jeremy Whiting + Copyright 2007 Frederik Gladhorn + + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef KEDUVOCCONTAINER_H +#define KEDUVOCCONTAINER_H + +#include "libkeduvocdocument_export.h" + +#include + +#include +#include + +class KEduVocExpression; + +/** class to store information about a lesson */ +class KEDUVOCDOCUMENT_EXPORT KEduVocContainer +{ +public: + enum EnumContainerType{ + LessonContainer, + LeitnerContainer, + WordTypeContainer, + WordTypeNounContainer, + WordTypeNounMaleContainer, + WordTypeNounFemaleContainer, + WordTypeNounNeutralContainer, + WordTypeVerbContainer, + WordTypeAdjectiveContainer, + WordTypeAdverbContainer + }; + + /** default constructor */ + explicit KEduVocContainer(const QString& name, EnumContainerType type = LessonContainer, KEduVocContainer *parent = 0); + + void appendChildContainer(KEduVocContainer *child); + + QList childContainers(); + + KEduVocContainer *childContainer(int row); + + /** + * Find a child container + * @param name + * @return + */ +// KEduVocContainer *childContainer(const QString& name); + + int childContainerCount() const; + + int row() const; + KEduVocContainer *parent(); + + /** copy constructor for d-pointer safe copying */ + KEduVocContainer( const KEduVocContainer &other ); + + /** destructor */ + ~KEduVocContainer(); + + /** assignment operator */ + KEduVocContainer& operator= ( const KEduVocContainer& ); + + /** set the container name + * @param name text to set for the name + */ + void setName( const QString &name ); + + /** get the container name */ + QString name(); + + /** get a list of all entries in the container */ + virtual QList < KEduVocExpression* > entries() =0; + + /** get a list of all entries in the container and its child containers */ + QList < KEduVocExpression* > entriesRecursive(); + + /** + * Removes a translation. This has to be called when a language is removed from a document. + * @param translation + */ + void removeTranslation(int translation); + + bool inPractice(); + void setInPractice( bool inPractice ); + + /** equality operator */ + bool operator==(const KEduVocContainer &other); + + /** + * The type of this container. @see EnumContainerType + * @return + */ + KEduVocContainer::EnumContainerType containerType(); + + /** + * Set the type of container. + * For convenience by default this is taken over from the parent, so no need to set. + * @param type the new type + */ + void setContainerType(KEduVocContainer::EnumContainerType type); + + /** get the image url for this container if it exists */ + KUrl imageUrl(); + + /** set the image url for this container + * @param url url of the image + */ + void setImageUrl(const KUrl &url); + +private: + class Private; + Private * const d; +}; + +#endif diff --git a/keduvocdocument/keduvoccsvreader.cpp b/keduvocdocument/keduvoccsvreader.cpp index 4afd7e5..9ab5913 100644 --- a/keduvocdocument/keduvoccsvreader.cpp +++ b/keduvocdocument/keduvoccsvreader.cpp @@ -56,7 +56,7 @@ bool KEduVocCsvReader::readDoc( KEduVocDocument *doc ) int languageCount = 0; KEduVocLesson* lesson = new KEduVocLesson( i18n("Vocabulary"), m_doc->lesson()); - m_doc->lesson()->appendChildLesson(lesson); + m_doc->lesson()->appendChildContainer(lesson); while ( !inputStream.atEnd() ) { QString s = inputStream.readLine(); diff --git a/keduvocdocument/keduvocexpression.h b/keduvocdocument/keduvocexpression.h index 6781df1..56762d3 100644 --- a/keduvocdocument/keduvocexpression.h +++ b/keduvocdocument/keduvocexpression.h @@ -116,6 +116,7 @@ private: */ void addLesson( KEduVocLesson * l ); void removeLesson( KEduVocLesson * l ); + friend class KEduVocLesson; }; diff --git a/keduvocdocument/keduvockvtml2reader.cpp b/keduvocdocument/keduvockvtml2reader.cpp index 0e3e8d3..dd685a5 100644 --- a/keduvocdocument/keduvockvtml2reader.cpp +++ b/keduvocdocument/keduvockvtml2reader.cpp @@ -200,7 +200,7 @@ bool KEduVocKvtml2Reader::readGroups( QDomElement &domElementParent ) if (defaultLesson->entryCount() > 0) { - m_doc->lesson()->appendChildLesson(defaultLesson); + m_doc->lesson()->appendChildContainer(defaultLesson); } else { delete defaultLesson; } @@ -423,7 +423,7 @@ bool KEduVocKvtml2Reader::readLesson( KEduVocLesson* parentLesson, QDomElement & //Lesson name QDomElement currentElement = lessonElement.firstChildElement( KVTML_NAME ); KEduVocLesson * lesson = new KEduVocLesson(currentElement.text(), parentLesson); - parentLesson->appendChildLesson( lesson ); + parentLesson->appendChildContainer( lesson ); readChildLessons( lesson, lessonElement ); @@ -528,7 +528,7 @@ bool KEduVocKvtml2Reader::readWordType( KEduVocLesson* parentContainer, QDomElem typeElement.firstChildElement( KVTML_NAME ).text(); KEduVocLesson * wordTypeContainer = new KEduVocLesson(typeName, parentContainer); - parentContainer->appendChildLesson(wordTypeContainer); + parentContainer->appendChildContainer(wordTypeContainer); QString specialType = typeElement.firstChildElement( KVTML_SPECIALWORDTYPE ).text(); if ( !specialType.isEmpty() ) { diff --git a/keduvocdocument/keduvockvtml2writer.cpp b/keduvocdocument/keduvockvtml2writer.cpp index 43cfd20..b877b57 100644 --- a/keduvocdocument/keduvockvtml2writer.cpp +++ b/keduvocdocument/keduvockvtml2writer.cpp @@ -169,8 +169,8 @@ bool KEduVocKvtml2Writer::writeLessons( KEduVocLesson *parentLesson, QDomElement { // iterate over child lessons. // the first time this is called with the root lesson which does not have a entry. - for( int i = 0; i < parentLesson->childLessonCount(); i++ ) { - KEduVocLesson *lesson = parentLesson->childLesson(i); + for( int i = 0; i < parentLesson->childContainerCount(); i++ ) { + KEduVocLesson *lesson = static_cast(parentLesson->childContainer(i)); // make lesson element QDomElement thisLessonElement = m_domDoc.createElement( KVTML_CONTAINER ); @@ -267,7 +267,9 @@ bool KEduVocKvtml2Writer::writeArticle( QDomElement &articleElement, int article bool KEduVocKvtml2Writer::writeWordTypes( QDomElement &typesElement, KEduVocLesson* parentContainer ) { - foreach( KEduVocLesson* wordType, parentContainer->childLessons() ) { + foreach( KEduVocContainer* container, parentContainer->childContainers() ) { + KEduVocLesson* wordType = static_cast(container); + kDebug() << "Writing type: " << wordType->name(); QDomElement typeDefinitionElement = m_domDoc.createElement( KVTML_CONTAINER ); diff --git a/keduvocdocument/keduvoclesson.cpp b/keduvocdocument/keduvoclesson.cpp index 0742664..f50da4a 100644 --- a/keduvocdocument/keduvoclesson.cpp +++ b/keduvocdocument/keduvoclesson.cpp @@ -1,12 +1,8 @@ /*************************************************************************** - manage lessons - ----------------------------------------------------------------------- - begin : August 11, 2007 + Copyright 2007 Jeremy Whiting + Copyright 2007 Frederik Gladhorn - copyright : (C) 2007 Jeremy Whiting - - ----------------------------------------------------------------------- ***************************************************************************/ /*************************************************************************** @@ -30,47 +26,25 @@ class KEduVocLesson::Private public: ~Private(); - // properties for this lesson - QString m_name; - bool m_inPractice; - - // other lessons in the tree - KEduVocLesson *m_parentLesson; - QList m_childLessons; - // entries QList m_entries; - - EnumContainerType m_type; }; KEduVocLesson::Private::~ Private() { - qDeleteAll(m_childLessons); - ///@todo delete children here???? is this a 1:1 mapping? qDeleteAll(m_entries); } KEduVocLesson::KEduVocLesson(const QString& name, KEduVocLesson *parent) - : d( new Private ) + : d( new Private ), KEduVocContainer(name, LessonContainer, parent) { - d->m_parentLesson = parent; - d->m_name = name; - d->m_inPractice = false; - if(parent) { - d->m_type = parent->containerType(); - } else { - d->m_type = LessonContainer; - } } KEduVocLesson::KEduVocLesson( const KEduVocLesson &other ) - : d( new Private ) + : d( new Private ), KEduVocContainer(other) { d->m_entries = other.d->m_entries; - d->m_name = other.d->m_name; - d->m_inPractice = other.d->m_inPractice; } KEduVocLesson::~KEduVocLesson() @@ -78,54 +52,6 @@ KEduVocLesson::~KEduVocLesson() delete d; } -void KEduVocLesson::appendChildLesson(KEduVocLesson * child) -{ - d->m_childLessons.append(child); -} - -KEduVocLesson * KEduVocLesson::childLesson(int row) -{ - return d->m_childLessons.value(row); -} - -int KEduVocLesson::childLessonCount() const -{ - return d->m_childLessons.count(); -} - -int KEduVocLesson::row() const -{ - if (d->m_parentLesson) { - return d->m_parentLesson->d->m_childLessons.indexOf(const_cast(this)); - } - return 0; -} - - -KEduVocLesson& KEduVocLesson::operator= ( const KEduVocLesson &other ) -{ - d->m_entries = other.d->m_entries; - d->m_name = other.d->m_name; - d->m_inPractice = other.d->m_inPractice; - return *this; -} - -bool KEduVocLesson::operator==(const KEduVocLesson &other) -{ - return d->m_entries == other.d->m_entries && - d->m_name == other.d->m_name && - d->m_inPractice == other.d->m_inPractice;; -} - -void KEduVocLesson::setName( const QString &name ) -{ - d->m_name = name; -} - -QString KEduVocLesson::name() -{ - return d->m_name; -} QList KEduVocLesson::entries() { @@ -140,9 +66,7 @@ int KEduVocLesson::entryCount() void KEduVocLesson::addEntry(KEduVocExpression* entry) { d->m_entries.append( entry ); - if(d->m_type == LessonContainer) { - entry->addLesson(this); - } + entry->addLesson(this); } void KEduVocLesson::removeEntry(KEduVocExpression* entry) @@ -151,76 +75,8 @@ void KEduVocLesson::removeEntry(KEduVocExpression* entry) entry->removeLesson(this); } - -bool KEduVocLesson::inPractice() -{ - return d->m_inPractice; -} - -void KEduVocLesson::setInPractice(bool inPractice) -{ - d->m_inPractice = inPractice; -} - -void KEduVocLesson::removeTranslation(int translation) -{ - foreach(KEduVocLesson *childLesson, d->m_childLessons) { - childLesson->removeTranslation(translation); - } - - foreach(KEduVocExpression *entry, d->m_entries ) { - entry->removeTranslation( translation ); - } -} - -QList< KEduVocExpression * > KEduVocLesson::entriesRecursive() -{ - QList< KEduVocExpression * > entryList = entries(); - foreach(KEduVocLesson *childLesson, d->m_childLessons) { - foreach(KEduVocExpression *childEntry, childLesson->entriesRecursive()) { - if(!entryList.contains(childEntry)) { - entryList.append(childEntry); - } - } - } - return entryList; -} - -QList< KEduVocLesson * > KEduVocLesson::childLessons() -{ - return d->m_childLessons; -} - KEduVocExpression * KEduVocLesson::entry(int row) { return d->m_entries.value(row); } -KEduVocLesson * KEduVocLesson::parent() -{ - return d->m_parentLesson; -} - -KEduVocLesson * KEduVocLesson::childLesson(const QString & name) -{ - for(int i = 0; im_childLessons.count(); i++){ - if(d->m_childLessons.value(i)->name() == name) { - return d->m_childLessons[i]; - } - } - - KEduVocLesson* newLesson = new KEduVocLesson(name, this); - appendChildLesson(newLesson); - return newLesson; -} - -void KEduVocLesson::setContainerType(KEduVocLesson::EnumContainerType type) -{ - d->m_type = type; -} - -KEduVocLesson::EnumContainerType KEduVocLesson::containerType() -{ - return d->m_type; -} - diff --git a/keduvocdocument/keduvoclesson.h b/keduvocdocument/keduvoclesson.h index f3d2525..b9a8b26 100644 --- a/keduvocdocument/keduvoclesson.h +++ b/keduvocdocument/keduvoclesson.h @@ -1,12 +1,8 @@ /*************************************************************************** - manage lessons - ----------------------------------------------------------------------- - begin : August 11, 2007 + Copyright 2007 Jeremy Whiting + Copyright 2007 Frederik Gladhorn - copyright : (C) 2007 Jeremy Whiting - - ----------------------------------------------------------------------- ***************************************************************************/ /*************************************************************************** @@ -23,45 +19,29 @@ #include "libkeduvocdocument_export.h" +#include "keduvoccontainer.h" + #include #include class KEduVocExpression; /** class to store information about a lesson */ -class KEDUVOCDOCUMENT_EXPORT KEduVocLesson +class KEDUVOCDOCUMENT_EXPORT KEduVocLesson :public KEduVocContainer { public: - enum EnumContainerType{ - LessonContainer, - LeitnerContainer, - WordTypeContainer, - WordTypeNounContainer, - WordTypeNounMaleContainer, - WordTypeNounFemaleContainer, - WordTypeNounNeutralContainer, - WordTypeVerbContainer, - WordTypeAdjectiveContainer, - WordTypeAdverbContainer - }; - /** default constructor */ explicit KEduVocLesson(const QString& name, KEduVocLesson *parent = 0); - void appendChildLesson(KEduVocLesson *child); +// void appendChildLesson(KEduVocLesson *child); - QList childLessons(); - KEduVocLesson *childLesson(int row); - /** - * Find a child lesson, creates a new lesson it if it does not exist! - * @param name - * @return - */ - KEduVocLesson *childLesson(const QString& name); - int childLessonCount() const; +// QList childLessons(); +// KEduVocLesson *childLesson(int row); + +// int childLessonCount() const; - int row() const; - KEduVocLesson *parent(); +// int row() const; +// KEduVocLesson *parent(); /** copy constructor for d-pointer safe copying */ KEduVocLesson( const KEduVocLesson &other ); @@ -72,22 +52,12 @@ public: /** assignment operator */ KEduVocLesson& operator= ( const KEduVocLesson& ); - /** set the lesson name - * @param name text to set for the name - */ - void setName( const QString &name ); - - /** get the lesson name */ - QString name(); KEduVocExpression* entry(int row); /** get a list of all entries in the lesson */ QList < KEduVocExpression* > entries(); - /** get a list of all entries in the lesson and its child lessons */ - QList < KEduVocExpression* > entriesRecursive(); - /** get the number of entries in the lesson */ int entryCount(); @@ -101,31 +71,6 @@ public: */ void removeEntry(KEduVocExpression* entry); - - void removeTranslation(int translation); - - //void resetGrades()? - - bool inPractice(); - void setInPractice( bool inPractice ); - - /** equality operator */ - bool operator==(const KEduVocLesson &other); - - - /** - * The type of this container. @see EnumContainerType - * @return - */ - KEduVocLesson::EnumContainerType containerType(); - - /** - * Set the type of container. - * For convenience by default this is taken over from the parent, so no need to set. - * @param type the new type - */ - void setContainerType(KEduVocLesson::EnumContainerType type); - private: class Private; Private * const d; diff --git a/keduvocdocument/keduvocpaukerreader.cpp b/keduvocdocument/keduvocpaukerreader.cpp index 51c8ce4..9f9de24 100644 --- a/keduvocdocument/keduvocpaukerreader.cpp +++ b/keduvocdocument/keduvocpaukerreader.cpp @@ -128,7 +128,7 @@ void KEduVocPaukerReader::readCard() } KEduVocLesson* lesson = new KEduVocLesson(i18n("Vocabulary"), m_doc->lesson()); - m_doc->lesson()->appendChildLesson(lesson); + m_doc->lesson()->appendChildContainer(lesson); KEduVocExpression* expr = new KEduVocExpression( QStringList() << front << back); lesson->addEntry( expr ); diff --git a/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp b/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp index 34a6421..9494bec 100644 --- a/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp +++ b/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp @@ -85,8 +85,8 @@ void KEduVocDocumentValidatorTest::testLessons() QString lesson3 = QString::fromLatin1( "Lesson 3" ); KEduVocDocument doc; - doc.lesson()->appendChildLesson(new KEduVocLesson(lesson1, doc.lesson())); - QCOMPARE(doc.lesson()->childLessonCount(), 1); + doc.lesson()->appendChildContainer(new KEduVocLesson(lesson1, doc.lesson())); + QCOMPARE(doc.lesson()->childContainerCount(), 1); /* QCOMPARE(doc.lesson(indexLesson1).name(), lesson1); -- 2.47.3