From cc12a7d907a958fa9d646c05c98ac1b2ad7efb72 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 5 Oct 2020 14:21:30 +0200 Subject: Manga: add f keybinding to fit image to window --- src/QuickMedia.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 8865adb..4b51960 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -476,6 +476,9 @@ namespace QuickMedia { if(rename(tmp_file.data.c_str(), copy_op.destination.data.c_str()) != 0) perror(tmp_file.data.c_str()); + + copy_op.destination.append(".upscaled"); + file_overwrite(copy_op.destination.data.c_str(), "1"); } }); } else { @@ -1947,12 +1950,20 @@ namespace QuickMedia { Path image_path = content_cache_dir; image_path.join(std::to_string(image_index + 1)); - if(get_file_type(image_path) == FileType::REGULAR) { + bool upscaled_ok = true; + if(upscale_image_action != UpscaleImageAction::NO) { + Path image_filepath_upcaled = image_path; + image_filepath_upcaled.append(".upscaled"); + if(get_file_type(image_filepath_upcaled) == FileType::FILE_NOT_FOUND && image_upscale_status[image_index] == 0) + upscaled_ok = false; + } + + if(get_file_type(image_path) == FileType::REGULAR && upscaled_ok) { sf::Image image; if(image.loadFromFile(image_path.data)) { if(image_texture.loadFromImage(image)) { image_texture.setSmooth(true); - //image_texture.generateMipmap(); + image_texture.generateMipmap(); return LoadImageResult::OK; } else { error_message = std::string("Failed to load image for page ") + std::to_string(image_index + 1); @@ -1990,11 +2001,21 @@ namespace QuickMedia { if(image_download_cancel) return false; + int image_index = page - 1; + // TODO: Save image with the file extension that url says it has? right now the file is saved without any extension Path image_filepath = content_cache_dir_; image_filepath.join(std::to_string(page++)); - if(get_file_type(image_filepath) != FileType::FILE_NOT_FOUND) + bool upscaled_ok = true; + if(upscale_image_action != UpscaleImageAction::NO) { + Path image_filepath_upcaled = image_filepath; + image_filepath_upcaled.append(".upscaled"); + if(get_file_type(image_filepath_upcaled) == FileType::FILE_NOT_FOUND && image_upscale_status[image_index] == 0) + upscaled_ok = false; + } + + if(get_file_type(image_filepath) != FileType::FILE_NOT_FOUND && upscaled_ok) return true; std::vector extra_args; @@ -2059,9 +2080,11 @@ namespace QuickMedia { image_upscale_cv.notify_one(); } else { fprintf(stderr, "Info: not upscaling %s because the file is already large on your monitor (screen height: %d, image height: %d)\n", image_filepath_tmp.data.c_str(), screen_height, image_height); + image_upscale_status[image_index] = 1; } } else { fprintf(stderr, "Warning: failed to upscale %s because QuickMedia failed to recognize the resolution of the image\n", image_filepath_tmp.data.c_str()); + image_upscale_status[image_index] = 1; } } else if(upscale_image_action == UpscaleImageAction::FORCE) { rename_immediately = false; @@ -2107,7 +2130,6 @@ namespace QuickMedia { current_page = Page::EPISODE_LIST; return; } - download_chapter_images_if_needed(image_plugin); int num_images = 0; if(image_plugin->get_number_of_images(images_url, num_images) != ImageResult::OK) { @@ -2116,6 +2138,11 @@ namespace QuickMedia { return; } image_index = std::min(image_index, num_images); + + if(num_images != (int)image_upscale_status.size()) + image_upscale_status.resize(num_images); + + download_chapter_images_if_needed(image_plugin); if(image_index < num_images) { sf::String error_msg; @@ -2215,6 +2242,9 @@ namespace QuickMedia { } else if(event.key.code == sf::Keyboard::I) { current_page = Page::IMAGES_CONTINUOUS; image_view_mode = ImageViewMode::SCROLL; + } else if(event.key.code == sf::Keyboard::F) { + fit_image_to_window = !fit_image_to_window; + redraw = true; } } } @@ -2250,7 +2280,11 @@ namespace QuickMedia { auto bounds = error_message.getLocalBounds(); error_message.setPosition(std::floor(content_size.x * 0.5f - bounds.width * 0.5f), std::floor(content_size.y * 0.5f - bounds.height)); } else { - auto image_scale = get_ratio(texture_size_f, clamp_to_size(texture_size_f, content_size)); + sf::Vector2f image_scale; + if(fit_image_to_window) + image_scale = get_ratio(texture_size_f, wrap_to_size(texture_size_f, content_size)); + else + image_scale = get_ratio(texture_size_f, clamp_to_size(texture_size_f, content_size)); image.setScale(image_scale); auto image_size = texture_size_f; @@ -2284,6 +2318,7 @@ namespace QuickMedia { image_download_cancel = true; std::unique_lock lock(image_upscale_mutex); images_to_upscale.clear(); + image_upscale_status.clear(); } } @@ -2301,6 +2336,17 @@ namespace QuickMedia { current_page = Page::EPISODE_LIST; return; } + + int num_images = 0; + if(image_plugin->get_number_of_images(images_url, num_images) != ImageResult::OK) { + show_notification("Plugin", "Failed to get number of images", Urgency::CRITICAL); + current_page = Page::EPISODE_LIST; + return; + } + + if(num_images != (int)image_upscale_status.size()) + image_upscale_status.resize(num_images); + download_chapter_images_if_needed(image_plugin); Json::Value &json_chapters = content_storage_json["chapters"]; @@ -2361,6 +2407,7 @@ namespace QuickMedia { image_download_cancel = true; std::unique_lock lock(image_upscale_mutex); images_to_upscale.clear(); + image_upscale_status.clear(); } } -- cgit v1.2.3