diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2023-08-17 22:35:26 +0200 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2023-08-25 18:48:48 +0200 |
commit | 32400a791522d90c3b9b731b73c751b7967f660c (patch) | |
tree | edd0051040e0cee83dcf85f3df2e96a570c66b0a /svx | |
parent | b44581d9cbb103390f99f73167ec37960895f95f (diff) |
svx: refactor text color theme changing and add undo support
This refactors text theme color changing to use editeng directly
and also makes it possible to add undo support.
Some refactoring changes were also needed in Writer and Calc
ThemeColorChanger implementations.
Change-Id: I5cc21205aa3866f65f5b2132ecaa760292401263
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/155819
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'svx')
-rw-r--r-- | svx/source/theme/ThemeColorChangerCommon.cxx | 192 |
1 files changed, 120 insertions, 72 deletions
diff --git a/svx/source/theme/ThemeColorChangerCommon.cxx b/svx/source/theme/ThemeColorChangerCommon.cxx index db78e1c343b1..29d65cdda960 100644 --- a/svx/source/theme/ThemeColorChangerCommon.cxx +++ b/svx/source/theme/ThemeColorChangerCommon.cxx @@ -20,106 +20,154 @@ #include <com/sun/star/beans/XPropertySet.hpp> #include <com/sun/star/util/XComplexColor.hpp> +#include <svx/xlnclit.hxx> +#include <svx/xflclit.hxx> +#include <svx/xdef.hxx> +#include <editeng/eeitem.hxx> +#include <svx/svdundo.hxx> +#include <svx/svdmodel.hxx> +#include <svx/svdotext.hxx> + +#include <editeng/editeng.hxx> +#include <editeng/section.hxx> +#include <editeng/eeitem.hxx> + using namespace css; namespace svx::theme { namespace { -/// Updates text portion property colors -void updateTextPortionColorSet(model::ColorSet const& rColorSet, - const uno::Reference<beans::XPropertySet>& xPortion) +const SvxColorItem* getColorItem(const editeng::Section& rSection) { - if (!xPortion->getPropertySetInfo()->hasPropertyByName(UNO_NAME_EDIT_CHAR_COMPLEX_COLOR)) - { - return; - } - - uno::Reference<util::XComplexColor> xComplexColor; - xPortion->getPropertyValue(UNO_NAME_EDIT_CHAR_COMPLEX_COLOR) >>= xComplexColor; - if (!xComplexColor.is()) - return; + auto iterator = std::find_if( + rSection.maAttributes.begin(), rSection.maAttributes.end(), + [](const SfxPoolItem* pPoolItem) { return pPoolItem->Which() == EE_CHAR_COLOR; }); - auto aComplexColor = model::color::getFromXComplexColor(xComplexColor); - - if (!aComplexColor.isValidThemeType()) - return; - - Color aColor = rColorSet.resolveColor(aComplexColor); - xPortion->setPropertyValue(UNO_NAME_EDIT_CHAR_COLOR, uno::Any(static_cast<sal_Int32>(aColor))); + if (iterator != rSection.maAttributes.end()) + return static_cast<const SvxColorItem*>(*iterator); + return nullptr; } -/// Updates the fill property colors -void updateFillColorSet(model::ColorSet const& rColorSet, - const uno::Reference<beans::XPropertySet>& xShape) +bool updateEditEngTextSections(model::ColorSet const& rColorSet, SdrObject* pObject, SdrView* pView) { - if (!xShape->getPropertySetInfo()->hasPropertyByName(UNO_NAME_FILL_COMPLEX_COLOR)) - return; + SdrTextObj* pTextObject = DynCastSdrTextObj(pObject); - uno::Reference<util::XComplexColor> xComplexColor; - xShape->getPropertyValue(UNO_NAME_FILL_COMPLEX_COLOR) >>= xComplexColor; - if (!xComplexColor.is()) - return; + if (!pTextObject) + return false; - auto aComplexColor = model::color::getFromXComplexColor(xComplexColor); + pView->SdrBeginTextEdit(pTextObject); - if (!aComplexColor.isValidThemeType()) - return; + auto* pOutlinerView = pView->GetTextEditOutlinerView(); + if (!pOutlinerView) + return false; - Color aColor = rColorSet.resolveColor(aComplexColor); - xShape->setPropertyValue(UNO_NAME_FILLCOLOR, uno::Any(static_cast<sal_Int32>(aColor))); -} + auto* pEditEngine = pOutlinerView->GetEditView().GetEditEngine(); + if (!pEditEngine) + return false; -/// Updates the line property colors -void updateLineColorSet(model::ColorSet const& rColorSet, - const uno::Reference<beans::XPropertySet>& xShape) -{ - if (!xShape->getPropertySetInfo()->hasPropertyByName(UNO_NAME_LINE_COMPLEX_COLOR)) - return; + OutlinerParaObject* pOutlinerParagraphObject = pTextObject->GetOutlinerParaObject(); + if (pOutlinerParagraphObject) + { + const EditTextObject& rEditText = pOutlinerParagraphObject->GetTextObject(); + std::vector<editeng::Section> aSections; + rEditText.GetAllSections(aSections); - uno::Reference<util::XComplexColor> xComplexColor; - xShape->getPropertyValue(UNO_NAME_LINE_COMPLEX_COLOR) >>= xComplexColor; - if (!xComplexColor.is()) - return; + for (editeng::Section const& rSection : aSections) + { + const SvxColorItem* pItem = getColorItem(rSection); + if (!pItem) + continue; - auto aComplexColor = model::color::getFromXComplexColor(xComplexColor); + model::ComplexColor const& rComplexColor = pItem->getComplexColor(); + if (rComplexColor.isValidThemeType()) + { + SfxItemSet aSet(pEditEngine->GetAttribs(rSection.mnParagraph, rSection.mnStart, + rSection.mnEnd, + GetAttribsFlags::CHARATTRIBS)); + Color aNewColor = rColorSet.resolveColor(rComplexColor); + std::unique_ptr<SvxColorItem> pNewItem(pItem->Clone()); + pNewItem->setColor(aNewColor); + aSet.Put(*pNewItem); + + ESelection aSelection(rSection.mnParagraph, rSection.mnStart, rSection.mnParagraph, + rSection.mnEnd); + pEditEngine->QuickSetAttribs(aSet, aSelection); + } + } + } - if (!aComplexColor.isValidThemeType()) - return; + pView->SdrEndTextEdit(); - Color aColor = rColorSet.resolveColor(aComplexColor); - xShape->setPropertyValue(UNO_NAME_LINECOLOR, uno::Any(static_cast<sal_Int32>(aColor))); + return true; } -} // end anonymous namespace - -/// Updates properties of the SdrObject -void updateSdrObject(model::ColorSet const& rColorSet, SdrObject* pObject) +bool updateObjectAttributes(model::ColorSet const& rColorSet, SdrObject& rObject, + SfxUndoManager* pUndoManager) { - uno::Reference<drawing::XShape> xShape = pObject->getUnoShape(); - uno::Reference<text::XTextRange> xShapeText(xShape, uno::UNO_QUERY); - if (xShapeText.is()) + if (pUndoManager) + { + pUndoManager->AddUndoAction( + rObject.getSdrModelFromSdrObject().GetSdrUndoFactory().CreateUndoAttrObject( + rObject, true, true)); + } + bool bChanged = false; + auto aItemSet = rObject.GetMergedItemSet(); + + if (const XFillColorItem* pItem = aItemSet.GetItemIfSet(XATTR_FILLCOLOR, false)) { - // E.g. group shapes have no text. - uno::Reference<container::XEnumerationAccess> xText(xShapeText->getText(), uno::UNO_QUERY); - uno::Reference<container::XEnumeration> xParagraphs = xText->createEnumeration(); - while (xParagraphs->hasMoreElements()) + model::ComplexColor const& rComplexColor = pItem->getComplexColor(); + if (rComplexColor.isValidThemeType()) { - uno::Reference<container::XEnumerationAccess> xParagraph(xParagraphs->nextElement(), - uno::UNO_QUERY); - uno::Reference<container::XEnumeration> xPortions = xParagraph->createEnumeration(); - while (xPortions->hasMoreElements()) - { - uno::Reference<beans::XPropertySet> xPortion(xPortions->nextElement(), - uno::UNO_QUERY); - updateTextPortionColorSet(rColorSet, xPortion); - } + Color aNewColor = rColorSet.resolveColor(rComplexColor); + std::unique_ptr<XFillColorItem> pNewItem(pItem->Clone()); + pNewItem->SetColorValue(aNewColor); + aItemSet.Put(*pNewItem); + bChanged = true; + } + } + if (const XLineColorItem* pItem = aItemSet.GetItemIfSet(XATTR_LINECOLOR, false)) + { + model::ComplexColor const& rComplexColor = pItem->getComplexColor(); + if (rComplexColor.isValidThemeType()) + { + Color aNewColor = rColorSet.resolveColor(rComplexColor); + std::unique_ptr<XLineColorItem> pNewItem(pItem->Clone()); + pNewItem->SetColorValue(aNewColor); + aItemSet.Put(*pNewItem); + bChanged = true; + } + } + if (const SvxColorItem* pItem = aItemSet.GetItemIfSet(EE_CHAR_COLOR, false)) + { + model::ComplexColor const& rComplexColor = pItem->getComplexColor(); + if (rComplexColor.isValidThemeType()) + { + Color aNewColor = rColorSet.resolveColor(rComplexColor); + std::unique_ptr<SvxColorItem> pNewItem(pItem->Clone()); + pNewItem->setColor(aNewColor); + aItemSet.Put(*pNewItem); + bChanged = true; } } + if (bChanged) + { + rObject.SetMergedItemSetAndBroadcast(aItemSet); + } + return bChanged; +} + +} // end anonymous namespace + +/// Updates properties of the SdrObject +void updateSdrObject(model::ColorSet const& rColorSet, SdrObject* pObject, SdrView* pView, + SfxUndoManager* pUndoManager) +{ + if (!pObject) + return; - uno::Reference<beans::XPropertySet> xShapeProps(xShape, uno::UNO_QUERY); - updateFillColorSet(rColorSet, xShapeProps); - updateLineColorSet(rColorSet, xShapeProps); + updateEditEngTextSections(rColorSet, pObject, pView); + updateObjectAttributes(rColorSet, *pObject, pUndoManager); } } // end svx::theme namespace |