sw/Library_sw.mk                           |    1 
 sw/inc/strings.hrc                         |    2 
 sw/inc/swundo.hxx                          |    1 
 sw/qa/core/theme/ThemeTest.cxx             |   78 +++++++++++++++++++++++++++++
 sw/source/core/inc/ThemeColorChanger.hxx   |    3 -
 sw/source/core/inc/UndoThemeChange.hxx     |   35 +++++++++++++
 sw/source/core/model/ThemeColorChanger.cxx |   17 ++++--
 sw/source/core/undo/UndoThemeChange.cxx    |   48 +++++++++++++++++
 sw/source/core/undo/undobj.cxx             |    3 +
 9 files changed, 181 insertions(+), 7 deletions(-)

New commits:
commit 33d35a2181c9cebf77bc1aa9322a2d11892de5fd
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Thu May 4 00:21:55 2023 +0900
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Tue May 9 12:41:37 2023 +0200

    sw: support Undo/Redo for theme colors
    
    Change-Id: Ic4166ec4836545467866a70b4160f1adba0bad96
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151448
    Tested-by: Tomaž Vajngerl <qui...@gmail.com>
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>

diff --git a/sw/Library_sw.mk b/sw/Library_sw.mk
index bc0067dcb8a8..f49117e58f86 100644
--- a/sw/Library_sw.mk
+++ b/sw/Library_sw.mk
@@ -483,6 +483,7 @@ $(eval $(call gb_Library_add_exception_objects,sw,\
     sw/source/core/undo/unspnd \
     sw/source/core/undo/untbl \
     sw/source/core/undo/untblk \
+    sw/source/core/undo/UndoThemeChange \
     sw/source/core/unocore/SwXTextDefaults \
     sw/source/core/unocore/TextCursorHelper  \
     sw/source/core/unocore/XMLRangeHelper \
diff --git a/sw/inc/strings.hrc b/sw/inc/strings.hrc
index 2c7eb4069688..733193103d1e 100644
--- a/sw/inc/strings.hrc
+++ b/sw/inc/strings.hrc
@@ -599,6 +599,8 @@
 #define STR_UNDO_UPDATE_FORM_FIELDS             
NC_("STR_UNDO_UPDATE_FORM_FIELDS", "Update form fields")
 #define STR_UNDO_DELETE_FORM_FIELDS             
NC_("STR_UNDO_DELETE_FORM_FIELDS", "Delete form fields")
 #define STR_UNDO_INSERT_PAGE_NUMBER             
NC_("STR_UNDO_INSERT_PAGE_NUMBER", "Insert page number")
+#define STR_UNDO_CHANGE_THEME_COLORS            
NC_("STR_UNDO_CHANGE_THEME_COLORS", "Change document theme color")
+
 #define STR_DROP_DOWN_FIELD_ITEM_LIMIT          
NC_("STR_DROP_DOWN_FIELD_ITEM_LIMIT", "You can specify maximum of 25 items for 
a drop-down form field.")
 
 #define STR_ACCESS_DOC_NAME                     NC_("STR_ACCESS_DOC_NAME", 
"Document view")
diff --git a/sw/inc/swundo.hxx b/sw/inc/swundo.hxx
index 5f0e006114f8..b8bf714aa580 100644
--- a/sw/inc/swundo.hxx
+++ b/sw/inc/swundo.hxx
@@ -177,6 +177,7 @@ enum class SwUndoId
     UPDATE_FIELDS,                     // 145
     DELETE_FIELDS,                     // 146
     UPDATE_SECTIONS,                   // 147
+    CHANGE_THEME = 148,
 };
 
 OUString GetUndoComment(SwUndoId eId);
diff --git a/sw/qa/core/theme/ThemeTest.cxx b/sw/qa/core/theme/ThemeTest.cxx
index 0802994aead6..f76460eb6b98 100644
--- a/sw/qa/core/theme/ThemeTest.cxx
+++ b/sw/qa/core/theme/ThemeTest.cxx
@@ -18,6 +18,8 @@
 #include <svx/svdpage.hxx>
 #include <docmodel/uno/UnoThemeColor.hxx>
 #include <docmodel/theme/Theme.hxx>
+#include <ThemeColorChanger.hxx>
+#include <svx/ColorSets.hxx>
 
 using namespace css;
 
@@ -392,6 +394,82 @@ CPPUNIT_TEST_FIXTURE(SwCoreThemeTest, 
testDrawPageThemeExistsODT)
     CPPUNIT_ASSERT_EQUAL(Color(0xCCDDEA), 
pTheme->GetColor(model::ThemeColorType::Light2));
 }
 
