/src/libreoffice/chart2/source/controller/dialogs/res_LegendPosition.cxx
Line | Count | Source |
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 <res_LegendPosition.hxx> |
21 | | #include <Legend.hxx> |
22 | | #include <LegendHelper.hxx> |
23 | | #include <ChartModel.hxx> |
24 | | #include <Diagram.hxx> |
25 | | |
26 | | #include <com/sun/star/chart2/LegendPosition.hpp> |
27 | | #include <com/sun/star/chart/ChartLegendExpansion.hpp> |
28 | | |
29 | | //itemset stuff |
30 | | #include <chartview/ChartSfxItemIds.hxx> |
31 | | #include <svl/intitem.hxx> |
32 | | #include <svl/eitem.hxx> |
33 | | #include <comphelper/diagnose_ex.hxx> |
34 | | #include <utility> |
35 | | #include <vcl/weld/Builder.hxx> |
36 | | #include <vcl/weld/CheckButton.hxx> |
37 | | #include <vcl/weld/RadioButton.hxx> |
38 | | |
39 | | namespace chart |
40 | | { |
41 | | |
42 | | using namespace ::com::sun::star; |
43 | | using namespace ::com::sun::star::chart2; |
44 | | |
45 | | LegendPositionResources::LegendPositionResources(weld::Builder& rBuilder) |
46 | 0 | : m_xRbtLeft(rBuilder.weld_radio_button(u"left"_ustr)) |
47 | 0 | , m_xRbtRight(rBuilder.weld_radio_button(u"right"_ustr)) |
48 | 0 | , m_xRbtTop(rBuilder.weld_radio_button(u"top"_ustr)) |
49 | 0 | , m_xRbtBottom(rBuilder.weld_radio_button(u"bottom"_ustr)) |
50 | 0 | { |
51 | 0 | impl_setRadioButtonToggleHdl(); |
52 | 0 | } |
53 | | |
54 | | LegendPositionResources::LegendPositionResources(weld::Builder& rBuilder, |
55 | | uno::Reference< uno::XComponentContext > xCC) |
56 | 0 | : m_xCC(std::move(xCC)) |
57 | 0 | , m_xCbxShow(rBuilder.weld_check_button(u"show"_ustr)) |
58 | 0 | , m_xRbtLeft(rBuilder.weld_radio_button(u"left"_ustr)) |
59 | 0 | , m_xRbtRight(rBuilder.weld_radio_button(u"right"_ustr)) |
60 | 0 | , m_xRbtTop(rBuilder.weld_radio_button(u"top"_ustr)) |
61 | 0 | , m_xRbtBottom(rBuilder.weld_radio_button(u"bottom"_ustr)) |
62 | 0 | { |
63 | 0 | m_xCbxShow->connect_toggled( LINK( this, LegendPositionResources, PositionEnableHdl ) ); |
64 | 0 | impl_setRadioButtonToggleHdl(); |
65 | 0 | } |
66 | | |
67 | | void LegendPositionResources::impl_setRadioButtonToggleHdl() |
68 | 0 | { |
69 | 0 | m_xRbtLeft->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) ); |
70 | 0 | m_xRbtTop->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) ); |
71 | 0 | m_xRbtRight->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) ); |
72 | 0 | m_xRbtBottom->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) ); |
73 | 0 | } |
74 | | |
75 | | LegendPositionResources::~LegendPositionResources() |
76 | 0 | { |
77 | 0 | } |
78 | | |
79 | | void LegendPositionResources::writeToResources( const rtl::Reference<::chart::ChartModel>& xChartModel ) |
80 | 0 | { |
81 | 0 | try |
82 | 0 | { |
83 | 0 | rtl::Reference< Diagram > xDiagram = xChartModel->getFirstChartDiagram(); |
84 | 0 | rtl::Reference< Legend > xLegend = xDiagram->getLegend2(); |
85 | 0 | if( xLegend.is() ) |
86 | 0 | { |
87 | | //show |
88 | 0 | bool bShowLegend = false; |
89 | 0 | xLegend->getPropertyValue( u"Show"_ustr ) >>= bShowLegend; |
90 | 0 | if (m_xCbxShow) |
91 | 0 | m_xCbxShow->set_active( bShowLegend ); |
92 | 0 | PositionEnable(); |
93 | | |
94 | | //position |
95 | 0 | chart2::LegendPosition ePos; |
96 | 0 | xLegend->getPropertyValue( u"AnchorPosition"_ustr ) >>= ePos; |
97 | 0 | switch( ePos ) |
98 | 0 | { |
99 | 0 | case chart2::LegendPosition_LINE_START: |
100 | 0 | m_xRbtLeft->set_active(true); |
101 | 0 | break; |
102 | 0 | case chart2::LegendPosition_PAGE_START: |
103 | 0 | m_xRbtTop->set_active(true); |
104 | 0 | break; |
105 | 0 | case chart2::LegendPosition_PAGE_END: |
106 | 0 | m_xRbtBottom->set_active(true); |
107 | 0 | break; |
108 | 0 | case chart2::LegendPosition_LINE_END: |
109 | 0 | default: |
110 | 0 | m_xRbtRight->set_active(true); |
111 | 0 | break; |
112 | 0 | } |
113 | 0 | } |
114 | 0 | } |
115 | 0 | catch( const uno::Exception & ) |
116 | 0 | { |
117 | 0 | DBG_UNHANDLED_EXCEPTION("chart2"); |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | | void LegendPositionResources::writeToModel( const rtl::Reference<::chart::ChartModel>& xChartModel ) const |
122 | 0 | { |
123 | 0 | try |
124 | 0 | { |
125 | 0 | bool bShowLegend = m_xCbxShow && m_xCbxShow->get_active(); |
126 | 0 | ChartModel& rModel = *xChartModel; |
127 | 0 | rtl::Reference< Legend > xProp = LegendHelper::getLegend(rModel, m_xCC, bShowLegend); |
128 | 0 | if( xProp.is() ) |
129 | 0 | { |
130 | | //show |
131 | 0 | xProp->setPropertyValue( u"Show"_ustr , uno::Any( bShowLegend )); |
132 | | |
133 | | //position |
134 | 0 | chart2::LegendPosition eNewPos; |
135 | 0 | css::chart::ChartLegendExpansion eExp = css::chart::ChartLegendExpansion_HIGH; |
136 | |
|
137 | 0 | if( m_xRbtLeft->get_active() ) |
138 | 0 | eNewPos = chart2::LegendPosition_LINE_START; |
139 | 0 | else if( m_xRbtRight->get_active() ) |
140 | 0 | { |
141 | 0 | eNewPos = chart2::LegendPosition_LINE_END; |
142 | 0 | } |
143 | 0 | else if( m_xRbtTop->get_active() ) |
144 | 0 | { |
145 | 0 | eNewPos = chart2::LegendPosition_PAGE_START; |
146 | 0 | eExp = css::chart::ChartLegendExpansion_WIDE; |
147 | 0 | } |
148 | 0 | else if( m_xRbtBottom->get_active() ) |
149 | 0 | { |
150 | 0 | eNewPos = chart2::LegendPosition_PAGE_END; |
151 | 0 | eExp = css::chart::ChartLegendExpansion_WIDE; |
152 | 0 | } |
153 | |
|
154 | 0 | xProp->setPropertyValue( u"AnchorPosition"_ustr , uno::Any( eNewPos )); |
155 | 0 | xProp->setPropertyValue( u"Expansion"_ustr , uno::Any( eExp )); |
156 | 0 | xProp->setPropertyValue( u"RelativePosition"_ustr , uno::Any()); |
157 | 0 | } |
158 | 0 | } |
159 | 0 | catch( const uno::Exception & ) |
160 | 0 | { |
161 | 0 | DBG_UNHANDLED_EXCEPTION("chart2" ); |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | | IMPL_LINK_NOARG(LegendPositionResources, PositionEnableHdl, weld::Toggleable&, void) |
166 | 0 | { |
167 | 0 | PositionEnable(); |
168 | 0 | } |
169 | | |
170 | | void LegendPositionResources::PositionEnable() |
171 | 0 | { |
172 | 0 | bool bEnable = !m_xCbxShow || m_xCbxShow->get_active(); |
173 | |
|
174 | 0 | m_xRbtLeft->set_sensitive( bEnable ); |
175 | 0 | m_xRbtTop->set_sensitive( bEnable ); |
176 | 0 | m_xRbtRight->set_sensitive( bEnable ); |
177 | 0 | m_xRbtBottom->set_sensitive( bEnable ); |
178 | |
|
179 | 0 | m_aChangeLink.Call(nullptr); |
180 | 0 | } |
181 | | |
182 | | void LegendPositionResources::initFromItemSet( const SfxItemSet& rInAttrs ) |
183 | 0 | { |
184 | 0 | if( const SfxInt32Item* pPosItem = rInAttrs.GetItemIfSet( SCHATTR_LEGEND_POS ) ) |
185 | 0 | { |
186 | 0 | chart2::LegendPosition nLegendPosition = static_cast<chart2::LegendPosition>(pPosItem->GetValue()); |
187 | 0 | switch( nLegendPosition ) |
188 | 0 | { |
189 | 0 | case chart2::LegendPosition_LINE_START: |
190 | 0 | m_xRbtLeft->set_active(true); |
191 | 0 | break; |
192 | 0 | case chart2::LegendPosition_PAGE_START: |
193 | 0 | m_xRbtTop->set_active(true); |
194 | 0 | break; |
195 | 0 | case chart2::LegendPosition_LINE_END: |
196 | 0 | m_xRbtRight->set_active(true); |
197 | 0 | break; |
198 | 0 | case chart2::LegendPosition_PAGE_END: |
199 | 0 | m_xRbtBottom->set_active(true); |
200 | 0 | break; |
201 | 0 | default: |
202 | 0 | break; |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | 0 | const SfxBoolItem* pShowItem; |
207 | 0 | if( m_xCbxShow && (pShowItem = rInAttrs.GetItemIfSet( SCHATTR_LEGEND_SHOW )) ) |
208 | 0 | { |
209 | 0 | m_xCbxShow->set_active(pShowItem->GetValue()); |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | | void LegendPositionResources::writeToItemSet( SfxItemSet& rOutAttrs ) const |
214 | 0 | { |
215 | 0 | chart2::LegendPosition nLegendPosition = chart2::LegendPosition_LINE_END; |
216 | 0 | if( m_xRbtLeft->get_active() ) |
217 | 0 | nLegendPosition = chart2::LegendPosition_LINE_START; |
218 | 0 | else if( m_xRbtTop->get_active() ) |
219 | 0 | nLegendPosition = chart2::LegendPosition_PAGE_START; |
220 | 0 | else if( m_xRbtRight->get_active() ) |
221 | 0 | nLegendPosition = chart2::LegendPosition_LINE_END; |
222 | 0 | else if( m_xRbtBottom->get_active() ) |
223 | 0 | nLegendPosition = chart2::LegendPosition_PAGE_END; |
224 | 0 | rOutAttrs.Put( SfxInt32Item(SCHATTR_LEGEND_POS, static_cast<sal_Int32>(nLegendPosition) ) ); |
225 | |
|
226 | 0 | rOutAttrs.Put( SfxBoolItem(SCHATTR_LEGEND_SHOW, !m_xCbxShow || m_xCbxShow->get_active()) ); |
227 | 0 | } |
228 | | |
229 | | IMPL_LINK (LegendPositionResources, PositionChangeHdl, weld::Toggleable&, rRadio, void) |
230 | 0 | { |
231 | | //for each radio click there are coming two change events |
232 | | //first uncheck of previous button -> ignore that call |
233 | | //the second call gives the check of the new button |
234 | 0 | if( rRadio.get_active() ) |
235 | 0 | m_aChangeLink.Call(nullptr); |
236 | 0 | } |
237 | | |
238 | | void LegendPositionResources::SetChangeHdl( const Link<LinkParamNone*,void>& rLink ) |
239 | 0 | { |
240 | 0 | m_aChangeLink = rLink; |
241 | 0 | } |
242 | | |
243 | | } //namespace chart |
244 | | |
245 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |