From 2c014e242fd3e100ba625eb1eb156e597aa7b1ec Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Wed, 2 Oct 2024 14:48:16 -0500 Subject: [PATCH] Refactoring to slide_model and service_item getting the data from db This is kinda a broken commit. There isn't any order to the adding of the slide anymore. So it'll need to find a way of keeping track of where all the current slides are and then insert the new ones in the correct order while moving the others around as well. --- src/main.cpp | 5 +- .../presenter/PreviewSlideListDelegate.qml | 12 +- src/qml/presenter/ServiceList.qml | 36 +- src/rust/image_model.rs | 22 +- src/rust/presentation_model.rs | 26 +- src/rust/service_item_model.rs | 65 +-- src/rust/slide_model.rs | 545 ++++++------------ src/rust/songs/song_model.rs | 211 +++---- src/rust/video_model.rs | 29 +- 9 files changed, 347 insertions(+), 604 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f5a834e..656ad12 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -142,10 +142,7 @@ int main(int argc, char *argv[]) if (!serviceItemModel.get()->load(settings->getLastSaveFile())) { qDebug() << "Last saved file is missing or there isn't a last saved file."; - serviceItemModel.get()->addItem(QString::fromUtf8("Black"), QString::fromUtf8("image"), - QString::fromUtf8("qrc:/assets/black.jpg"), - QString::fromUtf8("image"), QStringList() << QStringLiteral(""), - QString::fromUtf8(""), QString::fromUtf8(""), 0, 1, false, 0, 0, 0); + serviceItemModel.get()->addItem(QString::fromUtf8("Black"), QString::fromUtf8("image"), 0); } // apparently mpv needs this class set diff --git a/src/qml/presenter/PreviewSlideListDelegate.qml b/src/qml/presenter/PreviewSlideListDelegate.qml index bc34a4e..df9718a 100644 --- a/src/qml/presenter/PreviewSlideListDelegate.qml +++ b/src/qml/presenter/PreviewSlideListDelegate.qml @@ -50,12 +50,12 @@ Item { chosenFont: model.font text: model.text pdfIndex: model.slideIndex - Connections { - target: slideModel - function onDataChanged() { - Utils.dbg("hi") - } - } + /* Connections { */ + /* target: slideModel */ + /* function onDataChanged() { */ + /* Utils.dbg("hi") */ + /* } */ + /* } */ } /* WebEngineView { */ diff --git a/src/qml/presenter/ServiceList.qml b/src/qml/presenter/ServiceList.qml index f88bb8a..de3b1d6 100644 --- a/src/qml/presenter/ServiceList.qml +++ b/src/qml/presenter/ServiceList.qml @@ -559,9 +559,7 @@ Item { const image = imageModel.getItem(itemIndex); console.log("adding: " + image.title + " of type " + type); ServiceItemModel.insertItem(index, image.title, - "", type, image.filePath, - "image", "", - "", 0, 0, false, 0.0, 0.0, itemIndex); + type, image.id); serviceItemList.forceLayout() return; } @@ -569,9 +567,7 @@ Item { const video = videoModel.getItem(itemIndex); console.log("adding: " + video.title + " of type " + type); ServiceItemModel.insertItem(index, video.title, - "", type, video.filePath, - "video", "", - "", 0, 0, video.loop, video.startTime, video.endTime, itemIndex); + type, video.id); serviceItemList.forceLayout() return; } @@ -583,10 +579,7 @@ Item { " of type " + type + " with " + lyrics.length + " slides"); ServiceItemModel.insertItem(index, song.title, - lyrics, type, song.background, - song.backgroundType, - song.audio, song.font, song.fontSize, - lyrics.length, true, 0.0, 0.0, itemIndex); + type, song.id); serviceItemList.forceLayout() return; } @@ -595,10 +588,7 @@ Item { console.log("adding: " + pres.title + " of type " + type + " with " + pres.pageCount + " slides"); - ServiceItemModel.insertItem(index, pres.title, - "", type, pres.filePath, - "image", - "", "", 0, pres.pageCount, false, 0.0, 0.0, itemIndex); + ServiceItemModel.insertItem(index, pres.title, type, pres.id); serviceItemList.forceLayout() return; } @@ -614,9 +604,7 @@ Item { const image = imageModel.getItem(itemIndex); console.log("adding: " + image.title + " of type " + type); ServiceItemModel.addItem(image.title, - type, image.filePath, - "image", "", "", - "", 0, 0, false, 0.0, 0.0, itemIndex); + type, image.id); serviceItemList.forceLayout() return; } @@ -624,9 +612,7 @@ Item { const video = videoModel.getItem(itemIndex); console.log("adding: " + video.title + " of type " + type); ServiceItemModel.addItem(video.title, - type, video.filePath, - "video", "", "", - "", 0, 0, video.loop, video.startTime, video.endTime, itemIndex); + type, video.id); serviceItemList.forceLayout() return; } @@ -637,10 +623,7 @@ Item { " of type " + type + " with " + lyrics.length + " slides"); ServiceItemModel.addItem(song.title, - type, song.background, - song.backgroundType, lyrics, - song.audio, song.font, song.fontSize, - lyrics.length, true, 0.0, 0.0, itemIndex); + type, song.id); serviceItemList.forceLayout() return; } @@ -650,10 +633,7 @@ Item { " of type " + type + " with " + pres.pageCount + " slides"); ServiceItemModel.addItem(pres.title, - type, pres.filePath, - "image", "", - "", "", 0, pres.pageCount, - false, 0.0, 0.0, itemIndex); + type, pres.id); serviceItemList.forceLayout() return; } diff --git a/src/rust/image_model.rs b/src/rust/image_model.rs index abbd946..61ad142 100644 --- a/src/rust/image_model.rs +++ b/src/rust/image_model.rs @@ -172,7 +172,7 @@ use self::image_model::{ pub struct Image { id: i32, title: String, - path: String, + pub path: String, } #[derive(Debug)] @@ -206,10 +206,22 @@ impl Default for ImageModelRust { } } -impl ImageModelRust { - pub fn get_image(index: i32) -> Option<&Image> { - todo!() - } +pub fn get_image(index: i32) -> color_eyre::Result { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut db = { + let mut data = dirs::data_local_dir().unwrap(); + data.push("lumina"); + data.push("library-db.sqlite3"); + let mut db_url = String::from("sqlite://"); + db_url.push_str(data.to_str().unwrap()); + rt.block_on(async { + SqliteConnection::connect(&db_url).await.expect("problems") + }) + }; + rt.block_on(async { + let result = query_as!(Image, r#"SELECT id as "id: i32", title as "title!", filePath as "path!" from images where id = ?"#, index).fetch_one(&mut db).await?; + Ok(result) + }) } impl image_model::ImageModel { diff --git a/src/rust/presentation_model.rs b/src/rust/presentation_model.rs index bbd2884..1ea2dcc 100644 --- a/src/rust/presentation_model.rs +++ b/src/rust/presentation_model.rs @@ -197,9 +197,9 @@ use self::presentation_model::{ pub struct Presentation { id: i32, title: String, - html: bool, - path: String, - page_count: i32, + pub html: bool, + pub path: String, + pub page_count: i32, } #[derive(Debug)] @@ -233,10 +233,22 @@ impl Default for PresentationModelRust { } } -impl PresentationModelRust { - pub fn get_presentation(index: i32) -> Presentation { - todo!() - } +pub fn get_presentation(index: i32) -> color_eyre::Result { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut db = { + let mut data = dirs::data_local_dir().unwrap(); + data.push("lumina"); + data.push("library-db.sqlite3"); + let mut db_url = String::from("sqlite://"); + db_url.push_str(data.to_str().unwrap()); + rt.block_on(async { + SqliteConnection::connect(&db_url).await.expect("problems") + }) + }; + rt.block_on(async { + let result = query_as!(Presentation, r#"SELECT id as "id: i32", title as "title!", filePath as "path!", html as "html!", pageCount as "page_count!: i32" from presentations where id = ?"#, index).fetch_one(&mut db).await?; + Ok(result) + }) } impl presentation_model::PresentationModel { diff --git a/src/rust/service_item_model.rs b/src/rust/service_item_model.rs index f9325bf..d2178cc 100644 --- a/src/rust/service_item_model.rs +++ b/src/rust/service_item_model.rs @@ -78,7 +78,6 @@ mod service_item_model { #[qsignal] fn item_added( self: Pin<&mut ServiceItemModel>, - index: &i32, id: &i32, kind: &QString, ); @@ -122,16 +121,6 @@ mod service_item_model { self: Pin<&mut ServiceItemModel>, name: QString, ty: QString, - background: QString, - background_type: QString, - text: QStringList, - audio: QString, - font: QString, - font_size: i32, - slide_count: i32, - looping: bool, - video_start_time: f32, - video_end_time: f32, id: i32, ); @@ -140,17 +129,7 @@ mod service_item_model { self: Pin<&mut ServiceItemModel>, index: i32, name: QString, - text: QStringList, ty: QString, - background: QString, - background_type: QString, - audio: QString, - font: QString, - font_size: i32, - slide_count: i32, - looping: bool, - video_start_time: f32, - video_end_time: f32, id: i32, ); @@ -423,31 +402,11 @@ impl service_item_model::ServiceItemModel { mut self: Pin<&mut Self>, name: QString, ty: QString, - background: QString, - background_type: QString, - text: QStringList, - audio: QString, - font: QString, - font_size: i32, - slide_count: i32, - looping: bool, - video_start_time: f32, - video_end_time: f32, id: i32, ) { let service_item = ServiceItem { name, ty: ty.try_into().unwrap(), - text, - background, - background_type, - audio, - font, - font_size, - slide_count, - looping, - video_start_time, - video_end_time, id, ..Default::default() }; @@ -474,39 +433,19 @@ impl service_item_model::ServiceItemModel { self.as_mut().end_insert_rows(); } debug!("ADDING: {:?}", &service_item); - self.as_mut().item_added(&index, &service_item.id, &service_item.ty.to_string()); + self.as_mut().item_added(&service_item.id, &QString::from(&service_item.ty.to_string())); } pub fn insert_item( mut self: Pin<&mut Self>, index: i32, name: QString, - text: QStringList, ty: QString, - background: QString, - background_type: QString, - audio: QString, - font: QString, - font_size: i32, - slide_count: i32, - looping: bool, - video_start_time: f32, - video_end_time: f32, id: i32, ) { let service_item = ServiceItem { name, ty: ty.try_into().unwrap(), - text, - background, - background_type, - audio, - font, - font_size, - slide_count, - looping, - video_start_time, - video_end_time, id, ..Default::default() }; @@ -533,7 +472,7 @@ impl service_item_model::ServiceItemModel { self.as_mut().end_insert_rows(); } debug!("ADDING: {:?}", &service_item); - self.as_mut().item_inserted(&index, &service_item.id, &service_item.ty.to_string()); + self.as_mut().item_inserted(&index, &service_item.id, &QString::from(&service_item.ty.to_string())); } pub fn get_item( diff --git a/src/rust/slide_model.rs b/src/rust/slide_model.rs index 7791370..6072a1c 100644 --- a/src/rust/slide_model.rs +++ b/src/rust/slide_model.rs @@ -101,17 +101,16 @@ pub mod slide_model { fn insert_item_from_service( self: Pin<&mut SlideModel>, index: i32, - service_item: &QMap_QString_QVariant, - id: i32, - ); + item_model_id: i32, + kind: &QString, + ) -> Result<()>; #[qinvokable] fn add_item_from_service( self: Pin<&mut SlideModel>, - index: i32, - service_item: &QMap_QString_QVariant, - id: i32, - ); + item_model_id: i32, + kind: &QString, + ) -> Result<()>; #[qinvokable] fn move_item_from_service( @@ -229,20 +228,25 @@ pub mod slide_model { } use crate::image_model::image_model::ImageModel; +use crate::image_model::{self, Image, ImageModelRust}; use crate::presentation_model::presentation_model::PresentationModel; -use crate::songs::song_model::song_model::SongModel; -use crate::songs::song_model::SongModelRust; +use crate::presentation_model::{self, Presentation, PresentationModelRust}; +use crate::songs::song_model::song_model::{self, SongModel}; +use crate::songs::song_model::{get_song, Song, SongModelRust}; use crate::video_model::video_model::VideoModel; -use crate::video_model::VideoModelRust; +use crate::video_model::{self, Video, VideoModelRust}; use crate::{ffmpeg, slide_types::SlideType}; use crate::obs::Obs; use crate::slide_model::slide_model::QList_QString; -use color_eyre::owo_colors::OwoColorize; +use color_eyre::eyre::Result; +use color_eyre::Section; use cxx_qt::{CxxQtType, Threading}; use cxx_qt_lib::{ - CaseSensitivity, QByteArray, QModelIndex, QString, QStringList, QVariant + CaseSensitivity, QByteArray, QList, QModelIndex, QString, QStringList, QVariant }; use slide_model::SlideObject; +use std::error::Error; +use std::fmt::{Display}; use std::{path::PathBuf, pin::Pin}; use tracing::{debug, error}; @@ -302,6 +306,111 @@ impl Default for Slide { } } +#[derive(Debug)] +pub enum ParseSlideError { + UnknownType, +} + +impl Error for ParseSlideError {} + +impl Display for ParseSlideError { + fn fmt( + &self, + f: &mut std::fmt::Formatter<'_>, + ) -> std::fmt::Result { + let message = match self { + Self::UnknownType => "The type does not exist. It needs to be one of 'song', 'video', 'image', 'presentation', or 'content'", + }; + write!(f, "Error: {message}") + } +} + +impl TryFrom