sc/CppunitTest_sc_sparkline_test.mk | 60 ++++++++++ sc/Module_sc.mk | 1 sc/inc/SparklineGroup.hxx | 32 +++++ sc/qa/unit/SparklineImportExportTest.cxx | 159 +++++++++++++++++++++++++++++ sc/qa/unit/data/xlsx/Sparklines.xlsx |binary sc/source/filter/oox/SparklineFragment.cxx | 44 ++++++-- sc/source/ui/view/output.cxx | 7 - 7 files changed, 290 insertions(+), 13 deletions(-)
New commits: commit 4288e3b35da552c5446a0081f5d1c8f02da6d6ec Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Thu Feb 24 18:04:37 2022 +0900 Commit: Tomaž Vajngerl <qui...@gmail.com> CommitDate: Sun Apr 10 15:32:44 2022 +0200 sc: test Sparkline OOXML Import into Calc Sparkline doc. model Change-Id: I513571de398be419074d54c5861374effae07709 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131490 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <qui...@gmail.com> (cherry picked from commit 860299fe04ec5a2ff0d716186d43d517006069aa) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132551 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> diff --git a/sc/CppunitTest_sc_sparkline_test.mk b/sc/CppunitTest_sc_sparkline_test.mk new file mode 100644 index 000000000000..5dcd9a9921b5 --- /dev/null +++ b/sc/CppunitTest_sc_sparkline_test.mk @@ -0,0 +1,60 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +#************************************************************************* +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +#************************************************************************* + +$(eval $(call gb_CppunitTest_CppunitTest,sc_sparkline_test)) + +$(eval $(call gb_CppunitTest_use_common_precompiled_header,sc_sparkline_test)) + +$(eval $(call gb_CppunitTest_add_exception_objects,sc_sparkline_test, \ + sc/qa/unit/SparklineImportExportTest \ +)) + +$(eval $(call gb_CppunitTest_use_libraries,sc_sparkline_test, \ + basegfx \ + comphelper \ + cppu \ + cppuhelper \ + sal \ + salhelper \ + sax \ + sc \ + scqahelper \ + sfx \ + subsequenttest \ + test \ + tl \ + unotest \ + vcl \ +)) + +$(eval $(call gb_CppunitTest_set_include,sc_sparkline_test,\ + -I$(SRCDIR)/sc/source/ui/inc \ + -I$(SRCDIR)/sc/inc \ + $$(INCLUDE) \ +)) + +$(eval $(call gb_CppunitTest_use_api,sc_sparkline_test,\ + offapi \ + udkapi \ +)) + +$(eval $(call gb_CppunitTest_use_sdk_api,sc_sparkline_test)) + +$(eval $(call gb_CppunitTest_use_ure,sc_sparkline_test)) +$(eval $(call gb_CppunitTest_use_vcl,sc_sparkline_test)) + +$(eval $(call gb_CppunitTest_use_rdb,sc_sparkline_test,services)) + +$(eval $(call gb_CppunitTest_use_components,sc_sparkline_test)) + +$(eval $(call gb_CppunitTest_use_configuration,sc_sparkline_test)) + +# vim: set noet sw=4 ts=4: diff --git a/sc/Module_sc.mk b/sc/Module_sc.mk index 2d51de556655..d42adde3f937 100644 --- a/sc/Module_sc.mk +++ b/sc/Module_sc.mk @@ -207,6 +207,7 @@ $(eval $(call gb_Module_add_subsequentcheck_targets,sc,\ CppunitTest_sc_sheetlinkobj \ CppunitTest_sc_sheetlinksobj \ CppunitTest_sc_sortdescriptorbaseobj \ + CppunitTest_sc_sparkline_test \ CppunitTest_sc_spreadsheetsettings \ CppunitTest_sc_spreadsheetsettingsobj \ CppunitTest_sc_styleobj \ diff --git a/sc/qa/unit/SparklineImportExportTest.cxx b/sc/qa/unit/SparklineImportExportTest.cxx new file mode 100644 index 000000000000..cf970ae8995c --- /dev/null +++ b/sc/qa/unit/SparklineImportExportTest.cxx @@ -0,0 +1,159 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "helper/qahelper.hxx" + +#include <com/sun/star/lang/XComponent.hpp> +#include <docsh.hxx> +#include <Sparkline.hxx> + +using namespace css; + +class SparklineImportExportTest : public ScBootstrapFixture +{ +private: + uno::Reference<uno::XInterface> m_xCalcComponent; + +public: + SparklineImportExportTest() + : ScBootstrapFixture("sc/qa/unit/data") + { + } + + virtual void setUp() override + { + test::BootstrapFixture::setUp(); + + // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure, + // which is a private symbol to us, gets called + m_xCalcComponent = getMultiServiceFactory()->createInstance( + "com.sun.star.comp.Calc.SpreadsheetDocument"); + CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xCalcComponent.is()); + } + + virtual void tearDown() override + { + uno::Reference<lang::XComponent>(m_xCalcComponent, uno::UNO_QUERY_THROW)->dispose(); + test::BootstrapFixture::tearDown(); + } + + void testSparklines(); + + CPPUNIT_TEST_SUITE(SparklineImportExportTest); + CPPUNIT_TEST(testSparklines); + CPPUNIT_TEST_SUITE_END(); +}; + +void SparklineImportExportTest::testSparklines() +{ + ScDocShellRef xDocSh = loadDoc(u"Sparklines.", FORMAT_XLSX); + CPPUNIT_ASSERT(xDocSh); + + ScDocument& rDocument = xDocSh->GetDocument(); + // Sparkline at Sheet1:A2 + { + sc::Sparkline* pSparkline = rDocument.GetSparkline(ScAddress(0, 1, 0)); // A2 + CPPUNIT_ASSERT(pSparkline); + auto pSparklineGroup = pSparkline->getSparklineGroup(); + CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Line, pSparklineGroup->m_eType); + + CPPUNIT_ASSERT_EQUAL(Color(0x376092), pSparklineGroup->m_aColorSeries); + CPPUNIT_ASSERT_EQUAL(Color(0x00b050), pSparklineGroup->m_aColorNegative); + CPPUNIT_ASSERT_EQUAL(Color(0x000000), pSparklineGroup->m_aColorAxis); + CPPUNIT_ASSERT_EQUAL(Color(0x000000), pSparklineGroup->m_aColorMarkers); + CPPUNIT_ASSERT_EQUAL(Color(0x7030a0), pSparklineGroup->m_aColorFirst); + CPPUNIT_ASSERT_EQUAL(Color(0xff0000), pSparklineGroup->m_aColorLast); + CPPUNIT_ASSERT_EQUAL(Color(0x92d050), pSparklineGroup->m_aColorHigh); + CPPUNIT_ASSERT_EQUAL(Color(0x00b0f0), pSparklineGroup->m_aColorLow); + + CPPUNIT_ASSERT_EQUAL(1.0, pSparklineGroup->m_fLineWeight); + CPPUNIT_ASSERT_EQUAL(false, pSparklineGroup->m_bDateAxis); + CPPUNIT_ASSERT_EQUAL(sc::DisplayEmptyCellAs::Gap, pSparklineGroup->m_eDisplayEmptyCellsAs); + + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bMarkers); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bHigh); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bLow); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bFirst); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bLast); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bNegative); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bDisplayXAxis); + CPPUNIT_ASSERT_EQUAL(false, pSparklineGroup->m_bDisplayHidden); + CPPUNIT_ASSERT_EQUAL(false, pSparklineGroup->m_bRightToLeft); + + CPPUNIT_ASSERT_EQUAL(false, bool(pSparklineGroup->m_aManualMax)); + CPPUNIT_ASSERT_EQUAL(false, bool(pSparklineGroup->m_aManualMin)); + } + // Sparkline at Sheet1:A3 + { + sc::Sparkline* pSparkline = rDocument.GetSparkline(ScAddress(0, 2, 0)); // A3 + CPPUNIT_ASSERT(pSparkline); + auto pSparklineGroup = pSparkline->getSparklineGroup(); + CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Column, pSparklineGroup->m_eType); + + CPPUNIT_ASSERT_EQUAL(Color(0x376092), pSparklineGroup->m_aColorSeries); + CPPUNIT_ASSERT_EQUAL(Color(0xff0000), pSparklineGroup->m_aColorNegative); + CPPUNIT_ASSERT_EQUAL(Color(0x000000), pSparklineGroup->m_aColorAxis); + CPPUNIT_ASSERT_EQUAL(Color(0xd00000), pSparklineGroup->m_aColorMarkers); + CPPUNIT_ASSERT_EQUAL(Color(0x92d050), pSparklineGroup->m_aColorFirst); + CPPUNIT_ASSERT_EQUAL(Color(0x00b0f0), pSparklineGroup->m_aColorLast); + CPPUNIT_ASSERT_EQUAL(Color(0x7030a0), pSparklineGroup->m_aColorHigh); + CPPUNIT_ASSERT_EQUAL(Color(0xffc000), pSparklineGroup->m_aColorLow); + + CPPUNIT_ASSERT_EQUAL(0.75, pSparklineGroup->m_fLineWeight); + CPPUNIT_ASSERT_EQUAL(false, pSparklineGroup->m_bDateAxis); + CPPUNIT_ASSERT_EQUAL(sc::DisplayEmptyCellAs::Gap, pSparklineGroup->m_eDisplayEmptyCellsAs); + + CPPUNIT_ASSERT_EQUAL(false, pSparklineGroup->m_bMarkers); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bHigh); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bLow); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bFirst); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bLast); + CPPUNIT_ASSERT_EQUAL(true, pSparklineGroup->m_bNegative); + CPPUNIT_ASSERT_EQUAL(false, pSparklineGroup->m_bDisplayXAxis); + CPPUNIT_ASSERT_EQUAL(false, pSparklineGroup->m_bDisplayHidden); + CPPUNIT_ASSERT_EQUAL(false, pSparklineGroup->m_bRightToLeft); + + CPPUNIT_ASSERT_EQUAL(false, bool(pSparklineGroup->m_aManualMax)); + CPPUNIT_ASSERT_EQUAL(false, bool(pSparklineGroup->m_aManualMin)); + } + // Sparkline at Sheet2:B1 + { + sc::Sparkline* pSparkline = rDocument.GetSparkline(ScAddress(1, 0, 1)); //B1 + CPPUNIT_ASSERT(pSparkline); + auto pSparklineGroup = pSparkline->getSparklineGroup(); + CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Column, pSparklineGroup->m_eType); + } + // Sparkline at Sheet2:B2 + { + sc::Sparkline* pSparkline = rDocument.GetSparkline(ScAddress(1, 1, 1)); //B2 + CPPUNIT_ASSERT(pSparkline); + auto pSparklineGroup = pSparkline->getSparklineGroup(); + CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Line, pSparklineGroup->m_eType); + } + // Sparkline at Sheet2:B2 + { + sc::Sparkline* pSparkline = rDocument.GetSparkline(ScAddress(1, 1, 1)); //B2 + CPPUNIT_ASSERT(pSparkline); + auto pSparklineGroup = pSparkline->getSparklineGroup(); + CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Line, pSparklineGroup->m_eType); + } + // Sparkline doesn't exists at A4 + { + sc::Sparkline* pSparkline = rDocument.GetSparkline(ScAddress(0, 3, 0)); //A4 + CPPUNIT_ASSERT(!pSparkline); + } + + xDocSh->DoClose(); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(SparklineImportExportTest); + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/qa/unit/data/xlsx/Sparklines.xlsx b/sc/qa/unit/data/xlsx/Sparklines.xlsx new file mode 100644 index 000000000000..3725841603be Binary files /dev/null and b/sc/qa/unit/data/xlsx/Sparklines.xlsx differ commit c3e4faa0302de338b7dd07e1c6f4a0c53da59f35 Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Fri Feb 25 11:57:15 2022 +0900 Commit: Tomaž Vajngerl <qui...@gmail.com> CommitDate: Sun Apr 10 15:32:32 2022 +0200 sc: use enums instead strings for some SparklineGroup props. Change-Id: I082c0a7693c97a67bc4972398224bce4bdae85eb Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131489 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <qui...@gmail.com> (cherry picked from commit 055fcef3fe3c3f74ed485a35451c48f6723991b6) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132550 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> diff --git a/sc/inc/SparklineGroup.hxx b/sc/inc/SparklineGroup.hxx index d130084fd121..b06dd3e7a8d5 100644 --- a/sc/inc/SparklineGroup.hxx +++ b/sc/inc/SparklineGroup.hxx @@ -16,6 +16,30 @@ namespace sc { +/** Supported sparkline types */ +enum class SparklineType +{ + Line, + Column, + Stacked +}; + +/** The method of calculating the axis min or max value */ +enum class AxisType +{ + Individual, // calculate the min/max of a sparkline + Group, // calculate the min or max of the whole sparkline group + Custom // user defined +}; + +/** Determines how to display the empty cells */ +enum class DisplayEmptyCellAs +{ + Span, + Gap, + Zero // empty cell equals zero +}; + /** Common properties for a group of sparklines */ class SC_DLLPUBLIC SparklineGroup { @@ -29,16 +53,16 @@ public: Color m_aColorHigh; Color m_aColorLow; - OUString m_sMinAxisType; // individual, group, custom - OUString m_sMaxAxisType; // individual, group, custom + AxisType m_eMinAxisType; + AxisType m_eMaxAxisType; double m_fLineWeight; // In pt - OUString m_sType; // line, column, stacked + SparklineType m_eType; bool m_bDateAxis; - OUString m_sDisplayEmptyCellsAs; // span, gap, zero + DisplayEmptyCellAs m_eDisplayEmptyCellsAs; bool m_bMarkers; bool m_bHigh; diff --git a/sc/source/filter/oox/SparklineFragment.cxx b/sc/source/filter/oox/SparklineFragment.cxx index bfc5a259a883..f590aaee235b 100644 --- a/sc/source/filter/oox/SparklineFragment.cxx +++ b/sc/source/filter/oox/SparklineFragment.cxx @@ -90,6 +90,33 @@ void addColorsToSparklineGroup(sc::SparklineGroup& rSparklineGroup, sal_Int32 nE } } +sc::SparklineType parseSparklineType(std::u16string_view rString) +{ + if (rString == u"column") + return sc::SparklineType::Column; + else if (rString == u"stacked") + return sc::SparklineType::Stacked; + return sc::SparklineType::Line; +} + +sc::DisplayEmptyCellAs parseDisplayEmptyCellAs(std::u16string_view rString) +{ + if (rString == u"span") + return sc::DisplayEmptyCellAs::Span; + else if (rString == u"gap") + return sc::DisplayEmptyCellAs::Gap; + return sc::DisplayEmptyCellAs::Zero; +} + +sc::AxisType parseAxisType(std::u16string_view rString) +{ + if (rString == u"group") + return sc::AxisType::Group; + else if (rString == u"custom") + return sc::AxisType::Custom; + return sc::AxisType::Individual; +} + void addAttributesToSparklineGroup(sc::SparklineGroup& rSparklineGroup, const AttributeList& rAttribs) { @@ -98,11 +125,13 @@ void addAttributesToSparklineGroup(sc::SparklineGroup& rSparklineGroup, rSparklineGroup.m_fLineWeight = rAttribs.getDouble(XML_lineWeight, 0.75); - rSparklineGroup.m_sType = rAttribs.getString(XML_type, "line"); + OUString sType = rAttribs.getString(XML_type, "line"); + rSparklineGroup.m_eType = parseSparklineType(sType); rSparklineGroup.m_bDateAxis = rAttribs.getBool(XML_dateAxis, false); - rSparklineGroup.m_sDisplayEmptyCellsAs = rAttribs.getString(XML_displayEmptyCellsAs, "zero"); + OUString sDisplayEmptyCellsAs = rAttribs.getString(XML_displayEmptyCellsAs, "zero"); + rSparklineGroup.m_eDisplayEmptyCellsAs = parseDisplayEmptyCellAs(sDisplayEmptyCellsAs); rSparklineGroup.m_bMarkers = rAttribs.getBool(XML_markers, false); rSparklineGroup.m_bHigh = rAttribs.getBool(XML_high, false); @@ -113,16 +142,19 @@ void addAttributesToSparklineGroup(sc::SparklineGroup& rSparklineGroup, rSparklineGroup.m_bDisplayXAxis = rAttribs.getBool(XML_displayXAxis, false); rSparklineGroup.m_bDisplayHidden = rAttribs.getBool(XML_displayHidden, false); - rSparklineGroup.m_sMinAxisType = rAttribs.getString(XML_minAxisType, "individual"); - rSparklineGroup.m_sMaxAxisType = rAttribs.getString(XML_maxAxisType, "individual"); + OUString sMinAxisType = rAttribs.getString(XML_minAxisType, "individual"); + rSparklineGroup.m_eMinAxisType = parseAxisType(sMinAxisType); + + OUString sMaxAxisType = rAttribs.getString(XML_maxAxisType, "individual"); + rSparklineGroup.m_eMaxAxisType = parseAxisType(sMaxAxisType); rSparklineGroup.m_bRightToLeft = rAttribs.getBool(XML_rightToLeft, false); rSparklineGroup.m_sUID = rAttribs.getString(XML_uid, OUString()); - if (rSparklineGroup.m_sMaxAxisType == "custom") + if (rSparklineGroup.m_eMaxAxisType == sc::AxisType::Custom) rSparklineGroup.m_aManualMax = oManualMax.get(); - if (rSparklineGroup.m_sMinAxisType == "custom") + if (rSparklineGroup.m_eMinAxisType == sc::AxisType::Custom) rSparklineGroup.m_aManualMin = oManualMin.get(); } diff --git a/sc/source/ui/view/output.cxx b/sc/source/ui/view/output.cxx index 72c14d61ac31..86d37d8d6cc2 100644 --- a/sc/source/ui/view/output.cxx +++ b/sc/source/ui/view/output.cxx @@ -2424,12 +2424,13 @@ void drawSparkline(sc::Sparkline* pSparkline, vcl::RenderContext& rRenderContext } } - if (pSparklineGroup->m_sType == "column") + if (pSparklineGroup->m_eType == sc::SparklineType::Column) { drawColumn(rRenderContext, rRectangle, aValues, nMin, nMax); } - else if (pSparklineGroup->m_sType == "stacked") + else if (pSparklineGroup->m_eType == sc::SparklineType::Stacked) { + // transform the data to 1, -1 for (auto & rValue : aValues) { if (rValue != 0.0) @@ -2437,7 +2438,7 @@ void drawSparkline(sc::Sparkline* pSparkline, vcl::RenderContext& rRenderContext } drawColumn(rRenderContext, rRectangle, aValues, -1, 1); } - else if (pSparklineGroup->m_sType == "line") + else if (pSparklineGroup->m_eType == sc::SparklineType::Line) { drawLine(rRenderContext, rRectangle, aValues, nMin, nMax); }