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>
22 #include <LegendHelper.hxx>
23 #include <ChartModel.hxx>
24 #include <Diagram.hxx>
26 #include <com/sun/star/chart2/LegendPosition.hpp>
27 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
30 #include <chartview/ChartSfxItemIds.hxx>
31 #include <svl/intitem.hxx>
32 #include <svl/eitem.hxx>
33 #include <comphelper/diagnose_ex.hxx>
35 #include <vcl/weld.hxx>
40 using namespace ::com::sun::star
;
41 using namespace ::com::sun::star::chart2
;
43 LegendPositionResources::LegendPositionResources(weld::Builder
& rBuilder
)
44 : m_xRbtLeft(rBuilder
.weld_radio_button(u
"left"_ustr
))
45 , m_xRbtRight(rBuilder
.weld_radio_button(u
"right"_ustr
))
46 , m_xRbtTop(rBuilder
.weld_radio_button(u
"top"_ustr
))
47 , m_xRbtBottom(rBuilder
.weld_radio_button(u
"bottom"_ustr
))
49 impl_setRadioButtonToggleHdl();
52 LegendPositionResources::LegendPositionResources(weld::Builder
& rBuilder
,
53 uno::Reference
< uno::XComponentContext
> xCC
)
54 : m_xCC(std::move(xCC
))
55 , m_xCbxShow(rBuilder
.weld_check_button(u
"show"_ustr
))
56 , m_xRbtLeft(rBuilder
.weld_radio_button(u
"left"_ustr
))
57 , m_xRbtRight(rBuilder
.weld_radio_button(u
"right"_ustr
))
58 , m_xRbtTop(rBuilder
.weld_radio_button(u
"top"_ustr
))
59 , m_xRbtBottom(rBuilder
.weld_radio_button(u
"bottom"_ustr
))
61 m_xCbxShow
->connect_toggled( LINK( this, LegendPositionResources
, PositionEnableHdl
) );
62 impl_setRadioButtonToggleHdl();
65 void LegendPositionResources::impl_setRadioButtonToggleHdl()
67 m_xRbtLeft
->connect_toggled( LINK( this, LegendPositionResources
, PositionChangeHdl
) );
68 m_xRbtTop
->connect_toggled( LINK( this, LegendPositionResources
, PositionChangeHdl
) );
69 m_xRbtRight
->connect_toggled( LINK( this, LegendPositionResources
, PositionChangeHdl
) );
70 m_xRbtBottom
->connect_toggled( LINK( this, LegendPositionResources
, PositionChangeHdl
) );
73 LegendPositionResources::~LegendPositionResources()
77 void LegendPositionResources::writeToResources( const rtl::Reference
<::chart::ChartModel
>& xChartModel
)
81 rtl::Reference
< Diagram
> xDiagram
= xChartModel
->getFirstChartDiagram();
82 rtl::Reference
< Legend
> xLegend
= xDiagram
->getLegend2();
86 bool bShowLegend
= false;
87 xLegend
->getPropertyValue( u
"Show"_ustr
) >>= bShowLegend
;
89 m_xCbxShow
->set_active( bShowLegend
);
93 chart2::LegendPosition ePos
;
94 xLegend
->getPropertyValue( u
"AnchorPosition"_ustr
) >>= ePos
;
97 case chart2::LegendPosition_LINE_START
:
98 m_xRbtLeft
->set_active(true);
100 case chart2::LegendPosition_PAGE_START
:
101 m_xRbtTop
->set_active(true);
103 case chart2::LegendPosition_PAGE_END
:
104 m_xRbtBottom
->set_active(true);
106 case chart2::LegendPosition_LINE_END
:
108 m_xRbtRight
->set_active(true);
113 catch( const uno::Exception
& )
115 DBG_UNHANDLED_EXCEPTION("chart2");
119 void LegendPositionResources::writeToModel( const rtl::Reference
<::chart::ChartModel
>& xChartModel
) const
123 bool bShowLegend
= m_xCbxShow
&& m_xCbxShow
->get_active();
124 ChartModel
& rModel
= *xChartModel
;
125 rtl::Reference
< Legend
> xProp
= LegendHelper::getLegend(rModel
, m_xCC
, bShowLegend
);
129 xProp
->setPropertyValue( u
"Show"_ustr
, uno::Any( bShowLegend
));
132 chart2::LegendPosition eNewPos
;
133 css::chart::ChartLegendExpansion eExp
= css::chart::ChartLegendExpansion_HIGH
;
135 if( m_xRbtLeft
->get_active() )
136 eNewPos
= chart2::LegendPosition_LINE_START
;
137 else if( m_xRbtRight
->get_active() )
139 eNewPos
= chart2::LegendPosition_LINE_END
;
141 else if( m_xRbtTop
->get_active() )
143 eNewPos
= chart2::LegendPosition_PAGE_START
;
144 eExp
= css::chart::ChartLegendExpansion_WIDE
;
146 else if( m_xRbtBottom
->get_active() )
148 eNewPos
= chart2::LegendPosition_PAGE_END
;
149 eExp
= css::chart::ChartLegendExpansion_WIDE
;
152 xProp
->setPropertyValue( u
"AnchorPosition"_ustr
, uno::Any( eNewPos
));
153 xProp
->setPropertyValue( u
"Expansion"_ustr
, uno::Any( eExp
));
154 xProp
->setPropertyValue( u
"RelativePosition"_ustr
, uno::Any());
157 catch( const uno::Exception
& )
159 DBG_UNHANDLED_EXCEPTION("chart2" );
163 IMPL_LINK_NOARG(LegendPositionResources
, PositionEnableHdl
, weld::Toggleable
&, void)
168 void LegendPositionResources::PositionEnable()
170 bool bEnable
= !m_xCbxShow
|| m_xCbxShow
->get_active();
172 m_xRbtLeft
->set_sensitive( bEnable
);
173 m_xRbtTop
->set_sensitive( bEnable
);
174 m_xRbtRight
->set_sensitive( bEnable
);
175 m_xRbtBottom
->set_sensitive( bEnable
);
177 m_aChangeLink
.Call(nullptr);
180 void LegendPositionResources::initFromItemSet( const SfxItemSet
& rInAttrs
)
182 if( const SfxInt32Item
* pPosItem
= rInAttrs
.GetItemIfSet( SCHATTR_LEGEND_POS
) )
184 chart2::LegendPosition nLegendPosition
= static_cast<chart2::LegendPosition
>(pPosItem
->GetValue());
185 switch( nLegendPosition
)
187 case chart2::LegendPosition_LINE_START
:
188 m_xRbtLeft
->set_active(true);
190 case chart2::LegendPosition_PAGE_START
:
191 m_xRbtTop
->set_active(true);
193 case chart2::LegendPosition_LINE_END
:
194 m_xRbtRight
->set_active(true);
196 case chart2::LegendPosition_PAGE_END
:
197 m_xRbtBottom
->set_active(true);
204 const SfxBoolItem
* pShowItem
;
205 if( m_xCbxShow
&& (pShowItem
= rInAttrs
.GetItemIfSet( SCHATTR_LEGEND_SHOW
)) )
207 m_xCbxShow
->set_active(pShowItem
->GetValue());
211 void LegendPositionResources::writeToItemSet( SfxItemSet
& rOutAttrs
) const
213 chart2::LegendPosition nLegendPosition
= chart2::LegendPosition_LINE_END
;
214 if( m_xRbtLeft
->get_active() )
215 nLegendPosition
= chart2::LegendPosition_LINE_START
;
216 else if( m_xRbtTop
->get_active() )
217 nLegendPosition
= chart2::LegendPosition_PAGE_START
;
218 else if( m_xRbtRight
->get_active() )
219 nLegendPosition
= chart2::LegendPosition_LINE_END
;
220 else if( m_xRbtBottom
->get_active() )
221 nLegendPosition
= chart2::LegendPosition_PAGE_END
;
222 rOutAttrs
.Put( SfxInt32Item(SCHATTR_LEGEND_POS
, static_cast<sal_Int32
>(nLegendPosition
) ) );
224 rOutAttrs
.Put( SfxBoolItem(SCHATTR_LEGEND_SHOW
, !m_xCbxShow
|| m_xCbxShow
->get_active()) );
227 IMPL_LINK (LegendPositionResources
, PositionChangeHdl
, weld::Toggleable
&, rRadio
, void)
229 //for each radio click there are coming two change events
230 //first uncheck of previous button -> ignore that call
231 //the second call gives the check of the new button
232 if( rRadio
.get_active() )
233 m_aChangeLink
.Call(nullptr);
236 void LegendPositionResources::SetChangeHdl( const Link
<LinkParamNone
*,void>& rLink
)
238 m_aChangeLink
= rLink
;
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */