MainWidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2022, Dylan Katz <dykatz@uw.edu>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibDesktop/Launcher.h>
  7. #include <LibGUI/Action.h>
  8. #include <LibGUI/Application.h>
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/FilePicker.h>
  11. #include <LibGUI/Menu.h>
  12. #include <LibGUI/MessageBox.h>
  13. #include <LibGUI/Statusbar.h>
  14. #include <LibGUI/TabWidget.h>
  15. #include <LibGUI/TextDocument.h>
  16. #include <LibGUI/TextEditor.h>
  17. #include <LibGUI/Toolbar.h>
  18. #include <LibGUI/ToolbarContainer.h>
  19. #include "MainWidget.h"
  20. #include "ScriptEditor.h"
  21. namespace SQLStudio {
  22. MainWidget::MainWidget()
  23. {
  24. set_fill_with_background_color(true);
  25. set_layout<GUI::VerticalBoxLayout>();
  26. m_new_action = GUI::Action::create("&New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  27. open_new_script();
  28. });
  29. m_open_action = GUI::CommonActions::make_open_action([&](auto&) {
  30. auto maybe_load_path = GUI::FilePicker::get_open_filepath(window());
  31. if (!maybe_load_path.has_value())
  32. return;
  33. auto lexical_path = LexicalPath(maybe_load_path.release_value());
  34. open_script_from_file(lexical_path);
  35. });
  36. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  37. if (!m_tab_widget)
  38. return;
  39. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  40. if (!editor)
  41. return;
  42. auto save_attempt = editor->save();
  43. if (save_attempt.is_error())
  44. GUI::MessageBox::show_error(window(), String::formatted("Failed to save\n{}", save_attempt.release_error()));
  45. });
  46. m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  47. if (!m_tab_widget)
  48. return;
  49. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  50. if (!editor)
  51. return;
  52. auto save_attempt = editor->save_as();
  53. if (save_attempt.is_error())
  54. GUI::MessageBox::show_error(window(), String::formatted("Failed to save\n{}", save_attempt.release_error()));
  55. });
  56. m_save_all_action = GUI::Action::create("Save All", { Mod_Ctrl | Mod_Alt, Key_S }, [this](auto&) {
  57. auto current_active_widget = m_tab_widget->active_widget();
  58. ErrorOr<void> error {};
  59. m_tab_widget->for_each_child_widget([&](auto& child) {
  60. auto editor = dynamic_cast<ScriptEditor*>(&child);
  61. if (!editor)
  62. return IterationDecision::Continue;
  63. m_tab_widget->set_active_widget(editor);
  64. auto save_attempt = editor->save();
  65. if (save_attempt.is_error()) {
  66. error = save_attempt.release_error();
  67. return IterationDecision::Break;
  68. }
  69. auto save_result = save_attempt.release_value();
  70. if (save_result)
  71. return IterationDecision::Continue;
  72. return IterationDecision::Break;
  73. });
  74. if (error.is_error())
  75. GUI::MessageBox::show_error(window(), String::formatted("Failed to save all files\n{}", error.release_error()));
  76. m_tab_widget->set_active_widget(current_active_widget);
  77. });
  78. m_copy_action = GUI::CommonActions::make_copy_action([&](auto&) {
  79. if (!m_tab_widget)
  80. return;
  81. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  82. if (!editor)
  83. return;
  84. editor->copy_action().activate();
  85. update_editor_actions(editor);
  86. });
  87. m_cut_action = GUI::CommonActions::make_cut_action([&](auto&) {
  88. if (!m_tab_widget)
  89. return;
  90. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  91. if (!editor)
  92. return;
  93. editor->cut_action().activate();
  94. update_editor_actions(editor);
  95. });
  96. m_paste_action = GUI::CommonActions::make_paste_action([&](auto&) {
  97. if (!m_tab_widget)
  98. return;
  99. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  100. if (!editor)
  101. return;
  102. editor->paste_action().activate();
  103. update_editor_actions(editor);
  104. });
  105. m_undo_action = GUI::CommonActions::make_undo_action([&](auto&) {
  106. if (!m_tab_widget)
  107. return;
  108. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  109. if (!editor)
  110. return;
  111. editor->document().undo();
  112. update_editor_actions(editor);
  113. });
  114. m_redo_action = GUI::CommonActions::make_redo_action([&](auto&) {
  115. if (!m_tab_widget)
  116. return;
  117. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  118. if (!editor)
  119. return;
  120. editor->document().redo();
  121. update_editor_actions(editor);
  122. });
  123. auto& toolbar_container = add<GUI::ToolbarContainer>();
  124. auto& toolbar = toolbar_container.add<GUI::Toolbar>();
  125. toolbar.add_action(*m_new_action);
  126. toolbar.add_action(*m_open_action);
  127. toolbar.add_action(*m_save_action);
  128. toolbar.add_action(*m_save_as_action);
  129. toolbar.add_separator();
  130. toolbar.add_action(*m_copy_action);
  131. toolbar.add_action(*m_cut_action);
  132. toolbar.add_action(*m_paste_action);
  133. toolbar.add_separator();
  134. toolbar.add_action(*m_undo_action);
  135. toolbar.add_action(*m_redo_action);
  136. m_tab_widget = add<GUI::TabWidget>();
  137. m_tab_widget->set_close_button_enabled(true);
  138. m_tab_widget->set_reorder_allowed(true);
  139. m_tab_widget->on_tab_close_click = [&](auto& widget) {
  140. auto editor = dynamic_cast<ScriptEditor*>(&widget);
  141. if (!editor)
  142. return;
  143. auto close_attempt = editor->attempt_to_close();
  144. if (close_attempt.is_error()) {
  145. GUI::MessageBox::show_error(window(), String::formatted("Failed to save before closing\n{}", close_attempt.release_error()));
  146. return;
  147. }
  148. if (close_attempt.release_value()) {
  149. m_tab_widget->remove_tab(widget);
  150. update_title();
  151. }
  152. };
  153. m_tab_widget->on_change = [&](auto&) {
  154. update_title();
  155. on_editor_change();
  156. };
  157. m_statusbar = add<GUI::Statusbar>(3);
  158. m_statusbar->segment(1).set_mode(GUI::Statusbar::Segment::Mode::Fixed);
  159. m_statusbar->segment(1).set_fixed_width(font().width("000000 characters (00000 words) selected") + font().max_glyph_width());
  160. m_statusbar->segment(2).set_mode(GUI::Statusbar::Segment::Mode::Fixed);
  161. m_statusbar->segment(2).set_fixed_width(font().width("Ln 0000, Col 000") + font().max_glyph_width());
  162. }
  163. void MainWidget::initialize_menu(GUI::Window* window)
  164. {
  165. auto& file_menu = window->add_menu("&File");
  166. file_menu.add_action(*m_new_action);
  167. file_menu.add_action(*m_open_action);
  168. file_menu.add_action(*m_save_action);
  169. file_menu.add_action(*m_save_as_action);
  170. file_menu.add_action(*m_save_all_action);
  171. file_menu.add_separator();
  172. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  173. GUI::Application::the()->quit();
  174. }));
  175. auto& edit_menu = window->add_menu("&Edit");
  176. edit_menu.add_action(*m_copy_action);
  177. edit_menu.add_action(*m_cut_action);
  178. edit_menu.add_action(*m_paste_action);
  179. edit_menu.add_separator();
  180. edit_menu.add_action(*m_undo_action);
  181. edit_menu.add_action(*m_redo_action);
  182. auto& help_menu = window->add_menu("&Help");
  183. help_menu.add_action(GUI::CommonActions::make_about_action("SQL Studio", GUI::Icon::default_icon("app-sql-studio"), window));
  184. }
  185. void MainWidget::open_new_script()
  186. {
  187. auto new_script_name = String::formatted("New Script - {}", m_new_script_counter);
  188. ++m_new_script_counter;
  189. auto& editor = m_tab_widget->add_tab<ScriptEditor>(new_script_name);
  190. editor.new_script_with_temp_name(new_script_name);
  191. editor.on_cursor_change = [this] { on_editor_change(); };
  192. editor.on_selection_change = [this] { on_editor_change(); };
  193. editor.on_highlighter_change = [this] { on_editor_change(); };
  194. m_tab_widget->set_active_widget(&editor);
  195. }
  196. void MainWidget::open_script_from_file(LexicalPath const& file_path)
  197. {
  198. auto& editor = m_tab_widget->add_tab<ScriptEditor>(file_path.title());
  199. auto maybe_error = editor.open_script_from_file(file_path);
  200. if (maybe_error.is_error()) {
  201. GUI::MessageBox::show_error(window(), String::formatted("Failed to open {}\n{}", file_path, maybe_error.release_error()));
  202. return;
  203. }
  204. editor.on_cursor_change = [this] { on_editor_change(); };
  205. editor.on_selection_change = [this] { on_editor_change(); };
  206. editor.on_highlighter_change = [this] { on_editor_change(); };
  207. m_tab_widget->set_active_widget(&editor);
  208. }
  209. void MainWidget::open_database_from_file(LexicalPath const&)
  210. {
  211. TODO();
  212. }
  213. bool MainWidget::request_close()
  214. {
  215. auto any_scripts_modified { false };
  216. auto is_script_modified = [&](auto& child) {
  217. auto editor = dynamic_cast<ScriptEditor*>(&child);
  218. if (!editor)
  219. return IterationDecision::Continue;
  220. if (editor->document().is_modified()) {
  221. any_scripts_modified = true;
  222. return IterationDecision::Break;
  223. }
  224. return IterationDecision::Continue;
  225. };
  226. m_tab_widget->for_each_child_widget(is_script_modified);
  227. if (!any_scripts_modified)
  228. return true;
  229. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), {});
  230. switch (result) {
  231. case GUI::Dialog::ExecResult::ExecYes:
  232. break;
  233. case GUI::Dialog::ExecResult::ExecNo:
  234. return true;
  235. default:
  236. return false;
  237. }
  238. m_save_all_action->activate();
  239. any_scripts_modified = false;
  240. m_tab_widget->for_each_child_widget(is_script_modified);
  241. return !any_scripts_modified;
  242. }
  243. void MainWidget::update_title()
  244. {
  245. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  246. if (editor) {
  247. window()->set_title(String::formatted("{} - SQL Studio", editor->name()));
  248. } else {
  249. window()->set_title("SQL Studio");
  250. }
  251. }
  252. void MainWidget::on_editor_change()
  253. {
  254. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  255. if (editor) {
  256. update_statusbar(editor);
  257. update_editor_actions(editor);
  258. }
  259. }
  260. void MainWidget::update_statusbar(ScriptEditor* editor)
  261. {
  262. if (editor->has_selection()) {
  263. auto character_count = editor->selected_text().length();
  264. auto word_count = editor->number_of_selected_words();
  265. m_statusbar->set_text(1, String::formatted("{} {} ({} {}) selected", character_count, character_count == 1 ? "character" : "characters", word_count, word_count == 1 ? "word" : "words"));
  266. } else {
  267. auto character_count = editor->text().length();
  268. auto word_count = editor->number_of_words();
  269. m_statusbar->set_text(1, String::formatted("{} {} ({} {})", character_count, character_count == 1 ? "character" : "characters", word_count, word_count == 1 ? "word" : "words"));
  270. }
  271. m_statusbar->set_text(2, String::formatted("Ln {}, Col {}", editor->cursor().line() + 1, editor->cursor().column()));
  272. }
  273. void MainWidget::update_editor_actions(ScriptEditor* editor)
  274. {
  275. m_copy_action->set_enabled(editor->copy_action().is_enabled());
  276. m_cut_action->set_enabled(editor->cut_action().is_enabled());
  277. m_paste_action->set_enabled(editor->paste_action().is_enabled());
  278. m_undo_action->set_enabled(editor->undo_action().is_enabled());
  279. m_redo_action->set_enabled(editor->redo_action().is_enabled());
  280. }
  281. void MainWidget::drop_event(GUI::DropEvent& drop_event)
  282. {
  283. drop_event.accept();
  284. window()->move_to_front();
  285. if (drop_event.mime_data().has_urls()) {
  286. auto urls = drop_event.mime_data().urls();
  287. if (urls.is_empty())
  288. return;
  289. for (auto& url : urls) {
  290. auto& scheme = url.scheme();
  291. if (!scheme.equals_ignoring_case("file"))
  292. continue;
  293. auto lexical_path = LexicalPath(url.path());
  294. if (lexical_path.extension().equals_ignoring_case("sql"sv))
  295. open_script_from_file(lexical_path);
  296. if (lexical_path.extension().equals_ignoring_case("db"sv))
  297. open_database_from_file(lexical_path);
  298. }
  299. }
  300. }
  301. }