GalleryWidget.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) 2021-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GalleryWidget.h"
  7. #include "DemoWizardDialog.h"
  8. #include "GalleryModels.h"
  9. #include <AK/StringBuilder.h>
  10. #include <Demos/WidgetGallery/BasicsTabGML.h>
  11. #include <Demos/WidgetGallery/CursorsTabGML.h>
  12. #include <Demos/WidgetGallery/IconsTabGML.h>
  13. #include <Demos/WidgetGallery/SlidersTabGML.h>
  14. #include <Demos/WidgetGallery/WindowGML.h>
  15. #include <Demos/WidgetGallery/WizardsTabGML.h>
  16. #include <LibFileSystemAccessClient/Client.h>
  17. #include <LibGUI/Button.h>
  18. #include <LibGUI/ColorInput.h>
  19. #include <LibGUI/FilePicker.h>
  20. #include <LibGUI/FontPicker.h>
  21. #include <LibGUI/InputBox.h>
  22. #include <LibGUI/ItemListModel.h>
  23. #include <LibGUI/MessageBox.h>
  24. #include <LibGUI/SortingProxyModel.h>
  25. #include <LibGUI/SpinBox.h>
  26. #include <LibGUI/TabWidget.h>
  27. #include <LibGUI/TableView.h>
  28. #include <LibGUI/ValueSlider.h>
  29. #include <LibGfx/Font/FontDatabase.h>
  30. #include <LibGfx/Palette.h>
  31. GalleryWidget::GalleryWidget()
  32. {
  33. load_from_gml(window_gml).release_value_but_fixme_should_propagate_errors();
  34. auto& tab_widget = *find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  35. auto basics_tab = tab_widget.try_add_tab<GUI::Widget>("Basics"_short_string).release_value_but_fixme_should_propagate_errors();
  36. basics_tab->load_from_gml(basics_tab_gml).release_value_but_fixme_should_propagate_errors();
  37. m_enabled_label = basics_tab->find_descendant_of_type_named<GUI::Label>("enabled_label");
  38. m_label_frame = basics_tab->find_descendant_of_type_named<GUI::Frame>("label_frame");
  39. m_frame_shapes.append("No Frame");
  40. m_frame_shapes.append("Plain Box");
  41. m_frame_shapes.append("Plain Container");
  42. m_frame_shapes.append("Plain Panel");
  43. m_frame_shapes.append("Raised Box");
  44. m_frame_shapes.append("Raised Container");
  45. m_frame_shapes.append("Raised Panel");
  46. m_frame_shapes.append("Sunken Box");
  47. m_frame_shapes.append("Sunken Container");
  48. m_frame_shapes.append("Sunken Panel");
  49. m_frame_shape_combobox = basics_tab->find_descendant_of_type_named<GUI::ComboBox>("frame_shape_combobox");
  50. m_frame_shape_combobox->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_frame_shapes));
  51. m_frame_shape_combobox->on_change = [&](auto&, auto const& index) {
  52. m_label_frame->set_frame_shape(static_cast<Gfx::FrameShape>((index.row() - 1) % 3 + 1));
  53. m_label_frame->set_frame_shadow(static_cast<Gfx::FrameShadow>((index.row() - 1) / 3));
  54. m_label_frame->update();
  55. };
  56. m_frame_shape_combobox->on_return_pressed = [&]() {
  57. m_enabled_label->set_text(String::from_deprecated_string(m_frame_shape_combobox->text()).release_value_but_fixme_should_propagate_errors());
  58. };
  59. m_thickness_spinbox = basics_tab->find_descendant_of_type_named<GUI::SpinBox>("thickness_spinbox");
  60. m_thickness_spinbox->set_value(1);
  61. m_thickness_spinbox->on_change = [&](auto value) {
  62. m_label_frame->set_frame_thickness(value);
  63. };
  64. m_button_icons.append(Gfx::Bitmap::load_from_file("/res/icons/16x16/book-open.png"sv).release_value_but_fixme_should_propagate_errors());
  65. m_button_icons.append(Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"sv).release_value_but_fixme_should_propagate_errors());
  66. m_button_icons.append(Gfx::Bitmap::load_from_file("/res/icons/16x16/ladybug.png"sv).release_value_but_fixme_should_propagate_errors());
  67. m_icon_button = basics_tab->find_descendant_of_type_named<GUI::Button>("icon_button");
  68. m_icon_button->set_icon(*m_button_icons[2]);
  69. m_disabled_icon_button = basics_tab->find_descendant_of_type_named<GUI::Button>("disabled_icon_button");
  70. m_disabled_icon_button->set_icon(*m_button_icons[2]);
  71. m_icon_button->on_click = [&](auto) {
  72. static size_t i;
  73. if (i >= m_button_icons.size())
  74. i = 0;
  75. m_icon_button->set_icon(*m_button_icons[i]);
  76. m_disabled_icon_button->set_icon(*m_button_icons[i]);
  77. i++;
  78. };
  79. m_text_editor = basics_tab->find_descendant_of_type_named<GUI::TextEditor>("text_editor");
  80. m_font_button = basics_tab->find_descendant_of_type_named<GUI::Button>("font_button");
  81. m_font_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-font-editor.png"sv).release_value_but_fixme_should_propagate_errors());
  82. m_font_button->on_click = [&](auto) {
  83. auto picker = GUI::FontPicker::try_create(window(), &m_text_editor->font(), false).release_value_but_fixme_should_propagate_errors();
  84. if (picker->exec() == GUI::Dialog::ExecResult::OK) {
  85. m_text_editor->set_font(picker->font());
  86. }
  87. };
  88. m_file_button = basics_tab->find_descendant_of_type_named<GUI::Button>("file_button");
  89. m_file_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"sv).release_value_but_fixme_should_propagate_errors());
  90. m_file_button->on_click = [&](auto) {
  91. auto response = FileSystemAccessClient::Client::the().open_file(window());
  92. if (response.is_error())
  93. return;
  94. m_text_editor->set_text(response.release_value().filename());
  95. };
  96. m_input_button = basics_tab->find_descendant_of_type_named<GUI::Button>("input_button");
  97. m_input_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"sv).release_value_but_fixme_should_propagate_errors());
  98. m_input_button->on_click = [&](auto) {
  99. String value;
  100. if (GUI::InputBox::show(window(), value, "Enter input:"sv, "Input"sv, GUI::InputType::NonemptyText) == GUI::InputBox::ExecResult::OK)
  101. m_text_editor->set_text(value);
  102. };
  103. m_font_colorinput = basics_tab->find_descendant_of_type_named<GUI::ColorInput>("font_colorinput");
  104. m_font_colorinput->on_change = [&]() {
  105. auto palette = m_text_editor->palette();
  106. palette.set_color(Gfx::ColorRole::BaseText, m_font_colorinput->color());
  107. m_text_editor->set_palette(palette);
  108. m_text_editor->update();
  109. };
  110. m_msgbox_button = basics_tab->find_descendant_of_type_named<GUI::Button>("msgbox_button");
  111. m_msgbox_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-browser.png"sv).release_value_but_fixme_should_propagate_errors());
  112. m_msgbox_type = GUI::MessageBox::Type::None;
  113. m_msgbox_input_type = GUI::MessageBox::InputType::OK;
  114. m_msgbox_icons.append("None");
  115. m_msgbox_icons.append("Information");
  116. m_msgbox_icons.append("Warning");
  117. m_msgbox_icons.append("Error");
  118. m_msgbox_icons.append("Question");
  119. m_msgbox_buttons.append("OK");
  120. m_msgbox_buttons.append("OK Cancel");
  121. m_msgbox_buttons.append("Yes No");
  122. m_msgbox_buttons.append("Yes No Cancel");
  123. m_msgbox_icon_combobox = basics_tab->find_descendant_of_type_named<GUI::ComboBox>("msgbox_icon_combobox");
  124. m_msgbox_icon_combobox->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_msgbox_icons));
  125. m_msgbox_icon_combobox->set_selected_index(0);
  126. m_msgbox_icon_combobox->on_change = [&](auto&, auto const& index) {
  127. m_msgbox_type = static_cast<GUI::MessageBox::Type>(index.row());
  128. };
  129. m_msgbox_buttons_combobox = basics_tab->find_descendant_of_type_named<GUI::ComboBox>("msgbox_buttons_combobox");
  130. m_msgbox_buttons_combobox->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_msgbox_buttons));
  131. m_msgbox_buttons_combobox->set_selected_index(0);
  132. m_msgbox_buttons_combobox->on_change = [&](auto&, auto const& index) {
  133. m_msgbox_input_type = static_cast<GUI::MessageBox::InputType>(index.row());
  134. };
  135. m_msgbox_button->on_click = [&](auto) {
  136. GUI::MessageBox::show(window(), m_text_editor->text(), "Message"sv, m_msgbox_type, m_msgbox_input_type);
  137. };
  138. auto sliders_tab = tab_widget.try_add_tab<GUI::Widget>("Sliders"_short_string).release_value_but_fixme_should_propagate_errors();
  139. sliders_tab->load_from_gml(sliders_tab_gml).release_value_but_fixme_should_propagate_errors();
  140. m_vertical_progressbar_left = sliders_tab->find_descendant_of_type_named<GUI::VerticalProgressbar>("vertical_progressbar_left");
  141. m_vertical_progressbar_left->set_value(0);
  142. m_vertical_progressbar_right = sliders_tab->find_descendant_of_type_named<GUI::VerticalProgressbar>("vertical_progressbar_right");
  143. m_vertical_progressbar_right->set_value(100);
  144. m_vertical_slider_left = sliders_tab->find_descendant_of_type_named<GUI::VerticalSlider>("vertical_slider_left");
  145. m_vertical_slider_right = sliders_tab->find_descendant_of_type_named<GUI::VerticalSlider>("vertical_slider_right");
  146. m_vertical_slider_left->on_change = [&](auto value) {
  147. m_vertical_progressbar_left->set_value(m_vertical_slider_left->max() - value);
  148. };
  149. m_vertical_slider_right->on_change = [&](auto value) {
  150. m_vertical_progressbar_right->set_value((100 / m_vertical_slider_right->max()) * (m_vertical_slider_right->max() - value));
  151. };
  152. m_horizontal_progressbar = sliders_tab->find_descendant_of_type_named<GUI::HorizontalProgressbar>("horizontal_progressbar");
  153. m_horizontal_progressbar->set_value(0);
  154. m_horizontal_slider_left = sliders_tab->find_descendant_of_type_named<GUI::HorizontalSlider>("horizontal_slider_left");
  155. m_horizontal_slider_right = sliders_tab->find_descendant_of_type_named<GUI::HorizontalSlider>("horizontal_slider_right");
  156. m_horizontal_slider_left->on_change = [&](auto value) {
  157. m_horizontal_progressbar->set_value(value);
  158. if (!(value % (100 / m_horizontal_slider_right->max())))
  159. m_horizontal_slider_right->set_value(value / (100 / m_horizontal_slider_right->max()));
  160. };
  161. m_horizontal_slider_right->on_change = [&](auto value) {
  162. m_horizontal_progressbar->set_value((value * 100) / m_horizontal_slider_right->max());
  163. m_horizontal_slider_left->set_value((value * 100) / m_horizontal_slider_right->max());
  164. };
  165. m_enabled_scrollbar = sliders_tab->find_descendant_of_type_named<GUI::Scrollbar>("enabled_scrollbar");
  166. m_enabled_scrollbar->set_orientation(Orientation::Horizontal);
  167. m_disabled_scrollbar = sliders_tab->find_descendant_of_type_named<GUI::Scrollbar>("disabled_scrollbar");
  168. m_disabled_scrollbar->set_orientation(Orientation::Horizontal);
  169. m_opacity_imagewidget = sliders_tab->find_descendant_of_type_named<GUI::ImageWidget>("opacity_imagewidget");
  170. m_opacity_imagewidget->load_from_file("/res/graphics/brand-banner.png"sv);
  171. m_opacity_slider = sliders_tab->find_descendant_of_type_named<GUI::HorizontalOpacitySlider>("opacity_slider");
  172. m_opacity_slider->on_change = [&](auto percent) {
  173. m_opacity_imagewidget->set_opacity_percent(percent);
  174. m_opacity_value_slider->set_value(percent);
  175. };
  176. m_opacity_value_slider = sliders_tab->find_descendant_of_type_named<GUI::ValueSlider>("opacity_value_slider");
  177. m_opacity_value_slider->set_range(0, 100);
  178. m_opacity_value_slider->on_change = [&](auto percent) {
  179. m_opacity_imagewidget->set_opacity_percent(percent);
  180. m_opacity_slider->set_value(percent);
  181. };
  182. auto wizards_tab = tab_widget.try_add_tab<GUI::Widget>("Wizards"_short_string).release_value_but_fixme_should_propagate_errors();
  183. wizards_tab->load_from_gml(wizards_tab_gml).release_value_but_fixme_should_propagate_errors();
  184. m_wizard_button = wizards_tab->find_descendant_of_type_named<GUI::Button>("wizard_button");
  185. m_wizard_output = wizards_tab->find_descendant_of_type_named<GUI::TextEditor>("wizard_output");
  186. m_wizard_output->set_should_hide_unnecessary_scrollbars(true);
  187. char const* serenityos_ascii = {
  188. " ____ _ __ ____ ____\n"
  189. " / __/__ _______ ___ (_) /___ __/ __ \\/ __/\n"
  190. " _\\ \\/ -_) __/ -_) _ \\/ / __/ // / /_/ /\\ \\\n"
  191. "/___/\\__/_/ \\__/_//_/_/\\__/\\_, /\\____/___/\n"
  192. " /___/\n"
  193. };
  194. char const* wizard_ascii = {
  195. " _,-'|\n"
  196. " ,-'._ |\n"
  197. " .||, |####\\ |\n"
  198. "\\`' ,/ \\'L' | |\n"
  199. "= ,. = |-,#| |\n"
  200. "/ || \\ ,-'\\#/,'`.\n"
  201. " || ,' `,,. `.\n"
  202. " ,|____,' , ,;' \\| |\n"
  203. " (3|\\ _/|/' _| |\n"
  204. " ||/,-'' | >-'' _,\\\\\n"
  205. " ||' ==\\ ,-' ,'\n"
  206. " || | V \\ ,|\n"
  207. " || | |` |\n"
  208. " || | | \\\n"
  209. " || | \\ \\\n"
  210. " || | | \\\n"
  211. " || | \\_,-'\n"
  212. " || |___,,--')_\\\n"
  213. " || |_| _ccc/-\n"
  214. " || ccc/__\n"
  215. " _||_-\n"
  216. };
  217. m_wizard_output->set_text(DeprecatedString::formatted("{}{}", serenityos_ascii, wizard_ascii));
  218. m_wizard_button->on_click = [&](auto) {
  219. StringBuilder sb;
  220. sb.append(m_wizard_output->get_text());
  221. sb.append("\nWizard started."sv);
  222. m_wizard_output->set_text(sb.to_deprecated_string());
  223. auto wizard = DemoWizardDialog::try_create(window()).release_value_but_fixme_should_propagate_errors();
  224. auto result = wizard->exec();
  225. sb.append(DeprecatedString::formatted("\nWizard execution complete.\nDialog ExecResult code: {}", to_underlying(result)));
  226. if (result == GUI::Dialog::ExecResult::OK)
  227. sb.append(DeprecatedString::formatted(" (ExecResult::OK)\n'Installation' location: \"{}\"", wizard->page_1_location()));
  228. m_wizard_output->set_text(sb.string_view());
  229. };
  230. auto cursors_tab = tab_widget.try_add_tab<GUI::Widget>("Cursors"_short_string).release_value_but_fixme_should_propagate_errors();
  231. cursors_tab->load_from_gml(cursors_tab_gml).release_value_but_fixme_should_propagate_errors();
  232. m_cursors_tableview = cursors_tab->find_descendant_of_type_named<GUI::TableView>("cursors_tableview");
  233. m_cursors_tableview->set_highlight_selected_rows(true);
  234. m_cursors_tableview->set_alternating_row_colors(false);
  235. m_cursors_tableview->set_vertical_padding(16);
  236. m_cursors_tableview->set_column_headers_visible(false);
  237. m_cursors_tableview->set_highlight_key_column(false);
  238. auto sorting_proxy_model = MUST(GUI::SortingProxyModel::create(MouseCursorModel::create()));
  239. sorting_proxy_model->set_sort_role(GUI::ModelRole::Display);
  240. m_cursors_tableview->set_model(sorting_proxy_model);
  241. m_cursors_tableview->set_key_column_and_sort_order(MouseCursorModel::Column::Name, GUI::SortOrder::Ascending);
  242. m_cursors_tableview->model()->invalidate();
  243. m_cursors_tableview->set_column_width(0, 25);
  244. m_cursors_tableview->on_activation = [&](const GUI::ModelIndex& index) {
  245. auto icon_index = index.model()->index(index.row(), MouseCursorModel::Column::Bitmap);
  246. m_cursors_tableview->set_override_cursor(NonnullRefPtr<Gfx::Bitmap const>(icon_index.data().as_bitmap()));
  247. };
  248. auto icons_tab = tab_widget.try_add_tab<GUI::Widget>("Icons"_short_string).release_value_but_fixme_should_propagate_errors();
  249. icons_tab->load_from_gml(icons_tab_gml).release_value_but_fixme_should_propagate_errors();
  250. m_icons_tableview = icons_tab->find_descendant_of_type_named<GUI::TableView>("icons_tableview");
  251. m_icons_tableview->set_highlight_selected_rows(true);
  252. m_icons_tableview->set_alternating_row_colors(false);
  253. m_icons_tableview->set_vertical_padding(24);
  254. m_icons_tableview->set_column_headers_visible(false);
  255. m_icons_tableview->set_highlight_key_column(false);
  256. auto sorting_proxy_icons_model = MUST(GUI::SortingProxyModel::create(FileIconsModel::create()));
  257. sorting_proxy_icons_model->set_sort_role(GUI::ModelRole::Display);
  258. m_icons_tableview->set_model(sorting_proxy_icons_model);
  259. m_icons_tableview->set_key_column_and_sort_order(FileIconsModel::Column::Name, GUI::SortOrder::Ascending);
  260. m_icons_tableview->model()->invalidate();
  261. m_icons_tableview->set_column_width(0, 36);
  262. m_icons_tableview->set_column_width(1, 20);
  263. }