ソースを参照

Spreadsheet: Show errors and make them red

AnotherTest 4 年 前
コミット
8db5057dc4

+ 34 - 1
Applications/Spreadsheet/SpreadsheetModel.cpp

@@ -25,6 +25,8 @@
  */
  */
 
 
 #include "SpreadsheetModel.h"
 #include "SpreadsheetModel.h"
+#include <LibJS/Runtime/Error.h>
+#include <LibJS/Runtime/Object.h>
 
 
 namespace Spreadsheet {
 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
 GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
 {
 {
     if (!index.is_valid())
     if (!index.is_valid())
@@ -42,8 +54,16 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
         if (!value)
         if (!value)
             return String::empty();
             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->evaluated_data.is_empty() ? "" : value->evaluated_data.to_string_without_side_effects();
+        }
 
 
         return value->data;
         return value->data;
     }
     }
@@ -51,6 +71,19 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
     if (role == GUI::ModelRole::TextAlignment)
     if (role == GUI::ModelRole::TextAlignment)
         return {};
         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 {};
     return {};
 }
 }
 
 

+ 2 - 1
Applications/Spreadsheet/SpreadsheetView.cpp

@@ -102,9 +102,10 @@ void SpreadsheetView::TableCellPainter::paint(GUI::Painter& painter, const Gfx::
     if (m_table_view.selection().contains(index))
     if (m_table_view.selection().contains(index))
         painter.draw_rect(rect.inflated(m_table_view.horizontal_padding() * 2 + 1, 1), palette.ruler_border());
         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 data = index.data();
     auto text_alignment = index.data(GUI::ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
     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);
 }
 }
 
 
 }
 }