From 3c09565a120f699dc1d785ac32573b3b4d90402c Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Wed, 19 Apr 2023 15:03:32 -0500 Subject: [PATCH] bug: fixed inserting slides in the middle of songs Since songs had more than one slide, anything inserted after the first multi-slide item would break the insertion. ServiceItem indexes are not the same as the slide indexes so we needed to find the right index, then properly change the other indexes in the right spot. I think I've done this by using rust iterators better. --- src/cpp/serviceitemmodel.cpp | 4 ++-- src/rust/slide_model.rs | 37 +++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/cpp/serviceitemmodel.cpp b/src/cpp/serviceitemmodel.cpp index 41401c1..96b43e6 100644 --- a/src/cpp/serviceitemmodel.cpp +++ b/src/cpp/serviceitemmodel.cpp @@ -239,7 +239,7 @@ void ServiceItemModel::addItem(const QString &name, const QString &type, qDebug() << "*&"; qDebug() << itm; qDebug() << "*&"; - emit itemAdded(rowCount() - 1, *item); + // emit itemAdded(rowCount() - 1, *item); emit itemAddedRust(rowCount() - 1, itm); qDebug() << "EMITTED ITEM ADDED" << rowCount(); qDebug() << "#################################"; @@ -278,7 +278,7 @@ void ServiceItemModel::insertItem(const int &index, const QString &name, } else qDebug() << "idx isn't valid"; - emit itemInserted(index, *item); + // emit itemInserted(index, *item); emit itemInsertedRust(index, itm); qDebug() << "EMITTED ITEM INSERTED"; diff --git a/src/rust/slide_model.rs b/src/rust/slide_model.rs index 64c6d4a..5ed1c42 100644 --- a/src/rust/slide_model.rs +++ b/src/rust/slide_model.rs @@ -346,29 +346,40 @@ mod slide_model { .unwrap_or(false); slide.video_thumbnail = QString::from(""); - // We need to move all the current slides service_item_id's up by one. - let slides_len = self.as_mut().slides_mut().len() as i32; - for slide in index..slides_len { - if let Some(slide) = self.as_mut().slides_mut().get_mut(slide as usize) { - slide.service_item_id += 1; + let slides_iter = self.as_mut().slides_mut().iter_mut(); + let mut slide_index = 0; + for (i, slide) in slides_iter.enumerate().rev() { + if slide.service_item_id == index { + slide_index = i as i32; + break; } } + // We need to move all the current slides service_item_id's up by one. + let slides_iter = self.as_mut().slides_mut().iter_mut(); + for slide in slides_iter.filter(|x| x.service_item_id >= index) { + slide.service_item_id += 1; + } + match ty { Some(ty) if ty == QString::from("image") => { slide.ty = ty; slide.image_background = background; slide.video_background = QString::from(""); slide.slide_index = 0; - self.as_mut().insert_slide(&slide, index); + self.as_mut().insert_slide(&slide, slide_index); } Some(ty) if ty == QString::from("song") => { - for i in 0..text_vec.len() { - println!("add song of {:?} length", text_vec.len()); + let count = text_vec.len(); + for (i, text) in text_vec.iter().enumerate() { + println!( + "rust: add song of {:?} length at index {:?}", + &count, &slide_index + ); slide.ty = ty.clone(); // println!("{:?}", text_vec[i].clone()); - slide.text = text_vec[i].clone(); - slide.slide_count = text_vec.len() as i32; + slide.text = text.clone(); + slide.slide_count = count as i32; slide.slide_index = i as i32; if background_type == QString::from("image") { slide.image_background = background.clone(); @@ -377,7 +388,7 @@ mod slide_model { slide.video_background = background.clone(); slide.image_background = QString::from(""); } - self.as_mut().insert_slide(&slide, index + i as i32); + self.as_mut().insert_slide(&slide, slide_index + i as i32); } } Some(ty) if ty == QString::from("video") => { @@ -385,7 +396,7 @@ mod slide_model { slide.image_background = QString::from(""); slide.video_background = background; slide.slide_index = 0; - self.as_mut().insert_slide(&slide, index); + self.as_mut().insert_slide(&slide, slide_index); } Some(ty) if ty == QString::from("presentation") => { for i in 0..slide.slide_count { @@ -393,7 +404,7 @@ mod slide_model { slide.image_background = background.clone(); slide.video_background = QString::from(""); slide.slide_index = i; - self.as_mut().insert_slide(&slide, index + i as i32); + self.as_mut().insert_slide(&slide, slide_index + i as i32); } } _ => println!("It's somethign else!"),