Version 24.2.2.2, tag libreoffice-24.2.2.2
[LibreOffice.git] / xmloff / source / style / XMLThemeContext.cxx
blob564d934ff8fbeee6cbe662c1ca0bcdc5e9acf11e
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 <XMLThemeContext.hxx>
12 #include <xmloff/maptype.hxx>
13 #include <xmloff/xmlnamespace.hxx>
14 #include <xmloff/xmltoken.hxx>
15 #include <xmloff/xmlprcon.hxx>
16 #include <xmloff/xmlerror.hxx>
17 #include <xmloff/namespacemap.hxx>
18 #include <xmloff/xmlimp.hxx>
19 #include <xmloff/xmlement.hxx>
20 #include <xmloff/xmlprhdl.hxx>
22 #include <sal/log.hxx>
23 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <sax/tools/converter.hxx>
26 #include <comphelper/sequence.hxx>
28 #include <docmodel/uno/UnoTheme.hxx>
29 #include <docmodel/theme/Theme.hxx>
31 using namespace css;
32 using namespace xmloff::token;
34 XMLThemeContext::XMLThemeContext(
35 SvXMLImport& rImport, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList,
36 css::uno::Reference<css::uno::XInterface> const& xObjectWithThemeProperty)
37 : SvXMLImportContext(rImport)
38 , m_xObjectWithThemeProperty(xObjectWithThemeProperty)
39 , mpTheme(new model::Theme)
41 for (const auto& rAttribute : sax_fastparser::castToFastAttributeList(xAttrList))
43 switch (rAttribute.getToken())
45 case XML_ELEMENT(LO_EXT, XML_NAME):
47 OUString aName = rAttribute.toString();
48 mpTheme->SetName(aName);
49 break;
55 XMLThemeContext::~XMLThemeContext()
57 if (mpTheme && mpTheme->getColorSet())
59 uno::Reference<beans::XPropertySet> xPropertySet(m_xObjectWithThemeProperty,
60 uno::UNO_QUERY);
61 auto xTheme = model::theme::createXTheme(mpTheme);
62 xPropertySet->setPropertyValue("Theme", uno::Any(xTheme));
66 uno::Reference<xml::sax::XFastContextHandler> SAL_CALL XMLThemeContext::createFastChildContext(
67 sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttribs)
69 if (nElement == XML_ELEMENT(LO_EXT, XML_THEME_COLORS))
71 return new XMLThemeColorsContext(GetImport(), xAttribs, *mpTheme);
74 return nullptr;
77 XMLThemeColorsContext::XMLThemeColorsContext(
78 SvXMLImport& rImport, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList,
79 model::Theme& rTheme)
80 : SvXMLImportContext(rImport)
81 , mrTheme(rTheme)
83 for (const auto& rAttribute : sax_fastparser::castToFastAttributeList(xAttrList))
85 switch (rAttribute.getToken())
87 case XML_ELEMENT(LO_EXT, XML_NAME):
89 OUString aName = rAttribute.toString();
90 m_pColorSet.reset(new model::ColorSet(aName));
91 break;
97 XMLThemeColorsContext::~XMLThemeColorsContext()
99 if (m_pColorSet)
100 mrTheme.setColorSet(m_pColorSet);
103 uno::Reference<xml::sax::XFastContextHandler>
104 SAL_CALL XMLThemeColorsContext::createFastChildContext(
105 sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttribs)
107 if (nElement == XML_ELEMENT(LO_EXT, XML_COLOR))
109 if (m_pColorSet)
110 return new XMLColorContext(GetImport(), xAttribs, m_pColorSet);
113 return nullptr;
116 XMLColorContext::XMLColorContext(SvXMLImport& rImport,
117 const uno::Reference<xml::sax::XFastAttributeList>& xAttrList,
118 std::shared_ptr<model::ColorSet>& rpColorSet)
119 : SvXMLImportContext(rImport)
121 OUString aName;
122 ::Color aColor;
124 for (const auto& rAttribute : sax_fastparser::castToFastAttributeList(xAttrList))
126 switch (rAttribute.getToken())
128 case XML_ELEMENT(LO_EXT, XML_NAME):
130 aName = rAttribute.toString();
131 break;
133 case XML_ELEMENT(LO_EXT, XML_COLOR):
135 sax::Converter::convertColor(aColor, rAttribute.toView());
136 break;
141 if (!aName.isEmpty())
143 auto eType = model::ThemeColorType::Unknown;
144 if (aName == u"dark1")
145 eType = model::ThemeColorType::Dark1;
146 else if (aName == u"light1")
147 eType = model::ThemeColorType::Light1;
148 else if (aName == u"dark2")
149 eType = model::ThemeColorType::Dark2;
150 else if (aName == u"light2")
151 eType = model::ThemeColorType::Light2;
152 else if (aName == u"accent1")
153 eType = model::ThemeColorType::Accent1;
154 else if (aName == u"accent2")
155 eType = model::ThemeColorType::Accent2;
156 else if (aName == u"accent3")
157 eType = model::ThemeColorType::Accent3;
158 else if (aName == u"accent4")
159 eType = model::ThemeColorType::Accent4;
160 else if (aName == u"accent5")
161 eType = model::ThemeColorType::Accent5;
162 else if (aName == u"accent6")
163 eType = model::ThemeColorType::Accent6;
164 else if (aName == u"hyperlink")
165 eType = model::ThemeColorType::Hyperlink;
166 else if (aName == u"followed-hyperlink")
167 eType = model::ThemeColorType::FollowedHyperlink;
169 if (eType != model::ThemeColorType::Unknown)
171 rpColorSet->add(eType, aColor);
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */