From 273dd76d23d8594bf8650aa676cfde2bcddb6347 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Tue, 17 Sep 2024 06:23:27 -0500 Subject: [PATCH] adding a SlideType enum that can parse from a string This will start to allow us to have a much better data model for the slides and service items. They can have a type associated with them and parse which they are from this type. --- src/rust/lib.rs | 1 + src/rust/service_item_model.rs | 83 +++++++++++++++++------------- src/rust/slide_object.rs | 43 ++++++++++++++-- src/rust/slide_types.rs | 92 ++++++++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+), 37 deletions(-) create mode 100644 src/rust/slide_types.rs diff --git a/src/rust/lib.rs b/src/rust/lib.rs index 50d46b5..aaccd4d 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -14,4 +14,5 @@ pub mod songs; pub mod utils; pub mod video_model; pub mod ytdl; +pub mod slide_types; // mod video_thumbnail; diff --git a/src/rust/service_item_model.rs b/src/rust/service_item_model.rs index 6d0bc3a..b9305f7 100644 --- a/src/rust/service_item_model.rs +++ b/src/rust/service_item_model.rs @@ -310,13 +310,14 @@ use self::service_item_model::{ QHash_i32_QByteArray, QMap_QString_QVariant, QVector_i32, ServiceRoles, }; +use crate::slide_types::SlideType; use super::service_item_model::service_item_model::ServiceItemModel; #[derive(Clone, Debug)] pub struct ServiceItem { name: QString, - ty: QString, + ty: SlideType, audio: QString, background: QString, background_type: QString, @@ -329,21 +330,14 @@ pub struct ServiceItem { looping: bool, video_start_time: f32, video_end_time: f32, - obs_scene: QString, id: i32, } -impl ServiceItem { - fn debug(self) { - debug!(?self); - } -} - impl Default for ServiceItem { fn default() -> Self { Self { name: QString::default(), - ty: QString::default(), + ty: SlideType::Image, audio: QString::default(), background: QString::default(), background_type: QString::default(), @@ -356,7 +350,6 @@ impl Default for ServiceItem { looping: false, video_start_time: 0.0, video_end_time: 0.0, - obs_scene: QString::default(), id: 0, } } @@ -455,7 +448,7 @@ impl service_item_model::ServiceItemModel { ) { let service_item = ServiceItem { name, - ty, + ty: ty.try_into().unwrap(), text, background, background_type, @@ -515,7 +508,7 @@ impl service_item_model::ServiceItemModel { ) { let service_item = ServiceItem { name, - ty, + ty: ty.try_into().unwrap(), text, background, background_type, @@ -841,7 +834,8 @@ impl service_item_model::ServiceItemModel { debug!(activating_item = index, title = ?service_item.name, background = ?service_item.background, - background_type = ?service_item.background_type); + background_type = ?service_item.background_type, + kind = ?service_item.ty); service_item.active = true; // if let Some(obs) = obs { // match obs @@ -993,7 +987,7 @@ impl service_item_model::ServiceItemModel { } let item_json = json!({"name".to_owned(): Value::from(item.name.to_string()), - "type".to_owned(): Value::from(item.ty.to_string()), + "type".to_owned(): Value::from(item.ty.clone().to_string()), "audio".to_owned(): Value::from(item.audio.to_string()), "background".to_owned(): Value::from(item.background.to_string()), "backgroundType".to_owned(): Value::from(item.background_type.to_string()), @@ -1156,7 +1150,7 @@ impl service_item_model::ServiceItemModel { println!("size: {:?}", file.size()); if !file_path.exists() { match file.unpack_in(&datadir) { - Ok(t) => (), + Ok(_t) => (), Err(e) => error!("Error unpacking archive: {}", e), } } @@ -1326,25 +1320,46 @@ impl service_item_model::ServiceItemModel { .append(QString::from(txt.as_str().unwrap())); } let text = QStringList::from(&text_list); + if let Ok(ty) = ty.try_into() { - let service_item = ServiceItem { - name, - ty, - text, - background, - background_type, - audio, - font, - font_size, - slide_count, - looping, - video_start_time, - video_end_time, - ..Default::default() - }; - self.as_mut().add_service_item(&service_item); - println!("Loaded Service: {:?}", ds); - // // files implement the Read trait + let service_item = ServiceItem { + name, + ty, + text, + background, + background_type, + audio, + font, + font_size, + slide_count, + looping, + video_start_time, + video_end_time, + ..Default::default() + }; + self.as_mut().add_service_item(&service_item); + println!("Loaded Service: {:?}", ds); + // // files implement the Read trait + } else { + let service_item = ServiceItem { + name, + ty: SlideType::Content, + text, + background, + background_type, + audio, + font, + font_size, + slide_count, + looping, + video_start_time, + video_end_time, + ..Default::default() + }; + self.as_mut().add_service_item(&service_item); + error!("Loaded service item with generic type since it was an unknown type."); + // // files implement the Read trait + } } true } else { @@ -1393,7 +1408,7 @@ impl service_item_model::ServiceItemModel { QVariant::from(&service_item.name) } ServiceRoles::Type => { - QVariant::from(&service_item.ty) + QVariant::from(&QString::from(&service_item.ty.clone().to_string())) } ServiceRoles::Audio => { QVariant::from(&service_item.audio) diff --git a/src/rust/slide_object.rs b/src/rust/slide_object.rs index 84b848c..3142d38 100644 --- a/src/rust/slide_object.rs +++ b/src/rust/slide_object.rs @@ -6,8 +6,8 @@ mod slide_object { include!("cxx-qt-lib/qmap.h"); type QMap_QString_QVariant = cxx_qt_lib::QMap; - include!("cxx-qt-lib/qvariant.h"); - type QVariant = cxx_qt_lib::QVariant; + // include!("cxx-qt-lib/qvariant.h"); + // type QVariant = cxx_qt_lib::QVariant; // #[cxx_name = "SlideModel"] // type SlideModel = crate::slide_model::SlideModelRust; } @@ -88,7 +88,12 @@ use std::pin::Pin; use cxx_qt::CxxQtType; use cxx_qt_lib::{CaseSensitivity, QString, QVariant}; -use tracing::debug; +use tracing::{debug, error}; + +use crate::{ + slide_types::{PresType, SlideType}, + songs::song_model::{song_model::SongModel, SongModelRust}, +}; use self::slide_object::QMap_QString_QVariant; @@ -145,6 +150,38 @@ impl slide_object::SlideObject { item: QMap_QString_QVariant, slide_index: i32, ) { + // This function will grab the item from the relevant model + // and load it into the slide_object + let kind = item + .get(&QString::from("type")) + .unwrap_or(QVariant::from(&QString::default())) + .value() + .unwrap_or(QString::default()); + match SlideType::try_from(kind) { + Ok(st) => { + debug!(kind = ?st, "Changing slide"); + // let slide = match st { + // SlideType::Song => { + // SongModel::get_item(slide_index) + // } + // SlideType::Image => { + // ImageModel::get_item(slide_index) + // } + // SlideType::Video => { + // VideoModel::get_item(slide_index) + // } + // SlideType::Presentation(_) => { + // PresentationModel::get_item(slide_index) + // } + // SlideType::Content => { + // PresentationModel::get_item(slide_index) + // } + // }; + // debug!(slide); + } + Err(e) => error!(?e), + } + let _current_index = self.as_ref().get_ref().slide_index(); let icount_variant = item .get(&QString::from("imageCount")) diff --git a/src/rust/slide_types.rs b/src/rust/slide_types.rs new file mode 100644 index 0000000..d413915 --- /dev/null +++ b/src/rust/slide_types.rs @@ -0,0 +1,92 @@ +use std::{error::Error, fmt::Display}; + +use cxx_qt_lib::QString; + +#[derive(Debug, Clone)] +pub enum PresType { + Html, + Pdf, + Generic, +} + +#[derive(Debug, Clone)] +pub enum SlideType { + Song, + Video, + Image, + Presentation(PresType), + Content, +} + +impl std::fmt::Display for SlideType { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let s = match self { + Self::Song => "song".to_owned(), + Self::Image => "image".to_owned(), + Self::Video => "video".to_owned(), + Self::Presentation(PresType::Html) => "html".to_owned(), + Self::Presentation(PresType::Pdf) => "pdf".to_owned(), + Self::Presentation(PresType::Generic) => { + "presentation".to_owned() + } + Self::Content => "content".to_owned(), + }; + write!(f, "{s}") + } +} + +impl TryFrom for SlideType { + type Error = ParseError; + fn try_from(value: String) -> Result { + match value.as_str() { + "song" => Ok(Self::Song), + "image" => Ok(Self::Image), + "video" => Ok(Self::Video), + "presentation" => { + Ok(Self::Presentation(PresType::Generic)) + } + "html" => Ok(Self::Presentation(PresType::Html)), + "pdf" => Ok(Self::Presentation(PresType::Pdf)), + "content" => Ok(Self::Content), + _ => Err(ParseError::UnknownType), + } + } +} + +impl TryFrom for SlideType { + type Error = ParseError; + fn try_from(value: QString) -> Result { + value.to_string().try_into() + } +} + +impl From for String { + fn from(val: SlideType) -> String { + match val { + SlideType::Song => "song".to_owned(), + SlideType::Video => "video".to_owned(), + SlideType::Image => "image".to_owned(), + SlideType::Presentation(_) => "presentation".to_owned(), + SlideType::Content => "content".to_owned(), + } + } +} + +#[derive(Debug)] +pub enum ParseError { + UnknownType, +} + +impl Error for ParseError {} + +impl Display for ParseError { + 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}") + } +}