main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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 "QSWidget.h"
  27. #include <AK/URL.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/MimeData.h>
  30. #include <LibGUI/AboutDialog.h>
  31. #include <LibGUI/Action.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/BoxLayout.h>
  34. #include <LibGUI/FilePicker.h>
  35. #include <LibGUI/Label.h>
  36. #include <LibGUI/Menu.h>
  37. #include <LibGUI/MenuBar.h>
  38. #include <LibGUI/MessageBox.h>
  39. #include <LibGUI/ToolBar.h>
  40. #include <LibGUI/Window.h>
  41. #include <LibGfx/Bitmap.h>
  42. #include <stdio.h>
  43. #include <string.h>
  44. int main(int argc, char** argv)
  45. {
  46. if (pledge("stdio shared_buffer accept cpath rpath unix cpath fattr proc exec thread", nullptr) < 0) {
  47. perror("pledge");
  48. return 1;
  49. }
  50. GUI::Application app(argc, argv);
  51. if (pledge("stdio shared_buffer accept cpath rpath proc exec thread", nullptr) < 0) {
  52. perror("pledge");
  53. return 1;
  54. }
  55. const char* path = nullptr;
  56. Core::ArgsParser args_parser;
  57. args_parser.add_positional_argument(path, "The image file to be displayed.", "file", Core::ArgsParser::Required::No);
  58. args_parser.parse(argc, argv);
  59. auto window = GUI::Window::construct();
  60. window->set_double_buffering_enabled(true);
  61. window->set_rect(200, 200, 300, 200);
  62. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-image.png"));
  63. auto& root_widget = window->set_main_widget<GUI::Widget>();
  64. root_widget.set_layout<GUI::VerticalBoxLayout>();
  65. root_widget.layout()->set_spacing(0);
  66. auto& main_toolbar = root_widget.add<GUI::ToolBar>();
  67. auto& widget = root_widget.add<QSWidget>();
  68. widget.on_scale_change = [&](int scale) {
  69. if (widget.bitmap())
  70. window->set_title(String::format("%s %s %d%% - QuickShow", widget.path().characters(), widget.bitmap()->size().to_string().characters(), scale));
  71. else
  72. window->set_title("QuickShow");
  73. };
  74. widget.on_drop = [&](auto& event) {
  75. window->move_to_front();
  76. if (event.mime_data().has_urls()) {
  77. auto urls = event.mime_data().urls();
  78. if (!urls.is_empty()) {
  79. auto url = urls.first();
  80. widget.load_from_file(url.path());
  81. }
  82. for (size_t i = 1; i < urls.size(); ++i) {
  83. if (fork() == 0) {
  84. execl("/bin/QuickShow", "/bin/QuickShow", urls[i].path().characters(), nullptr);
  85. ASSERT_NOT_REACHED();
  86. }
  87. }
  88. }
  89. };
  90. // Actions
  91. auto open_action = GUI::CommonActions::make_open_action(
  92. [&](auto&) {
  93. Optional<String> path = GUI::FilePicker::get_open_filepath("Open image...");
  94. if (path.has_value()) {
  95. widget.load_from_file(path.value());
  96. }
  97. });
  98. auto delete_action = GUI::CommonActions::make_delete_action(
  99. [&](auto&) {
  100. auto path = widget.path();
  101. if (path.is_empty())
  102. return;
  103. auto msgbox_result = GUI::MessageBox::show(String::format("Really delete %s?", path.characters()),
  104. "Confirm deletion",
  105. GUI::MessageBox::Type::Warning,
  106. GUI::MessageBox::InputType::OKCancel,
  107. window);
  108. if (msgbox_result == GUI::MessageBox::ExecCancel)
  109. return;
  110. auto unlink_result = unlink(widget.path().characters());
  111. dbg() << "unlink_result::" << unlink_result;
  112. if (unlink_result < 0) {
  113. int saved_errno = errno;
  114. GUI::MessageBox::show(String::format("unlink(%s) failed: %s", path.characters(), strerror(saved_errno)),
  115. "Delete failed",
  116. GUI::MessageBox::Type::Error,
  117. GUI::MessageBox::InputType::OK,
  118. window);
  119. return;
  120. }
  121. widget.clear();
  122. });
  123. auto quit_action = GUI::CommonActions::make_quit_action(
  124. [&](auto&) {
  125. app.quit();
  126. });
  127. auto rotate_left_action = GUI::Action::create("Rotate Left", { Mod_None, Key_L },
  128. [&](auto&) {
  129. widget.rotate(Gfx::RotationDirection::Left);
  130. });
  131. auto rotate_right_action = GUI::Action::create("Rotate Right", { Mod_None, Key_R },
  132. [&](auto&) {
  133. widget.rotate(Gfx::RotationDirection::Right);
  134. });
  135. auto vertical_flip_action = GUI::Action::create("Vertical Flip", { Mod_None, Key_V },
  136. [&](auto&) {
  137. widget.flip(Gfx::Orientation::Vertical);
  138. });
  139. auto horizontal_flip_action = GUI::Action::create("Horizontal Flip", { Mod_None, Key_H },
  140. [&](auto&) {
  141. widget.flip(Gfx::Orientation::Horizontal);
  142. });
  143. auto go_first_action = GUI::Action::create("First", { Mod_None, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-first.png"),
  144. [&](auto&) {
  145. widget.navigate(QSWidget::Directions::First);
  146. });
  147. auto go_back_action = GUI::Action::create("Back", { Mod_None, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"),
  148. [&](auto&) {
  149. widget.navigate(QSWidget::Directions::Back);
  150. });
  151. auto go_forward_action = GUI::Action::create("Forward", { Mod_None, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"),
  152. [&](auto&) {
  153. widget.navigate(QSWidget::Directions::Forward);
  154. });
  155. auto go_last_action = GUI::Action::create("Last", { Mod_None, Key_End }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-last.png"),
  156. [&](auto&) {
  157. widget.navigate(QSWidget::Directions::Last);
  158. });
  159. auto full_sceen_action = GUI::CommonActions::make_fullscreen_action(
  160. [&](auto&) {
  161. window->set_fullscreen(!window->is_fullscreen());
  162. main_toolbar.set_visible(!window->is_fullscreen());
  163. });
  164. auto zoom_in_action = GUI::Action::create("Zoom In", { Mod_None, Key_Plus }, Gfx::Bitmap::load_from_file("/res/icons/16x16/zoom-in.png"),
  165. [&](auto&) {
  166. widget.set_scale(widget.scale() + 10);
  167. });
  168. auto zoom_reset_action = GUI::Action::create("Zoom 100%", { Mod_None, Key_0 },
  169. [&](auto&) {
  170. widget.set_scale(100);
  171. });
  172. auto zoom_out_action = GUI::Action::create("Zoom Out", { Mod_None, Key_Minus }, Gfx::Bitmap::load_from_file("/res/icons/16x16/zoom-out.png"),
  173. [&](auto&) {
  174. widget.set_scale(widget.scale() - 10);
  175. });
  176. auto hide_show_toolbar_action = GUI::Action::create("Hide/Show Toolbar", { Mod_Ctrl, Key_T },
  177. [&](auto&) {
  178. main_toolbar.set_visible(!main_toolbar.is_visible());
  179. if (main_toolbar.is_visible()) {
  180. widget.set_toolbar_height(main_toolbar.height());
  181. } else {
  182. widget.set_toolbar_height(0);
  183. }
  184. });
  185. auto about_action = GUI::Action::create("About",
  186. [&](auto&) {
  187. GUI::AboutDialog::show("QuickShow", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-image.png"), window);
  188. });
  189. main_toolbar.add_action(open_action);
  190. main_toolbar.add_action(delete_action);
  191. main_toolbar.add_separator();
  192. main_toolbar.add_action(go_first_action);
  193. main_toolbar.add_action(go_back_action);
  194. main_toolbar.add_action(go_forward_action);
  195. main_toolbar.add_action(go_last_action);
  196. main_toolbar.add_separator();
  197. main_toolbar.add_action(zoom_in_action);
  198. main_toolbar.add_action(zoom_out_action);
  199. auto menubar = make<GUI::MenuBar>();
  200. auto& app_menu = menubar->add_menu("QuickShow");
  201. app_menu.add_action(open_action);
  202. app_menu.add_action(delete_action);
  203. app_menu.add_separator();
  204. app_menu.add_action(quit_action);
  205. auto& image_menu = menubar->add_menu("Image");
  206. image_menu.add_action(rotate_left_action);
  207. image_menu.add_action(rotate_right_action);
  208. image_menu.add_action(vertical_flip_action);
  209. image_menu.add_action(horizontal_flip_action);
  210. auto& navigate_menu = menubar->add_menu("Navigate");
  211. navigate_menu.add_action(go_first_action);
  212. navigate_menu.add_action(go_back_action);
  213. navigate_menu.add_action(go_forward_action);
  214. navigate_menu.add_action(go_last_action);
  215. auto& view_menu = menubar->add_menu("View");
  216. view_menu.add_action(full_sceen_action);
  217. view_menu.add_separator();
  218. view_menu.add_action(zoom_in_action);
  219. view_menu.add_action(zoom_reset_action);
  220. view_menu.add_action(zoom_out_action);
  221. view_menu.add_separator();
  222. view_menu.add_action(hide_show_toolbar_action);
  223. auto& help_menu = menubar->add_menu("Help");
  224. help_menu.add_action(about_action);
  225. app.set_menubar(move(menubar));
  226. if (path != nullptr) {
  227. widget.load_from_file(path);
  228. }
  229. widget.on_scale_change(100);
  230. window->show();
  231. return app.exec();
  232. }