diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2022-03-21 17:26:30 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2022-04-04 09:52:29 +0200 |
commit | 20ed714f87dd22e5f11a65c4208045037dc20017 (patch) | |
tree | 425b924c13d638c8e0696bb413924818ec290f2c /sc | |
parent | 54536bca332651051dc8a5ba02995c069cb75fd2 (diff) |
sc: add Undo/Redo for deleting a Sparkline
Adds the code to delete the Sparkline via DocFunc + test.
Change-Id: I710a1ee59a5fe5f2bfb91f8bf487501ef39ce949
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132475
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'sc')
-rw-r--r-- | sc/Library_sc.mk | 1 | ||||
-rw-r--r-- | sc/inc/document.hxx | 1 | ||||
-rw-r--r-- | sc/inc/globstr.hrc | 2 | ||||
-rw-r--r-- | sc/qa/unit/SparklineTest.cxx | 69 | ||||
-rw-r--r-- | sc/source/core/data/document.cxx | 5 | ||||
-rw-r--r-- | sc/source/ui/docshell/docfunc.cxx | 16 | ||||
-rw-r--r-- | sc/source/ui/inc/docfunc.hxx | 2 | ||||
-rw-r--r-- | sc/source/ui/inc/undo/UndoDeleteSparkline.hxx | 43 | ||||
-rw-r--r-- | sc/source/ui/undo/UndoDeleteSparkline.cxx | 76 |
9 files changed, 212 insertions, 3 deletions
diff --git a/sc/Library_sc.mk b/sc/Library_sc.mk index 2a9c6f659a0a..7a7cf817e004 100644 --- a/sc/Library_sc.mk +++ b/sc/Library_sc.mk @@ -551,6 +551,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\ sc/source/ui/undo/undotab \ sc/source/ui/undo/undoutil \ sc/source/ui/undo/UndoInsertSparkline \ + sc/source/ui/undo/UndoDeleteSparkline \ sc/source/ui/unoobj/ChartRangeSelectionListener \ sc/source/ui/unoobj/addruno \ sc/source/ui/unoobj/afmtuno \ diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx index 979f6d6985f1..3ae441f895e3 100644 --- a/sc/inc/document.hxx +++ b/sc/inc/document.hxx @@ -1250,6 +1250,7 @@ public: /** Spaklines */ SC_DLLPUBLIC std::shared_ptr<sc::Sparkline> GetSparkline(ScAddress const & rPosition); + SC_DLLPUBLIC bool HasSparkline(ScAddress const & rPosition); SC_DLLPUBLIC sc::Sparkline* CreateSparkline(ScAddress const & rPosition, std::shared_ptr<sc::SparklineGroup> const& pSparklineGroup); SC_DLLPUBLIC sc::SparklineList* GetSparklineList(SCTAB nTab); SC_DLLPUBLIC bool DeleteSparkline(ScAddress const& rPosition); diff --git a/sc/inc/globstr.hrc b/sc/inc/globstr.hrc index 6ae26c9d4a0f..bed2e10f9b51 100644 --- a/sc/inc/globstr.hrc +++ b/sc/inc/globstr.hrc @@ -540,6 +540,8 @@ #define STR_HYPHENATECELL_OFF NC_("STR_HYPHENATECELL_OFF", "Hyphenate: Off") #define STR_INDENTCELL NC_("STR_INDENTCELL", "Indent: ") #define STR_UNDO_INSERT_SPARKLINE_GROUP NC_("STR_UNDO_INSERT_SPARKLINE", "Insert Sparkline Group") +#define STR_UNDO_DELETE_SPARKLINE NC_("STR_UNDO_DELETE_SPARKLINE", "Delete Sparkline") + #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/qa/unit/SparklineTest.cxx b/sc/qa/unit/SparklineTest.cxx index 7fafeef861ef..6173cea5f297 100644 --- a/sc/qa/unit/SparklineTest.cxx +++ b/sc/qa/unit/SparklineTest.cxx @@ -47,17 +47,19 @@ public: } void testAddSparkline(); - void testDeleteSprkline(); + void testClearContentSprkline(); void testCopyPasteSparkline(); void testCutPasteSparkline(); void testUndoRedoInsertSparkline(); + void testUndoRedoDeleteSparkline(); CPPUNIT_TEST_SUITE(SparklineTest); CPPUNIT_TEST(testAddSparkline); - CPPUNIT_TEST(testDeleteSprkline); + CPPUNIT_TEST(testClearContentSprkline); CPPUNIT_TEST(testCopyPasteSparkline); CPPUNIT_TEST(testCutPasteSparkline); CPPUNIT_TEST(testUndoRedoInsertSparkline); + CPPUNIT_TEST(testUndoRedoDeleteSparkline); CPPUNIT_TEST_SUITE_END(); }; @@ -117,7 +119,7 @@ void SparklineTest::testAddSparkline() xDocSh->DoClose(); } -void SparklineTest::testDeleteSprkline() +void SparklineTest::testClearContentSprkline() { ScDocShellRef xDocSh = loadEmptyDocument(); CPPUNIT_ASSERT(xDocSh); @@ -288,6 +290,67 @@ void SparklineTest::testUndoRedoInsertSparkline() xDocSh->DoClose(); } +void SparklineTest::testUndoRedoDeleteSparkline() +{ + ScDocShellRef xDocSh = loadEmptyDocument(); + CPPUNIT_ASSERT(xDocSh); + + ScDocument& rDocument = xDocSh->GetDocument(); + ScTabViewShell* pViewShell = xDocSh->GetBestViewShell(false); + CPPUNIT_ASSERT(pViewShell); + + auto& rDocFunc = xDocSh->GetDocFunc(); + + // Try to delete sparkline that doesn't exist - returns false + CPPUNIT_ASSERT(!rDocFunc.DeleteSparkline(ScAddress(0, 6, 0))); + + // insert test data - A1:A6 + insertTestData(rDocument); + + // Sparkline range + ScRange aRange(0, 6, 0, 0, 6, 0); + + // Check Sparkline at cell A7 doesn't exists + auto pSparkline = rDocument.GetSparkline(aRange.aStart); + CPPUNIT_ASSERT(!pSparkline); + + auto pSparklineGroup = std::make_shared<sc::SparklineGroup>(); + CPPUNIT_ASSERT(rDocFunc.InsertSparklines(ScRange(0, 0, 0, 0, 5, 0), aRange, pSparklineGroup)); + + // Check Sparkline at cell A7 exists + pSparkline = rDocument.GetSparkline(aRange.aStart); + CPPUNIT_ASSERT(pSparkline); + CPPUNIT_ASSERT_EQUAL(SCCOL(0), pSparkline->getColumn()); + CPPUNIT_ASSERT_EQUAL(SCROW(6), pSparkline->getRow()); + + // Delete Sparkline + CPPUNIT_ASSERT(rDocFunc.DeleteSparkline(ScAddress(0, 6, 0))); + + // Check Sparkline at cell A7 doesn't exists + pSparkline = rDocument.GetSparkline(aRange.aStart); + CPPUNIT_ASSERT(!pSparkline); + + // Undo + rDocument.GetUndoManager()->Undo(); + + // Check Sparkline at cell A7 exists + pSparkline = rDocument.GetSparkline(aRange.aStart); + CPPUNIT_ASSERT(pSparkline); + CPPUNIT_ASSERT_EQUAL(SCCOL(0), pSparkline->getColumn()); + CPPUNIT_ASSERT_EQUAL(SCROW(6), pSparkline->getRow()); + + // Redo + rDocument.GetUndoManager()->Redo(); + + // Check Sparkline at cell A7 doesn't exists + pSparkline = rDocument.GetSparkline(aRange.aStart); + CPPUNIT_ASSERT(!pSparkline); + + CPPUNIT_ASSERT(!rDocument.HasSparkline(aRange.aStart)); + + xDocSh->DoClose(); +} + CPPUNIT_TEST_SUITE_REGISTRATION(SparklineTest); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/core/data/document.cxx b/sc/source/core/data/document.cxx index 11e010afde8f..e45729e5432c 100644 --- a/sc/source/core/data/document.cxx +++ b/sc/source/core/data/document.cxx @@ -6577,6 +6577,11 @@ std::shared_ptr<sc::Sparkline> ScDocument::GetSparkline(ScAddress const& rPositi return std::shared_ptr<sc::Sparkline>(); } +bool ScDocument::HasSparkline(ScAddress const & rPosition) +{ + return bool(GetSparkline(rPosition)); +} + sc::Sparkline* ScDocument::CreateSparkline(ScAddress const& rPosition, std::shared_ptr<sc::SparklineGroup> const& pSparklineGroup) { SCTAB nTab = rPosition.Tab(); diff --git a/sc/source/ui/docshell/docfunc.cxx b/sc/source/ui/docshell/docfunc.cxx index f28ca7e74344..e5972d54b7af 100644 --- a/sc/source/ui/docshell/docfunc.cxx +++ b/sc/source/ui/docshell/docfunc.cxx @@ -97,6 +97,7 @@ #include <SparklineGroup.hxx> #include <SparklineData.hxx> #include <undo/UndoInsertSparkline.hxx> +#include <undo/UndoDeleteSparkline.hxx> #include <config_features.h> #include <memory> @@ -5841,4 +5842,19 @@ bool ScDocFunc::InsertSparklines(ScRange const& rDataRange, ScRange const& rSpar return true; } +bool ScDocFunc::DeleteSparkline(ScAddress const& rAddress) +{ + auto& rDocument = rDocShell.GetDocument(); + + if (!rDocument.HasSparkline(rAddress)) + return false; + + auto pUndoDeleteSparkline = std::make_unique<sc::UndoDeleteSparkline>(rDocShell, rAddress); + // delete sparkline by "redoing" + pUndoDeleteSparkline->Redo(); + rDocShell.GetUndoManager()->AddUndoAction(std::move(pUndoDeleteSparkline)); + + return true; +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/inc/docfunc.hxx b/sc/source/ui/inc/docfunc.hxx index 52d61cb2e86e..9ac438639585 100644 --- a/sc/source/ui/inc/docfunc.hxx +++ b/sc/source/ui/inc/docfunc.hxx @@ -239,6 +239,8 @@ public: SC_DLLPUBLIC bool InsertSparklines(ScRange const& rDataRange, ScRange const& rSparklineRange, std::shared_ptr<sc::SparklineGroup> pSparklineGroup); + SC_DLLPUBLIC bool DeleteSparkline(ScAddress const& rAddress); + private: void ProtectDocument(const ScDocProtection& rProtect); }; diff --git a/sc/source/ui/inc/undo/UndoDeleteSparkline.hxx b/sc/source/ui/inc/undo/UndoDeleteSparkline.hxx new file mode 100644 index 000000000000..97fcd77d9507 --- /dev/null +++ b/sc/source/ui/inc/undo/UndoDeleteSparkline.hxx @@ -0,0 +1,43 @@ +/* -*- 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/. + * + */ + +#pragma once + +#include <undobase.hxx> +#include <address.hxx> +#include <memory> + +namespace sc +{ +class SparklineGroup; +struct SparklineData; + +/** Undo action for deleting a Sparkline */ +class UndoDeleteSparkline : public ScSimpleUndo +{ +private: + std::shared_ptr<sc::SparklineGroup> mpSparklineGroup; + ScAddress maSparklinePosition; + +public: + UndoDeleteSparkline(ScDocShell& rDocShell, ScAddress const& rSparklinePosition); + + virtual ~UndoDeleteSparkline() override; + + void Undo() override; + void Redo() override; + bool CanRepeat(SfxRepeatTarget& rTarget) const override; + void Repeat(SfxRepeatTarget& rTarget) override; + OUString GetComment() const override; +}; + +} // namespace sc + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/undo/UndoDeleteSparkline.cxx b/sc/source/ui/undo/UndoDeleteSparkline.cxx new file mode 100644 index 000000000000..6c9df18090d1 --- /dev/null +++ b/sc/source/ui/undo/UndoDeleteSparkline.cxx @@ -0,0 +1,76 @@ +/* -*- 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 <undo/UndoDeleteSparkline.hxx> +#include <globstr.hrc> +#include <scresid.hxx> + +#include <Sparkline.hxx> +#include <SparklineGroup.hxx> + +namespace sc +{ +UndoDeleteSparkline::UndoDeleteSparkline(ScDocShell& rDocShell, ScAddress const& rSparklinePosition) + : ScSimpleUndo(&rDocShell) + , maSparklinePosition(rSparklinePosition) +{ +} + +UndoDeleteSparkline::~UndoDeleteSparkline() {} + +void UndoDeleteSparkline::Undo() +{ + BeginUndo(); + + ScDocument& rDocument = pDocShell->GetDocument(); + auto pSparkline = rDocument.GetSparkline(maSparklinePosition); + if (!pSparkline) + { + rDocument.CreateSparkline(maSparklinePosition, mpSparklineGroup); + } + else + { + SAL_WARN("sc", "Can't undo deletion if the sparkline at that address already exists."); + } + + pDocShell->PostPaintCell(maSparklinePosition); + + EndUndo(); +} + +void UndoDeleteSparkline::Redo() +{ + BeginRedo(); + + ScDocument& rDocument = pDocShell->GetDocument(); + if (auto pSparkline = rDocument.GetSparkline(maSparklinePosition)) + { + mpSparklineGroup = pSparkline->getSparklineGroup(); + rDocument.DeleteSparkline(maSparklinePosition); + } + else + { + SAL_WARN("sc", "Can't delete a sparkline that donesn't exist."); + } + + pDocShell->PostPaintCell(maSparklinePosition); + + EndRedo(); +} + +void UndoDeleteSparkline::Repeat(SfxRepeatTarget& /*rTarget*/) {} + +bool UndoDeleteSparkline::CanRepeat(SfxRepeatTarget& /*rTarget*/) const { return false; } + +OUString UndoDeleteSparkline::GetComment() const { return ScResId(STR_UNDO_DELETE_SPARKLINE); } + +} // end sc namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |