2020-08-23 00:55:16 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-23 00:55:16 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "SpreadsheetModel.h"
|
2020-09-25 16:01:39 -03:00
|
|
|
#include "ConditionalFormatting.h"
|
2020-11-02 13:51:07 -03:00
|
|
|
#include <AK/URL.h>
|
2020-11-07 16:48:41 -03:00
|
|
|
#include <LibGUI/AbstractView.h>
|
2020-08-26 00:22:54 -03:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
|
#include <LibJS/Runtime/Object.h>
|
2020-08-23 00:55:16 -03:00
|
|
|
|
|
|
|
|
namespace Spreadsheet {
|
|
|
|
|
|
|
|
|
|
SheetModel::~SheetModel()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
|
|
|
|
|
{
|
|
|
|
|
if (!index.is_valid())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
if (role == GUI::ModelRole::Display) {
|
2021-02-23 10:36:07 -03:00
|
|
|
const auto* cell = m_sheet->at({ (size_t)index.column(), (size_t)index.row() });
|
2020-08-28 10:25:06 -03:00
|
|
|
if (!cell)
|
2020-08-23 00:55:16 -03:00
|
|
|
return String::empty();
|
|
|
|
|
|
2020-12-20 06:28:14 -03:00
|
|
|
if (cell->kind() == Spreadsheet::Cell::Formula) {
|
2020-12-22 07:48:33 -03:00
|
|
|
if (auto exception = cell->exception()) {
|
2020-08-26 00:22:54 -03:00
|
|
|
StringBuilder builder;
|
|
|
|
|
builder.append("Error: ");
|
2020-12-22 07:48:33 -03:00
|
|
|
auto value = exception->value();
|
|
|
|
|
if (value.is_object()) {
|
|
|
|
|
auto& object = value.as_object();
|
2021-01-01 13:46:39 -03:00
|
|
|
if (is<JS::Error>(object)) {
|
2020-12-22 07:48:33 -03:00
|
|
|
auto error = object.get("message").to_string_without_side_effects();
|
|
|
|
|
builder.append(error);
|
|
|
|
|
return builder.to_string();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
auto error = value.to_string(cell->sheet().global_object());
|
|
|
|
|
// This is annoying, but whatever.
|
|
|
|
|
cell->sheet().interpreter().vm().clear_exception();
|
|
|
|
|
|
2020-08-26 00:22:54 -03:00
|
|
|
builder.append(error);
|
|
|
|
|
return builder.to_string();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-23 00:55:16 -03:00
|
|
|
|
2020-08-28 10:25:06 -03:00
|
|
|
return cell->typed_display();
|
2020-08-23 00:55:16 -03:00
|
|
|
}
|
|
|
|
|
|
2020-11-07 16:48:41 -03:00
|
|
|
if (role == GUI::ModelRole::MimeData)
|
2021-02-23 10:36:07 -03:00
|
|
|
return Position { (size_t)index.column(), (size_t)index.row() }.to_url(m_sheet).to_string();
|
2020-11-02 13:51:07 -03:00
|
|
|
|
2020-08-29 03:51:18 -03:00
|
|
|
if (role == GUI::ModelRole::TextAlignment) {
|
2021-02-23 10:36:07 -03:00
|
|
|
const auto* cell = m_sheet->at({ (size_t)index.column(), (size_t)index.row() });
|
2020-08-29 03:51:18 -03:00
|
|
|
if (!cell)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return cell->type_metadata().alignment;
|
|
|
|
|
}
|
2020-08-23 00:55:16 -03:00
|
|
|
|
2020-08-26 00:22:54 -03:00
|
|
|
if (role == GUI::ModelRole::ForegroundColor) {
|
2021-02-23 10:36:07 -03:00
|
|
|
const auto* cell = m_sheet->at({ (size_t)index.column(), (size_t)index.row() });
|
2020-08-28 10:25:06 -03:00
|
|
|
if (!cell)
|
2020-08-26 00:22:54 -03:00
|
|
|
return {};
|
|
|
|
|
|
2020-12-20 06:28:14 -03:00
|
|
|
if (cell->kind() == Spreadsheet::Cell::Formula) {
|
2020-12-22 07:48:33 -03:00
|
|
|
if (cell->exception())
|
2020-08-26 00:22:54 -03:00
|
|
|
return Color(Color::Red);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-25 16:01:39 -03:00
|
|
|
if (cell->evaluated_formats().foreground_color.has_value())
|
|
|
|
|
return cell->evaluated_formats().foreground_color.value();
|
|
|
|
|
|
|
|
|
|
if (auto color = cell->type_metadata().static_format.foreground_color; color.has_value())
|
2020-09-12 07:59:58 -03:00
|
|
|
return color.value();
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (role == GUI::ModelRole::BackgroundColor) {
|
2021-02-23 10:36:07 -03:00
|
|
|
const auto* cell = m_sheet->at({ (size_t)index.column(), (size_t)index.row() });
|
2020-09-12 07:59:58 -03:00
|
|
|
if (!cell)
|
|
|
|
|
return {};
|
|
|
|
|
|
2020-09-25 16:01:39 -03:00
|
|
|
if (cell->evaluated_formats().background_color.has_value())
|
|
|
|
|
return cell->evaluated_formats().background_color.value();
|
|
|
|
|
|
|
|
|
|
if (auto color = cell->type_metadata().static_format.background_color; color.has_value())
|
2020-09-12 07:59:58 -03:00
|
|
|
return color.value();
|
|
|
|
|
|
2020-08-26 00:22:54 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 00:55:16 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 16:48:41 -03:00
|
|
|
RefPtr<Core::MimeData> SheetModel::mime_data(const GUI::ModelSelection& selection) const
|
|
|
|
|
{
|
|
|
|
|
auto mime_data = GUI::Model::mime_data(selection);
|
|
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
const GUI::ModelIndex* cursor = nullptr;
|
|
|
|
|
const_cast<SheetModel*>(this)->for_each_view([&](const GUI::AbstractView& view) {
|
|
|
|
|
if (!first)
|
|
|
|
|
return;
|
|
|
|
|
cursor = &view.cursor_index();
|
|
|
|
|
first = false;
|
|
|
|
|
});
|
|
|
|
|
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(cursor);
|
2020-11-07 16:48:41 -03:00
|
|
|
|
2021-02-23 10:36:07 -03:00
|
|
|
Position cursor_position { (size_t)cursor->column(), (size_t)cursor->row() };
|
2021-09-10 18:42:39 -03:00
|
|
|
auto mime_data_buffer = mime_data->data("text/x-spreadsheet-data");
|
2020-11-07 16:48:41 -03:00
|
|
|
auto new_data = String::formatted("{}\n{}",
|
2021-02-23 10:36:07 -03:00
|
|
|
cursor_position.to_url(m_sheet).to_string(),
|
2021-09-10 18:42:39 -03:00
|
|
|
StringView(mime_data_buffer));
|
2020-11-07 16:48:41 -03:00
|
|
|
mime_data->set_data("text/x-spreadsheet-data", new_data.to_byte_buffer());
|
|
|
|
|
|
|
|
|
|
return mime_data;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 00:55:16 -03:00
|
|
|
String SheetModel::column_name(int index) const
|
|
|
|
|
{
|
|
|
|
|
if (index < 0)
|
|
|
|
|
return {};
|
|
|
|
|
|
2020-08-26 11:03:45 -03:00
|
|
|
return m_sheet->column(index);
|
2020-08-23 00:55:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SheetModel::is_editable(const GUI::ModelIndex& index) const
|
|
|
|
|
{
|
|
|
|
|
if (!index.is_valid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SheetModel::set_data(const GUI::ModelIndex& index, const GUI::Variant& value)
|
|
|
|
|
{
|
|
|
|
|
if (!index.is_valid())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-02-23 10:36:07 -03:00
|
|
|
auto& cell = m_sheet->ensure({ (size_t)index.column(), (size_t)index.row() });
|
2020-08-23 00:55:16 -03:00
|
|
|
cell.set_data(value.to_string());
|
2021-08-30 12:58:52 -03:00
|
|
|
did_update(UpdateFlag::DontInvalidateIndices);
|
2020-08-23 00:55:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SheetModel::update()
|
|
|
|
|
{
|
|
|
|
|
m_sheet->update();
|
2021-04-29 17:23:52 -03:00
|
|
|
did_update(UpdateFlag::DontInvalidateIndices);
|
2020-08-23 00:55:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|