summaryrefslogtreecommitdiff
path: root/Applications
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-08-26 07:52:54 +0430
committerAndreas Kling <kling@serenityos.org>2020-08-27 10:27:20 +0200
commit8db5057dc4b3c9bf8a5b07845147d24bf13f28e9 (patch)
treed3016a3c2977c84791615a424ebecc4aea0bfb51 /Applications
parentcb7fe4fe7c50dc3c9da8f129aa60c74e93ef1304 (diff)
downloadserenity-8db5057dc4b3c9bf8a5b07845147d24bf13f28e9.zip
Spreadsheet: Show errors and make them red
Diffstat (limited to 'Applications')
-rw-r--r--Applications/Spreadsheet/SpreadsheetModel.cpp35
-rw-r--r--Applications/Spreadsheet/SpreadsheetView.cpp3
2 files changed, 36 insertions, 2 deletions
diff --git a/Applications/Spreadsheet/SpreadsheetModel.cpp b/Applications/Spreadsheet/SpreadsheetModel.cpp
index b6addb0c27..2b92619784 100644
--- a/Applications/Spreadsheet/SpreadsheetModel.cpp
+++ b/Applications/Spreadsheet/SpreadsheetModel.cpp
@@ -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 {};
}
diff --git a/Applications/Spreadsheet/SpreadsheetView.cpp b/Applications/Spreadsheet/SpreadsheetView.cpp
index a8688ab8fc..dbfee44263 100644
--- a/Applications/Spreadsheet/SpreadsheetView.cpp
+++ b/Applications/Spreadsheet/SpreadsheetView.cpp
@@ -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);
}
}