Bump version to 24.04.3.4
[LibreOffice.git] / svx / source / theme / ThemeColorChangerCommon.cxx
blobd15d75839309b986785b9b71b502d9908358c18f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <comphelper/lok.hxx>
12 #include <docmodel/theme/ColorSet.hxx>
14 #include <editeng/colritem.hxx>
15 #include <editeng/editeng.hxx>
16 #include <editeng/eeitem.hxx>
17 #include <editeng/section.hxx>
19 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
21 #include <sal/config.h>
23 #include <sfx2/lokhelper.hxx>
25 #include <svx/PaletteManager.hxx>
26 #include <svx/svdmodel.hxx>
27 #include <svx/svdotext.hxx>
28 #include <svx/svdundo.hxx>
29 #include <svx/theme/ThemeColorChangerCommon.hxx>
30 #include <svx/theme/ThemeColorPaletteManager.hxx>
31 #include <svx/xdef.hxx>
32 #include <svx/xlnclit.hxx>
33 #include <svx/xflclit.hxx>
34 #include <tools/json_writer.hxx>
36 using namespace css;
38 namespace svx::theme
40 namespace
42 const SvxColorItem* getColorItem(const editeng::Section& rSection)
44 auto iterator = std::find_if(
45 rSection.maAttributes.begin(), rSection.maAttributes.end(),
46 [](const SfxPoolItem* pPoolItem) { return pPoolItem->Which() == EE_CHAR_COLOR; });
48 if (iterator != rSection.maAttributes.end())
49 return static_cast<const SvxColorItem*>(*iterator);
50 return nullptr;
53 bool updateEditEngTextSections(model::ColorSet const& rColorSet, SdrObject* pObject, SdrView& rView)
55 SdrTextObj* pTextObject = DynCastSdrTextObj(pObject);
57 if (!pTextObject)
58 return false;
60 rView.SdrBeginTextEdit(pTextObject);
62 auto* pOutlinerView = rView.GetTextEditOutlinerView();
63 if (!pOutlinerView)
64 return false;
66 auto* pEditEngine = pOutlinerView->GetEditView().GetEditEngine();
67 if (!pEditEngine)
68 return false;
70 OutlinerParaObject* pOutlinerParagraphObject = pTextObject->GetOutlinerParaObject();
71 if (pOutlinerParagraphObject)
73 const EditTextObject& rEditText = pOutlinerParagraphObject->GetTextObject();
74 std::vector<editeng::Section> aSections;
75 rEditText.GetAllSections(aSections);
77 for (editeng::Section const& rSection : aSections)
79 const SvxColorItem* pItem = getColorItem(rSection);
80 if (!pItem)
81 continue;
83 model::ComplexColor const& rComplexColor = pItem->getComplexColor();
84 if (rComplexColor.isValidThemeType())
86 SfxItemSet aSet(pEditEngine->GetAttribs(rSection.mnParagraph, rSection.mnStart,
87 rSection.mnEnd,
88 GetAttribsFlags::CHARATTRIBS));
89 Color aNewColor = rColorSet.resolveColor(rComplexColor);
90 std::unique_ptr<SvxColorItem> pNewItem(pItem->Clone());
91 pNewItem->setColor(aNewColor);
92 aSet.Put(*pNewItem);
94 ESelection aSelection(rSection.mnParagraph, rSection.mnStart, rSection.mnParagraph,
95 rSection.mnEnd);
96 pEditEngine->QuickSetAttribs(aSet, aSelection);
101 rView.SdrEndTextEdit();
103 return true;
106 bool updateObjectAttributes(model::ColorSet const& rColorSet, SdrObject& rObject,
107 SfxUndoManager* pUndoManager)
109 bool bChanged = false;
111 auto aItemSet = rObject.GetMergedItemSet();
113 if (const XFillColorItem* pItem = aItemSet.GetItemIfSet(XATTR_FILLCOLOR, false))
115 model::ComplexColor const& rComplexColor = pItem->getComplexColor();
116 if (rComplexColor.isValidThemeType())
118 Color aNewColor = rColorSet.resolveColor(rComplexColor);
119 std::unique_ptr<XFillColorItem> pNewItem(pItem->Clone());
120 pNewItem->SetColorValue(aNewColor);
121 aItemSet.Put(*pNewItem);
122 bChanged = true;
125 if (const XLineColorItem* pItem = aItemSet.GetItemIfSet(XATTR_LINECOLOR, false))
127 model::ComplexColor const& rComplexColor = pItem->getComplexColor();
128 if (rComplexColor.isValidThemeType())
130 Color aNewColor = rColorSet.resolveColor(rComplexColor);
131 std::unique_ptr<XLineColorItem> pNewItem(pItem->Clone());
132 pNewItem->SetColorValue(aNewColor);
133 aItemSet.Put(*pNewItem);
134 bChanged = true;
137 if (const SvxColorItem* pItem = aItemSet.GetItemIfSet(EE_CHAR_COLOR, false))
139 model::ComplexColor const& rComplexColor = pItem->getComplexColor();
140 if (rComplexColor.isValidThemeType())
142 Color aNewColor = rColorSet.resolveColor(rComplexColor);
143 std::unique_ptr<SvxColorItem> pNewItem(pItem->Clone());
144 pNewItem->setColor(aNewColor);
145 aItemSet.Put(*pNewItem);
146 bChanged = true;
149 if (bChanged)
151 const bool bUndo = pUndoManager && pUndoManager->IsInListAction();
152 if (bUndo)
154 pUndoManager->AddUndoAction(
155 rObject.getSdrModelFromSdrObject().GetSdrUndoFactory().CreateUndoAttrObject(
156 rObject));
158 rObject.SetMergedItemSetAndBroadcast(aItemSet);
160 return bChanged;
163 } // end anonymous namespace
165 /// Updates properties of the SdrObject
166 void updateSdrObject(model::ColorSet const& rColorSet, SdrObject* pObject, SdrView* pView,
167 SfxUndoManager* pUndoManager)
169 if (!pObject)
170 return;
171 updateObjectAttributes(rColorSet, *pObject, pUndoManager);
172 if (pView)
173 updateEditEngTextSections(rColorSet, pObject, *pView);
176 void notifyLOK(std::shared_ptr<model::ColorSet> const& pColorSet,
177 const std::set<Color>& rDocumentColors)
179 if (comphelper::LibreOfficeKit::isActive())
181 svx::ThemeColorPaletteManager aManager(pColorSet);
182 tools::JsonWriter aTree;
184 if (pColorSet)
185 aManager.generateJSON(aTree);
186 if (rDocumentColors.size())
187 PaletteManager::generateJSON(aTree, rDocumentColors);
189 SfxLokHelper::notifyAllViews(LOK_CALLBACK_COLOR_PALETTES, aTree.finishAndGetAsOString());
193 } // end svx::theme namespace
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */