Quite a few of the models have been ported.. need more though.
Still need all the songs and some small utility modules
This commit is contained in:
parent
81b430e768
commit
62daf316a3
7 changed files with 2781 additions and 2535 deletions
14
TODO.org
14
TODO.org
|
@ -1,9 +1,21 @@
|
||||||
#+TITLE: Todo List
|
#+TITLE: Todo List
|
||||||
|
#+filetags: :projectdev:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:CATEGORY: dev
|
:CATEGORY: dev
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
* Tasks [61%] [47/76]
|
* Tasks [0%] [0/0]
|
||||||
|
** TODO Port to CXX-QT 6.0 [37%] [3/8]
|
||||||
|
[[file:~/dev/lumina/src/rust/lib.rs]]
|
||||||
|
|
||||||
|
*** DONE image_model.rs
|
||||||
|
*** DONE video_model.rs
|
||||||
|
*** DONE presentation_model.rs
|
||||||
|
*** TODO songs/song_model.rs
|
||||||
|
*** TODO songs/song_editor.rs
|
||||||
|
*** TODO ytdl.rs
|
||||||
|
*** TODO utils.rs
|
||||||
|
*** TODO obs.rs
|
||||||
** TODO Write a function to handle switching to the next fragment in revealjs
|
** TODO Write a function to handle switching to the next fragment in revealjs
|
||||||
[[file:~/dev/lumina/src/qml/presenter/Slide.qml::WebEngineView {]]
|
[[file:~/dev/lumina/src/qml/presenter/Slide.qml::WebEngineView {]]
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,5 @@
|
||||||
#[cxx_qt::bridge]
|
#[cxx_qt::bridge]
|
||||||
mod image_model {
|
mod image_model {
|
||||||
use crate::image_model::image_model::Image;
|
|
||||||
use crate::schema::images::dsl::*;
|
|
||||||
use diesel::sqlite::SqliteConnection;
|
|
||||||
use diesel::{delete, insert_into, prelude::*, update};
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
unsafe extern "C++" {
|
unsafe extern "C++" {
|
||||||
include!(< QAbstractListModel >);
|
include!(< QAbstractListModel >);
|
||||||
include!("cxx-qt-lib/qhash.h");
|
include!("cxx-qt-lib/qhash.h");
|
||||||
|
@ -30,431 +24,468 @@ mod image_model {
|
||||||
type QList_QString = cxx_qt_lib::QList<QString>;
|
type QList_QString = cxx_qt_lib::QList<QString>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone, Debug)]
|
#[qenum(ImageModel)]
|
||||||
pub struct Image {
|
|
||||||
id: i32,
|
|
||||||
title: QString,
|
|
||||||
path: QString,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cxx_qt::qobject(base = "QAbstractListModel")]
|
|
||||||
#[derive(Default, Debug)]
|
|
||||||
pub struct ImageModel {
|
|
||||||
#[qproperty]
|
|
||||||
count_rows: i32,
|
|
||||||
highest_id: i32,
|
|
||||||
images: Vec<self::Image>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cxx_qt::qsignals(ImageModel)]
|
|
||||||
pub enum Signals<'a> {
|
|
||||||
#[inherit]
|
|
||||||
DataChanged {
|
|
||||||
top_left: &'a QModelIndex,
|
|
||||||
bottom_right: &'a QModelIndex,
|
|
||||||
roles: &'a QVector_i32,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Role {
|
enum Role {
|
||||||
IdRole,
|
Id,
|
||||||
PathRole,
|
Path,
|
||||||
TitleRole,
|
Title,
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl FromStr for Role {
|
unsafe extern "RustQt" {
|
||||||
// type Err = ();
|
#[qobject]
|
||||||
// fn from_str(input: &str) -> Result<Role, Self::Err> {
|
#[base = "QAbstractListModel"]
|
||||||
// match input {
|
#[qml_element]
|
||||||
// "id" => Ok(Role::IdRole),
|
#[qproperty(i32, count_rows)]
|
||||||
// "title" => Ok(Role::TitleRole),
|
type ImageModel = super::ImageModelRust;
|
||||||
// "path" => Ok(Role::PathRole),
|
|
||||||
// _ => Err(()),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// use crate::entities::{images, prelude::Images};
|
#[inherit]
|
||||||
// use sea_orm::{ConnectionTrait, Database, DbBackend, DbErr, Statement, ActiveValue};
|
#[qsignal]
|
||||||
impl qobject::ImageModel {
|
fn data_changed(
|
||||||
#[qinvokable]
|
self: Pin<&mut ImageModel>,
|
||||||
pub fn clear(mut self: Pin<&mut Self>) {
|
top_left: &QModelIndex,
|
||||||
unsafe {
|
bottom_right: &QModelIndex,
|
||||||
self.as_mut().begin_reset_model();
|
roles: &QVector_i32,
|
||||||
self.as_mut().images_mut().clear();
|
);
|
||||||
self.as_mut().end_reset_model();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[qinvokable]
|
#[qinvokable]
|
||||||
pub fn setup(mut self: Pin<&mut Self>) {
|
fn clear(self: Pin<&mut ImageModel>);
|
||||||
let db = &mut self.as_mut().get_db();
|
|
||||||
let results = images
|
|
||||||
.load::<crate::models::Image>(db)
|
|
||||||
.expect("Error loading images");
|
|
||||||
self.as_mut().set_highest_id(0);
|
|
||||||
|
|
||||||
println!("SHOWING IMAGES");
|
|
||||||
println!("--------------");
|
|
||||||
for image in results {
|
|
||||||
println!("{}", image.title);
|
|
||||||
println!("{}", image.id);
|
|
||||||
println!("{}", image.path);
|
|
||||||
println!("--------------");
|
|
||||||
if self.as_mut().highest_id() < &image.id {
|
|
||||||
self.as_mut().set_highest_id(image.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
let img = self::Image {
|
|
||||||
id: image.id,
|
|
||||||
title: QString::from(&image.title),
|
|
||||||
path: QString::from(&image.path),
|
|
||||||
};
|
|
||||||
|
|
||||||
self.as_mut().add_image(img);
|
|
||||||
}
|
|
||||||
println!("--------------------------------------");
|
|
||||||
println!("{:?}", self.as_mut().images());
|
|
||||||
println!("--------------------------------------");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[qinvokable]
|
#[qinvokable]
|
||||||
pub fn remove_item(
|
fn setup(self: Pin<&mut ImageModel>);
|
||||||
mut self: Pin<&mut Self>,
|
#[qinvokable]
|
||||||
|
fn remove_item(
|
||||||
|
self: Pin<&mut ImageModel>,
|
||||||
index: i32,
|
index: i32,
|
||||||
) -> bool {
|
) -> bool;
|
||||||
if index < 0 || (index as usize) >= self.images().len() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
let db = &mut self.as_mut().get_db();
|
|
||||||
|
|
||||||
let image_id =
|
|
||||||
self.images().get(index as usize).unwrap().id;
|
|
||||||
|
|
||||||
let result =
|
|
||||||
delete(images.filter(id.eq(image_id))).execute(db);
|
|
||||||
|
|
||||||
match result {
|
|
||||||
Ok(_i) => {
|
|
||||||
unsafe {
|
|
||||||
self.as_mut().begin_remove_rows(
|
|
||||||
&QModelIndex::default(),
|
|
||||||
index,
|
|
||||||
index,
|
|
||||||
);
|
|
||||||
self.as_mut()
|
|
||||||
.images_mut()
|
|
||||||
.remove(index as usize);
|
|
||||||
self.as_mut().end_remove_rows();
|
|
||||||
}
|
|
||||||
println!("removed-item-at-index: {:?}", image_id);
|
|
||||||
println!("new-Vec: {:?}", self.as_mut().images());
|
|
||||||
true
|
|
||||||
}
|
|
||||||
Err(_e) => {
|
|
||||||
println!("Cannot connect to database");
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_db(self: Pin<&mut Self>) -> SqliteConnection {
|
|
||||||
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());
|
|
||||||
println!("DB: {:?}", db_url);
|
|
||||||
|
|
||||||
SqliteConnection::establish(&db_url).unwrap_or_else(
|
|
||||||
|_| panic!("error connecting to {}", db_url),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[qinvokable]
|
#[qinvokable]
|
||||||
pub fn new_item(mut self: Pin<&mut Self>, url: QUrl) {
|
fn new_item(self: Pin<&mut ImageModel>, url: QUrl);
|
||||||
println!("LETS INSERT THIS SUCKER!");
|
|
||||||
let file_path = PathBuf::from(url.path().to_string());
|
|
||||||
let name =
|
|
||||||
file_path.file_stem().unwrap().to_str().unwrap();
|
|
||||||
let image_id = self.rust().highest_id + 1;
|
|
||||||
let image_title = QString::from(name);
|
|
||||||
let image_path = url.to_qstring();
|
|
||||||
|
|
||||||
if self.as_mut().add_item(
|
|
||||||
image_id,
|
|
||||||
image_title,
|
|
||||||
image_path,
|
|
||||||
) {
|
|
||||||
println!("filename: {:?}", name);
|
|
||||||
self.as_mut().set_highest_id(image_id);
|
|
||||||
} else {
|
|
||||||
println!("Error in inserting item");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_item(
|
|
||||||
mut self: Pin<&mut Self>,
|
|
||||||
image_id: i32,
|
|
||||||
image_title: QString,
|
|
||||||
image_path: QString,
|
|
||||||
) -> bool {
|
|
||||||
let db = &mut self.as_mut().get_db();
|
|
||||||
// println!("{:?}", db);
|
|
||||||
let image = self::Image {
|
|
||||||
id: image_id,
|
|
||||||
title: image_title.clone(),
|
|
||||||
path: image_path.clone(),
|
|
||||||
};
|
|
||||||
println!("{:?}", image);
|
|
||||||
|
|
||||||
let result = insert_into(images)
|
|
||||||
.values((
|
|
||||||
id.eq(&image_id),
|
|
||||||
title.eq(&image_title.to_string()),
|
|
||||||
path.eq(&image_path.to_string()),
|
|
||||||
))
|
|
||||||
.execute(db);
|
|
||||||
println!("{:?}", result);
|
|
||||||
|
|
||||||
match result {
|
|
||||||
Ok(_i) => {
|
|
||||||
self.as_mut().add_image(image);
|
|
||||||
println!("{:?}", self.as_mut().images());
|
|
||||||
true
|
|
||||||
}
|
|
||||||
Err(_e) => {
|
|
||||||
println!("Cannot connect to database");
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_image(mut self: Pin<&mut Self>, image: self::Image) {
|
|
||||||
let index = self.as_ref().images().len() as i32;
|
|
||||||
println!("{:?}", image);
|
|
||||||
unsafe {
|
|
||||||
self.as_mut().begin_insert_rows(
|
|
||||||
&QModelIndex::default(),
|
|
||||||
index,
|
|
||||||
index,
|
|
||||||
);
|
|
||||||
self.as_mut().images_mut().push(image);
|
|
||||||
self.as_mut().end_insert_rows();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[qinvokable]
|
#[qinvokable]
|
||||||
pub fn update_title(
|
fn update_title(
|
||||||
mut self: Pin<&mut Self>,
|
self: Pin<&mut ImageModel>,
|
||||||
index: i32,
|
index: i32,
|
||||||
updated_title: QString,
|
updated_title: QString,
|
||||||
) -> bool {
|
) -> bool;
|
||||||
let mut vector_roles = QVector_i32::default();
|
|
||||||
vector_roles
|
|
||||||
.append(self.as_ref().get_role_id(Role::TitleRole));
|
|
||||||
let model_index = &self.as_ref().index(
|
|
||||||
index,
|
|
||||||
0,
|
|
||||||
&QModelIndex::default(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let db = &mut self.as_mut().get_db();
|
|
||||||
let result = update(images.filter(id.eq(index)))
|
|
||||||
.set(title.eq(updated_title.to_string()))
|
|
||||||
.execute(db);
|
|
||||||
match result {
|
|
||||||
Ok(_i) => {
|
|
||||||
for image in self
|
|
||||||
.as_mut()
|
|
||||||
.images_mut()
|
|
||||||
.iter_mut()
|
|
||||||
.filter(|x| x.id == index)
|
|
||||||
{
|
|
||||||
image.title = updated_title.clone();
|
|
||||||
println!("rust-title: {:?}", image.title);
|
|
||||||
}
|
|
||||||
self.as_mut().emit_data_changed(
|
|
||||||
model_index,
|
|
||||||
model_index,
|
|
||||||
&vector_roles,
|
|
||||||
);
|
|
||||||
true
|
|
||||||
}
|
|
||||||
Err(_e) => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[qinvokable]
|
#[qinvokable]
|
||||||
pub fn update_file_path(
|
fn update_file_path(
|
||||||
mut self: Pin<&mut Self>,
|
self: Pin<&mut ImageModel>,
|
||||||
index: i32,
|
index: i32,
|
||||||
updated_file_path: QString,
|
updated_file_path: QString,
|
||||||
) -> bool {
|
) -> bool;
|
||||||
let mut vector_roles = QVector_i32::default();
|
|
||||||
vector_roles
|
|
||||||
.append(self.as_ref().get_role_id(Role::PathRole));
|
|
||||||
let model_index = &self.as_ref().index(
|
|
||||||
index,
|
|
||||||
0,
|
|
||||||
&QModelIndex::default(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let db = &mut self.as_mut().get_db();
|
|
||||||
let result = update(images.filter(id.eq(index)))
|
|
||||||
.set(path.eq(updated_file_path.to_string()))
|
|
||||||
.execute(db);
|
|
||||||
match result {
|
|
||||||
Ok(_i) => {
|
|
||||||
for image in self
|
|
||||||
.as_mut()
|
|
||||||
.images_mut()
|
|
||||||
.iter_mut()
|
|
||||||
.filter(|x| x.id == index)
|
|
||||||
{
|
|
||||||
image.path = updated_file_path.clone();
|
|
||||||
println!("rust-title: {:?}", image.path);
|
|
||||||
}
|
|
||||||
self.as_mut().emit_data_changed(
|
|
||||||
model_index,
|
|
||||||
model_index,
|
|
||||||
&vector_roles,
|
|
||||||
);
|
|
||||||
true
|
|
||||||
}
|
|
||||||
Err(_e) => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[qinvokable]
|
#[qinvokable]
|
||||||
pub fn get_item(
|
fn get_item(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut ImageModel>,
|
||||||
index: i32,
|
index: i32,
|
||||||
) -> QMap_QString_QVariant {
|
) -> QMap_QString_QVariant;
|
||||||
println!("{index}");
|
|
||||||
let mut qvariantmap = QMap_QString_QVariant::default();
|
|
||||||
let idx = self.index(index, 0, &QModelIndex::default());
|
|
||||||
if !idx.is_valid() {
|
|
||||||
return qvariantmap;
|
|
||||||
}
|
|
||||||
let role_names = self.as_ref().role_names();
|
|
||||||
let role_names_iter = role_names.iter();
|
|
||||||
if let Some(image) =
|
|
||||||
self.rust().images.get(index as usize)
|
|
||||||
{
|
|
||||||
for i in role_names_iter {
|
|
||||||
qvariantmap.insert(
|
|
||||||
QString::from(&i.1.to_string()),
|
|
||||||
self.as_ref().data(&idx, *i.0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
qvariantmap
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_role_id(&self, role: Role) -> i32 {
|
|
||||||
match role {
|
|
||||||
Role::IdRole => 0,
|
|
||||||
Role::TitleRole => 1,
|
|
||||||
Role::PathRole => 2,
|
|
||||||
_ => 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create Rust bindings for C++ functions of the base class (QAbstractItemModel)
|
impl cxx_qt::Threading for ImageModel {}
|
||||||
#[cxx_qt::inherit]
|
|
||||||
extern "C++" {
|
unsafe extern "RustQt" {
|
||||||
|
#[inherit]
|
||||||
unsafe fn begin_insert_rows(
|
unsafe fn begin_insert_rows(
|
||||||
self: Pin<&mut qobject::ImageModel>,
|
self: Pin<&mut ImageModel>,
|
||||||
parent: &QModelIndex,
|
parent: &QModelIndex,
|
||||||
first: i32,
|
first: i32,
|
||||||
last: i32,
|
last: i32,
|
||||||
);
|
);
|
||||||
unsafe fn end_insert_rows(
|
|
||||||
self: Pin<&mut qobject::ImageModel>,
|
|
||||||
);
|
|
||||||
|
|
||||||
|
#[inherit]
|
||||||
|
unsafe fn end_insert_rows(self: Pin<&mut ImageModel>);
|
||||||
|
|
||||||
|
#[inherit]
|
||||||
unsafe fn begin_remove_rows(
|
unsafe fn begin_remove_rows(
|
||||||
self: Pin<&mut qobject::ImageModel>,
|
self: Pin<&mut ImageModel>,
|
||||||
parent: &QModelIndex,
|
parent: &QModelIndex,
|
||||||
first: i32,
|
first: i32,
|
||||||
last: i32,
|
last: i32,
|
||||||
);
|
);
|
||||||
unsafe fn end_remove_rows(
|
|
||||||
self: Pin<&mut qobject::ImageModel>,
|
|
||||||
);
|
|
||||||
|
|
||||||
unsafe fn begin_reset_model(
|
#[inherit]
|
||||||
self: Pin<&mut qobject::ImageModel>,
|
unsafe fn begin_move_rows(
|
||||||
);
|
self: Pin<&mut ImageModel>,
|
||||||
unsafe fn end_reset_model(
|
source_parent: &QModelIndex,
|
||||||
self: Pin<&mut qobject::ImageModel>,
|
source_first: i32,
|
||||||
);
|
source_last: i32,
|
||||||
}
|
destination_parent: &QModelIndex,
|
||||||
|
destination_child: i32,
|
||||||
|
) -> bool;
|
||||||
|
|
||||||
#[cxx_qt::inherit]
|
#[inherit]
|
||||||
unsafe extern "C++" {
|
unsafe fn end_move_rows(self: Pin<&mut ImageModel>);
|
||||||
#[cxx_name = "canFetchMore"]
|
|
||||||
fn base_can_fetch_more(
|
#[inherit]
|
||||||
self: &qobject::ImageModel,
|
unsafe fn end_remove_rows(self: Pin<&mut ImageModel>);
|
||||||
|
|
||||||
|
#[inherit]
|
||||||
|
unsafe fn begin_reset_model(self: Pin<&mut ImageModel>);
|
||||||
|
|
||||||
|
#[inherit]
|
||||||
|
unsafe fn end_reset_model(self: Pin<&mut ImageModel>);
|
||||||
|
|
||||||
|
#[inherit]
|
||||||
|
fn can_fetch_more(
|
||||||
|
self: &ImageModel,
|
||||||
parent: &QModelIndex,
|
parent: &QModelIndex,
|
||||||
) -> bool;
|
) -> bool;
|
||||||
|
|
||||||
|
#[inherit]
|
||||||
fn index(
|
fn index(
|
||||||
self: &qobject::ImageModel,
|
self: &ImageModel,
|
||||||
row: i32,
|
row: i32,
|
||||||
column: i32,
|
column: i32,
|
||||||
parent: &QModelIndex,
|
parent: &QModelIndex,
|
||||||
) -> QModelIndex;
|
) -> QModelIndex;
|
||||||
}
|
|
||||||
|
|
||||||
// QAbstractListModel implementation
|
|
||||||
impl qobject::ImageModel {
|
|
||||||
#[qinvokable(cxx_override)]
|
|
||||||
fn data(&self, index: &QModelIndex, role: i32) -> QVariant {
|
|
||||||
if let Some(image) =
|
|
||||||
self.images().get(index.row() as usize)
|
|
||||||
{
|
|
||||||
return match role {
|
|
||||||
0 => QVariant::from(&image.id),
|
|
||||||
1 => QVariant::from(&image.title),
|
|
||||||
2 => QVariant::from(&image.path),
|
|
||||||
_ => QVariant::default(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariant::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 {
|
|
||||||
self.base_can_fetch_more(parent)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[qinvokable(cxx_override)]
|
|
||||||
pub fn role_names(&self) -> QHash_i32_QByteArray {
|
|
||||||
let mut roles = QHash_i32_QByteArray::default();
|
|
||||||
roles.insert(0, cxx_qt_lib::QByteArray::from("id"));
|
|
||||||
roles.insert(1, cxx_qt_lib::QByteArray::from("title"));
|
|
||||||
roles.insert(2, cxx_qt_lib::QByteArray::from("filePath"));
|
|
||||||
roles
|
|
||||||
}
|
|
||||||
|
|
||||||
#[qinvokable(cxx_override)]
|
|
||||||
pub fn row_count(&self, _parent: &QModelIndex) -> i32 {
|
|
||||||
let cnt = self.rust().images.len() as i32;
|
|
||||||
// self.as_mut().set_count(cnt);
|
|
||||||
// println!("row count is {cnt}");
|
|
||||||
cnt
|
|
||||||
}
|
|
||||||
|
|
||||||
#[qinvokable]
|
#[qinvokable]
|
||||||
pub fn count(mut self: Pin<&mut Self>) -> i32 {
|
#[cxx_override]
|
||||||
let cnt = self.rust().images.len() as i32;
|
fn data(
|
||||||
self.as_mut().set_count_rows(cnt);
|
self: &ImageModel,
|
||||||
cnt
|
index: &QModelIndex,
|
||||||
|
role: i32,
|
||||||
|
) -> QVariant;
|
||||||
|
|
||||||
|
#[qinvokable]
|
||||||
|
#[cxx_override]
|
||||||
|
fn role_names(self: &ImageModel) -> QHash_i32_QByteArray;
|
||||||
|
|
||||||
|
#[qinvokable]
|
||||||
|
#[cxx_override]
|
||||||
|
fn row_count(self: &ImageModel, _parent: &QModelIndex)
|
||||||
|
-> i32;
|
||||||
|
|
||||||
|
#[qinvokable]
|
||||||
|
fn count(self: &ImageModel) -> i32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use crate::image_model::image_model::Image;
|
||||||
|
use crate::schema::images::dsl::*;
|
||||||
|
use diesel::sqlite::SqliteConnection;
|
||||||
|
use diesel::{delete, insert_into, prelude::*, update};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
#[derive(Default, Clone, Debug)]
|
||||||
|
pub struct Image {
|
||||||
|
id: i32,
|
||||||
|
title: QString,
|
||||||
|
path: QString,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct ImageModelRust {
|
||||||
|
count_rows: i32,
|
||||||
|
highest_id: i32,
|
||||||
|
images: Vec<self::Image>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl qobject::ImageModel {
|
||||||
|
pub fn clear(mut self: Pin<&mut Self>) {
|
||||||
|
unsafe {
|
||||||
|
self.as_mut().begin_reset_model();
|
||||||
|
self.as_mut().images_mut().clear();
|
||||||
|
self.as_mut().end_reset_model();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setup(mut self: Pin<&mut Self>) {
|
||||||
|
let db = &mut self.as_mut().get_db();
|
||||||
|
let results = images
|
||||||
|
.load::<crate::models::Image>(db)
|
||||||
|
.expect("Error loading images");
|
||||||
|
self.as_mut().set_highest_id(0);
|
||||||
|
|
||||||
|
println!("SHOWING IMAGES");
|
||||||
|
println!("--------------");
|
||||||
|
for image in results {
|
||||||
|
println!("{}", image.title);
|
||||||
|
println!("{}", image.id);
|
||||||
|
println!("{}", image.path);
|
||||||
|
println!("--------------");
|
||||||
|
if self.as_mut().highest_id() < &image.id {
|
||||||
|
self.as_mut().set_highest_id(image.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
let img = self::Image {
|
||||||
|
id: image.id,
|
||||||
|
title: QString::from(&image.title),
|
||||||
|
path: QString::from(&image.path),
|
||||||
|
};
|
||||||
|
|
||||||
|
self.as_mut().add_image(img);
|
||||||
|
}
|
||||||
|
println!("--------------------------------------");
|
||||||
|
println!("{:?}", self.as_mut().images());
|
||||||
|
println!("--------------------------------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_item(mut self: Pin<&mut Self>, index: i32) -> bool {
|
||||||
|
if index < 0 || (index as usize) >= self.images().len() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let db = &mut self.as_mut().get_db();
|
||||||
|
|
||||||
|
let image_id = self.images().get(index as usize).unwrap().id;
|
||||||
|
|
||||||
|
let result =
|
||||||
|
delete(images.filter(id.eq(image_id))).execute(db);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(_i) => {
|
||||||
|
unsafe {
|
||||||
|
self.as_mut().begin_remove_rows(
|
||||||
|
&QModelIndex::default(),
|
||||||
|
index,
|
||||||
|
index,
|
||||||
|
);
|
||||||
|
self.as_mut().images_mut().remove(index as usize);
|
||||||
|
self.as_mut().end_remove_rows();
|
||||||
|
}
|
||||||
|
println!("removed-item-at-index: {:?}", image_id);
|
||||||
|
println!("new-Vec: {:?}", self.as_mut().images());
|
||||||
|
true
|
||||||
|
}
|
||||||
|
Err(_e) => {
|
||||||
|
println!("Cannot connect to database");
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_db(self: Pin<&mut Self>) -> SqliteConnection {
|
||||||
|
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());
|
||||||
|
println!("DB: {:?}", db_url);
|
||||||
|
|
||||||
|
SqliteConnection::establish(&db_url).unwrap_or_else(|_| {
|
||||||
|
panic!("error connecting to {}", db_url)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_item(mut self: Pin<&mut Self>, url: QUrl) {
|
||||||
|
println!("LETS INSERT THIS SUCKER!");
|
||||||
|
let file_path = PathBuf::from(url.path().to_string());
|
||||||
|
let name = file_path.file_stem().unwrap().to_str().unwrap();
|
||||||
|
let image_id = self.rust().highest_id + 1;
|
||||||
|
let image_title = QString::from(name);
|
||||||
|
let image_path = url.to_qstring();
|
||||||
|
|
||||||
|
if self.as_mut().add_item(image_id, image_title, image_path) {
|
||||||
|
println!("filename: {:?}", name);
|
||||||
|
self.as_mut().set_highest_id(image_id);
|
||||||
|
} else {
|
||||||
|
println!("Error in inserting item");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_item(
|
||||||
|
mut self: Pin<&mut Self>,
|
||||||
|
image_id: i32,
|
||||||
|
image_title: QString,
|
||||||
|
image_path: QString,
|
||||||
|
) -> bool {
|
||||||
|
let db = &mut self.as_mut().get_db();
|
||||||
|
// println!("{:?}", db);
|
||||||
|
let image = self::Image {
|
||||||
|
id: image_id,
|
||||||
|
title: image_title.clone(),
|
||||||
|
path: image_path.clone(),
|
||||||
|
};
|
||||||
|
println!("{:?}", image);
|
||||||
|
|
||||||
|
let result = insert_into(images)
|
||||||
|
.values((
|
||||||
|
id.eq(&image_id),
|
||||||
|
title.eq(&image_title.to_string()),
|
||||||
|
path.eq(&image_path.to_string()),
|
||||||
|
))
|
||||||
|
.execute(db);
|
||||||
|
println!("{:?}", result);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(_i) => {
|
||||||
|
self.as_mut().add_image(image);
|
||||||
|
println!("{:?}", self.as_mut().images());
|
||||||
|
true
|
||||||
|
}
|
||||||
|
Err(_e) => {
|
||||||
|
println!("Cannot connect to database");
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_image(mut self: Pin<&mut Self>, image: self::Image) {
|
||||||
|
let index = self.as_ref().images().len() as i32;
|
||||||
|
println!("{:?}", image);
|
||||||
|
unsafe {
|
||||||
|
self.as_mut().begin_insert_rows(
|
||||||
|
&QModelIndex::default(),
|
||||||
|
index,
|
||||||
|
index,
|
||||||
|
);
|
||||||
|
self.as_mut().images_mut().push(image);
|
||||||
|
self.as_mut().end_insert_rows();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_title(
|
||||||
|
mut self: Pin<&mut Self>,
|
||||||
|
index: i32,
|
||||||
|
updated_title: QString,
|
||||||
|
) -> bool {
|
||||||
|
let mut vector_roles = QVector_i32::default();
|
||||||
|
vector_roles
|
||||||
|
.append(self.as_ref().get_role_id(Role::TitleRole));
|
||||||
|
let model_index =
|
||||||
|
&self.as_ref().index(index, 0, &QModelIndex::default());
|
||||||
|
|
||||||
|
let db = &mut self.as_mut().get_db();
|
||||||
|
let result = update(images.filter(id.eq(index)))
|
||||||
|
.set(title.eq(updated_title.to_string()))
|
||||||
|
.execute(db);
|
||||||
|
match result {
|
||||||
|
Ok(_i) => {
|
||||||
|
for image in self
|
||||||
|
.as_mut()
|
||||||
|
.images_mut()
|
||||||
|
.iter_mut()
|
||||||
|
.filter(|x| x.id == index)
|
||||||
|
{
|
||||||
|
image.title = updated_title.clone();
|
||||||
|
println!("rust-title: {:?}", image.title);
|
||||||
|
}
|
||||||
|
self.as_mut().emit_data_changed(
|
||||||
|
model_index,
|
||||||
|
model_index,
|
||||||
|
&vector_roles,
|
||||||
|
);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
Err(_e) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_file_path(
|
||||||
|
mut self: Pin<&mut Self>,
|
||||||
|
index: i32,
|
||||||
|
updated_file_path: QString,
|
||||||
|
) -> bool {
|
||||||
|
let mut vector_roles = QVector_i32::default();
|
||||||
|
vector_roles
|
||||||
|
.append(self.as_ref().get_role_id(Role::PathRole));
|
||||||
|
let model_index =
|
||||||
|
&self.as_ref().index(index, 0, &QModelIndex::default());
|
||||||
|
|
||||||
|
let db = &mut self.as_mut().get_db();
|
||||||
|
let result = update(images.filter(id.eq(index)))
|
||||||
|
.set(path.eq(updated_file_path.to_string()))
|
||||||
|
.execute(db);
|
||||||
|
match result {
|
||||||
|
Ok(_i) => {
|
||||||
|
for image in self
|
||||||
|
.as_mut()
|
||||||
|
.images_mut()
|
||||||
|
.iter_mut()
|
||||||
|
.filter(|x| x.id == index)
|
||||||
|
{
|
||||||
|
image.path = updated_file_path.clone();
|
||||||
|
println!("rust-title: {:?}", image.path);
|
||||||
|
}
|
||||||
|
self.as_mut().emit_data_changed(
|
||||||
|
model_index,
|
||||||
|
model_index,
|
||||||
|
&vector_roles,
|
||||||
|
);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
Err(_e) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_item(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
index: i32,
|
||||||
|
) -> QMap_QString_QVariant {
|
||||||
|
println!("{index}");
|
||||||
|
let mut qvariantmap = QMap_QString_QVariant::default();
|
||||||
|
let idx = self.index(index, 0, &QModelIndex::default());
|
||||||
|
if !idx.is_valid() {
|
||||||
|
return qvariantmap;
|
||||||
|
}
|
||||||
|
let role_names = self.as_ref().role_names();
|
||||||
|
let role_names_iter = role_names.iter();
|
||||||
|
if let Some(image) = self.rust().images.get(index as usize) {
|
||||||
|
for i in role_names_iter {
|
||||||
|
qvariantmap.insert(
|
||||||
|
QString::from(&i.1.to_string()),
|
||||||
|
self.as_ref().data(&idx, *i.0),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
qvariantmap
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_role_id(&self, role: Role) -> i32 {
|
||||||
|
match role {
|
||||||
|
qobject::Role::Id => 0,
|
||||||
|
qobject::Role::Title => 1,
|
||||||
|
qobject::Role::Path => 2,
|
||||||
|
_ => 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QAbstractListModel implementation
|
||||||
|
impl qobject::ImageModel {
|
||||||
|
fn data(&self, index: &QModelIndex, role: i32) -> QVariant {
|
||||||
|
let role = qobject::Roles { repr: role };
|
||||||
|
if let Some(image) = self.images().get(index.row() as usize) {
|
||||||
|
return match role {
|
||||||
|
qobject::Roles::Id => QVariant::from(&image.id),
|
||||||
|
qobject::Roles::Title => QVariant::from(&image.title),
|
||||||
|
qobject::Roles::Path => QVariant::from(&image.path),
|
||||||
|
_ => QVariant::default(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Example of overriding a C++ virtual method and calling the base class implementation.
|
||||||
|
|
||||||
|
pub fn can_fetch_more(&self, parent: &QModelIndex) -> bool {
|
||||||
|
self.base_can_fetch_more(parent)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn role_names(&self) -> QHash_i32_QByteArray {
|
||||||
|
let mut roles = QHash_i32_QByteArray::default();
|
||||||
|
roles.insert(
|
||||||
|
qobject::Roles::Id.repr,
|
||||||
|
cxx_qt_lib::QByteArray::from("id"),
|
||||||
|
);
|
||||||
|
roles.insert(
|
||||||
|
qobject::Roles::Title.repr,
|
||||||
|
cxx_qt_lib::QByteArray::from("title"),
|
||||||
|
);
|
||||||
|
roles.insert(
|
||||||
|
qobject::Roles::Path.repr,
|
||||||
|
cxx_qt_lib::QByteArray::from("filePath"),
|
||||||
|
);
|
||||||
|
roles
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn row_count(&self, _parent: &QModelIndex) -> i32 {
|
||||||
|
let cnt = self.rust().images.len() as i32;
|
||||||
|
// self.as_mut().set_count(cnt);
|
||||||
|
// println!("row count is {cnt}");
|
||||||
|
cnt
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn count(mut self: Pin<&mut Self>) -> i32 {
|
||||||
|
let cnt = self.rust().images.len() as i32;
|
||||||
|
self.as_mut().set_count_rows(cnt);
|
||||||
|
cnt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -16,3 +16,4 @@ pub mod utils;
|
||||||
pub mod video_model;
|
pub mod video_model;
|
||||||
pub mod ytdl;
|
pub mod ytdl;
|
||||||
// mod video_thumbnail;
|
// mod video_thumbnail;
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -253,7 +253,6 @@ pub struct SlideModelRust {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl qobject::SlideModel {
|
impl qobject::SlideModel {
|
||||||
#[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,
|
||||||
|
@ -285,7 +284,6 @@ impl qobject::SlideModel {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
#[qinvokable]
|
|
||||||
pub fn clear(mut self: Pin<&mut Self>) {
|
pub fn clear(mut self: Pin<&mut Self>) {
|
||||||
println!("CLEARING ALL SLIDES");
|
println!("CLEARING ALL SLIDES");
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -295,7 +293,6 @@ impl qobject::SlideModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[qinvokable]
|
|
||||||
pub fn remove_item_from_service(
|
pub fn remove_item_from_service(
|
||||||
mut self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
index: i32,
|
index: i32,
|
||||||
|
@ -324,7 +321,6 @@ impl qobject::SlideModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[qinvokable]
|
|
||||||
pub fn remove_item(mut self: Pin<&mut Self>, index: i32) {
|
pub fn remove_item(mut self: Pin<&mut Self>, index: i32) {
|
||||||
if index < 0 || (index as usize) >= self.slides().len() {
|
if index < 0 || (index as usize) >= self.slides().len() {
|
||||||
return;
|
return;
|
||||||
|
@ -394,7 +390,6 @@ impl qobject::SlideModel {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[qinvokable]
|
|
||||||
pub fn insert_item_from_service(
|
pub fn insert_item_from_service(
|
||||||
mut self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
index: i32,
|
index: i32,
|
||||||
|
@ -586,7 +581,6 @@ impl qobject::SlideModel {
|
||||||
println!("Item added in slide model!");
|
println!("Item added in slide model!");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[qinvokable]
|
|
||||||
pub fn add_item_from_service(
|
pub fn add_item_from_service(
|
||||||
mut self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
index: i32,
|
index: i32,
|
||||||
|
@ -742,7 +736,6 @@ impl qobject::SlideModel {
|
||||||
println!("Item added in rust model!");
|
println!("Item added in rust model!");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[qinvokable]
|
|
||||||
pub fn move_item_from_service(
|
pub fn move_item_from_service(
|
||||||
mut self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
source_index: i32,
|
source_index: i32,
|
||||||
|
@ -948,7 +941,6 @@ impl qobject::SlideModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[qinvokable]
|
|
||||||
pub fn get_item(
|
pub fn get_item(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
index: i32,
|
index: i32,
|
||||||
|
@ -972,7 +964,6 @@ impl qobject::SlideModel {
|
||||||
qvariantmap
|
qvariantmap
|
||||||
}
|
}
|
||||||
|
|
||||||
#[qinvokable]
|
|
||||||
pub fn get_slide_from_service(
|
pub fn get_slide_from_service(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
index: i32,
|
index: i32,
|
||||||
|
@ -992,7 +983,6 @@ impl qobject::SlideModel {
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
#[qinvokable]
|
|
||||||
pub fn activate(mut self: Pin<&mut Self>, index: i32) -> bool {
|
pub fn activate(mut self: Pin<&mut Self>, index: i32) -> bool {
|
||||||
let rc = self.as_ref().count() - 1;
|
let rc = self.as_ref().count() - 1;
|
||||||
let tl = &self.as_ref().index(0, 0, &QModelIndex::default());
|
let tl = &self.as_ref().index(0, 0, &QModelIndex::default());
|
||||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue