sc/Library_sc.mk                              |    1 
 sc/inc/document.hxx                           |    1 
 sc/inc/globstr.hrc                            |    2 
 sc/qa/unit/SparklineTest.cxx                  |   69 ++++++++++++++++++++++-
 sc/source/core/data/document.cxx              |    5 +
 sc/source/ui/docshell/docfunc.cxx             |   16 +++++
 sc/source/ui/inc/docfunc.hxx                  |    2 
 sc/source/ui/inc/undo/UndoDeleteSparkline.hxx |   43 ++++++++++++++
 sc/source/ui/undo/UndoDeleteSparkline.cxx     |   76 ++++++++++++++++++++++++++
 9 files changed, 212 insertions(+), 3 deletions(-)

New commits:
commit f8a8cb5a752a10dba24adfb2b8387971a9b97003
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Mon Mar 21 17:26:30 2022 +0900
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Tue Apr 12 01:24:39 2022 +0200

    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 <qui...@gmail.com>
    (cherry picked from commit 20ed714f87dd22e5f11a65c4208045037dc20017)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132830
    Tested-by: Tomaž Vajngerl <qui...@gmail.com>

diff --git a/sc/Library_sc.mk b/sc/Library_sc.mk
index 55dafe7fa85c..5e4da178dacf 100644
--- a/sc/Library_sc.mk
+++ b/sc/Library_sc.mk
@@ -549,6 +549,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 a79584f3e7a6..f301105881e2 100644
--- a/sc/inc/document.hxx
+++ b/sc/inc/document.hxx
@@ -1242,6 +1242,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 e305b7c037f9..abb896a33bcf 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 5f4e6c466c07..f46b21df7334 100644
--- a/sc/source/core/data/document.cxx
+++ b/sc/source/core/data/document.cxx
@@ -6568,6 +6568,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 77c76fd6be97..51f79e99ce81 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>
@@ -5842,4 +5843,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 f0b5fc166e58..c701595f8eba 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: */

Reply via email to