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