CellTypeDialog.cpp 16 KB

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