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 --- README.md | 1 + include/QuickMedia.hpp | 2 ++ src/QuickMedia.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 057c50f..d9cc851 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Press `Ctrl + M` to begin writing a post to a thread (image boards), press `ESC` Press `1 to 9` or `Numpad 1 to 9` to select google captcha image when posting a comment on 4chan.\ Press `P` to preview the 4chan image of the selected row in full screen view, press `ESC` or `Backspace` to go back.\ Press `I` to switch between single image and scroll image view mode when reading manga.\ +Press `F` to fit image to window size when reading manga. Press `F` again to show original window size.\ Press `Middle mouse button` to "autoscroll" in scrolling image view mode.\ Press `Tab` to autocomplete a search when autocomplete is available (currently only available for youtube).\ Press `Tab` to switch between username/password field in login panel.\ diff --git a/include/QuickMedia.hpp b/include/QuickMedia.hpp index 62cee6b..016202c 100644 --- a/include/QuickMedia.hpp +++ b/include/QuickMedia.hpp @@ -117,6 +117,7 @@ namespace QuickMedia { std::thread image_upscale_thead; std::mutex image_upscale_mutex; std::deque images_to_upscale; + std::vector image_upscale_status; std::condition_variable image_upscale_cv; std::string downloading_chapter_url; bool image_download_cancel = false; @@ -132,5 +133,6 @@ namespace QuickMedia { Body *related_media_body; std::vector selected_files; FileManager *file_manager = nullptr; + bool fit_image_to_window = false; }; } \ No newline at end of file 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