1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <LegendItemConverter.hxx>
21 #include "SchWhichPairs.hxx"
22 #include <GraphicPropertyItemConverter.hxx>
23 #include <CharacterPropertyItemConverter.hxx>
24 #include <com/sun/star/chart2/LegendPosition.hpp>
25 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 
28 #include <svl/intitem.hxx>
29 #include <svl/eitem.hxx>
30 #include <tools/diagnose_ex.h>
31 
32 #include <memory>
33 
34 using namespace ::com::sun::star;
35 
36 namespace chart::wrapper
37 {
38 
LegendItemConverter(const css::uno::Reference<css::beans::XPropertySet> & rPropertySet,SfxItemPool & rItemPool,SdrModel & rDrawModel,const uno::Reference<lang::XMultiServiceFactory> & xNamedPropertyContainerFactory,const awt::Size * pRefSize)39 LegendItemConverter::LegendItemConverter(
40     const css::uno::Reference< css::beans::XPropertySet > & rPropertySet,
41     SfxItemPool& rItemPool,
42     SdrModel& rDrawModel,
43     const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory,
44     const awt::Size* pRefSize ) :
45         ItemConverter( rPropertySet, rItemPool )
46 {
47     m_aConverters.emplace_back( new GraphicPropertyItemConverter(
48                                  rPropertySet, rItemPool, rDrawModel, xNamedPropertyContainerFactory,
49                                  GraphicObjectType::LineAndFillProperties ));
50     m_aConverters.emplace_back( new CharacterPropertyItemConverter(
51                                  rPropertySet, rItemPool, pRefSize,
52                                  "ReferencePageSize" ));
53 }
54 
~LegendItemConverter()55 LegendItemConverter::~LegendItemConverter()
56 {
57 }
58 
FillItemSet(SfxItemSet & rOutItemSet) const59 void LegendItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
60 {
61     for( const auto& pConv : m_aConverters )
62         pConv->FillItemSet( rOutItemSet );
63 
64     // own items
65     ItemConverter::FillItemSet( rOutItemSet );
66 }
67 
ApplyItemSet(const SfxItemSet & rItemSet)68 bool LegendItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
69 {
70     bool bResult = false;
71 
72     for( const auto& pConv : m_aConverters )
73         bResult = pConv->ApplyItemSet( rItemSet ) || bResult;
74 
75     // own items
76     return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
77 }
78 
GetWhichPairs() const79 const sal_uInt16 * LegendItemConverter::GetWhichPairs() const
80 {
81     // must span all used items!
82     return nLegendWhichPairs;
83 }
84 
GetItemProperty(tWhichIdType,tPropertyNameWithMemberId &) const85 bool LegendItemConverter::GetItemProperty( tWhichIdType /*nWhichId*/, tPropertyNameWithMemberId & /*rOutProperty*/ ) const
86 {
87     // No own (non-special) properties
88     return false;
89 }
90 
ApplySpecialItem(sal_uInt16 nWhichId,const SfxItemSet & rInItemSet)91 bool LegendItemConverter::ApplySpecialItem( sal_uInt16 nWhichId, const SfxItemSet& rInItemSet )
92 {
93     bool bChanged = false;
94 
95     switch( nWhichId )
96     {
97         case SCHATTR_LEGEND_SHOW:
98         {
99             const SfxPoolItem* pPoolItem = nullptr;
100             if( rInItemSet.GetItemState( SCHATTR_LEGEND_SHOW, true, &pPoolItem ) == SfxItemState::SET )
101             {
102                 bool bShow = static_cast< const SfxBoolItem * >( pPoolItem )->GetValue();
103                 bool bWasShown = true;
104                 if( ! (GetPropertySet()->getPropertyValue( "Show" ) >>= bWasShown) ||
105                     ( bWasShown != bShow ))
106                 {
107                     GetPropertySet()->setPropertyValue( "Show" , uno::Any( bShow ));
108                     bChanged = true;
109                 }
110             }
111 
112         }
113         break;
114         case SCHATTR_LEGEND_POS:
115         {
116             const SfxPoolItem* pPoolItem = nullptr;
117             if( rInItemSet.GetItemState( SCHATTR_LEGEND_POS, true, &pPoolItem ) == SfxItemState::SET )
118             {
119                 chart2::LegendPosition eNewPos = static_cast<chart2::LegendPosition>(static_cast<const SfxInt32Item*>(pPoolItem)->GetValue());
120 
121                 css::chart::ChartLegendExpansion eExpansion = css::chart::ChartLegendExpansion_HIGH;
122                 switch( eNewPos )
123                 {
124                     case chart2::LegendPosition_LINE_START:
125                     case chart2::LegendPosition_LINE_END:
126                         eExpansion = css::chart::ChartLegendExpansion_HIGH;
127                         break;
128                     case chart2::LegendPosition_PAGE_START:
129                     case chart2::LegendPosition_PAGE_END:
130                         eExpansion = css::chart::ChartLegendExpansion_WIDE;
131                         break;
132                     default:
133                         break;
134                 }
135 
136                 try
137                 {
138                     chart2::LegendPosition eOldPos;
139                     if( ! ( GetPropertySet()->getPropertyValue( "AnchorPosition" ) >>= eOldPos ) ||
140                         ( eOldPos != eNewPos ))
141                     {
142                         GetPropertySet()->setPropertyValue( "AnchorPosition" , uno::Any( eNewPos ));
143                         GetPropertySet()->setPropertyValue( "Expansion" , uno::Any( eExpansion ));
144                         GetPropertySet()->setPropertyValue( "RelativePosition" , uno::Any());
145                         bChanged = true;
146                     }
147                 }
148                 catch( const uno::Exception & )
149                 {
150                     DBG_UNHANDLED_EXCEPTION("chart2");
151                 }
152             }
153         }
154         break;
155         case SCHATTR_LEGEND_NO_OVERLAY:
156         {
157             const SfxPoolItem* pPoolItem = nullptr;
158             if(rInItemSet.GetItemState(SCHATTR_LEGEND_NO_OVERLAY, true, &pPoolItem) == SfxItemState::SET)
159             {
160                 bool bOverlay = !static_cast<const SfxBoolItem *>(pPoolItem)->GetValue();
161                 bool bOldOverlay = false;
162                 if(!(GetPropertySet()->getPropertyValue("Overlay") >>= bOldOverlay) ||
163                     (bOldOverlay != bOverlay))
164                 {
165                     GetPropertySet()->setPropertyValue("Overlay", uno::Any(bOverlay));
166                     bChanged = true;
167                 }
168             }
169 
170         }
171         break;
172     }
173 
174     return bChanged;
175 }
176 
FillSpecialItem(sal_uInt16 nWhichId,SfxItemSet & rOutItemSet) const177 void LegendItemConverter::FillSpecialItem(
178     sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
179 {
180     switch( nWhichId )
181     {
182         case SCHATTR_LEGEND_SHOW:
183         {
184             bool bShow = true;
185             GetPropertySet()->getPropertyValue( "Show" ) >>= bShow;
186             rOutItemSet.Put( SfxBoolItem(SCHATTR_LEGEND_SHOW, bShow) );
187         }
188         break;
189         case SCHATTR_LEGEND_POS:
190         {
191             chart2::LegendPosition eLegendPos( chart2::LegendPosition_LINE_END );
192             GetPropertySet()->getPropertyValue( "AnchorPosition" ) >>= eLegendPos;
193             rOutItemSet.Put( SfxInt32Item(SCHATTR_LEGEND_POS, static_cast<sal_Int32>(eLegendPos) ) );
194         }
195         break;
196         case SCHATTR_LEGEND_NO_OVERLAY:
197         {
198             bool bOverlay = false;
199             GetPropertySet()->getPropertyValue("Overlay") >>= bOverlay;
200             rOutItemSet.Put(SfxBoolItem(SCHATTR_LEGEND_NO_OVERLAY, !bOverlay));
201         }
202         break;
203    }
204 }
205 
206 } //  namespace chart::wrapper
207 
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
209