QuickShow: Browse the files in the same folder

This commit is contained in:
Hüseyin ASLITÜRK 2020-04-12 14:35:15 +03:00 committed by Andreas Kling
parent 74a8e3fe1c
commit 2689fdf1d8
Notes: sideshowbarker 2024-07-19 07:40:49 +09:00
3 changed files with 89 additions and 0 deletions

View File

@ -25,6 +25,8 @@
*/
#include "QSWidget.h"
#include <AK/StringBuilder.h>
#include <LibCore/DirIterator.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>
@ -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())

View File

@ -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<void(int)> on_scale_change;
@ -65,4 +73,5 @@ private:
Gfx::Point m_click_position;
Gfx::FloatPoint m_saved_pan_origin;
Vector<String> m_files_in_same_dir;
};

View File

@ -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<GUI::MenuBar>();
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);