MainWidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_help_action([](auto&) {
  184. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/SQLStudio.md"), "/bin/Help");
  185. }));
  186. help_menu.add_action(GUI::CommonActions::make_about_action("SQL Studio", GUI::Icon::default_icon("app-sql-studio"), window));
  187. }
  188. void MainWidget::open_new_script()
  189. {
  190. auto new_script_name = String::formatted("New Script - {}", m_new_script_counter);
  191. ++m_new_script_counter;
  192. auto& editor = m_tab_widget->add_tab<ScriptEditor>(new_script_name);
  193. editor.new_script_with_temp_name(new_script_name);
  194. editor.on_cursor_change = [this] { on_editor_change(); };
  195. editor.on_selection_change = [this] { on_editor_change(); };
  196. editor.on_highlighter_change = [this] { on_editor_change(); };
  197. m_tab_widget->set_active_widget(&editor);
  198. }
  199. void MainWidget::open_script_from_file(LexicalPath const& file_path)
  200. {
  201. auto& editor = m_tab_widget->add_tab<ScriptEditor>(file_path.title());
  202. auto maybe_error = editor.open_script_from_file(file_path);
  203. if (maybe_error.is_error()) {
  204. GUI::MessageBox::show_error(window(), String::formatted("Failed to open {}\n{}", file_path, maybe_error.release_error()));
  205. return;
  206. }
  207. editor.on_cursor_change = [this] { on_editor_change(); };
  208. editor.on_selection_change = [this] { on_editor_change(); };
  209. editor.on_highlighter_change = [this] { on_editor_change(); };
  210. m_tab_widget->set_active_widget(&editor);
  211. }
  212. void MainWidget::open_database_from_file(LexicalPath const&)
  213. {
  214. TODO();
  215. }
  216. bool MainWidget::request_close()
  217. {
  218. auto any_scripts_modified { false };
  219. auto is_script_modified = [&](auto& child) {
  220. auto editor = dynamic_cast<ScriptEditor*>(&child);
  221. if (!editor)
  222. return IterationDecision::Continue;
  223. if (editor->document().is_modified()) {
  224. any_scripts_modified = true;
  225. return IterationDecision::Break;
  226. }
  227. return IterationDecision::Continue;
  228. };
  229. m_tab_widget->for_each_child_widget(is_script_modified);
  230. if (!any_scripts_modified)
  231. return true;
  232. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), {});
  233. switch (result) {
  234. case GUI::Dialog::ExecResult::Yes:
  235. break;
  236. case GUI::Dialog::ExecResult::No:
  237. return true;
  238. default:
  239. return false;
  240. }
  241. m_save_all_action->activate();
  242. any_scripts_modified = false;
  243. m_tab_widget->for_each_child_widget(is_script_modified);
  244. return !any_scripts_modified;
  245. }
  246. void MainWidget::update_title()
  247. {
  248. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  249. if (editor) {
  250. window()->set_title(String::formatted("{} - SQL Studio", editor->name()));
  251. } else {
  252. window()->set_title("SQL Studio");
  253. }
  254. }
  255. void MainWidget::on_editor_change()
  256. {
  257. auto editor = dynamic_cast<ScriptEditor*>(m_tab_widget->active_widget());
  258. if (editor) {
  259. update_statusbar(editor);
  260. update_editor_actions(editor);
  261. }
  262. }
  263. void MainWidget::update_statusbar(ScriptEditor* editor)
  264. {
  265. if (editor->has_selection()) {
  266. auto character_count = editor->selected_text().length();
  267. auto word_count = editor->number_of_selected_words();
  268. m_statusbar->set_text(1, String::formatted("{} {} ({} {}) selected", character_count, character_count == 1 ? "character" : "characters", word_count, word_count == 1 ? "word" : "words"));
  269. } else {
  270. auto character_count = editor->text().length();
  271. auto word_count = editor->number_of_words();
  272. m_statusbar->set_text(1, String::formatted("{} {} ({} {})", character_count, character_count == 1 ? "character" : "characters", word_count, word_count == 1 ? "word" : "words"));
  273. }
  274. m_statusbar->set_text(2, String::formatted("Ln {}, Col {}", editor->cursor().line() + 1, editor->cursor().column()));
  275. }
  276. void MainWidget::update_editor_actions(ScriptEditor* editor)
  277. {
  278. m_copy_action->set_enabled(editor->copy_action().is_enabled());
  279. m_cut_action->set_enabled(editor->cut_action().is_enabled());
  280. m_paste_action->set_enabled(editor->paste_action().is_enabled());
  281. m_undo_action->set_enabled(editor->undo_action().is_enabled());
  282. m_redo_action->set_enabled(editor->redo_action().is_enabled());
  283. }
  284. void MainWidget::drop_event(GUI::DropEvent& drop_event)
  285. {
  286. drop_event.accept();
  287. window()->move_to_front();
  288. if (drop_event.mime_data().has_urls()) {
  289. auto urls = drop_event.mime_data().urls();
  290. if (urls.is_empty())
  291. return;
  292. for (auto& url : urls) {
  293. auto& scheme = url.scheme();
  294. if (!scheme.equals_ignoring_case("file"))
  295. continue;
  296. auto lexical_path = LexicalPath(url.path());
  297. if (lexical_path.extension().equals_ignoring_case("sql"sv))
  298. open_script_from_file(lexical_path);
  299. if (lexical_path.extension().equals_ignoring_case("db"sv))
  300. open_database_from_file(lexical_path);
  301. }
  302. }
  303. }
  304. }