slides, settings, and file stuff ported

This commit is contained in:
Chris Cochrun 2023-11-21 17:31:19 -06:00
parent 5d571a7e6f
commit 81b430e768
5 changed files with 1564 additions and 1459 deletions

View file

@ -2,10 +2,6 @@
// of whether or not a file exists // of whether or not a file exists
#[cxx_qt::bridge] #[cxx_qt::bridge]
mod file_helper { mod file_helper {
use rfd::FileDialog;
use std::path::Path;
use tracing::{debug, debug_span, error, info, instrument};
unsafe extern "C++" { unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h"); include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString; type QString = cxx_qt_lib::QString;
@ -14,45 +10,57 @@ mod file_helper {
include!("cxx-qt-lib/qvariant.h"); include!("cxx-qt-lib/qvariant.h");
type QVariant = cxx_qt_lib::QVariant; type QVariant = cxx_qt_lib::QVariant;
} }
unsafe extern "RustQt" {
#[qobject]
#[qml_element]
#[qproperty(QString, name)]
#[qproperty(QString, file_path)]
type FileHelper = super::FileHelperRust;
#[derive(Clone)] #[qinvokable]
#[cxx_qt::qobject] fn load(
pub struct FileHelper { self: Pin<&mut FileHelper>,
#[qproperty] file: QUrl,
name: QString, ) -> Vec<String>;
#[qproperty]
file_path: QString, #[qinvokable]
fn validate(self: Pin<&mut FileHelper>, file: QUrl) -> bool;
#[qinvokable]
fn save_file(self: Pin<&mut FileHelper>) -> QUrl;
#[qinvokable]
fn load_file(
self: Pin<&mut FileHelper>,
title: QString,
filter: QString,
) -> QUrl;
} }
}
impl Default for FileHelper { use rfd::FileDialog;
use std::path::Path;
use tracing::{debug, debug_span, error, info, instrument};
#[derive(Clone)]
pub struct FileHelperRust {
name: QString,
file_path: QString,
}
impl Default for FileHelperRust {
fn default() -> Self { fn default() -> Self {
Self { Self {
name: QString::from(""), name: QString::from(""),
file_path: QString::from(""), file_path: QString::from(""),
} }
} }
} }
impl qobject::FileHelper { impl qobject::FileHelper {
// #[qinvokable]
// pub fn save(self: Pin<&mut Self>, file: QUrl, service_list: QVariant) -> bool {
// println!("{}", file);
// match service_list.value() {
// QVariantValue::<QString>(..) => println!("string"),
// QVariantValue::<QUrl>(..) => println!("url"),
// QVariantValue::<QDate>(..) => println!("date"),
// _ => println!("QVariant is..."),
// }
// return true;
// }
#[qinvokable]
pub fn load(self: Pin<&mut Self>, file: QUrl) -> Vec<String> { pub fn load(self: Pin<&mut Self>, file: QUrl) -> Vec<String> {
println!("{file}"); println!("{file}");
vec!["hi".to_string()] vec!["hi".to_string()]
} }
#[qinvokable]
pub fn validate(self: Pin<&mut Self>, file: QUrl) -> bool { pub fn validate(self: Pin<&mut Self>, file: QUrl) -> bool {
let file_string = file.to_string(); let file_string = file.to_string();
let file_string = file_string.strip_prefix("file://"); let file_string = file_string.strip_prefix("file://");
@ -63,15 +71,13 @@ mod file_helper {
exists exists
} }
None => { None => {
let exists = let exists = Path::new(&file.to_string()).exists();
Path::new(&file.to_string()).exists();
println!("{file} exists? {exists}"); println!("{file} exists? {exists}");
exists exists
} }
} }
} }
#[qinvokable]
pub fn save_file(self: Pin<&mut Self>) -> QUrl { pub fn save_file(self: Pin<&mut Self>) -> QUrl {
let file = FileDialog::new() let file = FileDialog::new()
.set_file_name("NVTFC.pres") .set_file_name("NVTFC.pres")
@ -92,19 +98,16 @@ mod file_helper {
} }
} }
#[qinvokable]
pub fn load_file( pub fn load_file(
self: Pin<&mut Self>, self: Pin<&mut Self>,
title: QString, title: QString,
filter: QString, filter: QString,
) -> QUrl { ) -> QUrl {
let video_filters = [ let video_filters = [
"mp4", "webm", "avi", "mkv", "MP4", "WEBM", "AVI", "mp4", "webm", "avi", "mkv", "MP4", "WEBM", "AVI", "MKV",
"MKV",
]; ];
let image_filters = [ let image_filters = [
"jpg", "png", "gif", "jpeg", "JPG", "PNG", "webp", "jpg", "png", "gif", "jpeg", "JPG", "PNG", "webp", "gif",
"gif",
]; ];
let audio_filters = ["mp3", "opus", "ogg", "flac", "wav"]; let audio_filters = ["mp3", "opus", "ogg", "flac", "wav"];
let title = title.to_string(); let title = title.to_string();
@ -137,5 +140,4 @@ mod file_helper {
QUrl::default() QUrl::default()
} }
} }
}
} }

View file

