Serenity Operating System
at hosted 136 lines 4.8 kB view raw
1/* 2 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@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 "ManualModel.h" 28#include "ManualNode.h" 29#include "ManualPageNode.h" 30#include "ManualSectionNode.h" 31 32static ManualSectionNode s_sections[] = { 33 { "1", "Command-line programs" }, 34 { "2", "System calls" }, 35 { "3", "Libraries" }, 36 { "4", "Special files" }, 37 { "5", "File formats" }, 38 { "6", "Games" }, 39 { "7", "Miscellanea" }, 40 { "8", "Sysadmin tools" } 41}; 42 43ManualModel::ManualModel() 44{ 45 // FIXME: need some help from the icon fairy ^) 46 m_section_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png")); 47 m_page_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png")); 48} 49 50String ManualModel::page_path(const GUI::ModelIndex& index) const 51{ 52 if (!index.is_valid()) 53 return {}; 54 auto* node = static_cast<const ManualNode*>(index.internal_data()); 55 if (!node->is_page()) 56 return {}; 57 auto* page = static_cast<const ManualPageNode*>(node); 58 return page->path(); 59} 60 61String ManualModel::page_and_section(const GUI::ModelIndex& index) const 62{ 63 if (!index.is_valid()) 64 return {}; 65 auto* node = static_cast<const ManualNode*>(index.internal_data()); 66 if (!node->is_page()) 67 return {}; 68 auto* page = static_cast<const ManualPageNode*>(node); 69 auto* section = static_cast<const ManualSectionNode*>(page->parent()); 70 return String::format("%s(%s)", page->name().characters(), section->section_name().characters()); 71} 72 73GUI::ModelIndex ManualModel::index(int row, int column, const GUI::ModelIndex& parent_index) const 74{ 75 if (!parent_index.is_valid()) 76 return create_index(row, column, &s_sections[row]); 77 auto* parent = static_cast<const ManualNode*>(parent_index.internal_data()); 78 auto* child = &parent->children()[row]; 79 return create_index(row, column, child); 80} 81 82GUI::ModelIndex ManualModel::parent_index(const GUI::ModelIndex& index) const 83{ 84 if (!index.is_valid()) 85 return {}; 86 auto* child = static_cast<const ManualNode*>(index.internal_data()); 87 auto* parent = child->parent(); 88 if (parent == nullptr) 89 return {}; 90 91 if (parent->parent() == nullptr) { 92 for (size_t row = 0; row < sizeof(s_sections) / sizeof(s_sections[0]); row++) 93 if (&s_sections[row] == parent) 94 return create_index(row, 0, parent); 95 ASSERT_NOT_REACHED(); 96 } 97 for (size_t row = 0; row < parent->parent()->children().size(); row++) { 98 ManualNode* child_at_row = &parent->parent()->children()[row]; 99 if (child_at_row == parent) 100 return create_index(row, 0, parent); 101 } 102 ASSERT_NOT_REACHED(); 103} 104 105int ManualModel::row_count(const GUI::ModelIndex& index) const 106{ 107 if (!index.is_valid()) 108 return sizeof(s_sections) / sizeof(s_sections[0]); 109 auto* node = static_cast<const ManualNode*>(index.internal_data()); 110 return node->children().size(); 111} 112 113int ManualModel::column_count(const GUI::ModelIndex&) const 114{ 115 return 1; 116} 117 118GUI::Variant ManualModel::data(const GUI::ModelIndex& index, Role role) const 119{ 120 auto* node = static_cast<const ManualNode*>(index.internal_data()); 121 switch (role) { 122 case Role::Display: 123 return node->name(); 124 case Role::Icon: 125 if (node->is_page()) 126 return m_page_icon; 127 return m_section_icon; 128 default: 129 return {}; 130 } 131} 132 133void ManualModel::update() 134{ 135 did_update(); 136}