CellTypeDialog.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CellTypeDialog.h"
  7. #include "Cell.h"
  8. #include "Spreadsheet.h"
  9. #include <AK/StringBuilder.h>
  10. #include <Applications/Spreadsheet/CondFormattingGML.h>
  11. #include <Applications/Spreadsheet/CondFormattingViewGML.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/Button.h>
  14. #include <LibGUI/CheckBox.h>
  15. #include <LibGUI/ColorInput.h>
  16. #include <LibGUI/ComboBox.h>
  17. #include <LibGUI/ItemListModel.h>
  18. #include <LibGUI/Label.h>
  19. #include <LibGUI/ListView.h>
  20. #include <LibGUI/SpinBox.h>
  21. #include <LibGUI/TabWidget.h>
  22. #include <LibGUI/TextEditor.h>
  23. #include <LibGUI/Widget.h>
  24. #include <LibGfx/Font/FontDatabase.h>
  25. #include <LibJS/SyntaxHighlighter.h>
  26. REGISTER_WIDGET(Spreadsheet, ConditionsView);
  27. namespace Spreadsheet {
  28. CellTypeDialog::CellTypeDialog(Vector<Position> const& positions, Sheet& sheet, GUI::Window* parent)
  29. : GUI::Dialog(parent)
  30. {
  31. VERIFY(!positions.is_empty());
  32. StringBuilder builder;
  33. if (positions.size() == 1)
  34. builder.appendff("Format cell {}", positions.first().to_cell_identifier(sheet));
  35. else
  36. builder.appendff("Format {} cells", positions.size());
  37. set_title(builder.string_view());
  38. set_icon(parent->icon());
  39. resize(285, 360);
  40. auto& main_widget = set_main_widget<GUI::Widget>();
  41. main_widget.set_layout<GUI::VerticalBoxLayout>().set_margins(4);
  42. main_widget.set_fill_with_background_color(true);
  43. auto& tab_widget = main_widget.add<GUI::TabWidget>();
  44. setup_tabs(tab_widget, positions, sheet);
  45. auto& buttonbox = main_widget.add<GUI::Widget>();
  46. buttonbox.set_shrink_to_fit(true);
  47. auto& button_layout = buttonbox.set_layout<GUI::HorizontalBoxLayout>();
  48. button_layout.set_spacing(10);
  49. button_layout.add_spacer();
  50. auto& ok_button = buttonbox.add<GUI::Button>("OK");
  51. ok_button.set_fixed_width(80);
  52. ok_button.on_click = [&](auto) { done(ExecResult::OK); };
  53. }
  54. Vector<String> const g_horizontal_alignments { "Left", "Center", "Right" };
  55. Vector<String> const g_vertical_alignments { "Top", "Center", "Bottom" };
  56. Vector<String> g_types;
  57. constexpr static CellTypeDialog::VerticalAlignment vertical_alignment_from(Gfx::TextAlignment alignment)
  58. {
  59. switch (alignment) {
  60. case Gfx::TextAlignment::CenterRight:
  61. case Gfx::TextAlignment::CenterLeft:
  62. case Gfx::TextAlignment::Center:
  63. return CellTypeDialog::VerticalAlignment::Center;
  64. case Gfx::TextAlignment::TopCenter:
  65. case Gfx::TextAlignment::TopRight:
  66. case Gfx::TextAlignment::TopLeft:
  67. return CellTypeDialog::VerticalAlignment::Top;
  68. case Gfx::TextAlignment::BottomCenter:
  69. case Gfx::TextAlignment::BottomLeft:
  70. case Gfx::TextAlignment::BottomRight:
  71. return CellTypeDialog::VerticalAlignment::Bottom;
  72. }
  73. return CellTypeDialog::VerticalAlignment::Center;
  74. }
  75. constexpr static CellTypeDialog::HorizontalAlignment horizontal_alignment_from(Gfx::TextAlignment alignment)
  76. {
  77. switch (alignment) {
  78. case Gfx::TextAlignment::BottomCenter:
  79. case Gfx::TextAlignment::Center:
  80. case Gfx::TextAlignment::TopCenter:
  81. return CellTypeDialog::HorizontalAlignment::Center;
  82. case Gfx::TextAlignment::TopRight:
  83. case Gfx::TextAlignment::CenterRight:
  84. case Gfx::TextAlignment::BottomRight:
  85. return CellTypeDialog::HorizontalAlignment::Right;
  86. case Gfx::TextAlignment::TopLeft:
  87. case Gfx::TextAlignment::CenterLeft:
  88. case Gfx::TextAlignment::BottomLeft:
  89. return CellTypeDialog::HorizontalAlignment::Left;
  90. }
  91. return CellTypeDialog::HorizontalAlignment::Right;
  92. }
  93. void CellTypeDialog::setup_tabs(GUI::TabWidget& tabs, Vector<Position> const& positions, Sheet& sheet)
  94. {
  95. g_types.clear();
  96. for (auto& type_name : CellType::names())
  97. g_types.append(type_name);
  98. Vector<Cell&> cells;
  99. for (auto& position : positions) {
  100. if (auto cell = sheet.at(position))
  101. cells.append(*cell);
  102. }
  103. if (cells.size() == 1) {
  104. auto& cell = cells.first();
  105. m_format = cell.type_metadata().format;
  106. m_length = cell.type_metadata().length;
  107. m_type = &cell.type();
  108. m_vertical_alignment = vertical_alignment_from(cell.type_metadata().alignment);
  109. m_horizontal_alignment = horizontal_alignment_from(cell.type_metadata().alignment);
  110. m_static_format = cell.type_metadata().static_format;
  111. m_conditional_formats = cell.conditional_formats();
  112. }
  113. auto& type_tab = tabs.add_tab<GUI::Widget>("Type");
  114. type_tab.set_layout<GUI::HorizontalBoxLayout>().set_margins(4);
  115. {
  116. auto& left_side = type_tab.add<GUI::Widget>();
  117. left_side.set_layout<GUI::VerticalBoxLayout>();
  118. auto& right_side = type_tab.add<GUI::Widget>();
  119. right_side.set_layout<GUI::VerticalBoxLayout>();
  120. right_side.set_fixed_width(170);
  121. auto& type_list = left_side.add<GUI::ListView>();
  122. type_list.set_model(*GUI::ItemListModel<String>::create(g_types));
  123. type_list.set_should_hide_unnecessary_scrollbars(true);
  124. type_list.on_selection_change = [&] {
  125. const auto& index = type_list.selection().first();
  126. if (!index.is_valid()) {
  127. m_type = nullptr;
  128. return;
  129. }
  130. m_type = CellType::get_by_name(g_types.at(index.row()));
  131. if (auto* editor = right_side.find_descendant_of_type_named<GUI::TextEditor>("format_editor"))
  132. editor->set_tooltip(m_type->metadata_hint(MetadataName::Format));
  133. };
  134. {
  135. auto& checkbox = right_side.add<GUI::CheckBox>("Override max length");
  136. auto& spinbox = right_side.add<GUI::SpinBox>();
  137. checkbox.set_checked(m_length != -1);
  138. spinbox.set_min(0);
  139. spinbox.set_enabled(m_length != -1);
  140. if (m_length > -1)
  141. spinbox.set_value(m_length);
  142. checkbox.on_checked = [&](auto checked) {
  143. spinbox.set_enabled(checked);
  144. if (!checked) {
  145. m_length = -1;
  146. spinbox.set_value(0);
  147. }
  148. };
  149. spinbox.on_change = [&](auto value) {
  150. m_length = value;
  151. };
  152. }
  153. {
  154. auto& checkbox = right_side.add<GUI::CheckBox>("Override display format");
  155. auto& editor = right_side.add<GUI::TextEditor>();
  156. checkbox.set_checked(!m_format.is_empty());
  157. editor.set_name("format_editor");
  158. editor.set_should_hide_unnecessary_scrollbars(true);
  159. editor.set_enabled(!m_format.is_empty());
  160. editor.set_text(m_format);
  161. checkbox.on_checked = [&](auto checked) {
  162. editor.set_enabled(checked);
  163. if (!checked)
  164. m_format = String::empty();
  165. editor.set_text(m_format);
  166. };
  167. editor.on_change = [&] {
  168. m_format = editor.text();
  169. };
  170. }
  171. }
  172. auto& alignment_tab = tabs.add_tab<GUI::Widget>("Alignment");
  173. alignment_tab.set_layout<GUI::VerticalBoxLayout>().set_margins(4);
  174. {
  175. // FIXME: Frame?
  176. // Horizontal alignment
  177. {
  178. auto& horizontal_alignment_selection_container = alignment_tab.add<GUI::Widget>();
  179. horizontal_alignment_selection_container.set_layout<GUI::HorizontalBoxLayout>();
  180. horizontal_alignment_selection_container.layout()->set_margins({ 4, 0, 0 });
  181. horizontal_alignment_selection_container.set_fixed_height(22);
  182. auto& horizontal_alignment_label = horizontal_alignment_selection_container.add<GUI::Label>();
  183. horizontal_alignment_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  184. horizontal_alignment_label.set_text("Horizontal text alignment");
  185. auto& horizontal_combobox = alignment_tab.add<GUI::ComboBox>();
  186. horizontal_combobox.set_only_allow_values_from_model(true);
  187. horizontal_combobox.set_model(*GUI::ItemListModel<String>::create(g_horizontal_alignments));
  188. horizontal_combobox.set_selected_index((int)m_horizontal_alignment);
  189. horizontal_combobox.on_change = [&](auto&, const GUI::ModelIndex& index) {
  190. switch (index.row()) {
  191. case 0:
  192. m_horizontal_alignment = HorizontalAlignment::Left;
  193. break;
  194. case 1:
  195. m_horizontal_alignment = HorizontalAlignment::Center;
  196. break;
  197. case 2:
  198. m_horizontal_alignment = HorizontalAlignment::Right;
  199. break;
  200. default:
  201. VERIFY_NOT_REACHED();
  202. }
  203. };
  204. }
  205. // Vertical alignment
  206. {
  207. auto& vertical_alignment_container = alignment_tab.add<GUI::Widget>();
  208. vertical_alignment_container.set_layout<GUI::HorizontalBoxLayout>();
  209. vertical_alignment_container.layout()->set_margins({ 4, 0, 0 });
  210. vertical_alignment_container.set_fixed_height(22);
  211. auto& vertical_alignment_label = vertical_alignment_container.add<GUI::Label>();
  212. vertical_alignment_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  213. vertical_alignment_label.set_text("Vertical text alignment");
  214. auto& vertical_combobox = alignment_tab.add<GUI::ComboBox>();
  215. vertical_combobox.set_only_allow_values_from_model(true);
  216. vertical_combobox.set_model(*GUI::ItemListModel<String>::create(g_vertical_alignments));
  217. vertical_combobox.set_selected_index((int)m_vertical_alignment);
  218. vertical_combobox.on_change = [&](auto&, const GUI::ModelIndex& index) {
  219. switch (index.row()) {
  220. case 0:
  221. m_vertical_alignment = VerticalAlignment::Top;
  222. break;
  223. case 1:
  224. m_vertical_alignment = VerticalAlignment::Center;
  225. break;
  226. case 2:
  227. m_vertical_alignment = VerticalAlignment::Bottom;
  228. break;
  229. default:
  230. VERIFY_NOT_REACHED();
  231. }
  232. };
  233. }
  234. }
  235. auto& colors_tab = tabs.add_tab<GUI::Widget>("Color");
  236. colors_tab.set_layout<GUI::VerticalBoxLayout>().set_margins(4);
  237. {
  238. // Static formatting
  239. {
  240. auto& static_formatting_container = colors_tab.add<GUI::Widget>();
  241. static_formatting_container.set_layout<GUI::VerticalBoxLayout>();
  242. // Foreground
  243. {
  244. // FIXME: Somehow allow unsetting these.
  245. auto& foreground_container = static_formatting_container.add<GUI::Widget>();
  246. foreground_container.set_layout<GUI::HorizontalBoxLayout>();
  247. foreground_container.layout()->set_margins({ 4, 0, 0 });
  248. foreground_container.set_shrink_to_fit(true);
  249. auto& foreground_label = foreground_container.add<GUI::Label>();
  250. foreground_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  251. foreground_label.set_text("Static foreground color");
  252. auto& foreground_selector = foreground_container.add<GUI::ColorInput>();
  253. if (m_static_format.foreground_color.has_value())
  254. foreground_selector.set_color(m_static_format.foreground_color.value());
  255. foreground_selector.on_change = [&]() {
  256. m_static_format.foreground_color = foreground_selector.color();
  257. };
  258. }
  259. // Background
  260. {
  261. // FIXME: Somehow allow unsetting these.
  262. auto& background_container = static_formatting_container.add<GUI::Widget>();
  263. background_container.set_layout<GUI::HorizontalBoxLayout>();
  264. background_container.layout()->set_margins({ 4, 0, 0 });
  265. background_container.set_shrink_to_fit(true);
  266. auto& background_label = background_container.add<GUI::Label>();
  267. background_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  268. background_label.set_text("Static background color");
  269. auto& background_selector = background_container.add<GUI::ColorInput>();
  270. if (m_static_format.background_color.has_value())
  271. background_selector.set_color(m_static_format.background_color.value());
  272. background_selector.on_change = [&]() {
  273. m_static_format.background_color = background_selector.color();
  274. };
  275. }
  276. }
  277. }
  278. auto& conditional_fmt_tab = tabs.add_tab<GUI::Widget>("Conditional format");
  279. conditional_fmt_tab.load_from_gml(cond_fmt_gml);
  280. {
  281. auto& view = *conditional_fmt_tab.find_descendant_of_type_named<Spreadsheet::ConditionsView>("conditions_view");
  282. view.set_formats(&m_conditional_formats);
  283. auto& add_button = *conditional_fmt_tab.find_descendant_of_type_named<GUI::Button>("add_button");
  284. add_button.on_click = [&](auto) {
  285. view.add_format();
  286. };
  287. // FIXME: Disable this when empty.
  288. auto& remove_button = *conditional_fmt_tab.find_descendant_of_type_named<GUI::Button>("remove_button");
  289. remove_button.on_click = [&](auto) {
  290. view.remove_top();
  291. };
  292. }
  293. }
  294. CellTypeMetadata CellTypeDialog::metadata() const
  295. {
  296. CellTypeMetadata metadata;
  297. metadata.format = m_format;
  298. metadata.length = m_length;
  299. metadata.static_format = m_static_format;
  300. switch (m_vertical_alignment) {
  301. case VerticalAlignment::Top:
  302. switch (m_horizontal_alignment) {
  303. case HorizontalAlignment::Left:
  304. metadata.alignment = Gfx::TextAlignment::TopLeft;
  305. break;
  306. case HorizontalAlignment::Center:
  307. metadata.alignment = Gfx::TextAlignment::TopCenter;
  308. break;
  309. case HorizontalAlignment::Right:
  310. metadata.alignment = Gfx::TextAlignment::TopRight;
  311. break;
  312. }
  313. break;
  314. case VerticalAlignment::Center:
  315. switch (m_horizontal_alignment) {
  316. case HorizontalAlignment::Left:
  317. metadata.alignment = Gfx::TextAlignment::CenterLeft;
  318. break;
  319. case HorizontalAlignment::Center:
  320. metadata.alignment = Gfx::TextAlignment::Center;
  321. break;
  322. case HorizontalAlignment::Right:
  323. metadata.alignment = Gfx::TextAlignment::CenterRight;
  324. break;
  325. }
  326. break;
  327. case VerticalAlignment::Bottom:
  328. switch (m_horizontal_alignment) {
  329. case HorizontalAlignment::Left:
  330. metadata.alignment = Gfx::TextAlignment::CenterLeft; // BottomLeft?
  331. break;
  332. case HorizontalAlignment::Center:
  333. metadata.alignment = Gfx::TextAlignment::Center;
  334. break;
  335. case HorizontalAlignment::Right:
  336. metadata.alignment = Gfx::TextAlignment::BottomRight;
  337. break;
  338. }
  339. break;
  340. }
  341. return metadata;
  342. }
  343. ConditionView::ConditionView(ConditionalFormat& fmt)
  344. : m_format(fmt)
  345. {
  346. load_from_gml(cond_fmt_view_gml);
  347. auto& fg_input = *find_descendant_of_type_named<GUI::ColorInput>("foreground_input");
  348. auto& bg_input = *find_descendant_of_type_named<GUI::ColorInput>("background_input");
  349. auto& formula_editor = *find_descendant_of_type_named<GUI::TextEditor>("formula_editor");
  350. if (m_format.foreground_color.has_value())
  351. fg_input.set_color(m_format.foreground_color.value());
  352. if (m_format.background_color.has_value())
  353. bg_input.set_color(m_format.background_color.value());
  354. formula_editor.set_text(m_format.condition);
  355. // FIXME: Allow unsetting these.
  356. fg_input.on_change = [&] {
  357. m_format.foreground_color = fg_input.color();
  358. };
  359. bg_input.on_change = [&] {
  360. m_format.background_color = bg_input.color();
  361. };
  362. formula_editor.set_syntax_highlighter(make<JS::SyntaxHighlighter>());
  363. formula_editor.set_should_hide_unnecessary_scrollbars(true);
  364. formula_editor.on_change = [&] {
  365. m_format.condition = formula_editor.text();
  366. };
  367. }
  368. ConditionView::~ConditionView()
  369. {
  370. }
  371. ConditionsView::ConditionsView()
  372. {
  373. set_layout<GUI::VerticalBoxLayout>().set_spacing(2);
  374. }
  375. void ConditionsView::set_formats(Vector<ConditionalFormat>* formats)
  376. {
  377. VERIFY(!m_formats);
  378. m_formats = formats;
  379. for (auto& entry : *m_formats)
  380. m_widgets.append(add<ConditionView>(entry));
  381. }
  382. void ConditionsView::add_format()
  383. {
  384. VERIFY(m_formats);
  385. m_formats->empend();
  386. auto& last = m_formats->last();
  387. m_widgets.append(add<ConditionView>(last));
  388. update();
  389. }
  390. void ConditionsView::remove_top()
  391. {
  392. VERIFY(m_formats);
  393. if (m_formats->is_empty())
  394. return;
  395. m_formats->take_last();
  396. m_widgets.take_last()->remove_from_parent();
  397. update();
  398. }
  399. ConditionsView::~ConditionsView()
  400. {
  401. }
  402. }