SpreadsheetModel.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SpreadsheetModel.h"
  7. #include "ConditionalFormatting.h"
  8. #include <AK/URL.h>
  9. #include <LibGUI/AbstractView.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/Object.h>
  12. namespace Spreadsheet {
  13. SheetModel::~SheetModel()
  14. {
  15. }
  16. GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  17. {
  18. if (!index.is_valid())
  19. return {};
  20. if (role == GUI::ModelRole::Display) {
  21. const auto* cell = m_sheet->at({ (size_t)index.column(), (size_t)index.row() });
  22. if (!cell)
  23. return String::empty();
  24. if (cell->kind() == Spreadsheet::Cell::Formula) {
  25. if (auto exception = cell->exception()) {
  26. StringBuilder builder;
  27. builder.append("Error: ");
  28. auto value = exception->value();
  29. if (value.is_object()) {
  30. auto& object = value.as_object();
  31. if (is<JS::Error>(object)) {
  32. auto error = object.get("message").to_string_without_side_effects();
  33. builder.append(error);
  34. return builder.to_string();
  35. }
  36. }
  37. auto error = value.to_string(cell->sheet().global_object());
  38. // This is annoying, but whatever.
  39. cell->sheet().interpreter().vm().clear_exception();
  40. builder.append(error);
  41. return builder.to_string();
  42. }
  43. }
  44. return cell->typed_display();
  45. }
  46. if (role == GUI::ModelRole::MimeData)
  47. return Position { (size_t)index.column(), (size_t)index.row() }.to_url(m_sheet).to_string();
  48. if (role == GUI::ModelRole::TextAlignment) {
  49. const auto* cell = m_sheet->at({ (size_t)index.column(), (size_t)index.row() });
  50. if (!cell)
  51. return {};
  52. return cell->type_metadata().alignment;
  53. }
  54. if (role == GUI::ModelRole::ForegroundColor) {
  55. const auto* cell = m_sheet->at({ (size_t)index.column(), (size_t)index.row() });
  56. if (!cell)
  57. return {};
  58. if (cell->kind() == Spreadsheet::Cell::Formula) {
  59. if (cell->exception())
  60. return Color(Color::Red);
  61. }
  62. if (cell->evaluated_formats().foreground_color.has_value())
  63. return cell->evaluated_formats().foreground_color.value();
  64. if (auto color = cell->type_metadata().static_format.foreground_color; color.has_value())
  65. return color.value();
  66. return {};
  67. }
  68. if (role == GUI::ModelRole::BackgroundColor) {
  69. const auto* cell = m_sheet->at({ (size_t)index.column(), (size_t)index.row() });
  70. if (!cell)
  71. return {};
  72. if (cell->evaluated_formats().background_color.has_value())
  73. return cell->evaluated_formats().background_color.value();
  74. if (auto color = cell->type_metadata().static_format.background_color; color.has_value())
  75. return color.value();
  76. return {};
  77. }
  78. return {};
  79. }
  80. RefPtr<Core::MimeData> SheetModel::mime_data(const GUI::ModelSelection& selection) const
  81. {
  82. auto mime_data = GUI::Model::mime_data(selection);
  83. bool first = true;
  84. const GUI::ModelIndex* cursor = nullptr;
  85. const_cast<SheetModel*>(this)->for_each_view([&](const GUI::AbstractView& view) {
  86. if (!first)
  87. return;
  88. cursor = &view.cursor_index();
  89. first = false;
  90. });
  91. VERIFY(cursor);
  92. Position cursor_position { (size_t)cursor->column(), (size_t)cursor->row() };
  93. auto new_data = String::formatted("{}\n{}",
  94. cursor_position.to_url(m_sheet).to_string(),
  95. StringView(mime_data->data("text/x-spreadsheet-data")));
  96. mime_data->set_data("text/x-spreadsheet-data", new_data.to_byte_buffer());
  97. return mime_data;
  98. }
  99. String SheetModel::column_name(int index) const
  100. {
  101. if (index < 0)
  102. return {};
  103. return m_sheet->column(index);
  104. }
  105. bool SheetModel::is_editable(const GUI::ModelIndex& index) const
  106. {
  107. if (!index.is_valid())
  108. return false;
  109. return true;
  110. }
  111. void SheetModel::set_data(const GUI::ModelIndex& index, const GUI::Variant& value)
  112. {
  113. if (!index.is_valid())
  114. return;
  115. auto& cell = m_sheet->ensure({ (size_t)index.column(), (size_t)index.row() });
  116. cell.set_data(value.to_string());
  117. invalidate();
  118. }
  119. void SheetModel::update()
  120. {
  121. m_sheet->update();
  122. did_update(UpdateFlag::DontInvalidateIndices);
  123. }
  124. }