ManualModel.cpp 5.7 KB

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