ManualModel.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // FIXME: need some help from the icon fairy ^)
  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. String ManualModel::page_path(const GUI::ModelIndex& index) const
  47. {
  48. if (!index.is_valid())
  49. return {};
  50. auto* node = static_cast<const ManualNode*>(index.internal_data());
  51. if (!node->is_page())
  52. return {};
  53. auto* page = static_cast<const ManualPageNode*>(node);
  54. return page->path();
  55. }
  56. String ManualModel::page_and_section(const GUI::ModelIndex& index) const
  57. {
  58. if (!index.is_valid())
  59. return {};
  60. auto* node = static_cast<const ManualNode*>(index.internal_data());
  61. if (!node->is_page())
  62. return {};
  63. auto* page = static_cast<const ManualPageNode*>(node);
  64. auto* section = static_cast<const ManualSectionNode*>(page->parent());
  65. return String::format("%s(%s)", page->name().characters(), section->section_name().characters());
  66. }
  67. GUI::ModelIndex ManualModel::index(int row, int column, const GUI::ModelIndex& parent_index) const
  68. {
  69. if (!parent_index.is_valid())
  70. return create_index(row, column, &s_sections[row]);
  71. auto* parent = static_cast<const ManualNode*>(parent_index.internal_data());
  72. auto* child = &parent->children()[row];
  73. return create_index(row, column, child);
  74. }
  75. GUI::ModelIndex ManualModel::parent_index(const GUI::ModelIndex& index) const
  76. {
  77. if (!index.is_valid())
  78. return {};
  79. auto* child = static_cast<const ManualNode*>(index.internal_data());
  80. auto* parent = child->parent();
  81. if (parent == nullptr)
  82. return {};
  83. if (parent->parent() == nullptr) {
  84. for (size_t row = 0; row < sizeof(s_sections) / sizeof(s_sections[0]); row++)
  85. if (&s_sections[row] == parent)
  86. return create_index(row, 0, parent);
  87. ASSERT_NOT_REACHED();
  88. }
  89. for (size_t row = 0; row < parent->parent()->children().size(); row++) {
  90. ManualNode* child_at_row = &parent->parent()->children()[row];
  91. if (child_at_row == parent)
  92. return create_index(row, 0, parent);
  93. }
  94. ASSERT_NOT_REACHED();
  95. }
  96. int ManualModel::row_count(const GUI::ModelIndex& index) const
  97. {
  98. if (!index.is_valid())
  99. return sizeof(s_sections) / sizeof(s_sections[0]);
  100. auto* node = static_cast<const ManualNode*>(index.internal_data());
  101. return node->children().size();
  102. }
  103. int ManualModel::column_count(const GUI::ModelIndex&) const
  104. {
  105. return 1;
  106. }
  107. GUI::Variant ManualModel::data(const GUI::ModelIndex& index, Role role) const
  108. {
  109. auto* node = static_cast<const ManualNode*>(index.internal_data());
  110. switch (role) {
  111. case Role::Display:
  112. return node->name();
  113. case Role::Icon:
  114. if (node->is_page())
  115. return m_page_icon;
  116. return m_section_icon;
  117. default:
  118. return {};
  119. }
  120. }
  121. void ManualModel::update()
  122. {
  123. did_update();
  124. }