InspectorWidget.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "InspectorWidget.h"
  27. #include <LibGUI/BoxLayout.h>
  28. #include <LibGUI/Splitter.h>
  29. #include <LibGUI/TabWidget.h>
  30. #include <LibGUI/TableView.h>
  31. #include <LibGUI/TreeView.h>
  32. #include <LibWeb/DOM/Document.h>
  33. #include <LibWeb/DOM/Element.h>
  34. #include <LibWeb/DOMTreeModel.h>
  35. #include <LibWeb/LayoutTreeModel.h>
  36. #include <LibWeb/StylePropertiesModel.h>
  37. namespace Browser {
  38. void InspectorWidget::set_inspected_node(Web::DOM::Node* node)
  39. {
  40. m_document->set_inspected_node(node);
  41. if (node && node->is_element()) {
  42. auto& element = downcast<Web::DOM::Element>(*node);
  43. if (element.resolved_style()) {
  44. m_style_table_view->set_model(Web::StylePropertiesModel::create(*element.resolved_style()));
  45. m_computed_style_table_view->set_model(Web::StylePropertiesModel::create(*element.computed_style()));
  46. }
  47. } else {
  48. m_style_table_view->set_model(nullptr);
  49. m_computed_style_table_view->set_model(nullptr);
  50. }
  51. }
  52. InspectorWidget::InspectorWidget()
  53. {
  54. set_layout<GUI::VerticalBoxLayout>();
  55. auto& splitter = add<GUI::VerticalSplitter>();
  56. auto& top_tab_widget = splitter.add<GUI::TabWidget>();
  57. m_dom_tree_view = top_tab_widget.add_tab<GUI::TreeView>("DOM");
  58. m_dom_tree_view->on_selection = [this](auto& index) {
  59. auto* node = static_cast<Web::DOM::Node*>(index.internal_data());
  60. set_inspected_node(node);
  61. };
  62. m_layout_tree_view = top_tab_widget.add_tab<GUI::TreeView>("Layout");
  63. m_layout_tree_view->on_selection = [this](auto& index) {
  64. auto* node = static_cast<Web::LayoutNode*>(index.internal_data());
  65. set_inspected_node(node->node());
  66. };
  67. auto& bottom_tab_widget = splitter.add<GUI::TabWidget>();
  68. m_style_table_view = bottom_tab_widget.add_tab<GUI::TableView>("Styles");
  69. m_computed_style_table_view = bottom_tab_widget.add_tab<GUI::TableView>("Computed");
  70. }
  71. InspectorWidget::~InspectorWidget()
  72. {
  73. }
  74. void InspectorWidget::set_document(Web::DOM::Document* document)
  75. {
  76. if (m_document == document)
  77. return;
  78. m_document = document;
  79. m_dom_tree_view->set_model(Web::DOMTreeModel::create(*document));
  80. m_layout_tree_view->set_model(Web::LayoutTreeModel::create(*document));
  81. }
  82. }