ManualModel.cpp 4.7 KB

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