From: Jeremy Paul Whiting Date: Wed, 12 Sep 2007 03:23:35 +0000 (+0000) Subject: fix removeEntry and insertEntry so they update all lessons entry ids that are affecte... X-Git-Tag: v3.94.0~63 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=d99ff03cde2854a310d820f08a6e64b62d57aced;p=libqmvoc.git fix removeEntry and insertEntry so they update all lessons entry ids that are affected by insertion/removal svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=711419 --- diff --git a/keduvocdocument/keduvocdocument.cpp b/keduvocdocument/keduvocdocument.cpp index 5542014..9e92146 100644 --- a/keduvocdocument/keduvocdocument.cpp +++ b/keduvocdocument/keduvocdocument.cpp @@ -163,6 +163,13 @@ void KEduVocDocument::appendEntry( KEduVocExpression *expression ) void KEduVocDocument::insertEntry( KEduVocExpression *expression, int index ) { d->m_vocabulary.insert( index, *expression ); + + // now we need to go fix the entryids that are greater than index in the lessons + for (int i = 0; i < d->m_lessons.size(); ++i) + { + d->m_lessons[i].incrementEntriesAbove(index); + } + setModified(); } @@ -388,8 +395,6 @@ bool KEduVocDocument::saveAs( const KUrl & url, FileType ft, const QString & gen return true; } - - void KEduVocDocument::merge( KEduVocDocument *docToMerge, bool matchIdentifiers ) { kDebug() << "Merging of docs is not implemented"; /// @todo IMPLEMENT ME @@ -616,8 +621,15 @@ KEduVocExpression *KEduVocDocument::entry( int index ) void KEduVocDocument::removeEntry( int index ) { - if ( index >= 0 && index < d->m_vocabulary.size() ) + if ( index >= 0 && index < d->m_vocabulary.size() ) { d->m_vocabulary.removeAt( index ); + } + + // now we need to go fix the entryids that are greater than index in the lessons + for (int i = 0; i < d->m_lessons.size(); ++i) + { + d->m_lessons[i].decrementEntriesAbove(index); + } } diff --git a/keduvocdocument/keduvocdocument.h b/keduvocdocument/keduvocdocument.h index 77be083..a44ef1a 100644 --- a/keduvocdocument/keduvocdocument.h +++ b/keduvocdocument/keduvocdocument.h @@ -424,7 +424,7 @@ public: /** * Delete a lesson. * @param lessonIndex which lesson - * @param deleteMode either KVocDocument::DeleteEmptyLesson (delete only if empty) or KVocDocument::DeleteEntriesAndLesson (delete including vocabulary in that lesson) + * @param deleteMode either KEduVocDocument::DeleteEmptyLesson (delete only if empty) or KEduVocDocument::DeleteEntriesAndLesson (delete including vocabulary in that lesson) * @returns if the deletion was successful. If there are vocabularies in the lesson, but DeleteEmptyLesson, this will return false and not delete the lesson. */ bool deleteLesson( int lessonIndex, int deleteMode ); diff --git a/keduvocdocument/keduvoclesson.cpp b/keduvocdocument/keduvoclesson.cpp index ae7d3e3..b1d60fd 100644 --- a/keduvocdocument/keduvoclesson.cpp +++ b/keduvocdocument/keduvoclesson.cpp @@ -93,6 +93,36 @@ void KEduVocLesson::removeEntry( int entryid ) d->m_entries.remove( entryid ); } +void KEduVocLesson::incrementEntriesAbove( int entryid ) +{ + QList entries = d->m_entries.toList(); + + // increment all entry id's above entryid + for (int i = 0; i < entries.size(); ++i) { + if (entries[i] >= entryid) { + entries[i] = entries[i] + 1; + } + } + + // then put the new list into the set + d->m_entries = entries.toSet(); +} + +void KEduVocLesson::decrementEntriesAbove( int entryid ) +{ + QList entries = d->m_entries.toList(); + + // increment all entry id's above entryid + for (int i = 0; i < entries.size(); ++i) { + if (entries[i] > entryid) { + entries[i] = entries[i] - 1; + } + } + + // then put the new list into the set + d->m_entries = entries.toSet(); +} + bool KEduVocLesson::inQuery() { return d->m_inQuery; diff --git a/keduvocdocument/keduvoclesson.h b/keduvocdocument/keduvoclesson.h index eb45a92..14e6b9f 100644 --- a/keduvocdocument/keduvoclesson.h +++ b/keduvocdocument/keduvoclesson.h @@ -65,6 +65,16 @@ public: * @param entryid id of the entry to remove */ void removeEntry( int entryid ); + + /** increments all entryids > entryid, because their entryid has been incremented + * @param entryid id of the entry that was inserted + */ + void incrementEntriesAbove( int entryid ); + + /** decrements all etryids > entryid, because their entryid has been decremented + * @param entryid id of the entry that was removed + */ + void decrementEntriesAbove( int entryid ); bool inQuery(); void setInQuery( bool inQuery );