PDFViewerWidget.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "PDFViewerWidget.h"
  7. #include <LibCore/File.h>
  8. #include <LibGUI/Application.h>
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/FilePicker.h>
  11. #include <LibGUI/Label.h>
  12. #include <LibGUI/Menu.h>
  13. #include <LibGUI/Menubar.h>
  14. #include <LibGUI/Splitter.h>
  15. #include <LibGUI/Toolbar.h>
  16. #include <LibGUI/ToolbarContainer.h>
  17. PDFViewerWidget::PDFViewerWidget()
  18. {
  19. set_fill_with_background_color(true);
  20. set_layout<GUI::VerticalBoxLayout>();
  21. create_toolbar();
  22. auto& splitter = add<GUI::HorizontalSplitter>();
  23. m_sidebar = splitter.add<SidebarWidget>();
  24. m_sidebar->set_fixed_width(0);
  25. m_viewer = splitter.add<PDFViewer>();
  26. }
  27. void PDFViewerWidget::initialize_menubar(GUI::Menubar& menubar)
  28. {
  29. auto& file_menu = menubar.add_menu("&File");
  30. file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
  31. Optional<String> open_path = GUI::FilePicker::get_open_filepath(window());
  32. if (open_path.has_value())
  33. open_file(open_path.value());
  34. }));
  35. file_menu.add_separator();
  36. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  37. GUI::Application::the()->quit();
  38. }));
  39. auto& help_menu = menubar.add_menu("&Help");
  40. help_menu.add_action(GUI::CommonActions::make_about_action("PDF Viewer", GUI::Icon::default_icon("app-pdf-viewer"), window()));
  41. }
  42. void PDFViewerWidget::create_toolbar()
  43. {
  44. auto& toolbar_container = add<GUI::ToolbarContainer>();
  45. auto& toolbar = toolbar_container.add<GUI::Toolbar>();
  46. auto open_outline_action = GUI::Action::create(
  47. "Open &Sidebar", { Mod_Ctrl, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/sidebar.png"), [&](auto& action) {
  48. m_sidebar_open = !m_sidebar_open;
  49. m_sidebar->set_fixed_width(m_sidebar_open ? 0 : 200);
  50. action.set_text(m_sidebar_open ? "Open &Sidebar" : "Close &Sidebar");
  51. },
  52. nullptr);
  53. open_outline_action->set_enabled(false);
  54. m_toggle_sidebar_action = open_outline_action;
  55. toolbar.add_action(*open_outline_action);
  56. toolbar.add_separator();
  57. m_go_to_prev_page_action = GUI::Action::create("Go to &Previous Page", Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"), [&](auto&) {
  58. VERIFY(m_viewer->current_page() > 0);
  59. m_page_text_box->set_current_number(m_viewer->current_page());
  60. });
  61. m_go_to_prev_page_action->set_enabled(false);
  62. m_go_to_next_page_action = GUI::Action::create("Go to &Next Page", Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [&](auto&) {
  63. VERIFY(m_viewer->current_page() < m_viewer->document()->get_page_count() - 1);
  64. m_page_text_box->set_current_number(m_viewer->current_page() + 2);
  65. });
  66. m_go_to_next_page_action->set_enabled(false);
  67. toolbar.add_action(*m_go_to_prev_page_action);
  68. toolbar.add_action(*m_go_to_next_page_action);
  69. m_page_text_box = toolbar.add<NumericInput>();
  70. m_page_text_box->set_enabled(false);
  71. m_page_text_box->set_fixed_width(30);
  72. m_page_text_box->set_min_number(1);
  73. m_page_text_box->on_number_changed = [&](i32 number) {
  74. auto page_count = m_viewer->document()->get_page_count();
  75. auto new_page_number = static_cast<u32>(number);
  76. VERIFY(new_page_number >= 1 && new_page_number <= page_count);
  77. m_viewer->set_current_page(new_page_number - 1);
  78. m_viewer->update();
  79. m_go_to_prev_page_action->set_enabled(new_page_number > 1);
  80. m_go_to_next_page_action->set_enabled(new_page_number < page_count);
  81. };
  82. m_total_page_label = toolbar.add<GUI::Label>();
  83. }
  84. void PDFViewerWidget::open_file(const String& path)
  85. {
  86. window()->set_title(String::formatted("{} - PDF Viewer", path));
  87. auto file_result = Core::File::open(path, Core::OpenMode::ReadOnly);
  88. VERIFY(!file_result.is_error());
  89. m_buffer = file_result.value()->read_all();
  90. auto document = adopt_ref(*new PDF::Document(m_buffer));
  91. m_viewer->set_document(document);
  92. m_total_page_label->set_text(String::formatted("of {}", document->get_page_count()));
  93. m_total_page_label->set_fixed_width(30);
  94. m_page_text_box->set_enabled(true);
  95. m_page_text_box->set_current_number(1, false);
  96. m_page_text_box->set_max_number(document->get_page_count());
  97. m_go_to_prev_page_action->set_enabled(false);
  98. m_go_to_next_page_action->set_enabled(document->get_page_count() > 1);
  99. m_toggle_sidebar_action->set_enabled(true);
  100. if (document->outline()) {
  101. auto outline = document->outline();
  102. m_sidebar->set_outline(outline.release_nonnull());
  103. m_sidebar->set_fixed_width(200);
  104. m_sidebar_open = true;
  105. } else {
  106. m_sidebar->set_outline({});
  107. m_sidebar->set_fixed_width(0);
  108. m_sidebar_open = false;
  109. }
  110. }