GalleryWidget.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "GalleryWidget.h"
  27. #include "DemoWizardDialog.h"
  28. #include "GalleryModels.h"
  29. #include <AK/StringBuilder.h>
  30. #include <Demos/WidgetGallery/BasicsTabGML.h>
  31. #include <Demos/WidgetGallery/CursorsTabGML.h>
  32. #include <Demos/WidgetGallery/IconsTabGML.h>
  33. #include <Demos/WidgetGallery/SlidersTabGML.h>
  34. #include <Demos/WidgetGallery/WindowGML.h>
  35. #include <Demos/WidgetGallery/WizardsTabGML.h>
  36. #include <LibGUI/Button.h>
  37. #include <LibGUI/ColorInput.h>
  38. #include <LibGUI/FilePicker.h>
  39. #include <LibGUI/FontPicker.h>
  40. #include <LibGUI/InputBox.h>
  41. #include <LibGUI/ItemListModel.h>
  42. #include <LibGUI/MessageBox.h>
  43. #include <LibGUI/SeparatorWidget.h>
  44. #include <LibGUI/SortingProxyModel.h>
  45. #include <LibGUI/SpinBox.h>
  46. #include <LibGUI/TabWidget.h>
  47. #include <LibGUI/TableView.h>
  48. #include <LibGfx/FontDatabase.h>
  49. #include <LibGfx/Palette.h>
  50. GalleryWidget::GalleryWidget()
  51. {
  52. load_from_gml(window_gml);
  53. auto& tab_widget = *find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  54. auto& basics_tab = tab_widget.add_tab<GUI::Widget>("Basics");
  55. basics_tab.load_from_gml(basics_tab_gml);
  56. m_enabled_label = basics_tab.find_descendant_of_type_named<GUI::Label>("enabled_label");
  57. m_label_frame = basics_tab.find_descendant_of_type_named<GUI::Frame>("label_frame");
  58. m_frame_shapes.append("No Frame");
  59. m_frame_shapes.append("Plain Box");
  60. m_frame_shapes.append("Plain Container");
  61. m_frame_shapes.append("Plain Panel");
  62. m_frame_shapes.append("Raised Box");
  63. m_frame_shapes.append("Raised Container");
  64. m_frame_shapes.append("Raised Panel");
  65. m_frame_shapes.append("Sunken Box");
  66. m_frame_shapes.append("Sunken Container");
  67. m_frame_shapes.append("Sunken Panel");
  68. m_frame_shape_combobox = basics_tab.find_descendant_of_type_named<GUI::ComboBox>("frame_shape_combobox");
  69. m_frame_shape_combobox->set_model(*GUI::ItemListModel<String>::create(m_frame_shapes));
  70. m_frame_shape_combobox->on_change = [&](auto&, const auto& index) {
  71. m_label_frame->set_frame_shape(static_cast<Gfx::FrameShape>((index.row() - 1) % 3 + 1));
  72. m_label_frame->set_frame_shadow(static_cast<Gfx::FrameShadow>((index.row() - 1) / 3));
  73. m_label_frame->update();
  74. };
  75. m_frame_shape_combobox->on_return_pressed = [&]() {
  76. m_enabled_label->set_text(m_frame_shape_combobox->text());
  77. };
  78. m_thickness_spinbox = basics_tab.find_descendant_of_type_named<GUI::SpinBox>("thickness_spinbox");
  79. m_thickness_spinbox->set_value(1);
  80. m_thickness_spinbox->on_change = [&](auto value) {
  81. m_label_frame->set_frame_thickness(value);
  82. };
  83. m_button_icons.append(Gfx::Bitmap::load_from_file("/res/icons/16x16/book-open.png"));
  84. m_button_icons.append(Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  85. m_button_icons.append(Gfx::Bitmap::load_from_file("/res/icons/16x16/ladybug.png"));
  86. m_icon_button = basics_tab.find_descendant_of_type_named<GUI::Button>("icon_button");
  87. m_icon_button->set_icon(*m_button_icons[2]);
  88. m_disabled_icon_button = basics_tab.find_descendant_of_type_named<GUI::Button>("disabled_icon_button");
  89. m_disabled_icon_button->set_icon(*m_button_icons[2]);
  90. m_icon_button->on_click = [&]() {
  91. static size_t i;
  92. if (i >= m_button_icons.size())
  93. i = 0;
  94. m_icon_button->set_icon(*m_button_icons[i]);
  95. m_disabled_icon_button->set_icon(*m_button_icons[i]);
  96. i++;
  97. };
  98. m_text_editor = basics_tab.find_descendant_of_type_named<GUI::TextEditor>("text_editor");
  99. m_font_button = basics_tab.find_descendant_of_type_named<GUI::Button>("font_button");
  100. m_font_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-font-editor.png"));
  101. m_font_button->on_click = [&]() {
  102. auto picker = GUI::FontPicker::construct(window(), &m_text_editor->font(), false);
  103. if (picker->exec() == GUI::Dialog::ExecOK) {
  104. m_text_editor->set_font(picker->font());
  105. }
  106. };
  107. m_file_button = basics_tab.find_descendant_of_type_named<GUI::Button>("file_button");
  108. m_file_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"));
  109. m_file_button->on_click = [&]() {
  110. Optional<String> open_path = GUI::FilePicker::get_open_filepath(window());
  111. if (!open_path.has_value())
  112. return;
  113. m_text_editor->set_text(open_path.value());
  114. };
  115. m_input_button = basics_tab.find_descendant_of_type_named<GUI::Button>("input_button");
  116. m_input_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"));
  117. m_input_button->on_click = [&]() {
  118. String value;
  119. if (GUI::InputBox::show(window(), value, "Enter input:", "Input") == GUI::InputBox::ExecOK && !value.is_empty())
  120. m_text_editor->set_text(value);
  121. };
  122. m_font_colorinput = basics_tab.find_descendant_of_type_named<GUI::ColorInput>("font_colorinput");
  123. m_font_colorinput->on_change = [&]() {
  124. auto palette = m_text_editor->palette();
  125. palette.set_color(Gfx::ColorRole::BaseText, m_font_colorinput->color());
  126. m_text_editor->set_palette(palette);
  127. m_text_editor->update();
  128. };
  129. m_msgbox_button = basics_tab.find_descendant_of_type_named<GUI::Button>("msgbox_button");
  130. m_msgbox_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-irc-client.png"));
  131. m_msgbox_type = GUI::MessageBox::Type::None;
  132. m_msgbox_input_type = GUI::MessageBox::InputType::OK;
  133. m_msgbox_icons.append("None");
  134. m_msgbox_icons.append("Information");
  135. m_msgbox_icons.append("Warning");
  136. m_msgbox_icons.append("Error");
  137. m_msgbox_icons.append("Question");
  138. m_msgbox_buttons.append("OK");
  139. m_msgbox_buttons.append("OK Cancel");
  140. m_msgbox_buttons.append("Yes No");
  141. m_msgbox_buttons.append("Yes No Cancel");
  142. m_msgbox_icon_combobox = basics_tab.find_descendant_of_type_named<GUI::ComboBox>("msgbox_icon_combobox");
  143. m_msgbox_icon_combobox->set_model(*GUI::ItemListModel<String>::create(m_msgbox_icons));
  144. m_msgbox_icon_combobox->set_selected_index(0);
  145. m_msgbox_icon_combobox->on_change = [&](auto&, const auto& index) {
  146. m_msgbox_type = static_cast<GUI::MessageBox::Type>(index.row());
  147. };
  148. m_msgbox_buttons_combobox = basics_tab.find_descendant_of_type_named<GUI::ComboBox>("msgbox_buttons_combobox");
  149. m_msgbox_buttons_combobox->set_model(*GUI::ItemListModel<String>::create(m_msgbox_buttons));
  150. m_msgbox_buttons_combobox->set_selected_index(0);
  151. m_msgbox_buttons_combobox->on_change = [&](auto&, const auto& index) {
  152. m_msgbox_input_type = static_cast<GUI::MessageBox::InputType>(index.row());
  153. };
  154. m_msgbox_button->on_click = [&]() {
  155. GUI::MessageBox::show(window(), m_text_editor->text(), "Message", m_msgbox_type, m_msgbox_input_type);
  156. };
  157. auto& sliders_tab = tab_widget.add_tab<GUI::Widget>("Sliders");
  158. sliders_tab.load_from_gml(sliders_tab_gml);
  159. m_vertical_progressbar_left = sliders_tab.find_descendant_of_type_named<GUI::VerticalProgressbar>("vertical_progressbar_left");
  160. m_vertical_progressbar_left->set_value(0);
  161. m_vertical_progressbar_right = sliders_tab.find_descendant_of_type_named<GUI::VerticalProgressbar>("vertical_progressbar_right");
  162. m_vertical_progressbar_right->set_value(100);
  163. m_vertical_slider_left = sliders_tab.find_descendant_of_type_named<GUI::VerticalSlider>("vertical_slider_left");
  164. m_vertical_slider_right = sliders_tab.find_descendant_of_type_named<GUI::VerticalSlider>("vertical_slider_right");
  165. m_vertical_slider_left->on_change = [&](auto value) {
  166. m_vertical_progressbar_left->set_value(m_vertical_slider_left->max() - value);
  167. };
  168. m_vertical_slider_right->on_change = [&](auto value) {
  169. m_vertical_progressbar_right->set_value((100 / m_vertical_slider_right->max()) * (m_vertical_slider_right->max() - value));
  170. };
  171. m_horizontal_progressbar = sliders_tab.find_descendant_of_type_named<GUI::HorizontalProgressbar>("horizontal_progressbar");
  172. m_horizontal_progressbar->set_value(0);
  173. m_horizontal_slider_left = sliders_tab.find_descendant_of_type_named<GUI::HorizontalSlider>("horizontal_slider_left");
  174. m_horizontal_slider_right = sliders_tab.find_descendant_of_type_named<GUI::HorizontalSlider>("horizontal_slider_right");
  175. m_horizontal_slider_left->on_change = [&](auto value) {
  176. m_horizontal_progressbar->set_value(value);
  177. if (!(value % (100 / m_horizontal_slider_right->max())))
  178. m_horizontal_slider_right->set_value(value / (100 / m_horizontal_slider_right->max()));
  179. };
  180. m_horizontal_slider_right->on_change = [&](auto value) {
  181. m_horizontal_progressbar->set_value((value * 100) / m_horizontal_slider_right->max());
  182. m_horizontal_slider_left->set_value((value * 100) / m_horizontal_slider_right->max());
  183. };
  184. m_enabled_scrollbar = sliders_tab.find_descendant_of_type_named<GUI::Scrollbar>("enabled_scrollbar");
  185. m_enabled_scrollbar->set_orientation(Orientation::Horizontal);
  186. m_disabled_scrollbar = sliders_tab.find_descendant_of_type_named<GUI::Scrollbar>("disabled_scrollbar");
  187. m_disabled_scrollbar->set_orientation(Orientation::Horizontal);
  188. m_opacity_imagewidget = sliders_tab.find_descendant_of_type_named<GUI::ImageWidget>("opacity_imagewidget");
  189. m_opacity_imagewidget->load_from_file("/res/graphics/brand-banner.png");
  190. m_opacity_slider = sliders_tab.find_descendant_of_type_named<GUI::OpacitySlider>("opacity_slider");
  191. m_opacity_slider->on_change = [&](auto percent) {
  192. m_opacity_imagewidget->set_opacity_percent(percent);
  193. };
  194. auto& wizards_tab = tab_widget.add_tab<GUI::Widget>("Wizards");
  195. wizards_tab.load_from_gml(wizards_tab_gml);
  196. m_wizard_button = wizards_tab.find_descendant_of_type_named<GUI::Button>("wizard_button");
  197. m_wizard_output = wizards_tab.find_descendant_of_type_named<GUI::TextEditor>("wizard_output");
  198. m_wizard_output->set_should_hide_unnecessary_scrollbars(true);
  199. const char* serenityos_ascii = {
  200. " ____ _ __ ____ ____\n"
  201. " / __/__ _______ ___ (_) /___ __/ __ \\/ __/\n"
  202. " _\\ \\/ -_) __/ -_) _ \\/ / __/ // / /_/ /\\ \\\n"
  203. "/___/\\__/_/ \\__/_//_/_/\\__/\\_, /\\____/___/\n"
  204. " /___/\n"
  205. };
  206. const char* wizard_ascii = {
  207. " _,-'|\n"
  208. " ,-'._ |\n"
  209. " .||, |####\\ |\n"
  210. "\\`' ,/ \\'L' | |\n"
  211. "= ,. = |-,#| |\n"
  212. "/ || \\ ,-'\\#/,'`.\n"
  213. " || ,' `,,. `.\n"
  214. " ,|____,' , ,;' \\| |\n"
  215. " (3|\\ _/|/' _| |\n"
  216. " ||/,-'' | >-'' _,\\\\\n"
  217. " ||' ==\\ ,-' ,'\n"
  218. " || | V \\ ,|\n"
  219. " || | |` |\n"
  220. " || | | \\\n"
  221. " || | \\ \\\n"
  222. " || | | \\\n"
  223. " || | \\_,-'\n"
  224. " || |___,,--')_\\\n"
  225. " || |_| _ccc/-\n"
  226. " || ccc/__\n"
  227. " _||_-\n"
  228. };
  229. StringBuilder sb;
  230. sb.appendf("%s%s", serenityos_ascii, wizard_ascii);
  231. m_wizard_output->set_text(sb.to_string());
  232. m_wizard_button->on_click = [&]() {
  233. StringBuilder sb;
  234. sb.append(m_wizard_output->get_text());
  235. sb.append("\nWizard started.");
  236. m_wizard_output->set_text(sb.to_string());
  237. auto wizard = DemoWizardDialog::construct(window());
  238. int result = wizard->exec();
  239. sb.append(String::formatted("\nWizard execution complete.\nDialog ExecResult code: {}", result));
  240. if (result == GUI::Dialog::ExecResult::ExecOK)
  241. sb.append(String::formatted(" (ExecOK)\n'Installation' location: \"{}\"", wizard->page_1_location()));
  242. m_wizard_output->set_text(sb.string_view());
  243. };
  244. auto& cursors_tab = tab_widget.add_tab<GUI::Widget>("Cursors");
  245. cursors_tab.load_from_gml(cursors_tab_gml);
  246. m_cursors_tableview = cursors_tab.find_descendant_of_type_named<GUI::TableView>("cursors_tableview");
  247. m_cursors_tableview->set_highlight_selected_rows(true);
  248. m_cursors_tableview->set_alternating_row_colors(false);
  249. m_cursors_tableview->set_vertical_padding(16);
  250. m_cursors_tableview->set_column_headers_visible(false);
  251. m_cursors_tableview->set_highlight_key_column(false);
  252. auto sorting_proxy_model = GUI::SortingProxyModel::create(MouseCursorModel::create());
  253. sorting_proxy_model->set_sort_role(GUI::ModelRole::Display);
  254. m_cursors_tableview->set_model(sorting_proxy_model);
  255. m_cursors_tableview->set_key_column_and_sort_order(MouseCursorModel::Column::Name, GUI::SortOrder::Ascending);
  256. m_cursors_tableview->model()->update();
  257. m_cursors_tableview->set_column_width(0, 25);
  258. m_cursors_tableview->on_activation = [&](const GUI::ModelIndex& index) {
  259. switch (index.row()) {
  260. case 0:
  261. window()->set_cursor(Gfx::StandardCursor::Arrow);
  262. break;
  263. case 1:
  264. window()->set_cursor(Gfx::StandardCursor::Crosshair);
  265. break;
  266. case 2:
  267. window()->set_cursor(Gfx::StandardCursor::Disallowed);
  268. break;
  269. case 3:
  270. window()->set_cursor(Gfx::StandardCursor::Drag);
  271. break;
  272. case 4:
  273. window()->set_cursor(Gfx::StandardCursor::Hand);
  274. break;
  275. case 5:
  276. window()->set_cursor(Gfx::StandardCursor::Help);
  277. break;
  278. case 6:
  279. window()->set_cursor(Gfx::StandardCursor::Hidden);
  280. break;
  281. case 7:
  282. window()->set_cursor(Gfx::StandardCursor::IBeam);
  283. break;
  284. case 8:
  285. window()->set_cursor(Gfx::StandardCursor::Move);
  286. break;
  287. case 9:
  288. window()->set_cursor(Gfx::StandardCursor::ResizeColumn);
  289. break;
  290. case 10:
  291. window()->set_cursor(Gfx::StandardCursor::ResizeDiagonalBLTR);
  292. break;
  293. case 11:
  294. window()->set_cursor(Gfx::StandardCursor::ResizeDiagonalTLBR);
  295. break;
  296. case 12:
  297. window()->set_cursor(Gfx::StandardCursor::ResizeHorizontal);
  298. break;
  299. case 13:
  300. window()->set_cursor(Gfx::StandardCursor::ResizeRow);
  301. break;
  302. case 14:
  303. window()->set_cursor(Gfx::StandardCursor::ResizeVertical);
  304. break;
  305. case 15:
  306. window()->set_cursor(Gfx::StandardCursor::Wait);
  307. break;
  308. default:
  309. window()->set_cursor(Gfx::StandardCursor::Arrow);
  310. }
  311. };
  312. auto& icons_tab = tab_widget.add_tab<GUI::Widget>("Icons");
  313. icons_tab.load_from_gml(icons_tab_gml);
  314. m_icons_tableview = icons_tab.find_descendant_of_type_named<GUI::TableView>("icons_tableview");
  315. m_icons_tableview->set_highlight_selected_rows(true);
  316. m_icons_tableview->set_alternating_row_colors(false);
  317. m_icons_tableview->set_vertical_padding(24);
  318. m_icons_tableview->set_column_headers_visible(false);
  319. m_icons_tableview->set_highlight_key_column(false);
  320. auto sorting_proxy_icons_model = GUI::SortingProxyModel::create(FileIconsModel::create());
  321. sorting_proxy_icons_model->set_sort_role(GUI::ModelRole::Display);
  322. m_icons_tableview->set_model(sorting_proxy_icons_model);
  323. m_icons_tableview->set_key_column_and_sort_order(FileIconsModel::Column::Name, GUI::SortOrder::Ascending);
  324. m_icons_tableview->model()->update();
  325. m_icons_tableview->set_column_width(0, 36);
  326. m_icons_tableview->set_column_width(1, 20);
  327. }
  328. GalleryWidget::~GalleryWidget()
  329. {
  330. }