+CPPUNIT_TEST_FIXTURE(SwCoreThemeTest, testThemeChanging)
+{
+    createSwDoc("ThemeColorInHeading.docx");
+    SwDoc* pDoc = getSwDoc();
+    CPPUNIT_ASSERT(pDoc);
+    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
+    CPPUNIT_ASSERT(pWrtShell);
+    SdrPage* pPage = 
pDoc->getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
+    CPPUNIT_ASSERT(pPage);
+
+    // Check current theme colors
+    {
+        auto const& pTheme = pPage->getSdrPageProperties().GetTheme();
+        CPPUNIT_ASSERT(pTheme);
+        CPPUNIT_ASSERT_EQUAL(OUString(u"Office Theme"), pTheme->GetName());
+
+        auto pColorSet = pTheme->getColorSet();
+        CPPUNIT_ASSERT(pColorSet);
+        CPPUNIT_ASSERT_EQUAL(OUString(u"Orange"), pColorSet->getName());
+        CPPUNIT_ASSERT_EQUAL(Color(0xE48312), 
pTheme->GetColor(model::ThemeColorType::Accent1));
+    }
+
+    // Change theme colors
+    {
+        auto const& rColorSets = svx::ColorSets::get();
+        model::ColorSet const& rNewColorSet = rColorSets.getColorSet(0);
+        // check that the theme colors are as expected
+        CPPUNIT_ASSERT_EQUAL(OUString(u"LibreOffice"), rNewColorSet.getName());
+
+        sw::ThemeColorChanger aChanger(pDoc->GetDocShell());
+        aChanger.apply(rNewColorSet);
+    }
+
+    // Check new theme colors
+    {
+        auto const& pTheme = pPage->getSdrPageProperties().GetTheme();
+        CPPUNIT_ASSERT(pTheme);
+        CPPUNIT_ASSERT_EQUAL(OUString(u"Office Theme"), pTheme->GetName());
+
+        auto pColorSet = pTheme->getColorSet();
+        CPPUNIT_ASSERT(pColorSet);
+        CPPUNIT_ASSERT_EQUAL(OUString(u"LibreOffice"), pColorSet->getName());
+        CPPUNIT_ASSERT_EQUAL(Color(0x18A303), 
pTheme->GetColor(model::ThemeColorType::Accent1));
+    }
+
+    // Undo
+    pWrtShell->Undo();
+
+    // Check theme colors have been reverted
+    {
+        auto const& pTheme = pPage->getSdrPageProperties().GetTheme();
+        CPPUNIT_ASSERT(pTheme);
+        CPPUNIT_ASSERT_EQUAL(OUString(u"Office Theme"), pTheme->GetName());
+
+        auto pColorSet = pTheme->getColorSet();
+        CPPUNIT_ASSERT(pColorSet);
+        CPPUNIT_ASSERT_EQUAL(OUString(u"Orange"), pColorSet->getName());
+        CPPUNIT_ASSERT_EQUAL(Color(0xE48312), 
pTheme->GetColor(model::ThemeColorType::Accent1));
+    }
+
+    // Redo
+    pWrtShell->Redo();
+
+    // Check theme colors have been applied again
+    {
+        auto const& pTheme = pPage->getSdrPageProperties().GetTheme();
+        CPPUNIT_ASSERT(pTheme);
+        CPPUNIT_ASSERT_EQUAL(OUString(u"Office Theme"), pTheme->GetName());
+
+        auto pColorSet = pTheme->getColorSet();
+        CPPUNIT_ASSERT(pColorSet);
+        CPPUNIT_ASSERT_EQUAL(OUString(u"LibreOffice"), pColorSet->getName());
+        CPPUNIT_ASSERT_EQUAL(Color(0x18A303), 
pTheme->GetColor(model::ThemeColorType::Accent1));
+    }
+}
+
 } // end anonymous namnespace
 
 CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sw/source/core/inc/ThemeColorChanger.hxx 
b/sw/source/core/inc/ThemeColorChanger.hxx
index 71526f79d216..e7e2620a41d0 100644
--- a/sw/source/core/inc/ThemeColorChanger.hxx
+++ b/sw/source/core/inc/ThemeColorChanger.hxx
@@ -9,13 +9,14 @@
  */
 #pragma once
 
