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.
This commit is contained in:
Chris Cochrun 2024-09-17 06:23:27 -05:00
parent 0cac11cb89
commit 273dd76d23
4 changed files with 182 additions and 37 deletions

View file

@ -14,4 +14,5 @@ pub mod songs;
pub mod utils; pub mod utils;
pub mod video_model; pub mod video_model;
pub mod ytdl; pub mod ytdl;
pub mod slide_types;
// mod video_thumbnail; // mod video_thumbnail;

View file

@ -310,13 +310,14 @@ use self::service_item_model::{
QHash_i32_QByteArray, QMap_QString_QVariant, QVector_i32, QHash_i32_QByteArray, QMap_QString_QVariant, QVector_i32,
ServiceRoles, ServiceRoles,
}; };
use crate::slide_types::SlideType;
use super::service_item_model::service_item_model::ServiceItemModel; use super::service_item_model::service_item_model::ServiceItemModel;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ServiceItem { pub struct ServiceItem {
name: QString, name: QString,
ty: QString, ty: SlideType,
audio: QString, audio: QString,
background: QString, background: QString,
background_type: QString, background_type: QString,
@ -329,21 +330,14 @@ pub struct ServiceItem {
looping: bool, looping: bool,
video_start_time: f32, video_start_time: f32,
video_end_time: f32, video_end_time: f32,
obs_scene: QString,
id: i32, id: i32,
} }
impl ServiceItem {
fn debug(self) {
debug!(?self);
}
}
impl Default for ServiceItem { impl Default for ServiceItem {
fn default() -> Self { fn default() -> Self {
Self { Self {
name: QString::default(), name: QString::default(),
ty: QString::default(), ty: SlideType::Image,
audio: QString::default(), audio: QString::default(),
background: QString::default(), background: QString::default(),
background_type: QString::default(), background_type: QString::default(),
@ -356,7 +350,6 @@ impl Default for ServiceItem {
looping: false, looping: false,
video_start_time: 0.0, video_start_time: 0.0,
video_end_time: 0.0, video_end_time: 0.0,
obs_scene: QString::default(),
id: 0, id: 0,
} }
} }
@ -455,7 +448,7 @@ impl service_item_model::ServiceItemModel {
) { ) {
let service_item = ServiceItem { let service_item = ServiceItem {
name, name,
ty, ty: ty.try_into().unwrap(),
text, text,
background, background,
background_type, background_type,
@ -515,7 +508,7 @@ impl service_item_model::ServiceItemModel {
) { ) {
let service_item = ServiceItem { let service_item = ServiceItem {
name, name,
ty, ty: ty.try_into().unwrap(),
text, text,
background, background,
background_type, background_type,
@ -841,7 +834,8 @@ impl service_item_model::ServiceItemModel {
debug!(activating_item = index, debug!(activating_item = index,
title = ?service_item.name, title = ?service_item.name,
background = ?service_item.background, background = ?service_item.background,
background_type = ?service_item.background_type); background_type = ?service_item.background_type,
kind = ?service_item.ty);
service_item.active = true; service_item.active = true;
// if let Some(obs) = obs { // if let Some(obs) = obs {
// match 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()), 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()), "audio".to_owned(): Value::from(item.audio.to_string()),
"background".to_owned(): Value::from(item.background.to_string()), "background".to_owned(): Value::from(item.background.to_string()),
"backgroundType".to_owned(): Value::from(item.background_type.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()); println!("size: {:?}", file.size());
if !file_path.exists() { if !file_path.exists() {
match file.unpack_in(&datadir) { match file.unpack_in(&datadir) {
Ok(t) => (), Ok(_t) => (),
Err(e) => error!("Error unpacking archive: {}", e), Err(e) => error!("Error unpacking archive: {}", e),
} }
} }
@ -1326,25 +1320,46 @@ impl service_item_model::ServiceItemModel {
.append(QString::from(txt.as_str().unwrap())); .append(QString::from(txt.as_str().unwrap()));
} }
let text = QStringList::from(&text_list); let text = QStringList::from(&text_list);
if let Ok(ty) = ty.try_into() {
let service_item = ServiceItem { let service_item = ServiceItem {
name, name,
ty, ty,
text, text,
background, background,
background_type, background_type,
audio, audio,
font, font,
font_size, font_size,
slide_count, slide_count,
looping, looping,
video_start_time, video_start_time,
video_end_time, video_end_time,
..Default::default() ..Default::default()
}; };
self.as_mut().add_service_item(&service_item); self.as_mut().add_service_item(&service_item);
println!("Loaded Service: {:?}", ds); println!("Loaded Service: {:?}", ds);
// // files implement the Read trait // // 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 true
} else { } else {
@ -1393,7 +1408,7 @@ impl service_item_model::ServiceItemModel {
QVariant::from(&service_item.name) QVariant::from(&service_item.name)
} }
ServiceRoles::Type => { ServiceRoles::Type => {
QVariant::from(&service_item.ty) QVariant::from(&QString::from(&service_item.ty.clone().to_string()))
} }
ServiceRoles::Audio => { ServiceRoles::Audio => {
QVariant::from(&service_item.audio) QVariant::from(&service_item.audio)

View file

@ -6,8 +6,8 @@ mod slide_object {
include!("cxx-qt-lib/qmap.h"); include!("cxx-qt-lib/qmap.h");
type QMap_QString_QVariant = type QMap_QString_QVariant =
cxx_qt_lib::QMap<cxx_qt_lib::QMapPair_QString_QVariant>; cxx_qt_lib::QMap<cxx_qt_lib::QMapPair_QString_QVariant>;
include!("cxx-qt-lib/qvariant.h"); // include!("cxx-qt-lib/qvariant.h");
type QVariant = cxx_qt_lib::QVariant; // type QVariant = cxx_qt_lib::QVariant;
// #[cxx_name = "SlideModel"] // #[cxx_name = "SlideModel"]
// type SlideModel = crate::slide_model::SlideModelRust; // type SlideModel = crate::slide_model::SlideModelRust;
} }
@ -88,7 +88,12 @@ use std::pin::Pin;
use cxx_qt::CxxQtType; use cxx_qt::CxxQtType;
use cxx_qt_lib::{CaseSensitivity, QString, QVariant}; 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; use self::slide_object::QMap_QString_QVariant;
@ -145,6 +150,38 @@ impl slide_object::SlideObject {
item: QMap_QString_QVariant, item: QMap_QString_QVariant,
slide_index: i32, 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 _current_index = self.as_ref().get_ref().slide_index();
let icount_variant = item let icount_variant = item
.get(&QString::from("imageCount")) .get(&QString::from("imageCount"))

92
src/rust/slide_types.rs Normal file
View file

@ -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<String> for SlideType {
type Error = ParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
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<QString> for SlideType {
type Error = ParseError;
fn try_from(value: QString) -> Result<Self, Self::Error> {
value.to_string().try_into()
}
}
impl From<SlideType> 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}")
}
}