PDFViewerWidget.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "PDFViewerWidget.h"
  8. #include <LibCore/File.h>
  9. #include <LibFileSystemAccessClient/Client.h>
  10. #include <LibGUI/Application.h>
  11. #include <LibGUI/BoxLayout.h>
  12. #include <LibGUI/FilePicker.h>
  13. #include <LibGUI/Label.h>
  14. #include <LibGUI/Menu.h>
  15. #include <LibGUI/Menubar.h>
  16. #include <LibGUI/MessageBox.h>
  17. #include <LibGUI/Splitter.h>
  18. #include <LibGUI/Toolbar.h>
  19. #include <LibGUI/ToolbarContainer.h>
  20. PDFViewerWidget::PDFViewerWidget()
  21. {
  22. set_fill_with_background_color(true);
  23. set_layout<GUI::VerticalBoxLayout>();
  24. create_toolbar();
  25. auto& splitter = add<GUI::HorizontalSplitter>();
  26. m_sidebar = splitter.add<SidebarWidget>();
  27. m_sidebar->set_fixed_width(0);
  28. m_viewer = splitter.add<PDFViewer>();
  29. m_viewer->on_page_change = [&](auto new_page) {
  30. m_page_text_box->set_current_number(new_page + 1);
  31. };
  32. }
  33. void PDFViewerWidget::initialize_menubar(GUI::Window& window)
  34. {
  35. auto& file_menu = window.add_menu("&File");
  36. file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
  37. auto response = FileSystemAccessClient::Client::the().open_file(window.window_id());
  38. if (response.error != 0) {
  39. if (response.error != -1)
  40. GUI::MessageBox::show_error(&window, String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
  41. return;
  42. }
  43. open_file(*response.fd, *response.chosen_file);
  44. }));
  45. file_menu.add_separator();
  46. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  47. GUI::Application::the()->quit();
  48. }));
  49. auto& help_menu = window.add_menu("&Help");
  50. help_menu.add_action(GUI::CommonActions::make_about_action("PDF Viewer", GUI::Icon::default_icon("app-pdf-viewer"), &window));
  51. }
  52. void PDFViewerWidget::create_toolbar()
  53. {
  54. auto& toolbar_container = add<GUI::ToolbarContainer>();
  55. auto& toolbar = toolbar_container.add<GUI::Toolbar>();
  56. auto open_outline_action = GUI::Action::create(
  57. "Open &Sidebar", { Mod_Ctrl, Key_O }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/sidebar.png"), [&](auto& action) {
  58. m_sidebar_open = !m_sidebar_open;
  59. m_sidebar->set_fixed_width(m_sidebar_open ? 0 : 200);
  60. action.set_text(m_sidebar_open ? "Open &Sidebar" : "Close &Sidebar");
  61. },
  62. nullptr);
  63. open_outline_action->set_enabled(false);
  64. m_toggle_sidebar_action = open_outline_action;
  65. toolbar.add_action(*open_outline_action);
  66. toolbar.add_separator();
  67. m_go_to_prev_page_action = GUI::Action::create("Go to &Previous Page", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-up.png"), [&](auto&) {
  68. VERIFY(m_viewer->current_page() > 0);
  69. m_page_text_box->set_current_number(m_viewer->current_page());
  70. });
  71. m_go_to_prev_page_action->set_enabled(false);
  72. m_go_to_next_page_action = GUI::Action::create("Go to &Next Page", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-down.png"), [&](auto&) {
  73. VERIFY(m_viewer->current_page() < m_viewer->document()->get_page_count() - 1);
  74. m_page_text_box->set_current_number(m_viewer->current_page() + 2);
  75. });
  76. m_go_to_next_page_action->set_enabled(false);
  77. toolbar.add_action(*m_go_to_prev_page_action);
  78. toolbar.add_action(*m_go_to_next_page_action);
  79. m_page_text_box = toolbar.add<NumericInput>();
  80. m_page_text_box->set_enabled(false);
  81. m_page_text_box->set_fixed_width(30);
  82. m_page_text_box->set_min_number(1);
  83. m_page_text_box->on_number_changed = [&](i32 number) {
  84. auto page_count = m_viewer->document()->get_page_count();
  85. auto new_page_number = static_cast<u32>(number);
  86. VERIFY(new_page_number >= 1 && new_page_number <= page_count);
  87. m_viewer->set_current_page(new_page_number - 1);
  88. m_viewer->update();
  89. m_go_to_prev_page_action->set_enabled(new_page_number > 1);
  90. m_go_to_next_page_action->set_enabled(new_page_number < page_count);
  91. };
  92. m_total_page_label = toolbar.add<GUI::Label>();
  93. }
  94. void PDFViewerWidget::open_file(int fd, String const& path)
  95. {
  96. auto file = Core::File::construct();
  97. if (!file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes) && file->error() != ENOENT) {
  98. GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  99. return;
  100. }
  101. window()->set_title(String::formatted("{} - PDF Viewer", path));
  102. m_buffer = file->read_all();
  103. auto document = PDF::Document::create(m_buffer);
  104. if (!document) {
  105. GUI::MessageBox::show_error(nullptr, String::formatted("Couldn't load PDF: {}", path));
  106. return;
  107. }
  108. m_viewer->set_document(document);
  109. m_total_page_label->set_text(String::formatted("of {}", document->get_page_count()));
  110. m_total_page_label->set_fixed_width(30);
  111. m_page_text_box->set_enabled(true);
  112. m_page_text_box->set_current_number(1, false);
  113. m_page_text_box->set_max_number(document->get_page_count());
  114. m_go_to_prev_page_action->set_enabled(false);
  115. m_go_to_next_page_action->set_enabled(document->get_page_count() > 1);
  116. m_toggle_sidebar_action->set_enabled(true);
  117. if (document->outline()) {
  118. auto outline = document->outline();
  119. m_sidebar->set_outline(outline.release_nonnull());
  120. m_sidebar->set_fixed_width(200);
  121. m_sidebar_open = true;
  122. } else {
  123. m_sidebar->set_outline({});
  124. m_sidebar->set_fixed_width(0);
  125. m_sidebar_open = false;
  126. }
  127. }