+#include <swdllapi.h>
 #include <docsh.hxx>
 #include <docmodel/theme/ColorSet.hxx>
 #include <svx/theme/ThemeColorChanger.hxx>
 
 namespace sw
 {
-class ThemeColorChanger : public svx::IThemeColorChanger
+class SW_DLLPUBLIC ThemeColorChanger : public svx::IThemeColorChanger
 {
 private:
     SwDocShell* mpDocSh;
diff --git a/sw/source/core/inc/UndoThemeChange.hxx 
b/sw/source/core/inc/UndoThemeChange.hxx
new file mode 100644
index 000000000000..5ce490080179
--- /dev/null
+++ b/sw/source/core/inc/UndoThemeChange.hxx
@@ -0,0 +1,35 @@
+/* -*- 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 <undobj.hxx>
+#include <memory>
+#include <docmodel/theme/ColorSet.hxx>
+
+namespace sw
+{
+class UndoThemeChange final : public SwUndo
+{
+private:
+    SwDoc& mrDocument;
+    std::shared_ptr<model::ColorSet> mpOldColorSet;
+    std::shared_ptr<model::ColorSet> mpNewColorSet;
+
+public:
+    UndoThemeChange(SwDoc& rDocument, std::shared_ptr<model::ColorSet> const& 
pOld,
+                    std::shared_ptr<model::ColorSet> const& pNew);
+    virtual ~UndoThemeChange() override;
+
+    virtual void UndoImpl(UndoRedoContext& rUndoRedoContext) override;
+    virtual void RedoImpl(UndoRedoContext& rUndoRedoContext) override;
+};
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/model/ThemeColorChanger.cxx 
b/sw/source/core/model/ThemeColorChanger.cxx
index 6c70e4bb2cd5..9f973d80aaf2 100644
--- a/sw/source/core/model/ThemeColorChanger.cxx
+++ b/sw/source/core/model/ThemeColorChanger.cxx
@@ -21,6 +21,7 @@
 #include <DocumentContentOperationsManager.hxx>
 #include <IDocumentDrawModelAccess.hxx>
 #include <IDocumentUndoRedo.hxx>
+#include <UndoThemeChange.hxx>
 
 #include <sal/config.h>
 #include <svx/svdpage.hxx>
@@ -154,18 +155,22 @@ void ThemeColorChanger::apply(model::ColorSet const& 
rColorSet)
     pDocument->GetIDocumentUndoRedo().StartUndo(SwUndoId::EMPTY, nullptr);
 
     SdrPage* pPage = 
pDocument->getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
+
     auto pTheme = pPage->getSdrPageProperties().GetTheme();
-    if (pTheme)
-    {
-        pTheme->setColorSet(std::make_shared<model::ColorSet>(rColorSet));
-    }
-    else
+    if (!pTheme)
     {
         pTheme = std::make_shared<model::Theme>("Office");
         pPage->getSdrPageProperties().SetTheme(pTheme);
-        pTheme->setColorSet(std::make_shared<model::ColorSet>(rColorSet));
     }
 
+    auto pNewColorSet = std::make_shared<model::ColorSet>(rColorSet);
+    auto pOldColorSet = pTheme->getColorSet();
+    pTheme->setColorSet(pNewColorSet);
+
+    auto pUndoThemeChange
+        = std::make_unique<sw::UndoThemeChange>(*pDocument, pOldColorSet, 
pNewColorSet);
+    pDocument->GetIDocumentUndoRedo().AppendUndo(std::move(pUndoThemeChange));
+
     SfxStyleSheetBasePool* pPool = mpDocSh->GetStyleSheetPool();
     SwDocStyleSheet* pStyle;
 
diff --git a/sw/source/core/undo/UndoThemeChange.cxx 
b/sw/source/core/undo/UndoThemeChange.cxx
new file mode 100644
index 000000000000..468df82274e3
--- /dev/null
+++ b/sw/source/core/undo/UndoThemeChange.cxx
@@ -0,0 +1,48 @@
+/* -*- 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 <UndoThemeChange.hxx>
+#include <svx/svdpage.hxx>
+#include <docmodel/theme/Theme.hxx>
+#include <doc.hxx>
+#include <drawdoc.hxx>
+#include <IDocumentDrawModelAccess.hxx>
+
+#include <memory>
+
+namespace sw
+{
+UndoThemeChange::UndoThemeChange(SwDoc& rDocument,
+                                 std::shared_ptr<model::ColorSet> const& 
pOldColorSet,
+                                 std::shared_ptr<model::ColorSet> const& 
pNewColorSet)
+    : SwUndo(SwUndoId::CHANGE_THEME, &rDocument)
+    , mrDocument(rDocument)
+    , mpOldColorSet(pOldColorSet)
+    , mpNewColorSet(pNewColorSet)
+{
+}
+
+UndoThemeChange::~UndoThemeChange() {}
+
+void UndoThemeChange::UndoImpl(UndoRedoContext& /*rUndoRedoContext*/)
+{
+    SdrPage* pPage = 
mrDocument.getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
+    auto pTheme = pPage->getSdrPageProperties().GetTheme();
+    pTheme->setColorSet(mpOldColorSet);
+}
+
+void UndoThemeChange::RedoImpl(UndoRedoContext& /*rUndoRedoContext*/)
+{
+    SdrPage* pPage = 
mrDocument.getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
+    auto pTheme = pPage->getSdrPageProperties().GetTheme();
+    pTheme->setColorSet(mpNewColorSet);
+}
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/undo/undobj.cxx b/sw/source/core/undo/undobj.cxx
index 6b6785d79c1e..e5d8469feb3c 100644
--- a/sw/source/core/undo/undobj.cxx
+++ b/sw/source/core/undo/undobj.cxx
@@ -676,6 +676,9 @@ OUString GetUndoComment(SwUndoId eId)
         case SwUndoId::UPDATE_SECTIONS:
             pId = STR_UPDATE_SECTIONS;
             break;
+        case SwUndoId::CHANGE_THEME:
+            pId = STR_UNDO_CHANGE_THEME_COLORS;
+            break;
     }
 
     assert(pId);

Reply via email to