Retrieve file conversion path

This commit is contained in:
Jaby 2025-02-10 22:16:25 +01:00
parent 7f831bc09b
commit 3054fab315
4 changed files with 145 additions and 26 deletions

View File

@ -2,7 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod gui;
use slint::Model;
use slint::{Model, SharedString};
use rfd::{FileDialog, MessageDialog};
use std::{cell::RefCell, ffi::OsStr, path::PathBuf, rc::Rc};
use tool_helper::Error;
@ -10,20 +10,22 @@ use tool_helper::Error;
slint::include_modules!();
struct GUIElements {
pub main_window: Rc<RefCell<MainWindow>>,
vram_file_list: Rc<slint::VecModel<slint::StandardListViewItem>>,
vram_image_list: Rc<slint::VecModel<VRAMImage>>
vram_image_list: Rc<slint::VecModel<VRAMImage>>,
}
impl GUIElements {
pub fn new(main_window: &MainWindow) -> GUIElements {
let vram_file_list:Vec<slint::StandardListViewItem> = main_window.get_main_tab_vram_file_list().iter().collect();
pub fn new(main_window: Rc<RefCell<MainWindow>>) -> GUIElements {
let vram_file_list:Vec<slint::StandardListViewItem> = main_window.borrow().get_main_tab_vram_file_list().iter().collect();
let vram_file_list = Rc::new(slint::VecModel::from(vram_file_list));
let vram_image_list = Rc::new(slint::VecModel::from(Vec::<VRAMImage>::new()));
main_window.set_main_tab_vram_file_list(vram_file_list.clone().into());
main_window.set_main_tab_vram_images(vram_image_list.clone().into());
GUIElements{vram_file_list, vram_image_list}
main_window.borrow().set_main_tab_vram_file_list(vram_file_list.clone().into());
main_window.borrow().set_main_tab_vram_images(vram_image_list.clone().into());
GUIElements{main_window, vram_file_list, vram_image_list}
}
pub fn add_new_vram_file(&mut self, file: PathBuf) -> Result<(), Error> {
@ -81,35 +83,57 @@ impl GUIElements {
}
fn main() -> Result<(), slint::PlatformError> {
let main_window = MainWindow::new()?;
let gui_elements = Rc::new(RefCell::new(GUIElements::new(&main_window)));
let main_window = Rc::new(RefCell::new(MainWindow::new()?));
let gui_elements = Rc::new(RefCell::new(GUIElements::new(main_window.clone())));
let main_window = main_window.borrow();
main_window.set_main_tab_vram_bg(GUIElements::create_bg()?);
let gui_elements_cloned = gui_elements.clone();
main_window.on_main_tab_add_file_clicked(move || {
let mut gui_elements = gui_elements_cloned.borrow_mut();
let file = FileDialog::new()
.add_filter("Images", &["png", "bmp", "jpeg"])
.add_filter("All", &[""])
.set_title("Add image")
//.set_directory("/")
.set_title("Add TIM image")
.pick_file();
if let Some(file) = file {
if let Result::Err(error) = gui_elements_cloned.borrow_mut().add_new_vram_file(file) {
if let Result::Err(error) = gui_elements.add_new_vram_file(file) {
MessageDialog::new().set_title("Loading Image failed").set_level(rfd::MessageLevel::Error).set_description(error.to_string()).show();
}
}
});
let gui_elements_cloned = gui_elements.clone();
main_window.on_main_tab_remove_file_clicked(move |idx: i32| {
let mut gui_elements = gui_elements_cloned.borrow_mut();
if idx >= 0 {
gui_elements_cloned.borrow_mut().remove_vram_file(idx as usize);
gui_elements.remove_vram_file(idx as usize);
}
});
let gui_elements_cloned = gui_elements.clone();
main_window.on_move_vram_image(move |idx: i32, dx: i32, dy: i32| {
gui_elements_cloned.borrow_mut().move_vram_image(idx as usize, dx, dy);
let mut gui_elements = gui_elements_cloned.borrow_mut();
gui_elements.move_vram_image(idx as usize, dx, dy);
});
let gui_elements_cloned = gui_elements.clone();
main_window.on_file_tab_browse_convert_image(move || {
let gui_elements = gui_elements_cloned.borrow_mut();
let file = FileDialog::new()
.add_filter("Images", &["png", "bmp", "jpeg"])
.add_filter("All", &[""])
.set_title("Select image file")
.pick_file();
if let Some(file) = file {
let main_window = gui_elements.main_window.borrow();
if let Some(file_path) = file.to_str() {
main_window.set_file_tab_browse_path(SharedString::from(file_path));
}
}
});
main_window.run()

View File

@ -13,6 +13,10 @@ export component MainWindow inherits Window {
callback main_tab_remove_file_clicked <=> main_tab.remove_file_clicked;
callback move_vram_image <=> main_tab.move_vram_image;
// Convert Image values
in-out property file_tab_browse_path <=> file_tab.conv_image_path;
callback file_tab_browse_convert_image <=> file_tab.conv_image_browse_clicked;
title: "TIM Tool 0.1.0";
width: tab_widget.width;
height: tab_widget.height;
@ -26,7 +30,7 @@ export component MainWindow inherits Window {
current-index: 1;
Tab {
title: "File";
FileTab {
file_tab := FileTab {
x: 0px;
y: 0px;
}

View File

@ -1,3 +1,94 @@
export component FileTab inherits Rectangle {
import { Button, TabWidget, LineEdit, GroupBox } from "std-widgets.slint";
enum State {
ConvertImage,
Test
}
component ConvertImageWidget inherits Rectangle {
in-out property <string> path;
callback browse_clicked();
background: #D0D0D0;
width: 100%;
height: 100%;
GroupBox {
title: "Convert image to TIM";
x: 4px;
y: 4px;
VerticalLayout {
alignment: start;
Text {
text: "Select image file to convert to TIM";
}
LineEdit {
width: 200%;
text: path;
}
HorizontalLayout {
alignment: start;
padding: 4px;
Button {
text: "Browse";
clicked => {browse_clicked();}
}
Button {
text: "Convert";
}
}
}
}
}
component TestWidget inherits Rectangle {
Text {
text: "!!Planschbecken!!";
}
}
export component FileTab inherits Rectangle {
in-out property <string> conv_image_path;
callback conv_image_browse_clicked;
property <State> state: ConvertImage;
x: 0px;
y: 0px;
HorizontalLayout {
padding: 4px;
VerticalLayout {
padding: 4px;
width: 20%;
alignment: start;
Button {
text: "Convert image file";
clicked => {
root.state = State.ConvertImage;
}
}
Button {
text: "Testing";
clicked => {
root.state = State.Test;
}
}
}
VerticalLayout {
padding: 4px;
alignment: start;
if root.state == State.ConvertImage : ConvertImageWidget {
path <=> root.conv_image_path;
browse_clicked => {
root.conv_image_browse_clicked();
}
}
if root.state == State.Test : TestWidget {
}
}
}
}

View File

@ -91,7 +91,7 @@ export component MainTab inherits Rectangle {
HorizontalLayout {
padding: 4px;
GroupBox {
title: "Added Files";
title: "Added TIMs";
VerticalLayout {
alignment: start;
padding: 4px;
@ -103,11 +103,11 @@ export component MainTab inherits Rectangle {
HorizontalLayout {
padding: 4px;
Button {
text: "Add File";
text: "Add TIM";
clicked => {root.add_file_clicked();}
}
Button {
text: "Remove File";
text: "Remove TIM";
clicked => {
root.remove_file_clicked(vram_files_list.current_item);
vram_files_list.current-item = -1;