send theme info when changing color (in the picker) via UNO command

Change-Id: I288f8fb3375e152b5ee746fab2c05d08150d6c99
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/146817
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
This commit is contained in:
Tomaž Vajngerl
2023-02-11 11:24:05 +09:00
committed by Tomaž Vajngerl
parent 79434c3215
commit 855f7c08d1
10 changed files with 204 additions and 24 deletions

View File

@@ -14,6 +14,7 @@ $(eval $(call gb_Library_add_exception_objects,docmodel,\
docmodel/source/uno/UnoTheme \
docmodel/source/theme/ColorSet \
docmodel/source/theme/Theme \
docmodel/source/theme/ThemeColorJSON \
))
$(eval $(call gb_Library_set_include,docmodel,\
@@ -22,7 +23,8 @@ $(eval $(call gb_Library_set_include,docmodel,\
))
$(eval $(call gb_Library_use_externals,docmodel,\
libxml2 \
libxml2 \
boost_headers \
))
$(eval $(call gb_Library_add_defs,docmodel,\

View File

@@ -0,0 +1,103 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#include <docmodel/theme/ThemeColorJSON.hxx>
#include <sstream>
#include <utility>
#include <sal/log.hxx>
#include <boost/property_tree/json_parser.hpp>
namespace model::theme
{
bool convertFromJSON(OString const& rJsonString, model::ThemeColor& rThemeColor)
{
model::ThemeColor aThemeColor;
std::stringstream aStream(rJsonString.getStr());
boost::property_tree::ptree aRootTree;
try
{
boost::property_tree::read_json(aStream, aRootTree);
}
catch (const boost::property_tree::json_parser_error& /*exception*/)
{
return false;
}
sal_Int32 nThemeType = aRootTree.get<sal_Int32>("ThemeIndex", -1);
aThemeColor.setType(model::convertToThemeColorType(nThemeType));
boost::property_tree::ptree aTransformTree = aRootTree.get_child("Transformations");
for (const auto& rEachTransformationNode :
boost::make_iterator_range(aTransformTree.equal_range("")))
{
auto const& rTransformationTree = rEachTransformationNode.second;
std::string sType = rTransformationTree.get<std::string>("Type", "");
sal_Int16 nValue = rTransformationTree.get<sal_Int16>("Value", 0);
auto eType = model::TransformationType::Undefined;
if (sType == "LumOff")
eType = model::TransformationType::LumOff;
else if (sType == "LumMod")
eType = model::TransformationType::LumMod;
else if (sType == "Tint")
eType = model::TransformationType::Tint;
else if (sType == "Shade")
eType = model::TransformationType::Shade;
if (eType != model::TransformationType::Undefined)
aThemeColor.addTransformation({ eType, nValue });
}
rThemeColor = aThemeColor;
return true;
}
OString convertToJSON(model::ThemeColor const& rThemeColor)
{
boost::property_tree::ptree aTree;
aTree.put("ThemeIndex", sal_Int16(rThemeColor.getType()));
boost::property_tree::ptree aTransformationsList;
for (auto const& rTransformation : rThemeColor.getTransformations())
{
std::string aType;
switch (rTransformation.meType)
{
case model::TransformationType::LumMod:
aType = "LumMod";
break;
case model::TransformationType::LumOff:
aType = "LumOff";
break;
case model::TransformationType::Tint:
aType = "Tint";
break;
case model::TransformationType::Shade:
aType = "Shade";
break;
default:
break;
}
if (!aType.empty())
{
boost::property_tree::ptree aChild;
aChild.put("Type", aType);
aChild.put("Value", rTransformation.mnValue);
aTransformationsList.push_back(std::make_pair("", aChild));
}
}
aTree.add_child("Transformations", aTransformationsList);
std::stringstream aStream;
boost::property_tree::write_json(aStream, aTree);
return OString(aStream.str().c_str());
}
} // end model::theme
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@@ -78,6 +78,7 @@
#include <editeng/itemtype.hxx>
#include <editeng/eerdll.hxx>
#include <docmodel/uno/UnoThemeColor.hxx>
#include <docmodel/theme/ThemeColorJSON.hxx>
#include <libxml/xmlwriter.h>
using namespace ::com::sun::star;
@@ -1367,6 +1368,13 @@ SvxColorItem::SvxColorItem( const Color& rCol, const sal_uInt16 nId ) :
{
}
SvxColorItem::SvxColorItem(Color const& rColor, model::ThemeColor const& rThemeColor, const sal_uInt16 nId)
: SfxPoolItem(nId)
, mColor(rColor)
, maThemeColor(rThemeColor)
{
}
SvxColorItem::~SvxColorItem()
{
}
@@ -1436,12 +1444,18 @@ bool SvxColorItem::QueryValue( uno::Any& rVal, sal_uInt8 nMemberId ) const
rVal <<= nValue;
break;
}
case MID_COLOR_THEME_REFERENCE:
case MID_COLOR_THEME_REFERENCE:
{
auto xThemeColor = model::theme::createXThemeColor(maThemeColor);
rVal <<= xThemeColor;
break;
}
case MID_COLOR_THEME_REFERENCE_JSON:
{
rVal <<= OStringToOUString(model::theme::convertToJSON(maThemeColor), RTL_TEXTENCODING_UTF8);
break;
}
case MID_COLOR_RGB:
default:
{
rVal <<= mColor;
@@ -1528,6 +1542,23 @@ bool SvxColorItem::PutValue( const uno::Any& rVal, sal_uInt8 nMemberId )
}
}
break;
case MID_COLOR_THEME_REFERENCE_JSON:
{
OUString sThemeJson;
if (!(rVal >>= sThemeJson))
return false;
if (sThemeJson.isEmpty())
{
return false;
}
OString aJSON = OUStringToOString(sThemeJson, RTL_TEXTENCODING_ASCII_US);
model::theme::convertFromJSON(aJSON, maThemeColor);
}
break;
case MID_COLOR_RGB:
default:
{
return rVal >>= mColor;

View File

@@ -0,0 +1,23 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#pragma once
#include <docmodel/dllapi.h>
#include <docmodel/theme/ThemeColor.hxx>
namespace model::theme
{
DOCMODEL_DLLPUBLIC OString convertToJSON(model::ThemeColor const& rThemeColor);
DOCMODEL_DLLPUBLIC bool convertFromJSON(OString const& rJsonString, model::ThemeColor& rThemeColor);
} // end of namespace model
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@@ -39,6 +39,7 @@ public:
explicit SvxColorItem(const sal_uInt16 nId);
SvxColorItem(const Color& aColor, const sal_uInt16 nId);
SvxColorItem(const Color& aColor, model::ThemeColor const& rThemeColor, const sal_uInt16 nId);
virtual ~SvxColorItem() override;
// "pure virtual Methods" from SfxPoolItem
@@ -63,6 +64,11 @@ public:
const model::ThemeColor& GetThemeColor() const { return maThemeColor; }
void setThemeColor(model::ThemeColor const& rThemeColor)
{
maThemeColor = rThemeColor;
}
void dumpAsXml(xmlTextWriterPtr pWriter) const override;
};

View File

@@ -189,6 +189,7 @@
#define MID_COLOR_LUM_MOD 6
#define MID_COLOR_LUM_OFF 7
#define MID_COLOR_THEME_REFERENCE 8
#define MID_COLOR_THEME_REFERENCE_JSON 9
#endif

View File

@@ -449,9 +449,10 @@ class XFillGradientItem;
#define SID_ATTR_TEXTCOLUMNS_NUMBER ( SID_SVX_START + 340 )
#define SID_ATTR_TEXTCOLUMNS_SPACING ( SID_SVX_START + 341 )
#define SID_ATTR_COLOR_THEME_INDEX TypedWhichId<SfxInt16Item>( SID_SVX_START + 342 )
#define SID_ATTR_COLOR_LUM_MOD TypedWhichId<SfxInt16Item>( SID_SVX_START + 343 )
#define SID_ATTR_COLOR_LUM_OFF TypedWhichId<SfxInt16Item>( SID_SVX_START + 344 )
#define SID_ATTR_COLOR_THEME_REFERENCE TypedWhichId<SvxColorItem>( SID_SVX_START + 342 )
#define SID_ATTR_COLOR_THEME_INDEX TypedWhichId<SfxInt16Item>( SID_SVX_START + 343 )
#define SID_ATTR_COLOR_LUM_MOD TypedWhichId<SfxInt16Item>( SID_SVX_START + 344 )
#define SID_ATTR_COLOR_LUM_OFF TypedWhichId<SfxInt16Item>( SID_SVX_START + 345 )
#define SID_SB_CONNECTIONPOOLING ( SID_SVX_START + 348 )
#define SID_SB_DBREGISTEROPTIONS ( SID_SVX_START + 349 )
@@ -683,7 +684,6 @@ class XFillGradientItem;
#define SID_PARAGRAPH_CHANGE_STATE ( SID_SVX_START + 754 )
#define SID_ATTR_SHADOW_BLUR ( SID_SVX_START + 755 )
//FREE
//FREE
//FREE

View File

@@ -169,7 +169,14 @@ item INT16 SvxParaVertAlignItem;
item INT16 SvxCharReliefItem;
item BOOL SvxBlinkItem;
item BOOL SvxAutoKernItem;
item INT32 SvxColorItem;
struct SvxColor
{
INT32 Color MID_COLOR_RGB;
String ThemeReferenceJSON MID_COLOR_THEME_REFERENCE_JSON;
};
item SvxColor SvxColorItem;
item BOOL SvxContourItem;
item INT16 SvxFormatBreakItem; // enum
item BOOL SvxFormatKeepItem;

View File

@@ -41,6 +41,11 @@
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/util/XURLTransformer.hpp>
#include <com/sun/star/util/URLTransformer.hpp>
#include <docmodel/theme/ThemeColor.hxx>
#include <docmodel/theme/ThemeColorJSON.hxx>
#include <editeng/colritem.hxx>
#include <svx/svxids.hrc>
#include <editeng/memberids.h>
#include <palettes.hxx>
@@ -420,6 +425,7 @@ void PaletteManager::PopupColorPicker(weld::Window* pParent, const OUString& aCo
void PaletteManager::DispatchColorCommand(const OUString& aCommand, const svx::NamedThemedColor& rColor)
{
using namespace css;
using namespace css::uno;
using namespace css::frame;
using namespace css::beans;
@@ -435,13 +441,22 @@ void PaletteManager::DispatchColorCommand(const OUString& aCommand, const svx::N
INetURLObject aObj( aCommand );
std::vector<PropertyValue> aArgs{
comphelper::makePropertyValue(aObj.GetURLPath(), sal_Int32(rColor.m_aColor)),
comphelper::makePropertyValue(aObj.GetURLPath()+ ".Color", sal_Int32(rColor.m_aColor)),
};
if (rColor.m_nThemeIndex != -1)
{
aArgs.push_back(comphelper::makePropertyValue("ColorThemeIndex", rColor.m_nThemeIndex));
aArgs.push_back(comphelper::makePropertyValue("ColorLumMod", rColor.m_nLumMod));
aArgs.push_back(comphelper::makePropertyValue("ColorLumOff", rColor.m_nLumOff));
model::ThemeColor aThemeColor;
aThemeColor.setType(model::convertToThemeColorType(rColor.m_nThemeIndex));
if (rColor.m_nLumMod != 10000)
aThemeColor.addTransformation({model::TransformationType::LumMod, rColor.m_nLumMod});
if (rColor.m_nLumMod != 0)
aThemeColor.addTransformation({model::TransformationType::LumOff, rColor.m_nLumOff});
uno::Any aAny;
aAny <<= OStringToOUString(model::theme::convertToJSON(aThemeColor), RTL_TEXTENCODING_UTF8);
aArgs.push_back(comphelper::makePropertyValue(aObj.GetURLPath() + ".ThemeReferenceJSON", aAny));
}
URL aTargetURL;

View File

@@ -1672,26 +1672,18 @@ void SwTextShell::Execute(SfxRequest &rReq)
case SID_ATTR_CHAR_COLOR2:
{
Color aSet;
bool bHasItem = false;
if(pItem)
{
aSet = static_cast<const SvxColorItem*>(pItem)->GetValue();
bHasItem = true;
}
if (bHasItem)
if (pItem)
{
auto* pColorItem = static_cast<const SvxColorItem*>(pItem);
SwEditWin& rEditWin = GetView().GetEditWin();
rEditWin.SetWaterCanTextColor(aSet);
rEditWin.SetWaterCanTextColor(pColorItem->GetValue());
SwApplyTemplate* pApply = rEditWin.GetApplyTemplate();
// If there is a selection, then set the color on it
// otherwise, it'll be the color for the next text to be typed
if(!pApply || pApply->nColor != SID_ATTR_CHAR_COLOR_EXT)
if (!pApply || pApply->nColor != SID_ATTR_CHAR_COLOR_EXT)
{
rWrtSh.SetAttrItem(SvxColorItem (aSet, RES_CHRATR_COLOR));
rWrtSh.SetAttrItem(SvxColorItem(pColorItem->GetValue(), pColorItem->GetThemeColor(), RES_CHRATR_COLOR));
}
rReq.Done();