/src/libreoffice/xmloff/source/style/XMLThemeContext.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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 | | */ |
9 | | |
10 | | #include <XMLThemeContext.hxx> |
11 | | |
12 | | #include <xmloff/xmlnamespace.hxx> |
13 | | #include <xmloff/xmltoken.hxx> |
14 | | #include <xmloff/xmlimp.hxx> |
15 | | |
16 | | #include <com/sun/star/beans/XPropertySet.hpp> |
17 | | |
18 | | #include <sax/tools/converter.hxx> |
19 | | |
20 | | #include <comphelper/diagnose_ex.hxx> |
21 | | #include <docmodel/uno/UnoTheme.hxx> |
22 | | #include <docmodel/theme/Theme.hxx> |
23 | | |
24 | | using namespace css; |
25 | | using namespace xmloff::token; |
26 | | |
27 | | XMLThemeContext::XMLThemeContext( |
28 | | SvXMLImport& rImport, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList, |
29 | | css::uno::Reference<css::uno::XInterface> const& xObjectWithThemeProperty) |
30 | 0 | : SvXMLImportContext(rImport) |
31 | 0 | , m_xObjectWithThemeProperty(xObjectWithThemeProperty) |
32 | 0 | , mpTheme(new model::Theme) |
33 | 0 | { |
34 | 0 | for (const auto& rAttribute : sax_fastparser::castToFastAttributeList(xAttrList)) |
35 | 0 | { |
36 | 0 | switch (rAttribute.getToken()) |
37 | 0 | { |
38 | 0 | case XML_ELEMENT(LO_EXT, XML_NAME): |
39 | 0 | { |
40 | 0 | OUString aName = rAttribute.toString(); |
41 | 0 | mpTheme->SetName(aName); |
42 | 0 | break; |
43 | 0 | } |
44 | 0 | } |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | | XMLThemeContext::~XMLThemeContext() |
49 | 0 | { |
50 | 0 | if (mpTheme && mpTheme->getColorSet()) |
51 | 0 | { |
52 | 0 | uno::Reference<beans::XPropertySet> xPropertySet(m_xObjectWithThemeProperty, |
53 | 0 | uno::UNO_QUERY); |
54 | 0 | auto xTheme = model::theme::createXTheme(mpTheme); |
55 | 0 | try |
56 | 0 | { |
57 | 0 | xPropertySet->setPropertyValue(u"Theme"_ustr, uno::Any(xTheme)); |
58 | 0 | } |
59 | 0 | catch (uno::Exception&) |
60 | 0 | { |
61 | 0 | DBG_UNHANDLED_EXCEPTION("xmloff"); |
62 | 0 | } |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | uno::Reference<xml::sax::XFastContextHandler> SAL_CALL XMLThemeContext::createFastChildContext( |
67 | | sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttribs) |
68 | 0 | { |
69 | 0 | if (nElement == XML_ELEMENT(LO_EXT, XML_THEME_COLORS)) |
70 | 0 | { |
71 | 0 | return new XMLThemeColorsContext(GetImport(), xAttribs, *mpTheme); |
72 | 0 | } |
73 | | |
74 | 0 | return nullptr; |
75 | 0 | } |
76 | | |
77 | | XMLThemeColorsContext::XMLThemeColorsContext( |
78 | | SvXMLImport& rImport, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList, |
79 | | model::Theme& rTheme) |
80 | 0 | : SvXMLImportContext(rImport) |
81 | 0 | , mrTheme(rTheme) |
82 | 0 | { |
83 | 0 | for (const auto& rAttribute : sax_fastparser::castToFastAttributeList(xAttrList)) |
84 | 0 | { |
85 | 0 | switch (rAttribute.getToken()) |
86 | 0 | { |
87 | 0 | case XML_ELEMENT(LO_EXT, XML_NAME): |
88 | 0 | { |
89 | 0 | OUString aName = rAttribute.toString(); |
90 | 0 | m_pColorSet.reset(new model::ColorSet(aName)); |
91 | 0 | break; |
92 | 0 | } |
93 | 0 | } |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | | XMLThemeColorsContext::~XMLThemeColorsContext() |
98 | 0 | { |
99 | 0 | if (m_pColorSet) |
100 | 0 | mrTheme.setColorSet(m_pColorSet); |
101 | 0 | } |
102 | | |
103 | | uno::Reference<xml::sax::XFastContextHandler> |
104 | | SAL_CALL XMLThemeColorsContext::createFastChildContext( |
105 | | sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttribs) |
106 | 0 | { |
107 | 0 | if (nElement == XML_ELEMENT(LO_EXT, XML_COLOR)) |
108 | 0 | { |
109 | 0 | if (m_pColorSet) |
110 | 0 | return new XMLColorContext(GetImport(), xAttribs, m_pColorSet); |
111 | 0 | } |
112 | | |
113 | 0 | return nullptr; |
114 | 0 | } |
115 | | |
116 | | XMLColorContext::XMLColorContext(SvXMLImport& rImport, |
117 | | const uno::Reference<xml::sax::XFastAttributeList>& xAttrList, |
118 | | const std::shared_ptr<model::ColorSet>& rpColorSet) |
119 | 0 | : SvXMLImportContext(rImport) |
120 | 0 | { |
121 | 0 | OUString aName; |
122 | 0 | ::Color aColor; |
123 | |
|
124 | 0 | for (const auto& rAttribute : sax_fastparser::castToFastAttributeList(xAttrList)) |
125 | 0 | { |
126 | 0 | switch (rAttribute.getToken()) |
127 | 0 | { |
128 | 0 | case XML_ELEMENT(LO_EXT, XML_NAME): |
129 | 0 | { |
130 | 0 | aName = rAttribute.toString(); |
131 | 0 | break; |
132 | 0 | } |
133 | 0 | case XML_ELEMENT(LO_EXT, XML_COLOR): |
134 | 0 | { |
135 | 0 | sax::Converter::convertColor(aColor, rAttribute.toView()); |
136 | 0 | break; |
137 | 0 | } |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | 0 | if (!aName.isEmpty()) |
142 | 0 | { |
143 | 0 | auto eType = model::ThemeColorType::Unknown; |
144 | 0 | if (aName == u"dark1") |
145 | 0 | eType = model::ThemeColorType::Dark1; |
146 | 0 | else if (aName == u"light1") |
147 | 0 | eType = model::ThemeColorType::Light1; |
148 | 0 | else if (aName == u"dark2") |
149 | 0 | eType = model::ThemeColorType::Dark2; |
150 | 0 | else if (aName == u"light2") |
151 | 0 | eType = model::ThemeColorType::Light2; |
152 | 0 | else if (aName == u"accent1") |
153 | 0 | eType = model::ThemeColorType::Accent1; |
154 | 0 | else if (aName == u"accent2") |
155 | 0 | eType = model::ThemeColorType::Accent2; |
156 | 0 | else if (aName == u"accent3") |
157 | 0 | eType = model::ThemeColorType::Accent3; |
158 | 0 | else if (aName == u"accent4") |
159 | 0 | eType = model::ThemeColorType::Accent4; |
160 | 0 | else if (aName == u"accent5") |
161 | 0 | eType = model::ThemeColorType::Accent5; |
162 | 0 | else if (aName == u"accent6") |
163 | 0 | eType = model::ThemeColorType::Accent6; |
164 | 0 | else if (aName == u"hyperlink") |
165 | 0 | eType = model::ThemeColorType::Hyperlink; |
166 | 0 | else if (aName == u"followed-hyperlink") |
167 | 0 | eType = model::ThemeColorType::FollowedHyperlink; |
168 | |
|
169 | 0 | if (eType != model::ThemeColorType::Unknown) |
170 | 0 | { |
171 | 0 | rpColorSet->add(eType, aColor); |
172 | 0 | } |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |