GalleryWidget.cpp 15 KB

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