diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-23 03:53:51 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-23 03:54:45 +0100 |
commit | 19fa70c8211739949c95404d9a87230656a9227f (patch) | |
tree | e11444f26703d7e67653728bb99713e915ed9481 /Applications | |
parent | 5707d7f547026544f4d9c7a320d97ccd6e055784 (diff) | |
download | serenity-19fa70c8211739949c95404d9a87230656a9227f.zip |
LibGUI: Add a GItemView class.
This is a GAbstractView subclass that implements a icon-based view onto
a GModel. It still need a bunch of work, but it's in basic usable shape.
Diffstat (limited to 'Applications')
-rw-r--r-- | Applications/FileManager/DirectoryModel.cpp | 4 | ||||
-rw-r--r-- | Applications/FileManager/DirectoryTableView.cpp | 68 | ||||
-rw-r--r-- | Applications/FileManager/DirectoryTableView.h | 19 | ||||
-rw-r--r-- | Applications/FileManager/main.cpp | 35 |
4 files changed, 89 insertions, 37 deletions
diff --git a/Applications/FileManager/DirectoryModel.cpp b/Applications/FileManager/DirectoryModel.cpp index 77cd8dfec7..5dd863df28 100644 --- a/Applications/FileManager/DirectoryModel.cpp +++ b/Applications/FileManager/DirectoryModel.cpp @@ -165,11 +165,15 @@ GVariant DirectoryModel::data(const GModelIndex& index, Role role) const case Column::Inode: return (int)entry.inode; } } + if (role == Role::Icon) { + return icon_for(entry); + } return { }; } void DirectoryModel::update() { + dbgprintf("DirectoryModel::update\n"); DIR* dirp = opendir(m_path.characters()); if (!dirp) { perror("opendir"); diff --git a/Applications/FileManager/DirectoryTableView.cpp b/Applications/FileManager/DirectoryTableView.cpp index a445f22216..5149410ec4 100644 --- a/Applications/FileManager/DirectoryTableView.cpp +++ b/Applications/FileManager/DirectoryTableView.cpp @@ -1,49 +1,75 @@ #include "DirectoryTableView.h" #include <LibGUI/GSortingProxyModel.h> -DirectoryTableView::DirectoryTableView(GWidget* parent) - : GTableView(parent) +DirectoryView::DirectoryView(GWidget* parent) + : GStackWidget(parent) , m_model(DirectoryModel::create()) { - set_model(GSortingProxyModel::create(m_model.copy_ref())); - GTableView::model()->set_key_column_and_sort_order(DirectoryModel::Column::Name, GSortOrder::Ascending); + set_active_widget(nullptr); + m_item_view = new GItemView(this); + m_item_view->set_model(model()); + + m_table_view = new GTableView(this); + m_table_view->set_model(GSortingProxyModel::create(m_model.copy_ref())); + + model().set_key_column_and_sort_order(DirectoryModel::Column::Name, GSortOrder::Ascending); + + m_item_view->set_model_column(DirectoryModel::Column::Name); + + m_table_view->on_model_notification = [this] (const GModelNotification& notification) { + if (notification.type() == GModelNotification::Type::ModelUpdated) { + set_status_message(String::format("%d item%s (%u byte%s)", + model().row_count(), + model().row_count() != 1 ? "s" : "", + model().bytes_in_files(), + model().bytes_in_files() != 1 ? "s" : "")); + + if (on_path_change) + on_path_change(model().path()); + } + }; + + set_view_mode(ViewMode::Icon); } -DirectoryTableView::~DirectoryTableView() +DirectoryView::~DirectoryView() { } -void DirectoryTableView::open(const String& path) +void DirectoryView::set_view_mode(ViewMode mode) { - model().open(path); + if (m_view_mode == mode) + return; + m_view_mode = mode; + update(); + if (mode == ViewMode::List) { + set_active_widget(m_table_view); + return; + } + if (mode == ViewMode::Icon) { + set_active_widget(m_item_view); + return; + } + ASSERT_NOT_REACHED(); } -void DirectoryTableView::model_notification(const GModelNotification& notification) +void DirectoryView::open(const String& path) { - if (notification.type() == GModelNotification::Type::ModelUpdated) { - set_status_message(String::format("%d item%s (%u byte%s)", - model().row_count(), - model().row_count() != 1 ? "s" : "", - model().bytes_in_files(), - model().bytes_in_files() != 1 ? "s" : "")); - - if (on_path_change) - on_path_change(model().path()); - } + model().open(path); } -void DirectoryTableView::set_status_message(const String& message) +void DirectoryView::set_status_message(const String& message) { if (on_status_message) on_status_message(message); } -void DirectoryTableView::open_parent_directory() +void DirectoryView::open_parent_directory() { model().open(String::format("%s/..", model().path().characters())); } -void DirectoryTableView::refresh() +void DirectoryView::refresh() { model().update(); } diff --git a/Applications/FileManager/DirectoryTableView.h b/Applications/FileManager/DirectoryTableView.h index 5725ff7d58..67b6ee5b17 100644 --- a/Applications/FileManager/DirectoryTableView.h +++ b/Applications/FileManager/DirectoryTableView.h @@ -1,13 +1,15 @@ #pragma once #include <LibGUI/GTableView.h> +#include <LibGUI/GItemView.h> +#include <LibGUI/GStackWidget.h> #include <sys/stat.h> #include "DirectoryModel.h" -class DirectoryTableView final : public GTableView { +class DirectoryView final : public GStackWidget { public: - explicit DirectoryTableView(GWidget* parent); - virtual ~DirectoryTableView() override; + explicit DirectoryView(GWidget* parent); + virtual ~DirectoryView() override; void open(const String& path); String path() const { return model().path(); } @@ -18,13 +20,20 @@ public: Function<void(const String&)> on_path_change; Function<void(String)> on_status_message; -private: - virtual void model_notification(const GModelNotification&) override; + enum ViewMode { Invalid, List, Icon }; + void set_view_mode(ViewMode); + ViewMode view_mode() const { return m_view_mode; } +private: DirectoryModel& model() { return *m_model; } const DirectoryModel& model() const { return *m_model; } void set_status_message(const String&); + ViewMode m_view_mode { Invalid }; + Retained<DirectoryModel> m_model; + + GTableView* m_table_view { nullptr }; + GItemView* m_item_view { nullptr }; }; diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index 6405d9df67..73ff945fad 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -47,22 +47,22 @@ int main(int argc, char** argv) auto* location_textbox = new GTextEditor(GTextEditor::SingleLine, location_toolbar); - auto* directory_table_view = new DirectoryTableView(widget); + auto* directory_view = new DirectoryView(widget); auto* statusbar = new GStatusBar(widget); - location_textbox->on_return_pressed = [directory_table_view] (auto& editor) { - directory_table_view->open(editor.text()); + location_textbox->on_return_pressed = [directory_view] (auto& editor) { + directory_view->open(editor.text()); }; - auto open_parent_directory_action = GAction::create("Open parent directory", { Mod_Alt, Key_Up }, GraphicsBitmap::load_from_file("/res/icons/parentdirectory16.png"), [directory_table_view] (const GAction&) { - directory_table_view->open_parent_directory(); + auto open_parent_directory_action = GAction::create("Open parent directory", { Mod_Alt, Key_Up }, GraphicsBitmap::load_from_file("/res/icons/parentdirectory16.png"), [directory_view] (const GAction&) { + directory_view->open_parent_directory(); }); auto mkdir_action = GAction::create("New directory...", GraphicsBitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&] (const GAction&) { GInputBox input_box("Enter name:", "New directory", window); if (input_box.exec() == GInputBox::ExecOK && !input_box.text_value().is_empty()) { auto new_dir_path = String::format("%s/%s", - directory_table_view->path().characters(), + directory_view->path().characters(), input_box.text_value().characters() ); int rc = mkdir(new_dir_path.characters(), 0777); @@ -70,11 +70,19 @@ int main(int argc, char** argv) GMessageBox message_box(String::format("mkdir() failed: %s", strerror(errno)), "Error", window); message_box.exec(); } else { - directory_table_view->refresh(); + directory_view->refresh(); } } }); + auto view_as_list_action = GAction::create("List view", { Mod_Ctrl, KeyCode::Key_L }, [&] (const GAction&) { + directory_view->set_view_mode(DirectoryView::ViewMode::List); + }); + + auto view_as_icons_action = GAction::create("Icon view", { Mod_Ctrl, KeyCode::Key_I }, [&] (const GAction&) { + directory_view->set_view_mode(DirectoryView::ViewMode::Icon); + }); + auto copy_action = GAction::create("Copy", GraphicsBitmap::load_from_file("/res/icons/copyfile16.png"), [] (const GAction&) { dbgprintf("'Copy' action activated!\n"); }); @@ -99,6 +107,11 @@ int main(int argc, char** argv) file_menu->add_action(delete_action.copy_ref()); menubar->add_menu(move(file_menu)); + auto view_menu = make<GMenu>("View"); + view_menu->add_action(view_as_list_action.copy_ref()); + view_menu->add_action(view_as_icons_action.copy_ref()); + menubar->add_menu(move(view_menu)); + auto help_menu = make<GMenu>("Help"); help_menu->add_action(GAction::create("About", [] (const GAction&) { dbgprintf("FIXME: Implement Help/About\n"); @@ -112,17 +125,17 @@ int main(int argc, char** argv) main_toolbar->add_action(copy_action.copy_ref()); main_toolbar->add_action(delete_action.copy_ref()); - directory_table_view->on_path_change = [window, location_textbox] (const String& new_path) { + directory_view->on_path_change = [window, location_textbox] (const String& new_path) { window->set_title(String::format("FileManager: %s", new_path.characters())); location_textbox->set_text(new_path); }; - directory_table_view->on_status_message = [statusbar] (String message) { + directory_view->on_status_message = [statusbar] (String message) { statusbar->set_text(move(message)); }; - directory_table_view->open("/"); - directory_table_view->set_focus(true); + directory_view->open("/"); + directory_view->set_focus(true); window->set_main_widget(widget); window->show(); |