main.cpp 11 KB

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