WidgetTreeModel.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "WidgetTreeModel.h"
  27. #include <AK/StringBuilder.h>
  28. #include <LibGUI/GWidget.h>
  29. #include <stdio.h>
  30. WidgetTreeModel::WidgetTreeModel(GUI::Widget& root)
  31. : m_root(root)
  32. {
  33. m_widget_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  34. }
  35. WidgetTreeModel::~WidgetTreeModel()
  36. {
  37. }
  38. GUI::ModelIndex WidgetTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const
  39. {
  40. if (!parent.is_valid()) {
  41. return create_index(row, column, m_root.ptr());
  42. }
  43. auto& parent_node = *static_cast<GUI::Widget*>(parent.internal_data());
  44. return create_index(row, column, parent_node.child_widgets().at(row));
  45. }
  46. GUI::ModelIndex WidgetTreeModel::parent_index(const GUI::ModelIndex& index) const
  47. {
  48. if (!index.is_valid())
  49. return {};
  50. auto& widget = *static_cast<GUI::Widget*>(index.internal_data());
  51. if (&widget == m_root.ptr())
  52. return {};
  53. if (widget.parent_widget() == m_root.ptr())
  54. return create_index(0, 0, m_root.ptr());
  55. // Walk the grandparent's children to find the index of widget's parent in its parent.
  56. // (This is needed to produce the row number of the GUI::ModelIndex corresponding to widget's parent.)
  57. int grandparent_child_index = 0;
  58. for (auto& grandparent_child : widget.parent_widget()->parent_widget()->child_widgets()) {
  59. if (grandparent_child == widget.parent_widget())
  60. return create_index(grandparent_child_index, 0, widget.parent_widget());
  61. ++grandparent_child_index;
  62. }
  63. ASSERT_NOT_REACHED();
  64. return {};
  65. }
  66. int WidgetTreeModel::row_count(const GUI::ModelIndex& index) const
  67. {
  68. if (!index.is_valid())
  69. return 1;
  70. auto& widget = *static_cast<GUI::Widget*>(index.internal_data());
  71. return widget.child_widgets().size();
  72. }
  73. int WidgetTreeModel::column_count(const GUI::ModelIndex&) const
  74. {
  75. return 1;
  76. }
  77. GUI::Variant WidgetTreeModel::data(const GUI::ModelIndex& index, Role role) const
  78. {
  79. auto* widget = static_cast<GUI::Widget*>(index.internal_data());
  80. if (role == Role::Icon) {
  81. return m_widget_icon;
  82. }
  83. if (role == Role::Display) {
  84. return String::format("%s (%s)", widget->class_name(), widget->relative_rect().to_string().characters());
  85. }
  86. return {};
  87. }
  88. void WidgetTreeModel::update()
  89. {
  90. did_update();
  91. }
  92. GUI::ModelIndex WidgetTreeModel::index_for_widget(GUI::Widget& widget) const
  93. {
  94. int parent_child_index = 0;
  95. for (auto& parent_child : widget.parent_widget()->child_widgets()) {
  96. if (parent_child == &widget)
  97. return create_index(parent_child_index, 0, &widget);
  98. ++parent_child_index;
  99. }
  100. return {};
  101. }