Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / itemsetwrapper / TitleItemConverter.cxx
blob7a3f57af5703962e6b54ccacd2dce3677d8e3272
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <TitleItemConverter.hxx>
21 #include "SchWhichPairs.hxx"
22 #include <ItemPropertyMap.hxx>
23 #include <GraphicPropertyItemConverter.hxx>
24 #include <CharacterPropertyItemConverter.hxx>
25 #include <MultipleItemConverter.hxx>
26 #include <svx/sdangitm.hxx>
27 #include <rtl/math.hxx>
29 #include <com/sun/star/chart2/XTitle.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <memory>
34 using namespace ::com::sun::star;
36 namespace chart::wrapper {
38 namespace {
40 ItemPropertyMapType & lcl_GetTitlePropertyMap()
42 static ItemPropertyMapType aTitlePropertyMap{
43 {SCHATTR_TEXT_STACKED, {"StackCharacters", 0}}};
44 return aTitlePropertyMap;
47 class FormattedStringsConverter : public MultipleItemConverter
49 public:
50 FormattedStringsConverter(
51 const uno::Sequence< uno::Reference< chart2::XFormattedString > > & aStrings,
52 SfxItemPool & rItemPool,
53 const awt::Size* pRefSize,
54 const uno::Reference< beans::XPropertySet > & xParentProp );
56 protected:
57 virtual const WhichRangesContainer& GetWhichPairs() const override;
60 } // anonymous namespace
62 FormattedStringsConverter::FormattedStringsConverter(
63 const uno::Sequence< uno::Reference< chart2::XFormattedString > > & aStrings,
64 SfxItemPool & rItemPool,
65 const awt::Size* pRefSize,
66 const uno::Reference< beans::XPropertySet > & xParentProp ) :
67 MultipleItemConverter( rItemPool )
69 bool bHasRefSize = (pRefSize && xParentProp.is());
70 for( uno::Reference< chart2::XFormattedString > const & formattedStr : aStrings )
72 uno::Reference< beans::XPropertySet > xProp( formattedStr, uno::UNO_QUERY );
73 if( xProp.is())
75 if( bHasRefSize )
76 m_aConverters.emplace_back(
77 new CharacterPropertyItemConverter(
78 xProp, rItemPool, pRefSize, "ReferencePageSize", xParentProp));
79 else
80 m_aConverters.emplace_back( new CharacterPropertyItemConverter( xProp, rItemPool ));
85 const WhichRangesContainer& FormattedStringsConverter::GetWhichPairs() const
87 return nCharacterPropertyWhichPairs;
90 TitleItemConverter::TitleItemConverter(
91 const uno::Reference<beans::XPropertySet> & rPropertySet,
92 SfxItemPool& rItemPool,
93 SdrModel& rDrawModel,
94 const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory,
95 const awt::Size* pRefSize ) :
96 ItemConverter( rPropertySet, rItemPool )
98 m_aConverters.emplace_back( new GraphicPropertyItemConverter(
99 rPropertySet, rItemPool, rDrawModel,
100 xNamedPropertyContainerFactory,
101 GraphicObjectType::LineAndFillProperties ));
103 // CharacterProperties are not at the title but at its contained XFormattedString objects
104 // take the first formatted string in the sequence
105 uno::Reference< chart2::XTitle > xTitle( rPropertySet, uno::UNO_QUERY );
106 if( xTitle.is())
108 uno::Sequence< uno::Reference< chart2::XFormattedString > > aStringSeq( xTitle->getText());
109 if( aStringSeq.hasElements() )
111 m_aConverters.emplace_back(
112 new FormattedStringsConverter( aStringSeq, rItemPool, pRefSize, rPropertySet ));
117 TitleItemConverter::~TitleItemConverter()
121 void TitleItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
123 for( const auto& pConv : m_aConverters )
124 pConv->FillItemSet( rOutItemSet );
126 // own items
127 ItemConverter::FillItemSet( rOutItemSet );
130 bool TitleItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
132 bool bResult = false;
134 for( const auto& pConv : m_aConverters )
135 bResult = pConv->ApplyItemSet( rItemSet ) || bResult;
137 // own items
138 return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
141 const WhichRangesContainer& TitleItemConverter::GetWhichPairs() const
143 // must span all used items!
144 return nTitleWhichPairs;
147 bool TitleItemConverter::GetItemProperty( tWhichIdType nWhichId, tPropertyNameWithMemberId & rOutProperty ) const
149 ItemPropertyMapType & rMap( lcl_GetTitlePropertyMap());
150 ItemPropertyMapType::const_iterator aIt( rMap.find( nWhichId ));
152 if( aIt == rMap.end())
153 return false;
155 rOutProperty =(*aIt).second;
156 return true;
159 bool TitleItemConverter::ApplySpecialItem(
160 sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
162 bool bChanged = false;
164 switch( nWhichId )
166 case SCHATTR_TEXT_DEGREES:
168 // convert int to double (divided by 100)
169 double fVal = static_cast< double >(
170 static_cast< const SdrAngleItem & >(
171 rItemSet.Get( nWhichId )).GetValue().get()) / 100.0;
172 double fOldVal = 0.0;
173 bool bPropExisted =
174 ( GetPropertySet()->getPropertyValue( "TextRotation" ) >>= fOldVal );
176 if( ! bPropExisted || fOldVal != fVal )
178 GetPropertySet()->setPropertyValue( "TextRotation" , uno::Any( fVal ));
179 bChanged = true;
182 break;
185 return bChanged;
188 void TitleItemConverter::FillSpecialItem(
189 sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
191 switch( nWhichId )
193 case SCHATTR_TEXT_DEGREES:
195 // convert double to int (times 100)
196 double fVal = 0;
198 if( GetPropertySet()->getPropertyValue( "TextRotation" ) >>= fVal )
200 rOutItemSet.Put( SdrAngleItem( SCHATTR_TEXT_DEGREES, Degree100(static_cast< sal_Int32 >(
201 ::rtl::math::round( fVal * 100.0 ) ) )));
204 break;
208 } // namespace chart::wrapper
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */