mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:47:34 +00:00
Spreadsheet: Setup and prepare for cell types
This commit adds a generic interface for cell types and hooks it up. There is no way to set these from the UI, and so they're not saved anywhere yet. Also implicitly converts numeric values (strictly integers) to numeric javascript values, as numbery-looking + numbery-looking === string is not very interesting. :^)
This commit is contained in:
parent
5715ed3dd6
commit
6614ee703b
13 changed files with 468 additions and 11 deletions
|
@ -1,6 +1,10 @@
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
Cell.cpp
|
Cell.cpp
|
||||||
CellSyntaxHighlighter.cpp
|
CellSyntaxHighlighter.cpp
|
||||||
|
CellType/Identity.cpp
|
||||||
|
CellType/Numeric.cpp
|
||||||
|
CellType/String.cpp
|
||||||
|
CellType/Type.cpp
|
||||||
HelpWindow.cpp
|
HelpWindow.cpp
|
||||||
JSIntegration.cpp
|
JSIntegration.cpp
|
||||||
Spreadsheet.cpp
|
Spreadsheet.cpp
|
||||||
|
|
|
@ -60,6 +60,45 @@ void Cell::set_data(JS::Value new_data)
|
||||||
evaluated_data = move(new_data);
|
evaluated_data = move(new_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Cell::set_type(const StringView& name)
|
||||||
|
{
|
||||||
|
auto* cell_type = CellType::get_by_name(name);
|
||||||
|
if (cell_type) {
|
||||||
|
m_type = cell_type;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cell::set_type_metadata(CellTypeMetadata&& metadata)
|
||||||
|
{
|
||||||
|
m_type_metadata = move(metadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
const CellType& Cell::type() const
|
||||||
|
{
|
||||||
|
if (m_type)
|
||||||
|
return *m_type;
|
||||||
|
|
||||||
|
if (kind == LiteralString) {
|
||||||
|
if (data.to_int().has_value())
|
||||||
|
return *CellType::get_by_name("Numeric");
|
||||||
|
}
|
||||||
|
|
||||||
|
return *CellType::get_by_name("Identity");
|
||||||
|
}
|
||||||
|
|
||||||
|
String Cell::typed_display() const
|
||||||
|
{
|
||||||
|
return type().display(const_cast<Cell&>(*this), m_type_metadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
JS::Value Cell::typed_js_data() const
|
||||||
|
{
|
||||||
|
return type().js_value(const_cast<Cell&>(*this), m_type_metadata);
|
||||||
|
}
|
||||||
|
|
||||||
void Cell::update_data()
|
void Cell::update_data()
|
||||||
{
|
{
|
||||||
TemporaryChange cell_change { sheet->current_evaluated_cell(), this };
|
TemporaryChange cell_change { sheet->current_evaluated_cell(), this };
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "CellType/Type.h"
|
||||||
#include "Forward.h"
|
#include "Forward.h"
|
||||||
#include "JSIntegration.h"
|
#include "JSIntegration.h"
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
@ -57,6 +58,16 @@ struct Cell : public Weakable<Cell> {
|
||||||
void set_data(String new_data);
|
void set_data(String new_data);
|
||||||
void set_data(JS::Value new_data);
|
void set_data(JS::Value new_data);
|
||||||
|
|
||||||
|
void set_type(const StringView& name);
|
||||||
|
void set_type_metadata(CellTypeMetadata&&);
|
||||||
|
|
||||||
|
String typed_display() const;
|
||||||
|
JS::Value typed_js_data() const;
|
||||||
|
|
||||||
|
const CellType& type() const;
|
||||||
|
const CellTypeMetadata& type_metadata() const { return m_type_metadata; }
|
||||||
|
CellTypeMetadata& type_metadata() { return m_type_metadata; }
|
||||||
|
|
||||||
String source() const;
|
String source() const;
|
||||||
|
|
||||||
JS::Value js_data();
|
JS::Value js_data();
|
||||||
|
@ -76,6 +87,8 @@ struct Cell : public Weakable<Cell> {
|
||||||
Kind kind { LiteralString };
|
Kind kind { LiteralString };
|
||||||
WeakPtr<Sheet> sheet;
|
WeakPtr<Sheet> sheet;
|
||||||
Vector<WeakPtr<Cell>> referencing_cells;
|
Vector<WeakPtr<Cell>> referencing_cells;
|
||||||
|
const CellType* m_type { nullptr };
|
||||||
|
CellTypeMetadata m_type_metadata;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void update_data();
|
void update_data();
|
||||||
|
|
52
Applications/Spreadsheet/CellType/Identity.cpp
Normal file
52
Applications/Spreadsheet/CellType/Identity.cpp
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Identity.h"
|
||||||
|
#include "../Cell.h"
|
||||||
|
#include "../Spreadsheet.h"
|
||||||
|
|
||||||
|
namespace Spreadsheet {
|
||||||
|
|
||||||
|
IdentityCell::IdentityCell()
|
||||||
|
: CellType("Identity")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
IdentityCell::~IdentityCell()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
String IdentityCell::display(Cell& cell, const CellTypeMetadata&) const
|
||||||
|
{
|
||||||
|
return cell.js_data().to_string_without_side_effects();
|
||||||
|
}
|
||||||
|
|
||||||
|
JS::Value IdentityCell::js_value(Cell& cell, const CellTypeMetadata&) const
|
||||||
|
{
|
||||||
|
return cell.js_data();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
42
Applications/Spreadsheet/CellType/Identity.h
Normal file
42
Applications/Spreadsheet/CellType/Identity.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Type.h"
|
||||||
|
|
||||||
|
namespace Spreadsheet {
|
||||||
|
|
||||||
|
class IdentityCell : public CellType {
|
||||||
|
|
||||||
|
public:
|
||||||
|
IdentityCell();
|
||||||
|
virtual ~IdentityCell() override;
|
||||||
|
virtual String display(Cell&, const CellTypeMetadata&) const override;
|
||||||
|
virtual JS::Value js_value(Cell&, const CellTypeMetadata&) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
62
Applications/Spreadsheet/CellType/Numeric.cpp
Normal file
62
Applications/Spreadsheet/CellType/Numeric.cpp
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Numeric.h"
|
||||||
|
#include "../Cell.h"
|
||||||
|
#include "../Spreadsheet.h"
|
||||||
|
|
||||||
|
namespace Spreadsheet {
|
||||||
|
|
||||||
|
NumericCell::NumericCell()
|
||||||
|
: CellType("Numeric")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
NumericCell::~NumericCell()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
String NumericCell::display(Cell& cell, const CellTypeMetadata& metadata) const
|
||||||
|
{
|
||||||
|
auto value = js_value(cell, metadata);
|
||||||
|
String string;
|
||||||
|
if (metadata.format.is_null())
|
||||||
|
string = value.to_string_without_side_effects();
|
||||||
|
else
|
||||||
|
string = String::format(metadata.format.characters(), value.to_double(cell.sheet->interpreter())); // FIXME: Somehow make this format less dependent on 'double'.
|
||||||
|
|
||||||
|
if (metadata.length >= 0)
|
||||||
|
return string.substring(0, metadata.length);
|
||||||
|
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
JS::Value NumericCell::js_value(Cell& cell, const CellTypeMetadata&) const
|
||||||
|
{
|
||||||
|
return cell.js_data().to_number(cell.sheet->interpreter());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
42
Applications/Spreadsheet/CellType/Numeric.h
Normal file
42
Applications/Spreadsheet/CellType/Numeric.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Type.h"
|
||||||
|
|
||||||
|
namespace Spreadsheet {
|
||||||
|
|
||||||
|
class NumericCell : public CellType {
|
||||||
|
|
||||||
|
public:
|
||||||
|
NumericCell();
|
||||||
|
virtual ~NumericCell() override;
|
||||||
|
virtual String display(Cell&, const CellTypeMetadata&) const override;
|
||||||
|
virtual JS::Value js_value(Cell&, const CellTypeMetadata&) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
57
Applications/Spreadsheet/CellType/String.cpp
Normal file
57
Applications/Spreadsheet/CellType/String.cpp
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "String.h"
|
||||||
|
#include "../Cell.h"
|
||||||
|
#include "../Spreadsheet.h"
|
||||||
|
|
||||||
|
namespace Spreadsheet {
|
||||||
|
|
||||||
|
StringCell::StringCell()
|
||||||
|
: CellType("String")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
StringCell::~StringCell()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
String StringCell::display(Cell& cell, const CellTypeMetadata& metadata) const
|
||||||
|
{
|
||||||
|
auto string = cell.js_data().to_string_without_side_effects();
|
||||||
|
if (metadata.length >= 0)
|
||||||
|
return string.substring(0, metadata.length);
|
||||||
|
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
JS::Value StringCell::js_value(Cell& cell, const CellTypeMetadata& metadata) const
|
||||||
|
{
|
||||||
|
auto string = display(cell, metadata);
|
||||||
|
return JS::js_string(cell.sheet->interpreter(), string);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
42
Applications/Spreadsheet/CellType/String.h
Normal file
42
Applications/Spreadsheet/CellType/String.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Type.h"
|
||||||
|
|
||||||
|
namespace Spreadsheet {
|
||||||
|
|
||||||
|
class StringCell : public CellType {
|
||||||
|
|
||||||
|
public:
|
||||||
|
StringCell();
|
||||||
|
virtual ~StringCell() override;
|
||||||
|
virtual String display(Cell&, const CellTypeMetadata&) const override;
|
||||||
|
virtual JS::Value js_value(Cell&, const CellTypeMetadata&) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
52
Applications/Spreadsheet/CellType/Type.cpp
Normal file
52
Applications/Spreadsheet/CellType/Type.cpp
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Type.h"
|
||||||
|
#include "Identity.h"
|
||||||
|
#include "Numeric.h"
|
||||||
|
#include "String.h"
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <AK/OwnPtr.h>
|
||||||
|
|
||||||
|
static HashMap<String, Spreadsheet::CellType*> s_cell_types;
|
||||||
|
static Spreadsheet::StringCell s_string_cell;
|
||||||
|
static Spreadsheet::NumericCell s_numeric_cell;
|
||||||
|
static Spreadsheet::IdentityCell s_identity_cell;
|
||||||
|
|
||||||
|
namespace Spreadsheet {
|
||||||
|
|
||||||
|
const CellType* CellType::get_by_name(const StringView& name)
|
||||||
|
{
|
||||||
|
return s_cell_types.get(name).value_or(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
CellType::CellType(const StringView& name)
|
||||||
|
{
|
||||||
|
ASSERT(!s_cell_types.contains(name));
|
||||||
|
s_cell_types.set(name, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
53
Applications/Spreadsheet/CellType/Type.h
Normal file
53
Applications/Spreadsheet/CellType/Type.h
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../Forward.h"
|
||||||
|
#include <AK/Forward.h>
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <LibJS/Forward.h>
|
||||||
|
|
||||||
|
namespace Spreadsheet {
|
||||||
|
|
||||||
|
struct CellTypeMetadata {
|
||||||
|
int length { -1 };
|
||||||
|
String format;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CellType {
|
||||||
|
public:
|
||||||
|
static const CellType* get_by_name(const StringView&);
|
||||||
|
|
||||||
|
virtual String display(Cell&, const CellTypeMetadata&) const = 0;
|
||||||
|
virtual JS::Value js_value(Cell&, const CellTypeMetadata&) const = 0;
|
||||||
|
virtual ~CellType() { }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
CellType(const StringView& name);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -49,7 +49,7 @@ JS::Value SheetGlobalObject::get(const JS::PropertyName& name, JS::Value receive
|
||||||
if (auto pos = Sheet::parse_cell_name(name.as_string()); pos.has_value()) {
|
if (auto pos = Sheet::parse_cell_name(name.as_string()); pos.has_value()) {
|
||||||
auto& cell = m_sheet.ensure(pos.value());
|
auto& cell = m_sheet.ensure(pos.value());
|
||||||
cell.reference_from(m_sheet.current_evaluated_cell());
|
cell.reference_from(m_sheet.current_evaluated_cell());
|
||||||
return cell.js_data();
|
return cell.typed_js_data();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,34 +50,33 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
if (role == GUI::ModelRole::Display) {
|
if (role == GUI::ModelRole::Display) {
|
||||||
const auto* value = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() });
|
const auto* cell = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() });
|
||||||
if (!value)
|
if (!cell)
|
||||||
return String::empty();
|
return String::empty();
|
||||||
|
|
||||||
if (value->kind == Spreadsheet::Cell::Formula) {
|
if (cell->kind == Spreadsheet::Cell::Formula) {
|
||||||
if (auto object = as_error(value->evaluated_data)) {
|
if (auto object = as_error(cell->evaluated_data)) {
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
auto error = object->get("message").to_string_without_side_effects();
|
auto error = object->get("message").to_string_without_side_effects();
|
||||||
builder.append("Error: ");
|
builder.append("Error: ");
|
||||||
builder.append(error);
|
builder.append(error);
|
||||||
return builder.to_string();
|
return builder.to_string();
|
||||||
}
|
}
|
||||||
return value->evaluated_data.is_empty() ? "" : value->evaluated_data.to_string_without_side_effects();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return value->data;
|
return cell->typed_display();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (role == GUI::ModelRole::TextAlignment)
|
if (role == GUI::ModelRole::TextAlignment)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
if (role == GUI::ModelRole::ForegroundColor) {
|
if (role == GUI::ModelRole::ForegroundColor) {
|
||||||
const auto* value = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() });
|
const auto* cell = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() });
|
||||||
if (!value)
|
if (!cell)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
if (value->kind == Spreadsheet::Cell::Formula) {
|
if (cell->kind == Spreadsheet::Cell::Formula) {
|
||||||
if (as_error(value->evaluated_data))
|
if (as_error(cell->evaluated_data))
|
||||||
return Color(Color::Red);
|
return Color(Color::Red);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue