From b32d35c385af32b910685f00d0dce6d194cb4f9f Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Tue, 21 Mar 2023 10:34:54 -0500 Subject: [PATCH] adding a slide_obj in rust This object is still unfinished, but it will later serve as our main object to connect to for the slides that are shown on screen. All of this needed some heavy tweaking and I still need to learn more about rust, but the beginnings are there and it will be worth it to have the safety and speed that rust provides --- Cargo.lock | 54 ++--------- Cargo.toml | 2 +- build.rs | 1 + flake.lock | 12 +-- src/rust/lib.rs | 1 + src/rust/slide_obj.rs | 194 +++++++++++++++++++++++++++++++++++++++ src/rust/slide_object.rs | 105 --------------------- 7 files changed, 212 insertions(+), 157 deletions(-) create mode 100644 src/rust/slide_obj.rs delete mode 100644 src/rust/slide_object.rs diff --git a/Cargo.lock b/Cargo.lock index 4dc72b6..d8588b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,41 +120,27 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7d4a3b9907daf013c9397acb5a71ddf3b2e18a0de73f85a660fe61638883392" dependencies = [ - "cxx-qt-gen 0.5.0", + "cxx-qt-gen", "proc-macro2", "syn", ] [[package]] name = "cxx-qt-build" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57ebd436cecf49c16288288e58f97092e161543360515e6ecce99f2a70e6531a" +checksum = "251a4c873b24d664eb9873b549aa07ad3162259ab02794996f39291c75e6a81c" dependencies = [ "cc", "convert_case", "cxx-gen", - "cxx-qt-gen 0.4.1", - "cxx-qt-lib-headers 0.4.1", + "cxx-qt-gen", + "cxx-qt-lib-headers", "proc-macro2", - "qt-build-utils 0.4.1", + "qt-build-utils", "quote", ] -[[package]] -name = "cxx-qt-gen" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354788c8c719c708c8793d84954e938394d1af7ae26ae2b74950f9823545346" -dependencies = [ - "clang-format", - "convert_case", - "indoc 1.0.7", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "cxx-qt-gen" version = "0.5.0" @@ -163,7 +149,7 @@ checksum = "a45da9033b061c35aef2a314b253d578d003f986faccaadb0a8e476d5dfbaa9b" dependencies = [ "clang-format", "convert_case", - "indoc 2.0.1", + "indoc", "proc-macro2", "quote", "syn", @@ -177,16 +163,10 @@ checksum = "7f45481479bf253b1d15bec29a8766e355150cba10c116e0ad4909ac1d539e12" dependencies = [ "cxx", "cxx-build", - "cxx-qt-lib-headers 0.5.0", - "qt-build-utils 0.5.0", + "cxx-qt-lib-headers", + "qt-build-utils", ] -[[package]] -name = "cxx-qt-lib-headers" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e74805ac9bd18d2907d158aa2696033c22a209146941bc3142df190d65a31414" - [[package]] name = "cxx-qt-lib-headers" version = "0.5.0" @@ -263,12 +243,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "indoc" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adab1eaa3408fb7f0c777a73e7465fd5656136fc93b670eb6df3c88c2c1344e3" - [[package]] name = "indoc" version = "2.0.1" @@ -365,16 +339,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "qt-build-utils" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44058bc241aeb84485cfb90e0f6ba28e12db0ed0f5b6eadb1853cf0ea68e21eb" -dependencies = [ - "thiserror", - "versions", -] - [[package]] name = "qt-build-utils" version = "0.5.0" diff --git a/Cargo.toml b/Cargo.toml index 5e336d8..477d2e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ cxx-qt-lib = "0.5.0" # cxx-qt-build generates C++ code from the `#[cxx_qt::bridge]` module # and compiles it together with the Rust static library [build-dependencies] -cxx-qt-build = "0.4.1" +cxx-qt-build = "0.5.0" [dependencies.confy] features = ["yaml_conf"] diff --git a/build.rs b/build.rs index b9d19c6..5af20ef 100644 --- a/build.rs +++ b/build.rs @@ -5,5 +5,6 @@ fn main() { .file("src/rust/service_thing.rs") .file("src/rust/settings.rs") .file("src/rust/file_helper.rs") + .file("src/rust/slide_obj.rs") .build(); } diff --git a/flake.lock b/flake.lock index 2f1a81d..b2c4a31 100644 --- a/flake.lock +++ b/flake.lock @@ -55,11 +55,11 @@ }, "flake-utils_2": { "locked": { - "lastModified": 1676283394, - "narHash": "sha256-XX2f9c3iySLCw54rJ/CZs+ZK6IQy7GXNY4nSOyu2QG4=", + "lastModified": 1678901627, + "narHash": "sha256-U02riOqrKKzwjsxc/400XnElV+UtPUQWpANPlyazjH0=", "owner": "numtide", "repo": "flake-utils", - "rev": "3db36a8b464d0c4532ba1c7dda728f4576d6d073", + "rev": "93a2b84fc4b70d9e089d029deacc3583435c2ed6", "type": "github" }, "original": { @@ -86,11 +86,11 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1677063315, - "narHash": "sha256-qiB4ajTeAOVnVSAwCNEEkoybrAlA+cpeiBxLobHndE8=", + "lastModified": 1679172431, + "narHash": "sha256-XEh5gIt5otaUbEAPUY5DILUTyWe1goAyeqQtmwaFPyI=", "owner": "nixos", "repo": "nixpkgs", - "rev": "988cc958c57ce4350ec248d2d53087777f9e1949", + "rev": "1603d11595a232205f03d46e635d919d1e1ec5b9", "type": "github" }, "original": { diff --git a/src/rust/lib.rs b/src/rust/lib.rs index 934ba2d..752986b 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -17,3 +17,4 @@ mod tests { mod file_helper; mod service_thing; mod settings; +mod slide_obj; diff --git a/src/rust/slide_obj.rs b/src/rust/slide_obj.rs new file mode 100644 index 0000000..36630ab --- /dev/null +++ b/src/rust/slide_obj.rs @@ -0,0 +1,194 @@ +#[cxx_qt::bridge] +mod slide_obj { + // use cxx_qt_lib::QVariantValue; + // use std::path::Path; + + unsafe extern "C++" { + include!("cxx-qt-lib/qstring.h"); + type QString = cxx_qt_lib::QString; + 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; + } + + // pub trait Slide { + // fn set_text(text: String) -> bool; + // fn set_type(ty: String) -> bool; + // fn set_audio(audio: String) -> bool; + // fn set_image_background(ib: String) -> bool; + // fn set_video_background(vb: String) -> bool; + // fn set_vtext_align(vta: String) -> bool; + // fn set_htext_align(hta: String) -> bool; + // fn set_font(font: String) -> bool; + // fn set_font_size(font_size: i32) -> bool; + // fn set_looping(lp: bool) -> bool; + // } + + #[cxx_qt::qsignals(SlideObj)] + pub enum Signals<'a> { + PlayingChanged { isPlaying: &'a bool }, + SlideIndexChanged { slideIndex: &'a i32 }, + SlideSizeChanged { slideSize: &'a i32 }, + SlideChanged { slide: &'a i32 }, + LoopChanged { looping: &'a bool }, + } + + #[derive(Clone)] + #[cxx_qt::qobject] + pub struct SlideObj { + #[qproperty] + slide_index: i32, + #[qproperty] + slide_size: i32, + #[qproperty] + image_count: i32, + #[qproperty] + is_playing: bool, + #[qproperty] + looping: bool, + #[qproperty] + text: QString, + #[qproperty] + ty: QString, + #[qproperty] + audio: QString, + #[qproperty] + image_background: QString, + #[qproperty] + video_background: QString, + #[qproperty] + vtext_alignment: QString, + #[qproperty] + htext_alignment: QString, + #[qproperty] + font: QString, + #[qproperty] + font_size: i32, + } + + impl Default for SlideObj { + fn default() -> Self { + Self { + slide_index: 0, + slide_size: 0, + is_playing: false, + looping: false, + text: QString::from(""), + ty: QString::from(""), + audio: QString::from(""), + image_background: QString::from(""), + video_background: QString::from(""), + vtext_alignment: QString::from(""), + htext_alignment: QString::from(""), + font: QString::from(""), + font_size: 50, + image_count: 0, + } + } + } + + // impl Slide for SlideObj { + // fn set_text(&self, text: String) -> bool { + // text = QString::from(text); + // true + // } + // } + + impl qobject::SlideObj { + // #[qinvokable] + // pub fn load(self: Pin<&mut Self>, file: i32) -> Vec { + // println!("{file}"); + // vec!["hi".to_string()] + // } + + #[qinvokable] + pub fn change_slide(mut self: Pin<&mut Self>, item: QMap_QString_QVariant, index: i32) { + let text = item.get(&QString::from("text")).unwrap(); + if let Some(txt) = text.value::() { + if &txt != self.as_ref().text() { + self.as_mut().set_text(txt); + }; + } + let audio = item.get(&QString::from("audio")).unwrap(); + if let Some(audio) = audio.value::() { + if &audio != self.as_ref().audio() { + self.as_mut().set_audio(audio); + } + } + let ty = item.get(&QString::from("ty")).unwrap(); + if let Some(ty) = ty.value::() { + if &ty != self.as_ref().ty() { + self.as_mut().set_ty(ty); + } + } + let image_background = item.get(&QString::from("image_background")).unwrap(); + if let Some(image_background) = image_background.value::() { + if &image_background != self.as_ref().image_background() { + self.as_mut().set_image_background(image_background); + } + } + let video_background = item + .get(&QString::from("video_background")) + .unwrap_or(QVariant::from(&QString::from(""))); + if let Some(video_background) = video_background.value::() { + if &video_background != self.as_ref().video_background() { + self.as_mut().set_video_background(video_background); + } + } + let font = item + .get(&QString::from("font")) + .unwrap_or(QVariant::from(&QString::from("Quicksand"))); + if let Some(font) = font.value::() { + if &font != self.as_ref().font() { + self.as_mut().set_font(font); + } + } + let vtext_alignment = item.get(&QString::from("vtext_alignment")).unwrap(); + if let Some(vtext_alignment) = vtext_alignment.value::() { + if &vtext_alignment != self.as_ref().vtext_alignment() { + self.as_mut().set_vtext_alignment(vtext_alignment); + } + } + let htext_alignment = item.get(&QString::from("htext_alignment")).unwrap(); + if let Some(htext_alignment) = htext_alignment.value::() { + if &htext_alignment != self.as_ref().htext_alignment() { + self.as_mut().set_htext_alignment(htext_alignment); + } + } + let font_size = item.get(&QString::from("font_size")).unwrap(); + if let Some(font_size) = font_size.value::() { + if &font_size != self.as_ref().font_size() { + self.as_mut().set_font_size(font_size); + } + } + let looping = item.get(&QString::from("looping")).unwrap(); + if let Some(looping) = looping.value::() { + if &looping != self.as_ref().looping() { + self.as_mut().set_looping(looping); + let lp = looping; + self.as_mut().emit(Signals::LoopChanged { looping: &lp }); + } + } + let slide_size = item.get(&QString::from("slide_size")).unwrap(); + if let Some(slide_size) = slide_size.value::() { + if &slide_size != self.as_ref().slide_size() { + self.as_mut().set_slide_size(slide_size); + } + } + let icount = item.get(&QString::from("imageCount")).unwrap(); + if let Some(int) = icount.value::() { + self.as_mut().set_image_count(int); + } + let slindex = item.get(&QString::from("slide_index")).unwrap(); + if let Some(int) = slindex.value::() { + self.as_mut().set_slide_index(int); + let si = int; + self.as_mut().emit(Signals::SlideChanged { slide: &si }); + }; + } + + // #[qinvokable] + // pub fn next(self: Pin<&mut Self>, next_item: QVariant, slide_model: slide_model) {} + } +} diff --git a/src/rust/slide_object.rs b/src/rust/slide_object.rs deleted file mode 100644 index e5911b2..0000000 --- a/src/rust/slide_object.rs +++ /dev/null @@ -1,105 +0,0 @@ -#[cxx_qt::bridge] -mod slide_object { - // use cxx_qt_lib::QVariantValue; - use std::path::Path; - - unsafe extern "C++" { - // include!("cxx-qt-lib/qstring.h"); - // type QString = cxx_qt_lib::QString; - // include!("cxx-qt-lib/qurl.h"); - // type QUrl = cxx_qt_lib::QUrl; - include!("cxx-qt-lib/qvariant.h"); - type QVariant = cxx_qt_lib::QVariant; - } - - pub trait Slide { - fn set_text(text: String) -> bool; - fn set_type(ty: String) -> bool; - fn set_audio(audio: String) -> bool; - fn set_image_background(ib: String) -> bool; - fn set_video_background(vb: String) -> bool; - fn set_vtext_align(vta: String) -> bool; - fn set_htext_align(hta: String) -> bool; - fn set_font(font: String) -> bool; - fn set_font_size(font_size: i32) -> bool; - fn set_looping(lp: bool) -> bool; - } - - #[derive(Clone)] - #[cxx_qt::qobject] - pub struct SlideObject { - #[qproperty] - slide_index: i32, - #[qproperty] - slide_size: i32, - #[qproperty] - is_playing: bool, - #[qproperty] - looping: bool, - #[qproperty] - text: QString, - #[qproperty] - ty: QString, - #[qproperty] - audio: QString, - #[qproperty] - image_background: QString, - #[qproperty] - video_background: QString, - #[qproperty] - vtext_alignment: QString, - #[qproperty] - htext_alignment: QString, - #[qproperty] - font: QString, - #[qproperty] - font_size: i32, - } - - impl Default for SlideObject { - fn default() -> Self { - Self { - slide_index: 0, - slide_size: 0, - is_playing: false, - looping: false, - } - } - } - - impl Slide for SlideObject { - fn set_text(&self, text: String) -> bool { - text = QString::from(text); - true - } - } - - impl qobject::SlideObject { - #[qinvokable] - pub fn load(self: Pin<&mut Self>, file: i32) -> Vec { - println!("{file}"); - vec!["hi".to_string()] - } - - #[qinvokable] - pub fn change_slide(self: Pin<&mut Self>, item: QMapPair_QString_QVariant, index: i32) { - if item.get("text").to_string() != text { - set_text(item.get("text").to_string()); - }; - let file_string = file.to_string(); - let _file_string = file_string.strip_prefix("file://"); - match _file_string { - None => { - let _exists = Path::new(&file.to_string()).exists(); - println!("{file} exists? {_exists}"); - _exists - } - Some(file) => { - let _exists = Path::new(&file).exists(); - println!("{file} exists? {_exists}"); - _exists - } - } - } - } -}