GalleryWidget.cpp 2.7 KB

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