CellTypeDialog.cpp 16 KB

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