PDFViewerWidget.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  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().try_open_file(&window);
  38. if (response.is_error())
  39. return;
  40. open_file(*response.value());
  41. }));
  42. file_menu.add_separator();
  43. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  44. GUI::Application::the()->quit();
  45. }));
  46. auto& view_menu = window.add_menu("&View");
  47. view_menu.add_action(*m_toggle_sidebar_action);
  48. view_menu.add_separator();
  49. view_menu.add_action(*m_zoom_in_action);
  50. view_menu.add_action(*m_zoom_out_action);
  51. view_menu.add_action(*m_reset_zoom_action);
  52. auto& help_menu = window.add_menu("&Help");
  53. help_menu.add_action(GUI::CommonActions::make_about_action("PDF Viewer", GUI::Icon::default_icon("app-pdf-viewer"), &window));
  54. }
  55. void PDFViewerWidget::create_toolbar()
  56. {
  57. auto& toolbar_container = add<GUI::ToolbarContainer>();
  58. auto& toolbar = toolbar_container.add<GUI::Toolbar>();
  59. auto open_outline_action = GUI::Action::create(
  60. "Toggle &Sidebar", { Mod_Ctrl, Key_S }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/sidebar.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
  61. m_sidebar_open = !m_sidebar_open;
  62. m_sidebar->set_fixed_width(m_sidebar_open ? 200 : 0);
  63. },
  64. nullptr);
  65. open_outline_action->set_enabled(false);
  66. m_toggle_sidebar_action = open_outline_action;
  67. toolbar.add_action(*open_outline_action);
  68. toolbar.add_separator();
  69. 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").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
  70. VERIFY(m_viewer->current_page() > 0);
  71. m_page_text_box->set_current_number(m_viewer->current_page());
  72. });
  73. m_go_to_prev_page_action->set_enabled(false);
  74. 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").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
  75. VERIFY(m_viewer->current_page() < m_viewer->document()->get_page_count() - 1);
  76. m_page_text_box->set_current_number(m_viewer->current_page() + 2);
  77. });
  78. m_go_to_next_page_action->set_enabled(false);
  79. toolbar.add_action(*m_go_to_prev_page_action);
  80. toolbar.add_action(*m_go_to_next_page_action);
  81. m_page_text_box = toolbar.add<NumericInput>();
  82. m_page_text_box->set_enabled(false);
  83. m_page_text_box->set_fixed_width(30);
  84. m_page_text_box->set_min_number(1);
  85. m_page_text_box->on_number_changed = [&](i32 number) {
  86. auto page_count = m_viewer->document()->get_page_count();
  87. auto new_page_number = static_cast<u32>(number);
  88. VERIFY(new_page_number >= 1 && new_page_number <= page_count);
  89. m_viewer->set_current_page(new_page_number - 1);
  90. m_viewer->update();
  91. m_go_to_prev_page_action->set_enabled(new_page_number > 1);
  92. m_go_to_next_page_action->set_enabled(new_page_number < page_count);
  93. };
  94. m_total_page_label = toolbar.add<GUI::Label>();
  95. m_total_page_label->set_fixed_width(30);
  96. toolbar.add_separator();
  97. m_zoom_in_action = GUI::CommonActions::make_zoom_in_action([&](auto&) {
  98. m_viewer->zoom_in();
  99. });
  100. m_zoom_out_action = GUI::CommonActions::make_zoom_out_action([&](auto&) {
  101. m_viewer->zoom_out();
  102. });
  103. m_reset_zoom_action = GUI::CommonActions::make_reset_zoom_action([&](auto&) {
  104. m_viewer->reset_zoom();
  105. });
  106. m_rotate_counterclockwise_action = GUI::CommonActions::make_rotate_counterclockwise_action([&](auto&) {
  107. m_viewer->rotate(-90);
  108. });
  109. m_rotate_clockwise_action = GUI::CommonActions::make_rotate_clockwise_action([&](auto&) {
  110. m_viewer->rotate(90);
  111. });
  112. m_zoom_in_action->set_enabled(false);
  113. m_zoom_out_action->set_enabled(false);
  114. m_reset_zoom_action->set_enabled(false);
  115. m_rotate_counterclockwise_action->set_enabled(false);
  116. m_rotate_clockwise_action->set_enabled(false);
  117. toolbar.add_action(*m_zoom_in_action);
  118. toolbar.add_action(*m_zoom_out_action);
  119. toolbar.add_action(*m_reset_zoom_action);
  120. toolbar.add_action(*m_rotate_counterclockwise_action);
  121. toolbar.add_action(*m_rotate_clockwise_action);
  122. }
  123. void PDFViewerWidget::open_file(Core::File& file)
  124. {
  125. window()->set_title(String::formatted("{} - PDF Viewer", file.filename()));
  126. m_buffer = file.read_all();
  127. auto document = PDF::Document::create(m_buffer);
  128. if (!document) {
  129. GUI::MessageBox::show_error(nullptr, String::formatted("Couldn't load PDF: {}", file.filename()));
  130. return;
  131. }
  132. m_viewer->set_document(document);
  133. m_total_page_label->set_text(String::formatted("of {}", document->get_page_count()));
  134. m_page_text_box->set_enabled(true);
  135. m_page_text_box->set_current_number(1, false);
  136. m_page_text_box->set_max_number(document->get_page_count());
  137. m_go_to_prev_page_action->set_enabled(false);
  138. m_go_to_next_page_action->set_enabled(document->get_page_count() > 1);
  139. m_toggle_sidebar_action->set_enabled(true);
  140. m_zoom_in_action->set_enabled(true);
  141. m_zoom_out_action->set_enabled(true);
  142. m_reset_zoom_action->set_enabled(true);
  143. m_rotate_counterclockwise_action->set_enabled(true);
  144. m_rotate_clockwise_action->set_enabled(true);
  145. if (document->outline()) {
  146. auto outline = document->outline();
  147. m_sidebar->set_outline(outline.release_nonnull());
  148. m_sidebar->set_fixed_width(200);
  149. m_sidebar_open = true;
  150. } else {
  151. m_sidebar->set_outline({});
  152. m_sidebar->set_fixed_width(0);
  153. m_sidebar_open = false;
  154. }
  155. }