Spreadsheet: Show errors and make them red

This commit is contained in:
AnotherTest 2020-08-26 07:52:54 +04:30 committed by Andreas Kling
parent cb7fe4fe7c
commit 8db5057dc4
Notes: sideshowbarker 2024-07-19 03:06:28 +09:00
2 changed files with 36 additions and 2 deletions

View file

@ -25,6 +25,8 @@
*/
#include "SpreadsheetModel.h"
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Object.h>
namespace Spreadsheet {
@ -32,6 +34,16 @@ SheetModel::~SheetModel()
{
}
static inline JS::Object* as_error(JS::Value value)
{
if (value.is_object()) {
auto& object = value.as_object();
return object.is_error() ? &object : nullptr;
}
return nullptr;
}
GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
{
if (!index.is_valid())
@ -42,8 +54,16 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
if (!value)
return String::empty();
if (value->kind == Spreadsheet::Cell::Formula)
if (value->kind == Spreadsheet::Cell::Formula) {
if (auto object = as_error(value->evaluated_data)) {
StringBuilder builder;
auto error = object->get("message").to_string_without_side_effects();
builder.append("Error: ");
builder.append(error);
return builder.to_string();
}
return value->evaluated_data.is_empty() ? "" : value->evaluated_data.to_string_without_side_effects();
}
return value->data;
}
@ -51,6 +71,19 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
if (role == GUI::ModelRole::TextAlignment)
return {};
if (role == GUI::ModelRole::ForegroundColor) {
const auto* value = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() });
if (!value)
return {};
if (value->kind == Spreadsheet::Cell::Formula) {
if (as_error(value->evaluated_data))
return Color(Color::Red);
}
return {};
}
return {};
}

View file

@ -102,9 +102,10 @@ void SpreadsheetView::TableCellPainter::paint(GUI::Painter& painter, const Gfx::
if (m_table_view.selection().contains(index))
painter.draw_rect(rect.inflated(m_table_view.horizontal_padding() * 2 + 1, 1), palette.ruler_border());
auto text_color = index.data(GUI::ModelRole::ForegroundColor).to_color(palette.color(m_table_view.foreground_role()));
auto data = index.data();
auto text_alignment = index.data(GUI::ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
painter.draw_text(rect, data.to_string(), m_table_view.font_for_index(index), text_alignment, palette.color(m_table_view.foreground_role()), Gfx::TextElision::Right);
painter.draw_text(rect, data.to_string(), m_table_view.font_for_index(index), text_alignment, text_color, Gfx::TextElision::Right);
}
}