SpreadsheetModel.cpp 5.7 KB

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