vcl : use frozen unordered_map to avoid allocations
[LibreOffice.git] / svx / source / theme / ThemeColorChangerCommon.cxx
blob39a20bd3441f9587bda4a5acb60e6bfd2d450b2c
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& rEditEngine = pOutlinerView->GetEditView().getEditEngine();
68 OutlinerParaObject* pOutlinerParagraphObject = pTextObject->GetOutlinerParaObject();
69 if (pOutlinerParagraphObject)
71 const EditTextObject& rEditText = pOutlinerParagraphObject->GetTextObject();
72 std::vector<editeng::Section> aSections;
73 rEditText.GetAllSections(aSections);
75 for (editeng::Section const& rSection : aSections)
77 const SvxColorItem* pItem = getColorItem(rSection);
78 if (!pItem)
79 continue;
81 model::ComplexColor const& rComplexColor = pItem->getComplexColor();
82 if (rComplexColor.isValidThemeType())
84 SfxItemSet aSet(rEditEngine.GetAttribs(rSection.mnParagraph, rSection.mnStart,
85 rSection.mnEnd,
86 GetAttribsFlags::CHARATTRIBS));
87 Color aNewColor = rColorSet.resolveColor(rComplexColor);
88 std::unique_ptr<SvxColorItem> pNewItem(pItem->Clone());
89 pNewItem->setColor(aNewColor);
90 aSet.Put(*pNewItem);
92 ESelection aSelection(rSection.mnParagraph, rSection.mnStart, rSection.mnParagraph,
93 rSection.mnEnd);
94 rEditEngine.QuickSetAttribs(aSet, aSelection);
99 rView.SdrEndTextEdit();
101 return true;
104 bool updateObjectAttributes(model::ColorSet const& rColorSet, SdrObject& rObject,
105 SfxUndoManager* pUndoManager)
107 bool bChanged = false;
109 auto aItemSet = rObject.GetMergedItemSet();
111 if (const XFillColorItem* pItem = aItemSet.GetItemIfSet(XATTR_FILLCOLOR, false))
113 model::ComplexColor const& rComplexColor = pItem->getComplexColor();
114 if (rComplexColor.isValidThemeType())
116 Color aNewColor = rColorSet.resolveColor(rComplexColor);
117 std::unique_ptr<XFillColorItem> pNewItem(pItem->Clone());
118 pNewItem->SetColorValue(aNewColor);
119 aItemSet.Put(*pNewItem);
120 bChanged = true;
123 if (const XLineColorItem* pItem = aItemSet.GetItemIfSet(XATTR_LINECOLOR, false))
125 model::ComplexColor const& rComplexColor = pItem->getComplexColor();
126 if (rComplexColor.isValidThemeType())
128 Color aNewColor = rColorSet.resolveColor(rComplexColor);
129 std::unique_ptr<XLineColorItem> pNewItem(pItem->Clone());
130 pNewItem->SetColorValue(aNewColor);
131 aItemSet.Put(*pNewItem);
132 bChanged = true;
135 if (const SvxColorItem* pItem = aItemSet.GetItemIfSet(EE_CHAR_COLOR, false))
137 model::ComplexColor const& rComplexColor = pItem->getComplexColor();
138 if (rComplexColor.isValidThemeType())
140 Color aNewColor = rColorSet.resolveColor(rComplexColor);
141 std::unique_ptr<SvxColorItem> pNewItem(pItem->Clone());
142 pNewItem->setColor(aNewColor);
143 aItemSet.Put(*pNewItem);
144 bChanged = true;
147 if (bChanged)
149 const bool bUndo = pUndoManager && pUndoManager->IsInListAction();
150 if (bUndo)
152 pUndoManager->AddUndoAction(
153 rObject.getSdrModelFromSdrObject().GetSdrUndoFactory().CreateUndoAttrObject(
154 rObject));
156 rObject.SetMergedItemSetAndBroadcast(aItemSet);
158 return bChanged;
161 } // end anonymous namespace
163 /// Updates properties of the SdrObject
164 void updateSdrObject(model::ColorSet const& rColorSet, SdrObject* pObject, SdrView* pView,
165 SfxUndoManager* pUndoManager)
167 if (!pObject)
168 return;
169 updateObjectAttributes(rColorSet, *pObject, pUndoManager);
170 if (pView)
171 updateEditEngTextSections(rColorSet, pObject, *pView);
174 void notifyLOK(std::shared_ptr<model::ColorSet> const& pColorSet,
175 const std::set<Color>& rDocumentColors)
177 if (comphelper::LibreOfficeKit::isActive())
179 svx::ThemeColorPaletteManager aManager(pColorSet);
180 tools::JsonWriter aTree;
182 if (pColorSet)
183 aManager.generateJSON(aTree);
184 if (rDocumentColors.size())
185 PaletteManager::generateJSON(aTree, rDocumentColors);
187 SfxLokHelper::notifyAllViews(LOK_CALLBACK_COLOR_PALETTES, aTree.finishAndGetAsOString());
191 } // end svx::theme namespace
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */