From 2689fdf1d8555c5cefc71bc4cd36e8daa11f1e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20ASLIT=C3=9CRK?= Date: Sun, 12 Apr 2020 14:35:15 +0300 Subject: [PATCH] QuickShow: Browse the files in the same folder --- Applications/QuickShow/QSWidget.cpp | 53 +++++++++++++++++++++++++++++ Applications/QuickShow/QSWidget.h | 9 +++++ Applications/QuickShow/main.cpp | 27 +++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/Applications/QuickShow/QSWidget.cpp b/Applications/QuickShow/QSWidget.cpp index 520d016d6e..456538c6f9 100644 --- a/Applications/QuickShow/QSWidget.cpp +++ b/Applications/QuickShow/QSWidget.cpp @@ -25,6 +25,8 @@ */ #include "QSWidget.h" +#include +#include #include #include #include @@ -40,6 +42,57 @@ QSWidget::~QSWidget() { } +void QSWidget::navigate(Directions direction) +{ + if (m_path == nullptr) + return; + + auto parts = m_path.split('/'); + parts.remove(parts.size() - 1); + StringBuilder sb; + sb.append("/"); + sb.join("/", parts); + AK::String current_dir = sb.to_string(); + + if (m_files_in_same_dir.is_empty()) { + Core::DirIterator iterator(current_dir, Core::DirIterator::Flags::SkipDots); + while (iterator.has_next()) { + String file = iterator.next_full_path(); + if (!file.ends_with(".png")) // TODO: Find a batter way to filter supported images. + continue; + m_files_in_same_dir.append(file); + } + } + + auto current_index = m_files_in_same_dir.find_first_index(m_path); + if (!current_index.has_value()) { + return; + } + + size_t index = current_index.value(); + if (direction == Directions::Back) { + if (index == 0) { + GUI::MessageBox::show(String::format("This is the first file.", index), "Cannot open image", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window()); + return; + } + + index--; + } else if (direction == Directions::Forward) { + if (index == m_files_in_same_dir.size() - 1) { + GUI::MessageBox::show(String::format("This is the last file.", index), "Cannot open image", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window()); + return; + } + + index++; + } else if (direction == Directions::First) { + index = 0; + } else if (direction == Directions::Last) { + index = m_files_in_same_dir.size() - 1; + } + + this->load_from_file(m_files_in_same_dir.at(index)); +} + void QSWidget::relayout() { if (m_bitmap.is_null()) diff --git a/Applications/QuickShow/QSWidget.h b/Applications/QuickShow/QSWidget.h index 79b1940720..bfdc4c84ee 100644 --- a/Applications/QuickShow/QSWidget.h +++ b/Applications/QuickShow/QSWidget.h @@ -34,11 +34,19 @@ class QSLabel; class QSWidget final : public GUI::Frame { C_OBJECT(QSWidget) public: + enum Directions { + First, + Back, + Forward, + Last + }; + virtual ~QSWidget() override; const Gfx::Bitmap* bitmap() const { return m_bitmap.ptr(); } const String& path() const { return m_path; } + void navigate(Directions); void load_from_file(const String&); Function on_scale_change; @@ -65,4 +73,5 @@ private: Gfx::Point m_click_position; Gfx::FloatPoint m_saved_pan_origin; + Vector m_files_in_same_dir; }; diff --git a/Applications/QuickShow/main.cpp b/Applications/QuickShow/main.cpp index 53ff0dfd68..22457ede5a 100644 --- a/Applications/QuickShow/main.cpp +++ b/Applications/QuickShow/main.cpp @@ -102,6 +102,27 @@ int main(int argc, char** argv) } }; + // Actions + auto go_first_action = GUI::Action::create("First", { Mod_None, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-first.png"), + [&](auto&) { + widget.navigate(QSWidget::Directions::First); + }); + + auto go_back_action = GUI::Action::create("Back", { Mod_None, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"), + [&](auto&) { + widget.navigate(QSWidget::Directions::Back); + }); + + auto go_forward_action = GUI::Action::create("Forward", { Mod_None, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), + [&](auto&) { + widget.navigate(QSWidget::Directions::Forward); + }); + + auto go_last_action = GUI::Action::create("Last", { Mod_None, Key_End }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-last.png"), + [&](auto&) { + widget.navigate(QSWidget::Directions::Last); + }); + auto menubar = make(); auto& app_menu = menubar->add_menu("QuickShow"); @@ -116,6 +137,12 @@ int main(int argc, char** argv) app.quit(); })); + auto& navigate_menu = menubar->add_menu("Navigate"); + navigate_menu.add_action(go_first_action); + navigate_menu.add_action(go_back_action); + navigate_menu.add_action(go_forward_action); + navigate_menu.add_action(go_last_action); + auto& help_menu = menubar->add_menu("Help"); help_menu.add_action(GUI::Action::create("About", [&](auto&) { GUI::AboutDialog::show("QuickShow", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-image.png"), window);