Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/docmodel/source/theme/Theme.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
11
#include <docmodel/theme/Theme.hxx>
12
#include <docmodel/uno/UnoTheme.hxx>
13
14
#include <utility>
15
#include <libxml/xmlwriter.h>
16
#include <comphelper/sequenceashashmap.hxx>
17
#include <comphelper/sequence.hxx>
18
#include <sal/types.h>
19
#include <o3tl/enumrange.hxx>
20
#include <o3tl/underlyingenumvalue.hxx>
21
#include <com/sun/star/util/Color.hpp>
22
#include <com/sun/star/beans/PropertyValue.hpp>
23
#include <com/sun/star/util/XTheme.hpp>
24
25
using namespace com::sun::star;
26
27
namespace model
28
{
29
7.61k
Theme::Theme() = default;
30
31
Theme::Theme(OUString const& rName)
32
120k
    : maName(rName)
33
120k
{
34
120k
}
35
36
Theme::Theme(Theme const& rTheme)
37
0
    : maName(rTheme.maName)
38
0
    , mpColorSet(new ColorSet(*rTheme.getColorSet()))
39
0
    , maFontScheme(rTheme.maFontScheme)
40
0
{
41
0
}
42
43
4.32k
void Theme::SetName(const OUString& rName) { maName = rName; }
44
45
4.01k
const OUString& Theme::GetName() const { return maName; }
46
47
void Theme::dumpAsXml(xmlTextWriterPtr pWriter) const
48
0
{
49
0
    (void)xmlTextWriterStartElement(pWriter, BAD_CAST("Theme"));
50
0
    (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this);
51
0
    (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("maName"),
52
0
                                      BAD_CAST(maName.toUtf8().getStr()));
53
54
0
    if (mpColorSet)
55
0
    {
56
0
        mpColorSet->dumpAsXml(pWriter);
57
0
    }
58
59
0
    (void)xmlTextWriterEndElement(pWriter);
60
0
}
61
62
void Theme::ToAny(uno::Any& rVal) const
63
0
{
64
0
    comphelper::SequenceAsHashMap aMap;
65
0
    aMap[u"Name"_ustr] <<= maName;
66
67
0
    if (mpColorSet)
68
0
    {
69
0
        std::vector<util::Color> aColorScheme;
70
0
        for (auto eThemeColorType : o3tl::enumrange<model::ThemeColorType>())
71
0
        {
72
0
            Color aColor = mpColorSet->getColor(eThemeColorType);
73
0
            aColorScheme.push_back(sal_Int32(aColor));
74
0
        }
75
76
0
        aMap[u"ColorSchemeName"_ustr] <<= mpColorSet->getName();
77
0
        aMap[u"ColorScheme"_ustr] <<= comphelper::containerToSequence(aColorScheme);
78
0
    }
79
80
0
    rVal <<= aMap.getAsConstPropertyValueList();
81
0
}
82
83
static std::shared_ptr<model::ColorSet> makeColorSet(const OUString& name,
84
                                                     const uno::Sequence<util::Color>& colors)
85
0
{
86
0
    auto colorset = std::make_shared<model::ColorSet>(name);
87
0
    for (auto eThemeColorType : o3tl::enumrange<model::ThemeColorType>())
88
0
    {
89
0
        const size_t nIndex(o3tl::to_underlying(eThemeColorType));
90
0
        if (nIndex >= colors.size())
91
0
            break;
92
0
        const Color aColor(ColorTransparency, colors[nIndex]);
93
0
        colorset->add(eThemeColorType, aColor);
94
0
    }
95
0
    return colorset;
96
0
}
97
98
std::shared_ptr<Theme> Theme::FromAny(const uno::Any& rVal)
99
0
{
100
0
    if (!rVal.hasValue())
101
0
        return {};
102
103
0
    if (css::uno::Reference<css::util::XTheme> xTheme; rVal >>= xTheme)
104
0
    {
105
0
        if (!xTheme)
106
0
            return {};
107
108
0
        if (auto* pUnoTheme = dynamic_cast<UnoTheme*>(xTheme.get()))
109
0
            return pUnoTheme->getTheme();
110
111
0
        auto pTheme = std::make_shared<Theme>(xTheme->getName());
112
0
        if (auto aColors = xTheme->getColorSet(); aColors.hasElements())
113
0
            pTheme->setColorSet(makeColorSet(xTheme->getName(), aColors)); // Reuse theme name
114
0
        return pTheme;
115
0
    }
116
117
0
    comphelper::SequenceAsHashMap aMap(rVal);
118
119
0
    OUString aThemeName;
120
0
    if (auto it = aMap.find(u"Name"_ustr); it != aMap.end())
121
0
        it->second >>= aThemeName;
122
0
    else
123
0
        return {};
124
0
    auto pTheme = std::make_shared<Theme>(aThemeName);
125
126
0
    if (auto it = aMap.find(u"ColorSchemeName"_ustr); it != aMap.end())
127
0
    {
128
0
        OUString aColorSchemeName;
129
0
        it->second >>= aColorSchemeName;
130
131
0
        pTheme->setColorSet(makeColorSet(
132
0
            aColorSchemeName,
133
0
            aMap.getUnpackedValueOrDefault(u"ColorScheme"_ustr, uno::Sequence<util::Color>{})));
134
0
    }
135
136
0
    return pTheme;
137
0
}
138
139
std::vector<Color> Theme::GetColors() const
140
0
{
141
0
    if (!mpColorSet)
142
0
        return {};
143
144
0
    std::vector<Color> aColors;
145
0
    for (auto eThemeColorType : o3tl::enumrange<model::ThemeColorType>())
146
0
    {
147
0
        aColors.push_back(mpColorSet->getColor(eThemeColorType));
148
0
    }
149
0
    return aColors;
150
0
}
151
152
Color Theme::GetColor(model::ThemeColorType eType) const
153
0
{
154
0
    if (!mpColorSet)
155
0
        return {};
156
157
0
    return mpColorSet->getColor(eType);
158
0
}
159
160
} // end of namespace model
161
162
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */