docmodel/source/theme/Theme.cxx | 85 ++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 46 deletions(-)
New commits: commit bd191351312bf279115b98370da33434a552f36a Author: Mike Kaganski <[email protected]> AuthorDate: Sun Jun 15 16:09:06 2025 +0500 Commit: Mike Kaganski <[email protected]> CommitDate: Sun Jun 15 15:09:10 2025 +0200 Don't check if enumrange iterates through ThemeColorType::Unknown It is not needed, because Unknown is -1, outside of the iterated range of [0, LAST]. Change-Id: I3ee1c7112d4594d76f2ed526d4c18dc0d8c73e7b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/186514 Tested-by: Jenkins Reviewed-by: Mike Kaganski <[email protected]> diff --git a/docmodel/source/theme/Theme.cxx b/docmodel/source/theme/Theme.cxx index 63a6ce6843e3..a21d13643ae8 100644 --- a/docmodel/source/theme/Theme.cxx +++ b/docmodel/source/theme/Theme.cxx @@ -71,11 +71,8 @@ void Theme::ToAny(uno::Any& rVal) const std::vector<util::Color> aColorScheme; for (auto eThemeColorType : o3tl::enumrange<model::ThemeColorType>()) { - if (eThemeColorType != model::ThemeColorType::Unknown) - { - Color aColor = mpColorSet->getColor(eThemeColorType); - aColorScheme.push_back(sal_Int32(aColor)); - } + Color aColor = mpColorSet->getColor(eThemeColorType); + aColorScheme.push_back(sal_Int32(aColor)); } aMap[u"ColorSchemeName"_ustr] <<= mpColorSet->getName(); @@ -146,8 +143,7 @@ std::vector<Color> Theme::GetColors() const std::vector<Color> aColors; for (auto eThemeColorType : o3tl::enumrange<model::ThemeColorType>()) { - if (eThemeColorType != model::ThemeColorType::Unknown) - aColors.push_back(mpColorSet->getColor(eThemeColorType)); + aColors.push_back(mpColorSet->getColor(eThemeColorType)); } return aColors; } commit 749b9a889e2e1d5dfd57ee624ada65e1f0f5ab76 Author: Mike Kaganski <[email protected]> AuthorDate: Sun Jun 15 15:40:48 2025 +0500 Commit: Mike Kaganski <[email protected]> CommitDate: Sun Jun 15 15:09:03 2025 +0200 Related: tdf#167018 Generalize and deduplicate Theme::FromAny Let it also handle a hypothetical non-UnoTheme implementation (it is possible to create these e.g. from Basic / Python code). When handling it, use theme name as color set name (there seems to be no UNO API to get color set name?). And use the same code for the creation of Theme from PropertyValue list. The check that enumrange<ThemeColorType> does not iterate through ThemeColorType::Unknown is redundant, since that value is outside of [0, LAST] range. Change-Id: Iae6770f196011deeb2a92d3b64af514fb6a53d06 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/186513 Reviewed-by: Mike Kaganski <[email protected]> Tested-by: Jenkins diff --git a/docmodel/source/theme/Theme.cxx b/docmodel/source/theme/Theme.cxx index 55a39f2b6afd..63a6ce6843e3 100644 --- a/docmodel/source/theme/Theme.cxx +++ b/docmodel/source/theme/Theme.cxx @@ -18,6 +18,7 @@ #include <sal/log.hxx> #include <sal/types.h> #include <o3tl/enumrange.hxx> +#include <o3tl/underlyingenumvalue.hxx> #include <com/sun/star/util/Color.hpp> #include <com/sun/star/beans/PropertyValue.hpp> #include <com/sun/star/lang/IllegalArgumentException.hpp> @@ -84,58 +85,54 @@ void Theme::ToAny(uno::Any& rVal) const rVal <<= aMap.getAsConstPropertyValueList(); } +static std::shared_ptr<model::ColorSet> makeColorSet(const OUString& name, + const uno::Sequence<util::Color>& colors) +{ + auto colorset = std::make_shared<model::ColorSet>(name); + for (auto eThemeColorType : o3tl::enumrange<model::ThemeColorType>()) + { + const size_t nIndex(o3tl::to_underlying(eThemeColorType)); + if (nIndex >= colors.size()) + break; + const Color aColor(ColorTransparency, colors[nIndex]); + colorset->add(eThemeColorType, aColor); + } + return colorset; +} + std::shared_ptr<Theme> Theme::FromAny(const uno::Any& rVal) { if (css::uno::Reference<css::util::XTheme> xTheme; rVal >>= xTheme) { + if (!xTheme) + return {}; + if (auto* pUnoTheme = dynamic_cast<UnoTheme*>(xTheme.get())) return pUnoTheme->getTheme(); - else - throw css::lang::IllegalArgumentException(); + + auto pTheme = std::make_shared<Theme>(xTheme->getName()); + if (auto aColors = xTheme->getColorSet(); aColors.hasElements()) + pTheme->setColorSet(makeColorSet(xTheme->getName(), aColors)); // Reuse theme name + return pTheme; } comphelper::SequenceAsHashMap aMap(rVal); - std::shared_ptr<Theme> pTheme; - std::shared_ptr<model::ColorSet> pColorSet; - - auto it = aMap.find(u"Name"_ustr); - if (it != aMap.end()) - { - OUString aName; - it->second >>= aName; - pTheme = std::make_shared<Theme>(aName); - } - it = aMap.find(u"ColorSchemeName"_ustr); - if (it != aMap.end() && pTheme) - { - OUString aName; - it->second >>= aName; - pColorSet = std::make_shared<model::ColorSet>(aName); - pTheme->setColorSet(pColorSet); - } + OUString aThemeName; + if (auto it = aMap.find(u"Name"_ustr); it != aMap.end()) + it->second >>= aThemeName; + else + return {}; + auto pTheme = std::make_shared<Theme>(aThemeName); - it = aMap.find(u"ColorScheme"_ustr); - if (it != aMap.end() && pColorSet) + if (auto it = aMap.find(u"ColorSchemeName"_ustr); it != aMap.end()) { - uno::Sequence<util::Color> aColors; - it->second >>= aColors; + OUString aColorSchemeName; + it->second >>= aColorSchemeName; - SAL_WARN_IF(aColors.size() > 12, "svx", - "Theme::FromAny: number of colors greater than max theme colors supported"); - - for (auto eThemeColorType : o3tl::enumrange<model::ThemeColorType>()) - { - if (eThemeColorType != model::ThemeColorType::Unknown) - { - size_t nIndex(static_cast<sal_Int16>(eThemeColorType)); - if (nIndex < aColors.size()) - { - Color aColor(ColorTransparency, aColors[nIndex]); - pColorSet->add(eThemeColorType, aColor); - } - } - } + pTheme->setColorSet(makeColorSet( + aColorSchemeName, + aMap.getUnpackedValueOrDefault(u"ColorScheme"_ustr, uno::Sequence<util::Color>{}))); } return pTheme;
