]> Git trees. - libqmvoc.git/commitdiff
fix removeEntry and insertEntry so they update all lessons entry ids that are affecte...
authorJeremy Paul Whiting <jpwhiting@kde.org>
Wed, 12 Sep 2007 03:23:35 +0000 (03:23 +0000)
committerJeremy Paul Whiting <jpwhiting@kde.org>
Wed, 12 Sep 2007 03:23:35 +0000 (03:23 +0000)
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=711419

keduvocdocument/keduvocdocument.cpp
keduvocdocument/keduvocdocument.h
keduvocdocument/keduvoclesson.cpp
keduvocdocument/keduvoclesson.h

index 5542014fd93c718dbd87ed424bd236720a0c06fb..9e92146783b710ec04e0bdb81ce5161adeb8fe96 100644 (file)
@@ -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);
+    }    
 }
 
 
index 77be0832f8f8ac6f53e96e56f3f44a49d01cdfd3..a44ef1a0ba9b839044dca5728a04d67d8735c2fd 100644 (file)
@@ -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 );
index ae7d3e37d02beb703c8ad69bb42a22651031a52b..b1d60fd34f209c887fe1b589ef3b4748ab1904ea 100644 (file)
@@ -93,6 +93,36 @@ void KEduVocLesson::removeEntry( int entryid )
     d->m_entries.remove( entryid );
 }
 
+void KEduVocLesson::incrementEntriesAbove( int entryid )
+{
+    QList<int> 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<int> 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;
index eb45a92183c3215d58fe5475634a428b0f7fc759..14e6b9fe4ca8a0237d226af91d7465e54cd9f3a8 100644 (file)
@@ -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 );