SpreadsheetModel.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. namespace Spreadsheet {
  28. SheetModel::~SheetModel()
  29. {
  30. }
  31. GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  32. {
  33. if (!index.is_valid())
  34. return {};
  35. if (role == GUI::ModelRole::Display) {
  36. if (index.column() == 0)
  37. return String::number(index.row());
  38. const auto* value = m_sheet->at({ m_sheet->column(index.column() - 1), (size_t)index.row() });
  39. if (!value)
  40. return String::empty();
  41. if (value->kind == Spreadsheet::Cell::Formula)
  42. return value->evaluated_data.is_empty() ? "" : value->evaluated_data.to_string_without_side_effects();
  43. return value->data;
  44. }
  45. if (role == GUI::ModelRole::TextAlignment) {
  46. if (index.column() == 0)
  47. return {};
  48. return {};
  49. }
  50. return {};
  51. }
  52. String SheetModel::column_name(int index) const
  53. {
  54. if (index < 0)
  55. return {};
  56. if (index == 0)
  57. return "";
  58. return m_sheet->column(index - 1);
  59. }
  60. bool SheetModel::is_editable(const GUI::ModelIndex& index) const
  61. {
  62. if (!index.is_valid())
  63. return false;
  64. if (index.column() == 0)
  65. return false;
  66. return true;
  67. }
  68. void SheetModel::set_data(const GUI::ModelIndex& index, const GUI::Variant& value)
  69. {
  70. if (!index.is_valid())
  71. return;
  72. if (index.column() == 0)
  73. return;
  74. auto& cell = m_sheet->ensure({ m_sheet->column(index.column() - 1), (size_t)index.row() });
  75. cell.set_data(value.to_string());
  76. update();
  77. }
  78. void SheetModel::update()
  79. {
  80. m_sheet->update();
  81. }
  82. }