tdf#164793: fix misplaced rounding
[LibreOffice.git] / xmloff / source / style / XMLThemeContext.cxx
blob971c2eaddeb0498908980492cd4bf3fdaaac8223
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/xmlnamespace.hxx>
13 #include <xmloff/xmltoken.hxx>
14 #include <xmloff/xmlimp.hxx>
16 #include <com/sun/star/beans/XPropertySet.hpp>
18 #include <sax/tools/converter.hxx>
20 #include <docmodel/uno/UnoTheme.hxx>
21 #include <docmodel/theme/Theme.hxx>
23 using namespace css;
24 using namespace xmloff::token;
26 XMLThemeContext::XMLThemeContext(
27 SvXMLImport& rImport, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList,
28 css::uno::Reference<css::uno::XInterface> const& xObjectWithThemeProperty)
29 : SvXMLImportContext(rImport)
30 , m_xObjectWithThemeProperty(xObjectWithThemeProperty)
31 , mpTheme(new model::Theme)
33 for (const auto& rAttribute : sax_fastparser::castToFastAttributeList(xAttrList))
35 switch (rAttribute.getToken())
37 case XML_ELEMENT(LO_EXT, XML_NAME):
39 OUString aName = rAttribute.toString();
40 mpTheme->SetName(aName);
41 break;
47 XMLThemeContext::~XMLThemeContext()
49 if (mpTheme && mpTheme->getColorSet())
51 uno::Reference<beans::XPropertySet> xPropertySet(m_xObjectWithThemeProperty,
52 uno::UNO_QUERY);
53 auto xTheme = model::theme::createXTheme(mpTheme);
54 xPropertySet->setPropertyValue(u"Theme"_ustr, uno::Any(xTheme));
58 uno::Reference<xml::sax::XFastContextHandler> SAL_CALL XMLThemeContext::createFastChildContext(
59 sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttribs)
61 if (nElement == XML_ELEMENT(LO_EXT, XML_THEME_COLORS))
63 return new XMLThemeColorsContext(GetImport(), xAttribs, *mpTheme);
66 return nullptr;
69 XMLThemeColorsContext::XMLThemeColorsContext(
70 SvXMLImport& rImport, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList,
71 model::Theme& rTheme)
72 : SvXMLImportContext(rImport)
73 , mrTheme(rTheme)
75 for (const auto& rAttribute : sax_fastparser::castToFastAttributeList(xAttrList))
77 switch (rAttribute.getToken())
79 case XML_ELEMENT(LO_EXT, XML_NAME):
81 OUString aName = rAttribute.toString();
82 m_pColorSet.reset(new model::ColorSet(aName));
83 break;
89 XMLThemeColorsContext::~XMLThemeColorsContext()
91 if (m_pColorSet)
92 mrTheme.setColorSet(m_pColorSet);
95 uno::Reference<xml::sax::XFastContextHandler>
96 SAL_CALL XMLThemeColorsContext::createFastChildContext(
97 sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttribs)
99 if (nElement == XML_ELEMENT(LO_EXT, XML_COLOR))
101 if (m_pColorSet)
102 return new XMLColorContext(GetImport(), xAttribs, m_pColorSet);
105 return nullptr;
108 XMLColorContext::XMLColorContext(SvXMLImport& rImport,
109 const uno::Reference<xml::sax::XFastAttributeList>& xAttrList,
110 std::shared_ptr<model::ColorSet>& rpColorSet)
111 : SvXMLImportContext(rImport)
113 OUString aName;
114 ::Color aColor;
116 for (const auto& rAttribute : sax_fastparser::castToFastAttributeList(xAttrList))
118 switch (rAttribute.getToken())
120 case XML_ELEMENT(LO_EXT, XML_NAME):
122 aName = rAttribute.toString();
123 break;
125 case XML_ELEMENT(LO_EXT, XML_COLOR):
127 sax::Converter::convertColor(aColor, rAttribute.toView());
128 break;
133 if (!aName.isEmpty())
135 auto eType = model::ThemeColorType::Unknown;
136 if (aName == u"dark1")
137 eType = model::ThemeColorType::Dark1;
138 else if (aName == u"light1")
139 eType = model::ThemeColorType::Light1;
140 else if (aName == u"dark2")
141 eType = model::ThemeColorType::Dark2;
142 else if (aName == u"light2")
143 eType = model::ThemeColorType::Light2;
144 else if (aName == u"accent1")
145 eType = model::ThemeColorType::Accent1;
146 else if (aName == u"accent2")
147 eType = model::ThemeColorType::Accent2;
148 else if (aName == u"accent3")
149 eType = model::ThemeColorType::Accent3;
150 else if (aName == u"accent4")
151 eType = model::ThemeColorType::Accent4;
152 else if (aName == u"accent5")
153 eType = model::ThemeColorType::Accent5;
154 else if (aName == u"accent6")
155 eType = model::ThemeColorType::Accent6;
156 else if (aName == u"hyperlink")
157 eType = model::ThemeColorType::Hyperlink;
158 else if (aName == u"followed-hyperlink")
159 eType = model::ThemeColorType::FollowedHyperlink;
161 if (eType != model::ThemeColorType::Unknown)
163 rpColorSet->add(eType, aColor);
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */