main.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <LibGUI/Action.h>
  28. #include <LibGUI/Application.h>
  29. #include <LibGUI/BoxLayout.h>
  30. #include <LibGUI/Label.h>
  31. #include <LibGUI/Menu.h>
  32. #include <LibGUI/MenuBar.h>
  33. #include <LibGUI/Window.h>
  34. #include <LibGfx/Bitmap.h>
  35. #include <stdio.h>
  36. int main(int argc, char** argv)
  37. {
  38. if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
  39. perror("pledge");
  40. return 1;
  41. }
  42. GUI::Application app(argc, argv);
  43. if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
  44. perror("pledge");
  45. return 1;
  46. }
  47. auto menubar = make<GUI::MenuBar>();
  48. auto app_menu = GUI::Menu::construct("QuickShow");
  49. app_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  50. GUI::Application::the().quit(0);
  51. return;
  52. }));
  53. menubar->add_menu(move(app_menu));
  54. auto file_menu = GUI::Menu::construct("File");
  55. menubar->add_menu(move(file_menu));
  56. auto help_menu = GUI::Menu::construct("Help");
  57. help_menu->add_action(GUI::Action::create("About", [](const GUI::Action&) {
  58. dbgprintf("FIXME: Implement Help/About\n");
  59. }));
  60. menubar->add_menu(move(help_menu));
  61. app.set_menubar(move(menubar));
  62. #if 0
  63. if (argc != 2) {
  64. printf("usage: qs <image-file>\n");
  65. return 0;
  66. }
  67. #endif
  68. const char* path = "/res/wallpapers/sunset-retro.png";
  69. if (argc > 1)
  70. path = argv[1];
  71. auto bitmap = Gfx::Bitmap::load_from_file(path);
  72. if (!bitmap) {
  73. fprintf(stderr, "Failed to load %s\n", path);
  74. return 1;
  75. }
  76. auto window = GUI::Window::construct();
  77. auto& widget = window->set_main_widget<QSWidget>();
  78. widget.set_path(path);
  79. widget.set_bitmap(*bitmap);
  80. auto update_window_title = [&](int scale) {
  81. window->set_title(String::format("QuickShow: %s %s %d%%", widget.path().characters(), widget.bitmap()->size().to_string().characters(), scale));
  82. };
  83. window->set_double_buffering_enabled(true);
  84. update_window_title(100);
  85. window->set_rect(200, 200, bitmap->width(), bitmap->height());
  86. widget.on_scale_change = [&](int scale) {
  87. update_window_title(scale);
  88. };
  89. window->show();
  90. bitmap = nullptr;
  91. return app.exec();
  92. }