ValueInspectorModel.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Hex.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/String.h>
  10. #include <AK/Utf8View.h>
  11. #include <AK/Vector.h>
  12. #include <LibGUI/Model.h>
  13. class ValueInspectorModel final : public GUI::Model {
  14. public:
  15. enum ValueType {
  16. SignedByte,
  17. UnsignedByte,
  18. SignedShort,
  19. UnsignedShort,
  20. SignedInt,
  21. UnsignedInt,
  22. SignedLong,
  23. UnsignedLong,
  24. Float,
  25. Double,
  26. ASCII,
  27. __Count
  28. };
  29. enum Column {
  30. Type,
  31. Value
  32. };
  33. explicit ValueInspectorModel(bool is_little_endian)
  34. : m_is_little_endian(is_little_endian)
  35. {
  36. for (int i = 0; i < ValueType::__Count; i++)
  37. set_parsed_value(static_cast<ValueType>(i), "");
  38. }
  39. void set_parsed_value(ValueType type, String value)
  40. {
  41. m_values[type] = value;
  42. }
  43. virtual int row_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override
  44. {
  45. return m_values.size();
  46. }
  47. virtual int column_count(GUI::ModelIndex const&) const override
  48. {
  49. return 2;
  50. }
  51. String column_name(int column) const override
  52. {
  53. switch (column) {
  54. case Column::Type:
  55. return "Type";
  56. case Column::Value:
  57. return m_is_little_endian ? "Value (Little Endian)" : "Value (Big Endian)";
  58. }
  59. VERIFY_NOT_REACHED();
  60. }
  61. String inspector_value_type_to_string(ValueType type) const
  62. {
  63. switch (type) {
  64. case SignedByte:
  65. return "Signed Byte";
  66. case UnsignedByte:
  67. return "Unsigned Byte";
  68. case SignedShort:
  69. return "Signed Short";
  70. case UnsignedShort:
  71. return "Unsigned Short";
  72. case SignedInt:
  73. return "Signed Int";
  74. case UnsignedInt:
  75. return "Unsigned Int";
  76. case SignedLong:
  77. return "Signed Long";
  78. case UnsignedLong:
  79. return "Unsigned Long";
  80. case Float:
  81. return "Float";
  82. case Double:
  83. return "Double";
  84. case ASCII:
  85. return "ASCII";
  86. default:
  87. return "";
  88. }
  89. }
  90. virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
  91. {
  92. if (role == GUI::ModelRole::TextAlignment)
  93. return Gfx::TextAlignment::CenterLeft;
  94. if (role == GUI::ModelRole::Display) {
  95. switch (index.column()) {
  96. case Column::Type:
  97. return inspector_value_type_to_string(static_cast<ValueType>(index.row()));
  98. case Column::Value:
  99. return m_values.at(index.row());
  100. }
  101. }
  102. if (role == GUI::ModelRole::Custom) {
  103. ValueType selected_type = static_cast<ValueType>(index.row());
  104. switch (selected_type) {
  105. case SignedByte:
  106. case UnsignedByte:
  107. case ASCII:
  108. return 1;
  109. case SignedShort:
  110. case UnsignedShort:
  111. return 2;
  112. case SignedInt:
  113. case UnsignedInt:
  114. case Float:
  115. return 4;
  116. case SignedLong:
  117. case UnsignedLong:
  118. case Double:
  119. return 8;
  120. default:
  121. return 0;
  122. }
  123. }
  124. return {};
  125. }
  126. private:
  127. bool m_is_little_endian = false;
  128. Array<String, ValueType::__Count> m_values = {};
  129. };