CellTypeDialog.cpp 18 KB

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