@ -64,7 +64,6 @@ impl Default for ServiceThingRust {
} }
impl qobject::ServiceThing { impl qobject::ServiceThing {
#[qinvokable]
pub fn activate(self: Pin<&mut Self>) { pub fn activate(self: Pin<&mut Self>) {
println!("{}", self.active()); println!("{}", self.active());
let active: bool = *self.active(); let active: bool = *self.active();
@ -72,7 +71,6 @@ impl qobject::ServiceThing {
println!("{}", !active); println!("{}", !active);
} }
#[qinvokable]
pub fn check_active(self: Pin<&mut Self>) { pub fn check_active(self: Pin<&mut Self>) {
println!("Are we active?: {}", self.active()); println!("Are we active?: {}", self.active());
} }

View file

@ -15,6 +15,12 @@ mod settings {
#[qproperty(QUrl, last_save_file)] #[qproperty(QUrl, last_save_file)]
#[qproperty(QUrl, loaded_file)] #[qproperty(QUrl, loaded_file)]
type Settings = super::SettingsRust; type Settings = super::SettingsRust;
#[qinvokable]
fn setup(self: Pin<&mut Settings>);
#[qinvokable]
fn set_save_file(self: Pin<&mut Settings>, file: QUrl);
} }
} }
@ -28,13 +34,9 @@ use std::path::PathBuf;
pub struct SettingsRust { pub struct SettingsRust {
config: Ini, config: Ini,
#[qproperty]
screen: QString, screen: QString,
#[qproperty]
sound_effect: QString, sound_effect: QString,
#[qproperty]
last_save_file: QUrl, last_save_file: QUrl,
#[qproperty]
loaded_file: QUrl, loaded_file: QUrl,
} }
@ -51,15 +53,6 @@ impl Default for SettingsRust {
} }
impl qobject::Settings { impl qobject::Settings {
#[qinvokable]
pub fn print_sound(self: Pin<&mut Self>) {
let mut config = Ini::new();
let _map = config.load("~/.config/lumina/lumina.conf");
println!("{}", self.sound_effect());
}
#[qinvokable]
pub fn setup(mut self: Pin<&mut Self>) { pub fn setup(mut self: Pin<&mut Self>) {
let home = dirs::config_dir(); let home = dirs::config_dir();
println!("{:?}", home); println!("{:?}", home);
@ -92,7 +85,6 @@ impl qobject::Settings {
} }
} }
#[qinvokable]
pub fn set_save_file(mut self: Pin<&mut Self>, file: QUrl) { pub fn set_save_file(mut self: Pin<&mut Self>, file: QUrl) {
println!("{file}"); println!("{file}");
match self.as_mut().config_mut().set( match self.as_mut().config_mut().set(

View file

@ -1,7 +1,5 @@
#[cxx_qt::bridge] #[cxx_qt::bridge]
mod slide_model { mod slide_model {
use cxx_qt_lib::CaseSensitivity;
use tracing::{debug, debug_span, error, info, instrument};
unsafe extern "C++" { unsafe extern "C++" {
include!(< QAbstractListModel >); include!(< QAbstractListModel >);
include!("cxx-qt-lib/qhash.h"); include!("cxx-qt-lib/qhash.h");
@ -28,9 +26,179 @@ mod slide_model {
// type QVector_Slidey = cxx_qt_lib::QVector<Slidey>; // type QVector_Slidey = cxx_qt_lib::QVector<Slidey>;
} }
#[cxx_qt::qobject] #[qenum(SlideModel)]
#[derive(Clone, Debug)] enum Role {
pub struct Slidey { Ty,
Text,
Audio,
ImageBackground,
VideoBackground,
HTextAlignment,
VTextAlignment,
Font,
FontSize,
ServiceItemId,
SlideIndex,
ImageCount,
Active,
Selected,
Looping,
VideoThumbnail,
VideoStartTime,
VideoEndTime,
Html,
}
unsafe extern "RustQt" {
#[qobject]
#[base = "QAbstractListModel"]
#[qml_element]
type SlideModel = super::SlideModelRust;
#[inherit]
#[qsignal]
fn data_changed(
self: Pin<&mut SlideModel>,
top_left: &QModelIndex,
bottom_right: &QModelIndex,
roles: &QVector_i32,
);
#[qsignal]
fn active_change(self: Pin<&mut SlideModel>, index: &i32);
#[qinvokable]
fn add_video_thumbnail(
self: Pin<&mut SlideModel>,
index: i32,
) -> bool;
#[qinvokable]
fn clear(self: Pin<&mut SlideModel>);
#[qinvokable]
fn remove_item_from_service(
self: Pin<&mut SlideModel>,
index: i32,
_service_item: &QMap_QString_QVariant,
);
#[qinvokable]
fn remove_item(self: Pin<&mut SlideModel>, index: i32);
#[qinvokable]
fn insert_item_from_service(
self: Pin<&mut SlideModel>,
index: i32,
service_item: &QMap_QString_QVariant,
);
#[qinvokable]
fn add_item_from_service(
self: Pin<&mut SlideModel>,
index: i32,
service_item: &QMap_QString_QVariant,
);
#[qinvokable]
fn move_item_from_service(
self: Pin<&mut SlideModel>,
source_index: i32,
destination_index: i32,
_service_item: &QMap_QString_QVariant,
);
#[qinvokable]
fn get_item(
self: Pin<&mut SlideModel>,
index: i32,
) -> QMap_QString_QVariant;
#[qinvokable]
fn get_slide_from_service(
self: Pin<&mut SlideModel>,
index: i32,
) -> i32;
#[qinvokable]
fn activate(self: Pin<&mut SlideModel>, index: i32) -> bool;
}
impl cxx_qt::Threading for SlideModel {}
unsafe extern "RustQt" {
#[inherit]
unsafe fn begin_insert_rows(
self: Pin<&mut SlideModel>,
parent: &QModelIndex,
first: i32,
last: i32,
);
#[inherit]
unsafe fn end_insert_rows(self: Pin<&mut SlideModel>);
#[inherit]
unsafe fn begin_remove_rows(
self: Pin<&mut SlideModel>,
parent: &QModelIndex,
first: i32,
last: i32,
);
#[inherit]
unsafe fn end_remove_rows(self: Pin<&mut SlideModel>);
#[inherit]
unsafe fn begin_reset_model(self: Pin<&mut SlideModel>);
#[inherit]
unsafe fn end_reset_model(self: Pin<&mut SlideModel>);
#[inherit]
fn can_fetch_more(
self: &SlideModel,
parent: &QModelIndex,
) -> bool;
#[inherit]
fn index(
self: &SlideModel,
row: i32,
column: i32,
parent: &QModelIndex,
) -> QModelIndex;
#[qinvokable]
#[cxx_override]
fn data(
self: &SlideModel,
index: &QModelIndex,
role: i32,
) -> QVariant;
#[qinvokable]
#[cxx_override]
fn role_names(self: &SlideModel) -> QHash_i32_QByteArray;
#[qinvokable]
#[cxx_override]
fn row_count(self: &SlideModel, _parent: &QModelIndex)
-> i32;
#[qinvokable]
fn count(self: &SlideModel) -> i32;
}
}
use crate::ffmpeg;
use cxx_qt_lib::CaseSensitivity;
use std::path::PathBuf;
use std::thread;
use tracing::{debug, debug_span, error, info, instrument};
#[derive(Clone, Debug)]
pub struct Slide {
text: QString, text: QString,
ty: QString, ty: QString,
audio: QString, audio: QString,
@ -50,9 +218,9 @@ mod slide_model {
video_start_time: f32, video_start_time: f32,
video_end_time: f32, video_end_time: f32,
html: bool, html: bool,
} }
impl Default for Slidey { impl Default for Slide {
fn default() -> Self { fn default() -> Self {
Self { Self {
text: QString::default(), text: QString::default(),
@ -76,52 +244,22 @@ mod slide_model {
html: false, html: false,
} }
} }
} }
#[cxx_qt::qobject(base = "QAbstractListModel")] #[derive(Default, Debug)]
#[derive(Default, Debug)] pub struct SlideModelRust {
pub struct SlideyMod {
id: i32, id: i32,
slides: Vec<Slidey>, slides: Vec<Slide>,
} }
#[cxx_qt::qsignals(SlideyMod)] impl qobject::SlideModel {
pub enum Signals<'a> {
#[inherit]
DataChanged {
top_left: &'a QModelIndex,
bottom_right: &'a QModelIndex,
roles: &'a QVector_i32,
},
ActiveChanged {
index: &'a i32,
},
}
enum Role {
ActiveRole,
SelectedRole,
LoopingRole,
TextRole,
VideoThumbnailRole,
VideoStartTimeRole,
VideoEndTimeRole,
}
// use crate::video_thumbnail;
// use image::{ImageBuffer, Rgba};
use crate::ffmpeg;
use std::path::PathBuf;
use std::thread;
impl qobject::SlideyMod {
#[qinvokable] #[qinvokable]
pub fn add_video_thumbnail( pub fn add_video_thumbnail(
mut self: Pin<&mut Self>, mut self: Pin<&mut Self>,
index: i32, index: i32,
) -> bool { ) -> bool {
let mut vector_roles = QVector_i32::default(); let mut vector_roles = QVector_i32::default();
vector_roles vector_roles.append(self.get_role(Role::VideoThumbnailRole));
.append(self.get_role(Role::VideoThumbnailRole));
let model_index = let model_index =
&self.index(index, 0, &QModelIndex::default()); &self.index(index, 0, &QModelIndex::default());
@ -129,13 +267,10 @@ mod slide_model {
self.as_mut().slides_mut().get_mut(index as usize) self.as_mut().slides_mut().get_mut(index as usize)
{ {
if !slide.video_background.is_empty() { if !slide.video_background.is_empty() {
let path = PathBuf::from( let path =
slide.video_background.to_string(), PathBuf::from(slide.video_background.to_string());
);
let video = QString::from( let video = QString::from(
ffmpeg::bg_from_video(&path) ffmpeg::bg_from_video(&path).to_str().unwrap(),
.to_str()
.unwrap(),
) )
.insert(0, &QString::from("file://")) .insert(0, &QString::from("file://"))
.to_owned(); .to_owned();
@ -207,7 +342,7 @@ mod slide_model {
println!("removed-row: {:?}", index); println!("removed-row: {:?}", index);
} }
fn add_slide(mut self: Pin<&mut Self>, slide: &Slidey) { fn add_slide(mut self: Pin<&mut Self>, slide: &Slide) {
let index = self.as_ref().slides().len() as i32; let index = self.as_ref().slides().len() as i32;
println!("{:?}", slide); println!("{:?}", slide);
let slide = slide.clone(); let slide = slide.clone();
@ -234,7 +369,7 @@ mod slide_model {
fn insert_slide( fn insert_slide(
mut self: Pin<&mut Self>, mut self: Pin<&mut Self>,
slide: &Slidey, slide: &Slide,
index: i32, index: i32,
) { ) {
let mut slide = slide.clone(); let mut slide = slide.clone();
@ -246,9 +381,7 @@ mod slide_model {
index, index,
index, index,
); );
self.as_mut() self.as_mut().slides_mut().insert(index as usize, slide);
.slides_mut()
.insert(index as usize, slide);
self.as_mut().end_insert_rows(); self.as_mut().end_insert_rows();
} }
let thread = self.qt_thread(); let thread = self.qt_thread();
@ -270,9 +403,7 @@ mod slide_model {
for (key, data) in service_item.iter() { for (key, data) in service_item.iter() {
debug!( debug!(
?key, ?key,
data = data data = data.value_or_default::<QString>().to_string()
.value_or_default::<QString>()
.to_string()
); );
} }
let ty = service_item let ty = service_item
@ -302,7 +433,7 @@ mod slide_model {
Vec::<QString>::from(&QList_QString::from(&textlist)); Vec::<QString>::from(&QList_QString::from(&textlist));
// let vec_slize: &[usize] = &text_vec; // let vec_slize: &[usize] = &text_vec;
let mut slide = Slidey::default(); let mut slide = Slide::default();
slide.ty = service_item slide.ty = service_item
.get(&QString::from("type")) .get(&QString::from("type"))
@ -416,20 +547,14 @@ mod slide_model {
slide.slide_count = count as i32; slide.slide_count = count as i32;
slide.slide_index = i as i32; slide.slide_index = i as i32;
if background_type == QString::from("image") { if background_type == QString::from("image") {
slide.image_background = slide.image_background = background.clone();
background.clone(); slide.video_background = QString::from("");
slide.video_background =
QString::from("");
} else { } else {
slide.video_background = slide.video_background = background.clone();
background.clone(); slide.image_background = QString::from("");
slide.image_background =
QString::from("");
} }
self.as_mut().insert_slide( self.as_mut()
&slide, .insert_slide(&slide, slide_index + i as i32);
slide_index + i as i32,
);
} }
} }
Some(ty) if ty == QString::from("video") => { Some(ty) if ty == QString::from("video") => {
@ -451,10 +576,8 @@ mod slide_model {
slide.image_background = background.clone(); slide.image_background = background.clone();
slide.video_background = QString::from(""); slide.video_background = QString::from("");
slide.slide_index = i; slide.slide_index = i;
self.as_mut().insert_slide( self.as_mut()
&slide, .insert_slide(&slide, slide_index + i as i32);
slide_index + i as i32,
);
} }
} }
_ => println!("It's somethign else!"), _ => println!("It's somethign else!"),
@ -497,7 +620,7 @@ mod slide_model {
Vec::<QString>::from(&QList_QString::from(&textlist)); Vec::<QString>::from(&QList_QString::from(&textlist));
// let vec_slize: &[usize] = &text_vec; // let vec_slize: &[usize] = &text_vec;
let mut slide = Slidey::default(); let mut slide = Slide::default();
slide.ty = service_item slide.ty = service_item
.get(&QString::from("type")) .get(&QString::from("type"))
@ -588,15 +711,11 @@ mod slide_model {
slide.slide_count = text_vec.len() as i32; slide.slide_count = text_vec.len() as i32;
slide.slide_index = i as i32; slide.slide_index = i as i32;
if background_type == QString::from("image") { if background_type == QString::from("image") {
slide.image_background = slide.image_background = background.clone();
background.clone(); slide.video_background = QString::from("");
slide.video_background =
QString::from("");
} else { } else {
slide.video_background = slide.video_background = background.clone();
background.clone(); slide.image_background = QString::from("");
slide.image_background =
QString::from("");
} }
self.as_mut().add_slide(&slide); self.as_mut().add_slide(&slide);
} }
@ -726,9 +845,7 @@ mod slide_model {
.clone() .clone()
.enumerate() .enumerate()
.filter(|x| x.0 >= dest_slide as usize) .filter(|x| x.0 >= dest_slide as usize)
.filter(|x| { .filter(|x| x.0 < (dest_slide + count) as usize)
x.0 < (dest_slide + count) as usize
})
{ {
if let Some(slide) = if let Some(slide) =
self.as_mut().slides_mut().get_mut(i) self.as_mut().slides_mut().get_mut(i)
@ -758,12 +875,8 @@ mod slide_model {
if move_down { if move_down {
for (i, slide) in slides_iter for (i, slide) in slides_iter
.enumerate() .enumerate()
.filter(|x| { .filter(|x| x.0 < (first_slide + dest_count) as usize)
x.0 < (first_slide + dest_count) as usize .filter(|x| x.1.service_item_id <= destination_index)
})
.filter(|x| {
x.1.service_item_id <= destination_index
})
.filter(|x| x.1.service_item_id >= source_index) .filter(|x| x.1.service_item_id >= source_index)
{ {
if let Some(slide) = if let Some(slide) =
@ -784,9 +897,7 @@ mod slide_model {
.filter(|x| { .filter(|x| {
x.0 >= (dest_slide as usize + count as usize) x.0 >= (dest_slide as usize + count as usize)
}) })
.filter(|x| { .filter(|x| x.1.service_item_id >= destination_index)
x.1.service_item_id >= destination_index
})
.filter(|x| x.1.service_item_id <= source_index) .filter(|x| x.1.service_item_id <= source_index)
{ {
if let Some(slide) = if let Some(slide) =
@ -825,15 +936,12 @@ mod slide_model {
let move_amount = let move_amount =
dest_index - source_index - count + 1; dest_index - source_index - count + 1;
// println!("rust-move_amount: {:?}", move_amount); // println!("rust-move_amount: {:?}", move_amount);
self.as_mut().slides_mut() self.as_mut().slides_mut()[source_index..=dest_index]
[source_index..=dest_index]
.rotate_right(move_amount); .rotate_right(move_amount);
} else { } else {
let move_amount = let move_amount = end_slide - dest_index - count + 1;
end_slide - dest_index - count + 1;
println!("rust-move_amount: {:?}", move_amount); println!("rust-move_amount: {:?}", move_amount);
self.as_mut().slides_mut() self.as_mut().slides_mut()[dest_index..=end_slide]
[dest_index..=end_slide]
.rotate_left(move_amount); .rotate_left(move_amount);
} }
self.as_mut().end_reset_model(); self.as_mut().end_reset_model();
@ -853,9 +961,7 @@ mod slide_model {
} }
let rn = self.as_ref().role_names(); let rn = self.as_ref().role_names();
let rn_iter = rn.iter(); let rn_iter = rn.iter();
if let Some(slide) = if let Some(slide) = self.rust().slides.get(index as usize) {
self.rust().slides.get(index as usize)
{
for i in rn_iter { for i in rn_iter {
qvariantmap.insert( qvariantmap.insert(
QString::from(&i.1.to_string()), QString::from(&i.1.to_string()),
@ -873,10 +979,7 @@ mod slide_model {
) -> i32 { ) -> i32 {
let slides = self.slides().clone(); let slides = self.slides().clone();
let slides_iter = slides.iter(); let slides_iter = slides.iter();
debug!( debug!(service_item = index, "Getting slide from this item");
service_item = index,
"Getting slide from this item"
);
let mut id = 0; let mut id = 0;
for (i, slide) in slides_iter for (i, slide) in slides_iter
.enumerate() .enumerate()
@ -890,15 +993,10 @@ mod slide_model {
} }
#[qinvokable] #[qinvokable]
pub fn activate( pub fn activate(mut self: Pin<&mut Self>, index: i32) -> bool {
mut self: Pin<&mut Self>,
index: i32,
) -> bool {
let rc = self.as_ref().count() - 1; let rc = self.as_ref().count() - 1;
let tl = let tl = &self.as_ref().index(0, 0, &QModelIndex::default());
&self.as_ref().index(0, 0, &QModelIndex::default()); let br = &self.as_ref().index(rc, 0, &QModelIndex::default());
let br =
&self.as_ref().index(rc, 0, &QModelIndex::default());
let mut vector_roles = QVector_i32::default(); let mut vector_roles = QVector_i32::default();
vector_roles.append(self.get_role(Role::ActiveRole)); vector_roles.append(self.get_role(Role::ActiveRole));
for slide in self.as_mut().slides_mut().iter_mut() { for slide in self.as_mut().slides_mut().iter_mut() {
@ -924,11 +1022,7 @@ mod slide_model {
// slide.video_background // slide.video_background
// ); // );
slide.active = true; slide.active = true;
self.as_mut().emit_data_changed( self.as_mut().emit_data_changed(tl, br, &vector_roles);
tl,
br,
&vector_roles,
);
// We use this signal generated by our signals enum to tell QML that // We use this signal generated by our signals enum to tell QML that
// the active slide has changed which is used to reposition views. // the active slide has changed which is used to reposition views.
self.as_mut().emit_active_changed(&index); self.as_mut().emit_active_changed(&index);
@ -950,76 +1044,61 @@ mod slide_model {
_ => 0, _ => 0,
} }
} }
} }
// Create Rust bindings for C++ functions of the base class (QAbstractItemModel) // QAbstractListModel implementation
#[cxx_qt::inherit] impl qobject::SlideModel {
extern "C++" { pub fn data(&self, index: &QModelIndex, role: i32) -> QVariant {
unsafe fn begin_insert_rows( let role = qobject::Roles { repr: role };
self: Pin<&mut qobject::SlideyMod>, if let Some(slide) = self.slides().get(index.row() as usize) {
parent: &QModelIndex,
first: i32,
last: i32,
);
unsafe fn end_insert_rows(self: Pin<&mut qobject::SlideyMod>);
unsafe fn begin_remove_rows(
self: Pin<&mut qobject::SlideyMod>,
parent: &QModelIndex,
first: i32,
last: i32,
);
unsafe fn end_remove_rows(self: Pin<&mut qobject::SlideyMod>);
unsafe fn begin_reset_model(
self: Pin<&mut qobject::SlideyMod>,
);
unsafe fn end_reset_model(self: Pin<&mut qobject::SlideyMod>);
}
#[cxx_qt::inherit]
unsafe extern "C++" {
#[cxx_name = "canFetchMore"]
fn base_can_fetch_more(
self: &qobject::SlideyMod,
parent: &QModelIndex,
) -> bool;
fn index(
self: &qobject::SlideyMod,
row: i32,
column: i32,
parent: &QModelIndex,
) -> QModelIndex;
}
// QAbstractListModel implementation
impl qobject::SlideyMod {
#[qinvokable(cxx_override)]
fn data(&self, index: &QModelIndex, role: i32) -> QVariant {
if let Some(slide) =
self.slides().get(index.row() as usize)
{
return match role { return match role {
0 => QVariant::from(&slide.ty), qobject::Roles::Ty => QVariant::from(&slide.ty),
1 => QVariant::from(&slide.text), qobject::Roles::Text => QVariant::from(&slide.text),
2 => QVariant::from(&slide.audio), qobject::Roles::Audio => QVariant::from(&slide.audio),
3 => QVariant::from(&slide.image_background), qobject::Roles::ImageBackground => {
4 => QVariant::from(&slide.video_background), QVariant::from(&slide.image_background)
5 => QVariant::from(&slide.htext_alignment), }
6 => QVariant::from(&slide.vtext_alignment), qobject::Roles::VideoBackground => {
7 => QVariant::from(&slide.font), QVariant::from(&slide.video_background)
8 => QVariant::from(&slide.font_size), }
9 => QVariant::from(&slide.service_item_id), qobject::Roles::HTextAlignment => {
10 => QVariant::from(&slide.slide_index), QVariant::from(&slide.htext_alignment)
11 => QVariant::from(&slide.slide_count), }
12 => QVariant::from(&slide.active), qobject::Roles::VTextAlignment => {
13 => QVariant::from(&slide.selected), QVariant::from(&slide.vtext_alignment)
14 => QVariant::from(&slide.looping), }
15 => QVariant::from(&slide.video_thumbnail), qobject::Roles::Font => QVariant::from(&slide.font),
16 => QVariant::from(&slide.video_start_time), qobject::Roles::FontSize => {
17 => QVariant::from(&slide.video_end_time), QVariant::from(&slide.font_size)
18 => QVariant::from(&slide.html), }
qobject::Roles::ServiceItemId => {
QVariant::from(&slide.service_item_id)
}
qobject::Roles::SlideIndex => {
QVariant::from(&slide.slide_index)
}
qobject::Roles::SlideCount => {
QVariant::from(&slide.slide_count)
}
qobject::Roles::Active => {
QVariant::from(&slide.active)
}
qobject::Roles::Selected => {
QVariant::from(&slide.selected)
}
qobject::Roles::Looping => {
QVariant::from(&slide.looping)
}
qobject::Roles::VideoThumbnail => {
QVariant::from(&slide.video_thumbnail)
}
qobject::Roles::VideoStartTime => {
QVariant::from(&slide.video_start_time)
}
qobject::Roles::VideoEndTime => {
QVariant::from(&slide.video_end_time)
}
qobject::Roles::Html => QVariant::from(&slide.html),
_ => QVariant::default(), _ => QVariant::default(),
}; };
} }
@ -1028,77 +1107,98 @@ mod slide_model {
} }
// Example of overriding a C++ virtual method and calling the base class implementation. // Example of overriding a C++ virtual method and calling the base class implementation.
#[qinvokable(cxx_override)]
pub fn can_fetch_more(&self, parent: &QModelIndex) -> bool { pub fn can_fetch_more(&self, parent: &QModelIndex) -> bool {
self.base_can_fetch_more(parent) self.base_can_fetch_more(parent)
} }
#[qinvokable(cxx_override)]
pub fn role_names(&self) -> QHash_i32_QByteArray { pub fn role_names(&self) -> QHash_i32_QByteArray {
let mut roles = QHash_i32_QByteArray::default(); let mut roles = QHash_i32_QByteArray::default();
roles.insert(0, cxx_qt_lib::QByteArray::from("type"));
roles.insert(1, cxx_qt_lib::QByteArray::from("text"));
roles.insert(2, cxx_qt_lib::QByteArray::from("audio"));
roles.insert( roles.insert(
3, qobject::Roles::Ty.repr,
cxx_qt_lib::QByteArray::from("type"),
);
roles.insert(
qobject::Roles::Text.repr,
cxx_qt_lib::QByteArray::from("text"),
);
roles.insert(
qobject::Roles::Audio.repr,
cxx_qt_lib::QByteArray::from("audio"),
);
roles.insert(
qobject::Roles::ImageBackground.repr,
cxx_qt_lib::QByteArray::from("imageBackground"), cxx_qt_lib::QByteArray::from("imageBackground"),
); );
roles.insert( roles.insert(
4, qobject::Roles::VideoBackground.repr,
cxx_qt_lib::QByteArray::from("videoBackground"), cxx_qt_lib::QByteArray::from("videoBackground"),
); );
roles.insert( roles.insert(
5, qobject::Roles::HTextAlignment.repr,
cxx_qt_lib::QByteArray::from("hTextAlignment"), cxx_qt_lib::QByteArray::from("hTextAlignment"),
); );
roles.insert( roles.insert(
6, qobject::Roles::VTextAlignment.repr,
cxx_qt_lib::QByteArray::from("vTextAlignment"), cxx_qt_lib::QByteArray::from("vTextAlignment"),
); );
roles.insert(7, cxx_qt_lib::QByteArray::from("font"));
roles.insert(8, cxx_qt_lib::QByteArray::from("fontSize"));
roles.insert( roles.insert(
9, qobject::Roles::Font.repr,
cxx_qt_lib::QByteArray::from("font"),
);
roles.insert(
qobject::Roles::FontSize.repr,
cxx_qt_lib::QByteArray::from("fontSize"),
);
roles.insert(
qobject::Roles::ServiceItemId.repr,
cxx_qt_lib::QByteArray::from("serviceItemId"), cxx_qt_lib::QByteArray::from("serviceItemId"),
); );
roles.insert( roles.insert(
10, qobject::Roles::SlideIndex.repr,
cxx_qt_lib::QByteArray::from("slideIndex"), cxx_qt_lib::QByteArray::from("slideIndex"),
); );
roles.insert( roles.insert(
11, qobject::Roles::ImageCount.repr,
cxx_qt_lib::QByteArray::from("imageCount"), cxx_qt_lib::QByteArray::from("imageCount"),
); );
roles.insert(12, cxx_qt_lib::QByteArray::from("active"));
roles
.insert(13, cxx_qt_lib::QByteArray::from("selected"));
roles.insert(14, cxx_qt_lib::QByteArray::from("looping"));
roles.insert( roles.insert(
15, qobject::Roles::Active.repr,
cxx_qt_lib::QByteArray::from("active"),
);
roles.insert(
qobject::Roles::Selected.repr,
cxx_qt_lib::QByteArray::from("selected"),
);
roles.insert(
qobject::Roles::Looping.repr,
cxx_qt_lib::QByteArray::from("looping"),
);
roles.insert(
qobject::Roles::VideoThumbnail.repr,
cxx_qt_lib::QByteArray::from("videoThumbnail"), cxx_qt_lib::QByteArray::from("videoThumbnail"),
); );
roles.insert( roles.insert(
16, qobject::Roles::VideoStartTime.repr,
cxx_qt_lib::QByteArray::from("videoStartTime"), cxx_qt_lib::QByteArray::from("videoStartTime"),
); );
roles.insert( roles.insert(
17, qobject::Roles::VideoEndTime.repr,
cxx_qt_lib::QByteArray::from("videoEndTime"), cxx_qt_lib::QByteArray::from("videoEndTime"),
); );
roles.insert(18, cxx_qt_lib::QByteArray::from("html")); roles.insert(
qobject::Roles::Html.repr,
cxx_qt_lib::QByteArray::from("html"),
);
roles roles
} }
#[qinvokable(cxx_override)]
pub fn row_count(&self, _parent: &QModelIndex) -> i32 { pub fn row_count(&self, _parent: &QModelIndex) -> i32 {
let cnt = self.rust().slides.len() as i32; let cnt = self.rust().slides.len() as i32;
// println!("row count is {cnt}"); // println!("row count is {cnt}");
cnt cnt
} }
#[qinvokable]
pub fn count(&self) -> i32 { pub fn count(&self) -> i32 {
self.rust().slides.len() as i32 self.rust().slides.len() as i32
} }
}
} }

View file

@ -1,11 +1,5 @@
#[cxx_qt::bridge] #[cxx_qt::bridge]
mod slide_object { mod slide_object {
// use cxx_qt_lib::QVariantValue;
// use std::path::Path;
// use std::task::Context;
use tracing::debug;
unsafe extern "C++" { unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h"); include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString; type QString = cxx_qt_lib::QString;
@ -16,58 +10,101 @@ mod slide_object {
type QVariant = cxx_qt_lib::QVariant; type QVariant = cxx_qt_lib::QVariant;
} }
#[derive(Debug)] unsafe extern "RustQt" {
#[cxx_qt::qsignals(SlideObject)] #[qsignal]
pub enum Signals<'a> { fn playing_changed(
PlayingChanged { is_playing: &'a bool }, self: Pin<&mut SlideObject>,
SlideIndexChanged { slide_index: &'a i32 },
SlideSizeChanged { slide_size: &'a i32 },
SlideChanged { slide: &'a i32 },
LoopChanged { looping: &'a bool },
RevealNext,
RevealPrev,
}
#[derive(Clone, Debug)]
#[cxx_qt::qobject]
pub struct SlideObject {
#[qproperty]
slide_index: i32,
#[qproperty]
slide_size: i32,
#[qproperty]
image_count: i32,
#[qproperty]
is_playing: bool, is_playing: bool,
#[qproperty] );
looping: bool, // #[qsignal]
#[qproperty] // fn slide_index_changed(
text: QString, // self: Pin<&mut SlideObject>,
#[qproperty] // slide_index: i32,
ty: QString, // );
#[qproperty] // #[qsignal]
audio: QString, // fn slide_size_changed(
#[qproperty] // self: Pin<&mut SlideObject>,
image_background: QString, // slide_size: i32,
#[qproperty] // );
video_background: QString, #[qsignal]
#[qproperty] fn slide_changed(self: Pin<&mut SlideObject>, slide: i32);
html: QString, #[qsignal]
#[qproperty] fn loop_changed(self: Pin<&mut SlideObject>, looping: bool);
vtext_alignment: QString, #[qsignal]
#[qproperty] fn reveal_next(self: Pin<&mut SlideObject>);
htext_alignment: QString, #[qsignal]
#[qproperty] fn reveal_prev(self: Pin<&mut SlideObject>);
font: QString,
#[qproperty]
font_size: i32,
#[qproperty]
video_start_time: f32,
#[qproperty]
video_end_time: f32,
}
impl Default for SlideObject { #[qobject]
#[qml_element]
#[qproperty(i32, slide_index)]
#[qproperty(i32, slide_size)]
#[qproperty(i32, image_count)]
#[qproperty(bool, is_playing)]
#[qproperty(bool, looping)]
#[qproperty(QString, text)]
#[qproperty(QString, ty)]
#[qproperty(QString, audio)]
#[qproperty(QString, image_background)]
#[qproperty(QString, video_background)]
#[qproperty(QString, html)]
#[qproperty(QString, vtext_alignment)]
#[qproperty(QString, htext_alignment)]
#[qproperty(QString, font)]
#[qproperty(bool, font_size)]
#[qproperty(f32, video_start_time)]
#[qproperty(f32, video_end_time)]
type SlideObject = super::SlideObjectRust;
#[qinvokable]
fn change_slide(
self: Pin<&mut SlideObject>,
item: QMap_QString_QVariant,
index: i32,
);
#[qinvokable]
fn next(
self: Pin<&mut SlideObject>,
next_item: QMap_QString_QVariant,
) -> bool;
#[qinvokable]
fn previous(
self: Pin<&mut SlideObject>,
next_item: QMap_QString_QVariant,
) -> bool;
#[qinvokable]
pub fn play(self: Pin<&mut SlideObject>) -> bool;
#[qinvokable]
pub fn pause(self: Pin<&mut SlideObject>) -> bool;
#[qinvokable]
pub fn play_pause(self: Pin<&mut SlideObject>) -> bool;
}
}
use tracing::debug;
#[derive(Clone, Debug)]
pub struct SlideObjectRust {
slide_index: i32,
slide_size: i32,
image_count: i32,
is_playing: bool,
looping: bool,
text: QString,
ty: QString,
audio: QString,
image_background: QString,
video_background: QString,
html: QString,
vtext_alignment: QString,
htext_alignment: QString,
font: QString,
font_size: i32,
video_start_time: f32,
video_end_time: f32,
}
impl Default for SlideObjectRust {
fn default() -> Self { fn default() -> Self {
Self { Self {
slide_index: 0, slide_index: 0,
@ -89,10 +126,9 @@ mod slide_object {
video_end_time: 0.0, video_end_time: 0.0,
} }
} }
} }
impl qobject::SlideObject { impl qobject::SlideObject {
#[qinvokable]
pub fn change_slide( pub fn change_slide(
mut self: Pin<&mut Self>, mut self: Pin<&mut Self>,
item: QMap_QString_QVariant, item: QMap_QString_QVariant,
@ -102,14 +138,12 @@ mod slide_object {
let icount_variant = item let icount_variant = item
.get(&QString::from("imageCount")) .get(&QString::from("imageCount"))
.unwrap_or(QVariant::from(&1)); .unwrap_or(QVariant::from(&1));
let count = let count = icount_variant.value::<i32>().unwrap_or_default();
icount_variant.value::<i32>().unwrap_or_default();
let slindex = item let slindex = item
.get(&QString::from("slideIndex")) .get(&QString::from("slideIndex"))
.unwrap_or(QVariant::from(&0)); .unwrap_or(QVariant::from(&0));
let slide_index = let slide_index = slindex.value::<i32>().unwrap_or_default();
slindex.value::<i32>().unwrap_or_default();
let html = item let html = item
.get(&QString::from("html")) .get(&QString::from("html"))
@ -172,12 +206,9 @@ mod slide_object {
if let Some(image_background) = if let Some(image_background) =
image_background.value::<QString>() image_background.value::<QString>()
{ {
if &image_background if &image_background != self.as_ref().image_background() {
!= self.as_ref().image_background()
{
println!("image-bg: {image_background}"); println!("image-bg: {image_background}");
self.as_mut() self.as_mut().set_image_background(image_background);
.set_image_background(image_background);
} }
} else { } else {
println!("image-bg: empty"); println!("image-bg: empty");
@ -188,19 +219,16 @@ mod slide_object {
if let Some(video_background) = if let Some(video_background) =
video_background.value::<QString>() video_background.value::<QString>()
{ {
if &video_background if &video_background != self.as_ref().video_background() {
!= self.as_ref().video_background()
{
println!("video-bg: {video_background}"); println!("video-bg: {video_background}");
self.as_mut() self.as_mut().set_video_background(video_background);
.set_video_background(video_background);
} }
} else { } else {
println!("video-bg: empty"); println!("video-bg: empty");
} }
let font = item.get(&QString::from("font")).unwrap_or( let font = item
QVariant::from(&QString::from("Quicksand")), .get(&QString::from("font"))
); .unwrap_or(QVariant::from(&QString::from("Quicksand")));
if let Some(font) = font.value::<QString>() { if let Some(font) = font.value::<QString>() {
if &font != self.as_ref().font() { if &font != self.as_ref().font() {
println!("font: {font}"); println!("font: {font}");
@ -215,13 +243,9 @@ mod slide_object {
if let Some(vtext_alignment) = if let Some(vtext_alignment) =
vtext_alignment.value::<QString>() vtext_alignment.value::<QString>()
{ {
if &vtext_alignment != self.as_ref().vtext_alignment() if &vtext_alignment != self.as_ref().vtext_alignment() {
{ println!("vertical-text-align: {vtext_alignment}");
println!( self.as_mut().set_vtext_alignment(vtext_alignment);
"vertical-text-align: {vtext_alignment}"
);
self.as_mut()
.set_vtext_alignment(vtext_alignment);
} }
} else { } else {
println!("vertical-text-align: empty"); println!("vertical-text-align: empty");
@ -232,13 +256,9 @@ mod slide_object {
if let Some(htext_alignment) = if let Some(htext_alignment) =
htext_alignment.value::<QString>() htext_alignment.value::<QString>()
{ {
if &htext_alignment != self.as_ref().htext_alignment() if &htext_alignment != self.as_ref().htext_alignment() {
{ println!("horizontal-text-align: {htext_alignment}");
println!( self.as_mut().set_htext_alignment(htext_alignment);
"horizontal-text-align: {htext_alignment}"
);
self.as_mut()
.set_htext_alignment(htext_alignment);
} }
} else { } else {
println!("horizontal-text-align: empty"); println!("horizontal-text-align: empty");
@ -288,12 +308,10 @@ mod slide_object {
self.as_mut().set_slide_index(slide_index); self.as_mut().set_slide_index(slide_index);
self.as_mut() self.as_mut().emit(Signals::SlideChanged { slide: &index });
.emit(Signals::SlideChanged { slide: &index });
println!("## Slide End ##"); println!("## Slide End ##");
} }
#[qinvokable]
pub fn next( pub fn next(
mut self: Pin<&mut Self>, mut self: Pin<&mut Self>,
next_item: QMap_QString_QVariant, next_item: QMap_QString_QVariant,
@ -302,7 +320,6 @@ mod slide_object {
self.as_mut().change_slide(next_item, new_id); self.as_mut().change_slide(next_item, new_id);
true true
} }
#[qinvokable]
pub fn previous( pub fn previous(
mut self: Pin<&mut Self>, mut self: Pin<&mut Self>,
prev_item: QMap_QString_QVariant, prev_item: QMap_QString_QVariant,
@ -311,19 +328,16 @@ mod slide_object {
self.as_mut().change_slide(prev_item, new_id); self.as_mut().change_slide(prev_item, new_id);
true true
} }
#[qinvokable]
pub fn play(mut self: Pin<&mut Self>) -> bool { pub fn play(mut self: Pin<&mut Self>) -> bool {
self.as_mut().set_is_playing(true); self.as_mut().set_is_playing(true);
self.as_mut().emit_playing_changed(&true); self.as_mut().emit_playing_changed(&true);
true true
} }
#[qinvokable]
pub fn pause(mut self: Pin<&mut Self>) -> bool { pub fn pause(mut self: Pin<&mut Self>) -> bool {
self.as_mut().set_is_playing(false); self.as_mut().set_is_playing(false);
self.as_mut().emit_playing_changed(&false); self.as_mut().emit_playing_changed(&false);
false false
} }
#[qinvokable]
pub fn play_pause(mut self: Pin<&mut Self>) -> bool { pub fn play_pause(mut self: Pin<&mut Self>) -> bool {
let playing = self.as_ref().is_playing().clone(); let playing = self.as_ref().is_playing().clone();
match playing { match playing {
@ -333,5 +347,4 @@ mod slide_object {
self.as_mut().emit_playing_changed(&!playing); self.as_mut().emit_playing_changed(&!playing);
!playing !playing
} }
}
} }