1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 #include "TitleItemConverter.hxx"
27 #include "SchWhichPairs.hxx"
28 #include "macros.hxx"
29 #include "ItemPropertyMap.hxx"
30 #include "GraphicPropertyItemConverter.hxx"
31 #include "CharacterPropertyItemConverter.hxx"
32 #include "MultipleItemConverter.hxx"
33 #include <svl/intitem.hxx>
34 #include <rtl/math.hxx>
35 
36 #include <com/sun/star/chart2/XTitled.hpp>
37 
38 #include <functional>
39 #include <algorithm>
40 
41 using namespace ::com::sun::star;
42 
43 namespace
44 {
lcl_GetTitlePropertyMap()45 ::comphelper::ItemPropertyMapType & lcl_GetTitlePropertyMap()
46 {
47     static ::comphelper::ItemPropertyMapType aTitlePropertyMap(
48         ::comphelper::MakeItemPropertyMap
49         IPM_MAP_ENTRY( SCHATTR_TEXT_STACKED, "StackCharacters", 0 )
50         );
51 
52     return aTitlePropertyMap;
53 };
54 } // anonymous namespace
55 
56 namespace chart
57 {
58 namespace wrapper
59 {
60 
61 // ========================================
62 
63 class FormattedStringsConverter : public ::comphelper::MultipleItemConverter
64 {
65 public:
66     FormattedStringsConverter(
67         const uno::Sequence< uno::Reference< chart2::XFormattedString > > & aStrings,
68         SfxItemPool & rItemPool,
69         ::std::auto_ptr< awt::Size > pRefSize,
70         const uno::Reference< beans::XPropertySet > & xParentProp );
71     virtual ~FormattedStringsConverter();
72 
73 protected:
74     virtual const sal_uInt16 * GetWhichPairs() const;
75 };
76 
77 // ----------------------------------------
78 
FormattedStringsConverter(const uno::Sequence<uno::Reference<chart2::XFormattedString>> & aStrings,SfxItemPool & rItemPool,::std::auto_ptr<::com::sun::star::awt::Size> pRefSize,const uno::Reference<beans::XPropertySet> & xParentProp)79 FormattedStringsConverter::FormattedStringsConverter(
80     const uno::Sequence< uno::Reference< chart2::XFormattedString > > & aStrings,
81     SfxItemPool & rItemPool,
82     ::std::auto_ptr< ::com::sun::star::awt::Size > pRefSize,
83     const uno::Reference< beans::XPropertySet > & xParentProp ) :
84         MultipleItemConverter( rItemPool )
85 {
86     bool bHasRefSize = (pRefSize.get() && xParentProp.is());
87     for( sal_Int32 i = 0; i < aStrings.getLength(); ++i )
88     {
89         uno::Reference< beans::XPropertySet > xProp( aStrings[ i ], uno::UNO_QUERY );
90         if( xProp.is())
91         {
92             if( bHasRefSize )
93                 m_aConverters.push_back( new CharacterPropertyItemConverter(
94                                              xProp, rItemPool,
95                                              ::std::auto_ptr< awt::Size >( new awt::Size( *pRefSize )),
96                                              C2U( "ReferencePageSize" ),
97                                              xParentProp ));
98             else
99                 m_aConverters.push_back( new CharacterPropertyItemConverter( xProp, rItemPool ));
100         }
101     }
102 }
103 
~FormattedStringsConverter()104 FormattedStringsConverter::~FormattedStringsConverter()
105 {
106 }
107 
GetWhichPairs() const108 const sal_uInt16 * FormattedStringsConverter::GetWhichPairs() const
109 {
110     return nCharacterPropertyWhichPairs;
111 }
112 
113 // ========================================
114 
TitleItemConverter(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> & rPropertySet,SfxItemPool & rItemPool,SdrModel & rDrawModel,const uno::Reference<lang::XMultiServiceFactory> & xNamedPropertyContainerFactory,::std::auto_ptr<::com::sun::star::awt::Size> pRefSize)115 TitleItemConverter::TitleItemConverter(
116     const ::com::sun::star::uno::Reference<
117     ::com::sun::star::beans::XPropertySet > & rPropertySet,
118     SfxItemPool& rItemPool,
119     SdrModel& rDrawModel,
120     const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory,
121     ::std::auto_ptr< ::com::sun::star::awt::Size > pRefSize ) :
122         ItemConverter( rPropertySet, rItemPool )
123 {
124     m_aConverters.push_back( new GraphicPropertyItemConverter(
125                                  rPropertySet, rItemPool, rDrawModel,
126                                  xNamedPropertyContainerFactory,
127                                  GraphicPropertyItemConverter::LINE_AND_FILL_PROPERTIES ));
128 
129     // CharacterProperties are not at the title but at its contained XFormattedString objects
130     // take the first formatted string in the sequence
131     uno::Reference< chart2::XTitle > xTitle( rPropertySet, uno::UNO_QUERY );
132     if( xTitle.is())
133     {
134         uno::Sequence< uno::Reference< chart2::XFormattedString > > aStringSeq( xTitle->getText());
135         if( aStringSeq.getLength() > 0 )
136         {
137             m_aConverters.push_back(
138                 new FormattedStringsConverter( aStringSeq, rItemPool, pRefSize, rPropertySet ));
139         }
140     }
141 }
142 
~TitleItemConverter()143 TitleItemConverter::~TitleItemConverter()
144 {
145     ::std::for_each( m_aConverters.begin(), m_aConverters.end(),
146                      ::comphelper::DeleteItemConverterPtr() );
147 }
148 
FillItemSet(SfxItemSet & rOutItemSet) const149 void TitleItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
150 {
151     ::std::for_each( m_aConverters.begin(), m_aConverters.end(),
152                      ::comphelper::FillItemSetFunc( rOutItemSet ));
153 
154     // own items
155     ItemConverter::FillItemSet( rOutItemSet );
156 }
157 
ApplyItemSet(const SfxItemSet & rItemSet)158 bool TitleItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
159 {
160     bool bResult = false;
161 
162     ::std::for_each( m_aConverters.begin(), m_aConverters.end(),
163                      ::comphelper::ApplyItemSetFunc( rItemSet, bResult ));
164 
165     // own items
166     return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
167 }
168 
GetWhichPairs() const169 const sal_uInt16 * TitleItemConverter::GetWhichPairs() const
170 {
171     // must span all used items!
172     return nTitleWhichPairs;
173 }
174 
GetItemProperty(tWhichIdType nWhichId,tPropertyNameWithMemberId & rOutProperty) const175 bool TitleItemConverter::GetItemProperty( tWhichIdType nWhichId, tPropertyNameWithMemberId & rOutProperty ) const
176 {
177     ::comphelper::ItemPropertyMapType & rMap( lcl_GetTitlePropertyMap());
178     ::comphelper::ItemPropertyMapType::const_iterator aIt( rMap.find( nWhichId ));
179 
180     if( aIt == rMap.end())
181         return false;
182 
183     rOutProperty =(*aIt).second;
184     return true;
185 }
186 
187 
ApplySpecialItem(sal_uInt16 nWhichId,const SfxItemSet & rItemSet)188 bool TitleItemConverter::ApplySpecialItem(
189     sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
190     throw( uno::Exception )
191 {
192     bool bChanged = false;
193 
194     switch( nWhichId )
195     {
196         case SCHATTR_TEXT_DEGREES:
197         {
198             // convert int to double (divided by 100)
199             double fVal = static_cast< double >(
200                 static_cast< const SfxInt32Item & >(
201                     rItemSet.Get( nWhichId )).GetValue()) / 100.0;
202             double fOldVal = 0.0;
203             bool bPropExisted =
204                 ( GetPropertySet()->getPropertyValue( C2U( "TextRotation" )) >>= fOldVal );
205 
206             if( ! bPropExisted ||
207                 ( bPropExisted && fOldVal != fVal ))
208             {
209                 GetPropertySet()->setPropertyValue( C2U( "TextRotation" ), uno::makeAny( fVal ));
210                 bChanged = true;
211             }
212         }
213         break;
214     }
215 
216     return bChanged;
217 }
218 
FillSpecialItem(sal_uInt16 nWhichId,SfxItemSet & rOutItemSet) const219 void TitleItemConverter::FillSpecialItem(
220     sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
221     throw( uno::Exception )
222 {
223     switch( nWhichId )
224     {
225         case SCHATTR_TEXT_DEGREES:
226         {
227             // convert double to int (times 100)
228             double fVal = 0;
229 
230             if( GetPropertySet()->getPropertyValue( C2U( "TextRotation" )) >>= fVal )
231             {
232                 rOutItemSet.Put( SfxInt32Item( nWhichId, static_cast< sal_Int32 >(
233                                                    ::rtl::math::round( fVal * 100.0 ) ) ));
234             }
235         }
236         break;
237    }
238 }
239 
240 } //  namespace wrapper
241 } //  namespace chart
242