Serenity Operating System
1/*
2 * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include "GalleryWidget.h"
8#include <Demos/ModelGallery/BasicModelTabGML.h>
9
10GalleryWidget::GalleryWidget()
11{
12 set_fill_with_background_color(true);
13 set_layout<GUI::VerticalBoxLayout>();
14
15 auto& inner_widget = add<GUI::Widget>();
16 inner_widget.try_set_layout<GUI::VerticalBoxLayout>(4).release_value_but_fixme_should_propagate_errors();
17
18 m_tab_widget = inner_widget.try_add<GUI::TabWidget>().release_value_but_fixme_should_propagate_errors();
19 m_statusbar = add<GUI::Statusbar>();
20
21 (void)load_basic_model_tab();
22 load_sorting_filtering_tab();
23}
24
25ErrorOr<void> GalleryWidget::load_basic_model_tab()
26{
27 auto tab = TRY(m_tab_widget->try_add_tab<GUI::Widget>("Basic Model"));
28 TRY(tab->load_from_gml(basic_model_tab_gml));
29
30 m_basic_model = BasicModel::create();
31 m_basic_model_table = *tab->find_descendant_of_type_named<GUI::TableView>("model_table");
32 m_basic_model_table->set_model(m_basic_model);
33
34 m_basic_model->on_invalidate = [&] {
35 m_invalidation_count++;
36 m_statusbar->set_text(DeprecatedString::formatted("Times invalidated: {}", m_invalidation_count));
37 };
38
39 m_statusbar->set_text(DeprecatedString::formatted("Times invalidated: {}", m_invalidation_count));
40
41 m_basic_model->add_item("Well...");
42 m_basic_model->add_item("...hello...");
43 m_basic_model->add_item("...friends! :^)");
44
45 m_new_item_name = *tab->find_descendant_of_type_named<GUI::TextBox>("new_item_name");
46 m_add_new_item = *tab->find_descendant_of_type_named<GUI::Button>("add_new_item");
47 m_remove_selected_item = *tab->find_descendant_of_type_named<GUI::Button>("remove_selected_item");
48
49 m_add_new_item->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/plus.png"sv).release_value_but_fixme_should_propagate_errors());
50 m_remove_selected_item->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/minus.png"sv).release_value_but_fixme_should_propagate_errors());
51
52 m_new_item_name->on_return_pressed = [&] { add_textbox_contents_to_basic_model(); };
53 m_add_new_item->on_click = [&](auto) { add_textbox_contents_to_basic_model(); };
54
55 m_remove_selected_item->on_click = [&](auto) {
56 auto index = m_basic_model_table->cursor_index();
57 if (index.is_valid()) {
58 m_basic_model->remove_item(index);
59 }
60 };
61
62 return {};
63}
64
65void GalleryWidget::load_sorting_filtering_tab()
66{
67 // TODO: Add the SortingFilteringProxyModel here.
68}
69
70void GalleryWidget::add_textbox_contents_to_basic_model()
71{
72 if (!m_new_item_name->current_line().is_empty()) {
73 m_basic_model->add_item(m_new_item_name->current_line().to_utf8());
74 m_new_item_name->set_text(""sv);
75 }
76}