158 lines
6.2 KiB
Rust
158 lines
6.2 KiB
Rust
mod callbacks;
|
|
|
|
use crate::gui::MutexTIMManager;
|
|
use super::{main_tab::SharedMainTab, MainWindowRef, display_error};
|
|
use slint::{Image, SharedString};
|
|
use std::{cell::RefCell, rc::Rc};
|
|
use tim_tool::logic::tim::types::Encoding;
|
|
use tool_helper::Error;
|
|
|
|
pub struct FileTab {
|
|
main_window: MainWindowRef,
|
|
encoding_options: Rc<slint::VecModel<SharedString>>
|
|
}
|
|
|
|
pub type SharedFileTab = Rc<RefCell<FileTab>>;
|
|
|
|
impl FileTab {
|
|
fn update_encoding_options(&self, width: u32, height: u32, has_palette: bool) -> Result<&str, Error> {
|
|
self.encoding_options.clear();
|
|
|
|
if has_palette {
|
|
let mut selected_str = Encoding::EightBit.to_str();
|
|
let has_4bit = width%4 == 0;
|
|
let has_8bit = width%2 == 0;
|
|
|
|
if !has_4bit && !has_8bit {
|
|
return Err(Error::from_text(format!("Image width must be multiple of 2 and or 4 ({}/{})", width, height)));
|
|
}
|
|
|
|
if has_4bit {
|
|
self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Encoding::FourBit.to_str(), width/4, height)));
|
|
selected_str = Encoding::FourBit.to_str();
|
|
}
|
|
|
|
if has_8bit {
|
|
self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Encoding::EightBit.to_str(), width/2, height)));
|
|
}
|
|
Ok(selected_str)
|
|
}
|
|
|
|
else {
|
|
self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Encoding::FullColor.to_str(), width, height)));
|
|
Ok(Encoding::FullColor.to_str())
|
|
}
|
|
}
|
|
|
|
pub fn new(main_window: MainWindowRef, main_tab: SharedMainTab, tim_manager: MutexTIMManager) -> SharedFileTab {
|
|
let encoding_options = Rc::new(slint::VecModel::from(Vec::<SharedString>::new()));
|
|
main_window.borrow().set_file_tab_encoding_options(encoding_options.clone().into());
|
|
|
|
let object = Rc::new(RefCell::new(FileTab{main_window: main_window.clone(), encoding_options}));
|
|
|
|
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_browse_file(tim_manager.clone()));
|
|
main_window.borrow().on_file_tab_browse_convert_image(move || {
|
|
if let Err(error) = function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow()) {
|
|
display_error("Loading file failed", &error.to_string());
|
|
}
|
|
});
|
|
|
|
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_update_palette_size(tim_manager.clone()));
|
|
main_window.borrow().on_file_tab_update_palette_size(move |width, height| {
|
|
if let Err(error) = function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow(), width as u32, height as u32) {
|
|
display_error("Loadind file failed", &error.to_string());
|
|
}
|
|
});
|
|
|
|
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_add_image(tim_manager.clone()));
|
|
main_window.borrow().on_file_tab_add_convert_image(move || {
|
|
if let Err(error) = function(&mut cloned_object.borrow_mut(), &mut main_tab.borrow_mut(), &cloned_main_window.borrow()) {
|
|
display_error("Adding file failed", &error.to_string());
|
|
}
|
|
});
|
|
|
|
object
|
|
}
|
|
|
|
pub fn clear_load(&self) {
|
|
let main_window = self.main_window.borrow();
|
|
|
|
self.encoding_options.clear();
|
|
main_window.set_file_tab_browse_path("".into());
|
|
main_window.set_file_tab_image_data(Image::default());
|
|
main_window.set_file_tab_image_width(0);
|
|
main_window.set_file_tab_image_height(0);
|
|
main_window.set_file_tab_palette_data(Image::default());
|
|
main_window.set_file_tab_palette_width(0);
|
|
main_window.set_file_tab_palette_height(0);
|
|
main_window.set_file_tab_image_name("".into());
|
|
main_window.set_file_tab_enable(false);
|
|
}
|
|
|
|
pub fn update_new_loaded_file(&self, file_name: Option<String>, image: Image, palette: Option<Image>) -> Result<(), Error> {
|
|
let has_palette = palette.is_some();
|
|
|
|
self.update_palette(palette);
|
|
|
|
let main_window = self.main_window.borrow();
|
|
let image_size = image.size();
|
|
|
|
main_window.set_file_tab_image_data(image);
|
|
main_window.set_file_tab_image_width(image_size.width as i32);
|
|
main_window.set_file_tab_image_height(image_size.height as i32);
|
|
if let Some(file_name) = file_name {
|
|
main_window.set_file_tab_image_name(file_name.into());
|
|
}
|
|
|
|
else {
|
|
main_window.set_file_tab_image_name("".into());
|
|
}
|
|
|
|
main_window.set_file_tab_selected_encoding(SharedString::from(self.update_encoding_options(image_size.width, image_size.height, has_palette)?));
|
|
main_window.set_file_tab_enable(true);
|
|
Ok(())
|
|
}
|
|
|
|
pub fn update_palette(&self, palette: Option<Image>) {
|
|
let main_window = self.main_window.borrow();
|
|
|
|
if let Some(palette) = palette {
|
|
let size = palette.size();
|
|
|
|
main_window.set_file_tab_palette_data(palette);
|
|
main_window.set_file_tab_palette_width(size.width as i32);
|
|
main_window.set_file_tab_palette_height(size.height as i32);
|
|
main_window.set_file_tab_palette_visible(true);
|
|
}
|
|
|
|
else {
|
|
main_window.set_file_tab_palette_width(0);
|
|
main_window.set_file_tab_palette_height(0);
|
|
main_window.set_file_tab_palette_visible(false);
|
|
}
|
|
}
|
|
|
|
pub fn get_file_name(&self) -> String {
|
|
self.main_window.borrow().get_file_tab_image_name().to_string()
|
|
}
|
|
|
|
pub fn get_encoding(&self) -> Result<Encoding, Error> {
|
|
let selected_encoding = self.main_window.borrow().get_file_tab_selected_encoding();
|
|
|
|
if selected_encoding.starts_with(Encoding::FourBit.to_str()) {
|
|
Ok(Encoding::FourBit)
|
|
}
|
|
|
|
else if selected_encoding.starts_with(Encoding::EightBit.to_str()) {
|
|
Ok(Encoding::EightBit)
|
|
}
|
|
|
|
else if selected_encoding.starts_with(Encoding::FullColor.to_str()) {
|
|
Ok(Encoding::FullColor)
|
|
}
|
|
|
|
else {
|
|
Err(Error::from_str("Failed to obtain encoding"))
|
|
}
|
|
}
|
|
} |