search function for all models... however...

There is some sort of problem. Somehow I sometimes get a signal 11
SEGV_MAPERR error and then it segfaults. *sigh*... So this function
needs some help yet.
This commit is contained in:
Chris Cochrun 2024-09-15 06:44:47 -05:00
parent 114ffb5bdc
commit 5b0462db65
4 changed files with 120 additions and 15 deletions

View file

@ -75,6 +75,8 @@ mod image_model {
self: Pin<&mut ImageModel>,
index: i32,
) -> QMap_QString_QVariant;
#[qinvokable]
fn search(self: Pin<&mut ImageModel>, search_term: QString);
}
impl cxx_qt::Threading for ImageModel {}
@ -161,6 +163,7 @@ use diesel::sqlite::SqliteConnection;
use diesel::{delete, insert_into, prelude::*, update};
use std::path::PathBuf;
use std::pin::Pin;
use tracing::debug;
use self::image_model::{
ImageRoles, QHash_i32_QByteArray, QMap_QString_QVariant,
@ -171,8 +174,8 @@ use self::image_model::{
/// Idk what this is but it's cool
pub struct Image {
id: i32,
title: QString,
path: QString,
title: String,
path: String,
}
#[derive(Default, Debug)]
@ -180,6 +183,7 @@ pub struct ImageModelRust {
count: i32,
highest_id: i32,
images: Vec<Image>,
inner_images: Vec<Image>,
}
impl image_model::ImageModel {
@ -211,8 +215,8 @@ impl image_model::ImageModel {
let img = self::Image {
id: image.id,
title: QString::from(&image.title),
path: QString::from(&image.path),
title: image.title,
path: image.path,
};
self.as_mut().add_image(img);
@ -245,6 +249,10 @@ impl image_model::ImageModel {
.rust_mut()
.images
.remove(index as usize);
self.as_mut()
.rust_mut()
.inner_images
.remove(index as usize);
self.as_mut().end_remove_rows();
}
println!("removed-item-at-index: {:?}", image_id);
@ -297,8 +305,8 @@ impl image_model::ImageModel {
// println!("{:?}", db);
let image = self::Image {
id: image_id,
title: image_title.clone(),
path: image_path.clone(),
title: image_title.clone().to_string(),
path: image_path.clone().to_string(),
};
println!("{:?}", image);
@ -335,7 +343,8 @@ impl image_model::ImageModel {
index,
index,
);
self.as_mut().rust_mut().images.push(image);
self.as_mut().rust_mut().images.push(image.clone());
self.as_mut().rust_mut().inner_images.push(image);
self.as_mut().end_insert_rows();
}
}
@ -364,7 +373,7 @@ impl image_model::ImageModel {
.iter_mut()
.filter(|x| x.id == index)
{
image.title = updated_title.clone();
image.title = updated_title.clone().to_string();
println!("rust-title: {:?}", image.title);
}
self.as_mut().data_changed(
@ -402,7 +411,8 @@ impl image_model::ImageModel {
.iter_mut()
.filter(|x| x.id == index)
{
image.path = updated_file_path.clone();
image.path =
updated_file_path.clone().to_string();
println!("rust-title: {:?}", image.path);
}
self.as_mut().data_changed(
@ -439,6 +449,25 @@ impl image_model::ImageModel {
qvariantmap
}
fn search(mut self: Pin<&mut Self>, search_term: QString) {
let search_term = search_term.to_string().to_lowercase();
debug!(search_term);
let searched_images: Vec<Image> = self
.inner_images
.iter()
.filter(|image| {
image.title.to_lowercase().contains(&search_term)
})
.cloned()
.collect();
debug!(search = ?&searched_images);
unsafe {
self.as_mut().begin_reset_model();
self.as_mut().rust_mut().images = searched_images;
self.as_mut().end_reset_model();
}
}
fn get_role_id(&self, role: ImageRoles) -> i32 {
match role {
ImageRoles::Id => 0,
@ -456,8 +485,12 @@ impl image_model::ImageModel {
if let Some(image) = self.images.get(index.row() as usize) {
return match role {
ImageRoles::Id => QVariant::from(&image.id),
ImageRoles::Title => QVariant::from(&image.title),
ImageRoles::Path => QVariant::from(&image.path),
ImageRoles::Title => {
QVariant::from(&QString::from(&image.title))
}
ImageRoles::Path => {
QVariant::from(&QString::from(&image.path))
}
_ => QVariant::default(),
};
}