From 23a24651a9c1e0e183dba7357553b9d3d5897f62 Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Sat, 17 May 2008 11:43:46 +0000 Subject: [PATCH] Implement copy, cut and paste. Vocabulary is now copied including grades, conjugations etc. Not copied are synonym, antonym and false friend. Some cleanup of the copy constructors of expression/translation to get copies right. Fix deletion of entries. Testing appreciated :) CCMAIL: parley-devel@kde.org svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=808703 --- keduvocdocument/keduvocexpression.cpp | 49 ++++++++++++++------------ keduvocdocument/keduvocexpression.h | 1 + keduvocdocument/keduvoclesson.cpp | 5 +++ keduvocdocument/keduvoctranslation.cpp | 19 +++++++--- keduvocdocument/keduvoctranslation.h | 6 +++- 5 files changed, 51 insertions(+), 29 deletions(-) diff --git a/keduvocdocument/keduvocexpression.cpp b/keduvocdocument/keduvocexpression.cpp index 37f9f07..466c0b9 100644 --- a/keduvocdocument/keduvocexpression.cpp +++ b/keduvocdocument/keduvocexpression.cpp @@ -43,29 +43,22 @@ public: QMap m_translations; }; - KEduVocExpression::KEduVocExpressionPrivate::~KEduVocExpressionPrivate() { qDeleteAll(m_translations); } - KEduVocExpression::KEduVocExpressionPrivate::KEduVocExpressionPrivate(const KEduVocExpressionPrivate & other) { m_active = other.m_active; - - foreach (int key, other.m_translations.keys()) { - m_translations[key] = new KEduVocTranslation(*other.m_translations.value(key)); - } + m_lesson = 0; } KEduVocExpression::KEduVocExpressionPrivate & KEduVocExpression::KEduVocExpressionPrivate::operator =(const KEduVocExpressionPrivate & other) { m_active = other.m_active; + m_lesson = 0; - foreach (int key, other.m_translations.keys()) { - m_translations[key] = new KEduVocTranslation(*other.m_translations.value(key)); - } return *this; } @@ -100,18 +93,28 @@ KEduVocExpression::KEduVocExpression( const QStringList & translations) KEduVocExpression::KEduVocExpression(const KEduVocExpression & other) : d(new KEduVocExpressionPrivate(*other.d)) { - d->m_lesson = 0; - if (other.lesson()) { - other.lesson()->appendEntry(this); + kDebug() << "Expression copy constructor"; + foreach (int key, other.d->m_translations.keys()) { + d->m_translations[key] = new KEduVocTranslation(*other.d->m_translations.value(key)); + kDebug() << "copy translation: " << other.d->m_translations.value(key)->text(); + d->m_translations[key]->setEntry(this); } } +KEduVocExpression& KEduVocExpression::operator= ( const KEduVocExpression &other ) +{ + *d = *other.d; + foreach (int key, other.d->m_translations.keys()) { + d->m_translations[key] = new KEduVocTranslation(*other.d->m_translations.value(key)); + kDebug() << "copy translation: " << other.d->m_translations.value(key)->text(); + d->m_translations[key]->setEntry(this); + } + return *this; +} KEduVocExpression::~KEduVocExpression() { - if (d->m_lesson) { - d->m_lesson->removeEntry(this); - } + setLesson(0); delete d; } @@ -173,14 +176,6 @@ void KEduVocExpression::resetGrades( int index ) } } - -KEduVocExpression& KEduVocExpression::operator= ( const KEduVocExpression &expression ) -{ - *d = *expression.d; - return *this; -} - - bool KEduVocExpression::operator== ( const KEduVocExpression &expression ) const { return ( *d == *expression.d ); @@ -195,6 +190,14 @@ KEduVocTranslation* KEduVocExpression::translation( int index ) return d->m_translations[index]; } +KEduVocTranslation * KEduVocExpression::translation(int index) const +{ + if(d->m_translations.contains(index)) { + return 0; + } + return d->m_translations[index]; +} + QList< int > KEduVocExpression::translationIndices() const { return d->m_translations.keys(); diff --git a/keduvocdocument/keduvocexpression.h b/keduvocdocument/keduvocexpression.h index 4369bb8..373033a 100644 --- a/keduvocdocument/keduvocexpression.h +++ b/keduvocdocument/keduvocexpression.h @@ -102,6 +102,7 @@ public: * @return the translation */ KEduVocTranslation* translation( int index ); + KEduVocTranslation* translation( int index ) const; QList translationIndices() const; diff --git a/keduvocdocument/keduvoclesson.cpp b/keduvocdocument/keduvoclesson.cpp index 77e2ffe..2e525c1 100644 --- a/keduvocdocument/keduvoclesson.cpp +++ b/keduvocdocument/keduvoclesson.cpp @@ -19,6 +19,7 @@ #include "keduvocexpression.h" #include +#include #include /** private class to store information about a lesson */ @@ -81,6 +82,10 @@ void KEduVocLesson::insertEntry(int index, KEduVocExpression * entry) void KEduVocLesson::removeEntry(KEduVocExpression* entry) { Q_ASSERT(entry); + if (d->m_entries.indexOf(entry) == -1) { + kDebug() << "attempting to remove entry from lesson that does not contain it!"; + return; + } d->m_entries.removeAt( d->m_entries.indexOf(entry) ); invalidateChildLessonEntries(); } diff --git a/keduvocdocument/keduvoctranslation.cpp b/keduvocdocument/keduvoctranslation.cpp index 6cc4ff7..f219a6e 100644 --- a/keduvocdocument/keduvoctranslation.cpp +++ b/keduvocdocument/keduvoctranslation.cpp @@ -97,9 +97,12 @@ KEduVocTranslation::KEduVocTranslation(KEduVocExpression* entry, const QString & KEduVocTranslation::KEduVocTranslation( const KEduVocTranslation &other ) : KEduVocText(other), - d( new KEduVocTranslationPrivate(other.d->m_entry) ) + // set the entry to 0, the translation will be put into a copied entry by the expression copy constructor + d( new KEduVocTranslationPrivate(0) ) { - d->m_wordType = other.d->m_wordType; + // beter no word type copy as this is pointer copying + // will not work as this is not added to the word type container! +// d->m_wordType = other.d->m_wordType; d->m_comment = other.d->m_comment; d->m_paraphrase = other.d->m_paraphrase; d->m_example = other.d->m_example; @@ -110,9 +113,10 @@ KEduVocTranslation::KEduVocTranslation( const KEduVocTranslation &other ) d->m_multipleChoice = other.d->m_multipleChoice; d->m_imageUrl = other.d->m_imageUrl; d->m_soundUrl = other.d->m_soundUrl; - d->m_synonyms = other.d->m_synonyms; - d->m_antonyms = other.d->m_antonyms; - d->m_falseFriends = other.d->m_falseFriends; +// no copies of the following for now. we don't know enough to also add this as synonym/etc +// d->m_synonyms = other.d->m_synonyms; +// d->m_antonyms = other.d->m_antonyms; +// d->m_falseFriends = other.d->m_falseFriends; if (other.d->m_declension) { d->m_declension = new KEduVocDeclension(*other.d->m_declension); } @@ -439,3 +443,8 @@ void KEduVocTranslation::fromKVTML2(QDomElement & parent) ///@todo false friends } +void KEduVocTranslation::setEntry(KEduVocExpression * entry) +{ + d->m_entry = entry; +} + diff --git a/keduvocdocument/keduvoctranslation.h b/keduvocdocument/keduvoctranslation.h index ddaab1e..f44c692 100644 --- a/keduvocdocument/keduvoctranslation.h +++ b/keduvocdocument/keduvoctranslation.h @@ -53,7 +53,7 @@ public: */ KEduVocTranslation(KEduVocExpression* entry, const QString &translation ); - /** copy constructor for d-pointer safet */ + /** copy constructor for d-pointer safety */ KEduVocTranslation( const KEduVocTranslation &other ); /** @@ -278,6 +278,10 @@ public: private: class KEduVocTranslationPrivate; KEduVocTranslationPrivate* const d; + + // for the copy constructor + void setEntry(KEduVocExpression* entry); + friend class KEduVocExpression; }; #endif -- 2.47.3