1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <res_LegendPosition.hxx>
21 #include <ChartModelHelper.hxx>
22 #include <LegendHelper.hxx>
23 #include <ChartModel.hxx>
25 #include <com/sun/star/chart2/LegendPosition.hpp>
26 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
29 #include <chartview/ChartSfxItemIds.hxx>
30 #include <svl/intitem.hxx>
31 #include <svl/eitem.hxx>
32 #include <tools/diagnose_ex.h>
33 #include <vcl/weld.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::chart2
;
41 LegendPositionResources::LegendPositionResources(weld::Builder
& rBuilder
)
42 : m_xRbtLeft(rBuilder
.weld_radio_button("left"))
43 , m_xRbtRight(rBuilder
.weld_radio_button("right"))
44 , m_xRbtTop(rBuilder
.weld_radio_button("top"))
45 , m_xRbtBottom(rBuilder
.weld_radio_button("bottom"))
47 impl_setRadioButtonToggleHdl();
50 LegendPositionResources::LegendPositionResources(weld::Builder
& rBuilder
,
51 const uno::Reference
< uno::XComponentContext
>& xCC
)
53 , m_xCbxShow(rBuilder
.weld_check_button("show"))
54 , m_xRbtLeft(rBuilder
.weld_radio_button("left"))
55 , m_xRbtRight(rBuilder
.weld_radio_button("right"))
56 , m_xRbtTop(rBuilder
.weld_radio_button("top"))
57 , m_xRbtBottom(rBuilder
.weld_radio_button("bottom"))
59 m_xCbxShow
->connect_toggled( LINK( this, LegendPositionResources
, PositionEnableHdl
) );
60 impl_setRadioButtonToggleHdl();
63 void LegendPositionResources::impl_setRadioButtonToggleHdl()
65 m_xRbtLeft
->connect_toggled( LINK( this, LegendPositionResources
, PositionChangeHdl
) );
66 m_xRbtTop
->connect_toggled( LINK( this, LegendPositionResources
, PositionChangeHdl
) );
67 m_xRbtRight
->connect_toggled( LINK( this, LegendPositionResources
, PositionChangeHdl
) );
68 m_xRbtBottom
->connect_toggled( LINK( this, LegendPositionResources
, PositionChangeHdl
) );
71 LegendPositionResources::~LegendPositionResources()
75 void LegendPositionResources::writeToResources( const uno::Reference
< frame::XModel
>& xChartModel
)
79 uno::Reference
< XDiagram
> xDiagram
= ChartModelHelper::findDiagram( xChartModel
);
80 uno::Reference
< beans::XPropertySet
> xProp( xDiagram
->getLegend(), uno::UNO_QUERY
);
84 bool bShowLegend
= false;
85 xProp
->getPropertyValue( "Show" ) >>= bShowLegend
;
87 m_xCbxShow
->set_active( bShowLegend
);
88 PositionEnableHdl(*m_xCbxShow
);
91 chart2::LegendPosition ePos
;
92 xProp
->getPropertyValue( "AnchorPosition" ) >>= ePos
;
95 case chart2::LegendPosition_LINE_START
:
96 m_xRbtLeft
->set_active(true);
98 case chart2::LegendPosition_LINE_END
:
99 m_xRbtRight
->set_active(true);
101 case chart2::LegendPosition_PAGE_START
:
102 m_xRbtTop
->set_active(true);
104 case chart2::LegendPosition_PAGE_END
:
105 m_xRbtBottom
->set_active(true);
107 case chart2::LegendPosition_CUSTOM
:
109 m_xRbtRight
->set_active(true);
114 catch( const uno::Exception
& )
116 DBG_UNHANDLED_EXCEPTION("chart2");
120 void LegendPositionResources::writeToModel( const css::uno::Reference
< frame::XModel
>& xChartModel
) const
124 bool bShowLegend
= m_xCbxShow
&& m_xCbxShow
->get_active();
125 ChartModel
& rModel
= dynamic_cast<ChartModel
&>(*xChartModel
);
126 uno::Reference
< beans::XPropertySet
> xProp(LegendHelper::getLegend(rModel
, m_xCC
, bShowLegend
), uno::UNO_QUERY
);
130 xProp
->setPropertyValue( "Show" , uno::Any( bShowLegend
));
133 chart2::LegendPosition eNewPos
;
134 css::chart::ChartLegendExpansion eExp
= css::chart::ChartLegendExpansion_HIGH
;
136 if( m_xRbtLeft
->get_active() )
137 eNewPos
= chart2::LegendPosition_LINE_START
;
138 else if( m_xRbtRight
->get_active() )
140 eNewPos
= chart2::LegendPosition_LINE_END
;
142 else if( m_xRbtTop
->get_active() )
144 eNewPos
= chart2::LegendPosition_PAGE_START
;
145 eExp
= css::chart::ChartLegendExpansion_WIDE
;
147 else if( m_xRbtBottom
->get_active() )
149 eNewPos
= chart2::LegendPosition_PAGE_END
;
150 eExp
= css::chart::ChartLegendExpansion_WIDE
;
153 xProp
->setPropertyValue( "AnchorPosition" , uno::Any( eNewPos
));
154 xProp
->setPropertyValue( "Expansion" , uno::Any( eExp
));
155 xProp
->setPropertyValue( "RelativePosition" , uno::Any());
158 catch( const uno::Exception
& )
160 DBG_UNHANDLED_EXCEPTION("chart2" );
164 IMPL_LINK_NOARG(LegendPositionResources
, PositionEnableHdl
, weld::ToggleButton
&, void)
166 bool bEnable
= !m_xCbxShow
|| m_xCbxShow
->get_active();
168 m_xRbtLeft
->set_sensitive( bEnable
);
169 m_xRbtTop
->set_sensitive( bEnable
);
170 m_xRbtRight
->set_sensitive( bEnable
);
171 m_xRbtBottom
->set_sensitive( bEnable
);
173 m_aChangeLink
.Call(nullptr);
176 void LegendPositionResources::initFromItemSet( const SfxItemSet
& rInAttrs
)
178 const SfxPoolItem
* pPoolItem
= nullptr;
179 if( rInAttrs
.GetItemState( SCHATTR_LEGEND_POS
, true, &pPoolItem
) == SfxItemState::SET
)
181 chart2::LegendPosition nLegendPosition
= static_cast<chart2::LegendPosition
>(static_cast<const SfxInt32Item
*>(pPoolItem
)->GetValue());
182 switch( nLegendPosition
)
184 case chart2::LegendPosition_LINE_START
:
185 m_xRbtLeft
->set_active(true);
187 case chart2::LegendPosition_PAGE_START
:
188 m_xRbtTop
->set_active(true);
190 case chart2::LegendPosition_LINE_END
:
191 m_xRbtRight
->set_active(true);
193 case chart2::LegendPosition_PAGE_END
:
194 m_xRbtBottom
->set_active(true);
201 if( m_xCbxShow
&& rInAttrs
.GetItemState( SCHATTR_LEGEND_SHOW
, true, &pPoolItem
) == SfxItemState::SET
)
203 bool bShow
= static_cast< const SfxBoolItem
* >( pPoolItem
)->GetValue();
204 m_xCbxShow
->set_active(bShow
);
208 void LegendPositionResources::writeToItemSet( SfxItemSet
& rOutAttrs
) const
210 chart2::LegendPosition nLegendPosition
= chart2::LegendPosition_CUSTOM
;
211 if( m_xRbtLeft
->get_active() )
212 nLegendPosition
= chart2::LegendPosition_LINE_START
;
213 else if( m_xRbtTop
->get_active() )
214 nLegendPosition
= chart2::LegendPosition_PAGE_START
;
215 else if( m_xRbtRight
->get_active() )
216 nLegendPosition
= chart2::LegendPosition_LINE_END
;
217 else if( m_xRbtBottom
->get_active() )
218 nLegendPosition
= chart2::LegendPosition_PAGE_END
;
219 rOutAttrs
.Put( SfxInt32Item(SCHATTR_LEGEND_POS
, static_cast<sal_Int32
>(nLegendPosition
) ) );
221 rOutAttrs
.Put( SfxBoolItem(SCHATTR_LEGEND_SHOW
, !m_xCbxShow
|| m_xCbxShow
->get_active()) );
224 IMPL_LINK (LegendPositionResources
, PositionChangeHdl
, weld::ToggleButton
&, rRadio
, void)
226 //for each radio click there are coming two change events
227 //first uncheck of previous button -> ignore that call
228 //the second call gives the check of the new button
229 if( rRadio
.get_active() )
230 m_aChangeLink
.Call(nullptr);
233 void LegendPositionResources::SetChangeHdl( const Link
<LinkParamNone
*,void>& rLink
)
235 m_aChangeLink
= rLink
;
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */