Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / controller / dialogs / res_DataLabel.cxx
blobfb3cc7783eb884005dd94619dbf55d700a2b9e74
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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_DataLabel.hxx"
22 #include <chartview/ChartSfxItemIds.hxx>
23 #include "dlg_NumberFormat.hxx"
25 #include <svx/numinf.hxx>
26 #include <svl/eitem.hxx>
27 #include <svl/intitem.hxx>
28 #include <svl/stritem.hxx>
29 #include <svl/ilstitem.hxx>
30 #include <editeng/eeitem.hxx>
31 #include <editeng/frmdiritem.hxx>
32 #include <svx/svxids.hrc>
33 #include <svl/zforlist.hxx>
34 #include <svtools/controldims.hxx>
36 namespace chart
39 namespace
42 const OUStringLiteral our_aLBEntryMap[] = {" ", ", ", "; ", "\n"};
44 bool lcl_ReadNumberFormatFromItemSet( const SfxItemSet& rSet, sal_uInt16 nValueWhich, sal_uInt16 nSourceFormatWhich, sal_uLong& rnFormatKeyOut, bool& rbSourceFormatOut, bool& rbSourceFormatMixedStateOut )
46 bool bSet = false;
47 const SfxPoolItem *pItem1 = nullptr;
48 if( rSet.GetItemState( nValueWhich, true, &pItem1 ) == SfxItemState::SET )
50 const SfxUInt32Item * pNumItem = dynamic_cast< const SfxUInt32Item * >( pItem1 );
51 if( pNumItem )
53 rnFormatKeyOut = pNumItem->GetValue();
54 bSet = true;
58 rbSourceFormatMixedStateOut=true;
59 const SfxPoolItem *pItem2 = nullptr;
60 if( rSet.GetItemState( nSourceFormatWhich, true, &pItem2 ) == SfxItemState::SET )
62 const SfxBoolItem * pBoolItem = dynamic_cast< const SfxBoolItem * >( pItem2 );
63 if( pBoolItem )
65 rbSourceFormatOut = pBoolItem->GetValue();
66 rbSourceFormatMixedStateOut=false;
69 return bSet;
72 void lcl_setBoolItemToCheckBox( const SfxItemSet& rInAttrs, sal_uInt16 nWhichId, CheckBox& rCheckbox )
74 rCheckbox.EnableTriState( false );
76 const SfxPoolItem *pPoolItem = nullptr;
77 if( rInAttrs.GetItemState(nWhichId, true, &pPoolItem) == SfxItemState::SET )
78 rCheckbox.Check( static_cast<const SfxBoolItem*>(pPoolItem)->GetValue() );
79 else
81 rCheckbox.EnableTriState();
82 rCheckbox.SetState( TRISTATE_INDET );
86 }//end anonymous namespace
88 DataLabelResources::DataLabelResources(VclBuilderContainer* pWindow, vcl::Window* pParent, const SfxItemSet& rInAttrs )
90 m_pNumberFormatter(nullptr),
91 m_bNumberFormatMixedState(true),
92 m_bPercentFormatMixedState(true),
93 m_nNumberFormatForValue(0),
94 m_nNumberFormatForPercent(11),
95 m_bSourceFormatMixedState(true),
96 m_bPercentSourceMixedState(true),
97 m_bSourceFormatForValue(true),
98 m_bSourceFormatForPercent(true),
99 m_pWindow(pParent),
100 m_pPool(rInAttrs.GetPool())
102 pWindow->get(m_pCBNumber, "CB_VALUE_AS_NUMBER");
103 pWindow->get(m_pPB_NumberFormatForValue, "PB_NUMBERFORMAT");
104 pWindow->get(m_pCBPercent, "CB_VALUE_AS_PERCENTAGE");
105 pWindow->get(m_pPB_NumberFormatForPercent, "PB_PERCENT_NUMBERFORMAT");
106 pWindow->get(m_pFT_NumberFormatForPercent,"STR_DLG_NUMBERFORMAT_FOR_PERCENTAGE_VALUE");
107 pWindow->get(m_pCBCategory, "CB_CATEGORY");
108 pWindow->get(m_pCBSymbol, "CB_SYMBOL");
109 pWindow->get(m_pCBWrapText, "CB_WRAP_TEXT");
111 pWindow->get(m_pBxLabelPlacement, "boxPLACEMENT");
112 pWindow->get(m_pLB_LabelPlacement, "LB_LABEL_PLACEMENT");
114 pWindow->get(m_pDC_Dial, "CT_DIAL");
115 pWindow->get(m_pFT_Dial, "CT_LABEL_DIAL");
117 m_pDC_Dial->SetText(m_pFT_Dial->GetText());
119 pWindow->get(m_pNF_Degrees, "NF_LABEL_DEGREES");
120 pWindow->get(m_pLB_TextDirection, "LB_LABEL_TEXTDIR");
121 pWindow->get(m_pBxTextDirection, "boxTXT_DIRECTION");
122 pWindow->get(m_pBxOrientation, "boxORIENTATION");
124 pWindow->get(m_pSeparatorResources, "boxSEPARATOR");
125 pWindow->get(m_pLB_Separator, "LB_TEXT_SEPARATOR");
127 //fill label placement list
128 std::map< sal_Int32, OUString > aPlacementToStringMap;
129 for( sal_Int32 nEnum=0; nEnum<m_pLB_LabelPlacement->GetEntryCount(); ++nEnum )
130 aPlacementToStringMap[nEnum] = m_pLB_LabelPlacement->GetEntry(static_cast<sal_uInt16>(nEnum));
133 std::vector< sal_Int32 > aAvailablePlacementList;
134 const SfxPoolItem *pPoolItem = nullptr;
135 if( rInAttrs.GetItemState(SCHATTR_DATADESCR_AVAILABLE_PLACEMENTS, true, &pPoolItem) == SfxItemState::SET )
136 aAvailablePlacementList = static_cast<const SfxIntegerListItem*>(pPoolItem)->GetList();
138 m_pLB_LabelPlacement->Clear();
139 for( size_t nN=0; nN<aAvailablePlacementList.size(); ++nN )
141 sal_uInt16 nListBoxPos = static_cast<sal_uInt16>( nN );
142 sal_Int32 nPlacement = aAvailablePlacementList[nN];
143 m_aPlacementToListBoxMap[nPlacement]=nListBoxPos;
144 m_aListBoxToPlacementMap[nListBoxPos]=nPlacement;
145 m_pLB_LabelPlacement->InsertEntry( aPlacementToStringMap[nPlacement] );
147 m_pLB_LabelPlacement->SetDropDownLineCount(m_pLB_LabelPlacement->GetEntryCount());
149 //some click handler
150 m_pPB_NumberFormatForValue->SetClickHdl( LINK( this, DataLabelResources, NumberFormatDialogHdl ) );
151 m_pPB_NumberFormatForPercent->SetClickHdl( LINK( this, DataLabelResources, NumberFormatDialogHdl ) );
152 m_pCBNumber->SetClickHdl( LINK( this, DataLabelResources, CheckHdl ));
153 m_pCBPercent->SetClickHdl( LINK( this, DataLabelResources, CheckHdl ));
154 m_pCBCategory->SetClickHdl( LINK( this, DataLabelResources, CheckHdl ));
155 m_pCBSymbol->SetClickHdl( LINK( this, DataLabelResources, CheckHdl ));
156 m_pCBWrapText->SetClickHdl( LINK( this, DataLabelResources, CheckHdl ));
158 m_bNumberFormatMixedState = !lcl_ReadNumberFormatFromItemSet( rInAttrs, SID_ATTR_NUMBERFORMAT_VALUE, SID_ATTR_NUMBERFORMAT_SOURCE, m_nNumberFormatForValue, m_bSourceFormatForValue, m_bSourceFormatMixedState );
159 m_bPercentFormatMixedState = !lcl_ReadNumberFormatFromItemSet( rInAttrs, SCHATTR_PERCENT_NUMBERFORMAT_VALUE, SCHATTR_PERCENT_NUMBERFORMAT_SOURCE, m_nNumberFormatForPercent, m_bSourceFormatForPercent , m_bPercentSourceMixedState);
161 if( rInAttrs.GetItemState(SCHATTR_DATADESCR_NO_PERCENTVALUE, true, &pPoolItem) == SfxItemState::SET )
163 bool bForbidPercentValue = rInAttrs.Get( SCHATTR_DATADESCR_NO_PERCENTVALUE ).GetValue();
164 if( bForbidPercentValue )
165 m_pCBPercent->Enable(false);
168 m_pDC_Dial->SetLinkedField( m_pNF_Degrees );
171 DataLabelResources::~DataLabelResources()
175 void DataLabelResources::SetNumberFormatter( SvNumberFormatter* pFormatter )
177 m_pNumberFormatter = pFormatter;
180 IMPL_LINK( DataLabelResources, NumberFormatDialogHdl, Button *, pButton, void )
182 if( !m_pPool || !m_pNumberFormatter )
184 OSL_FAIL("Missing item pool or number formatter");
185 return;
188 if( pButton == m_pPB_NumberFormatForValue && !m_pCBNumber->IsChecked())
189 m_pCBNumber->Check();
190 else if( pButton == m_pPB_NumberFormatForPercent && !m_pCBPercent->IsChecked())
191 m_pCBPercent->Check();
193 SfxItemSet aNumberSet = NumberFormatDialog::CreateEmptyItemSetForNumberFormatDialog( *m_pPool );
194 aNumberSet.Put (SvxNumberInfoItem( m_pNumberFormatter, static_cast<sal_uInt16>(SID_ATTR_NUMBERFORMAT_INFO)));
196 bool bPercent = ( pButton == m_pPB_NumberFormatForPercent );
198 sal_uLong& rnFormatKey = bPercent ? m_nNumberFormatForPercent : m_nNumberFormatForValue;
199 bool& rUseSourceFormat = bPercent ? m_bSourceFormatForPercent : m_bSourceFormatForValue;
200 bool& rbMixedState = bPercent ? m_bPercentFormatMixedState : m_bNumberFormatMixedState;
201 bool& rbSourceMixedState = bPercent ? m_bPercentSourceMixedState : m_bSourceFormatMixedState;
203 if(!rbMixedState)
204 aNumberSet.Put( SfxUInt32Item( SID_ATTR_NUMBERFORMAT_VALUE, rnFormatKey ));
205 aNumberSet.Put( SfxBoolItem( SID_ATTR_NUMBERFORMAT_SOURCE, rUseSourceFormat ));
207 ScopedVclPtrInstance< NumberFormatDialog > aDlg(m_pWindow, aNumberSet);
208 if( bPercent )
209 aDlg->SetText( m_pFT_NumberFormatForPercent->GetText());
210 if( aDlg->Execute() == RET_OK )
212 const SfxItemSet* pResult = aDlg->GetOutputItemSet();
213 if( pResult )
215 bool bOldSource = rUseSourceFormat;
216 sal_uLong nOldFormat = rnFormatKey;
217 bool bOldMixedState = rbMixedState || rbSourceMixedState;
219 rbMixedState = !lcl_ReadNumberFormatFromItemSet( *pResult, SID_ATTR_NUMBERFORMAT_VALUE, SID_ATTR_NUMBERFORMAT_SOURCE, rnFormatKey, rUseSourceFormat, rbSourceMixedState );
221 //todo this maybe can be removed when the numberformatter dialog does handle mixed state for source format correctly
222 if( bOldMixedState && bOldSource == rUseSourceFormat && nOldFormat == rnFormatKey )
223 rbMixedState = rbSourceMixedState = true;
228 IMPL_LINK( DataLabelResources, CheckHdl, Button*, pBox, void )
230 if( pBox )
231 static_cast<CheckBox*>(pBox)->EnableTriState( false );
232 EnableControls();
235 void DataLabelResources::EnableControls()
237 m_pCBSymbol->Enable( m_pCBNumber->IsChecked() || (m_pCBPercent->IsChecked() && m_pCBPercent->IsEnabled())
238 || m_pCBCategory->IsChecked() );
240 m_pCBWrapText->Enable( m_pCBNumber->IsChecked() || (m_pCBPercent->IsChecked() && m_pCBPercent->IsEnabled())
241 || m_pCBCategory->IsChecked() );
243 // Enable or disable separator, placement and direction based on the check
244 // box states. Note that the check boxes are tri-state.
246 long nNumberOfCheckedLabelParts = 0;
247 if (m_pCBNumber->GetState() != TRISTATE_FALSE)
248 ++nNumberOfCheckedLabelParts;
249 if (m_pCBPercent->GetState() != TRISTATE_FALSE && m_pCBPercent->IsEnabled())
250 ++nNumberOfCheckedLabelParts;
251 if (m_pCBCategory->GetState() != TRISTATE_FALSE)
252 ++nNumberOfCheckedLabelParts;
254 m_pSeparatorResources->Enable( nNumberOfCheckedLabelParts > 1 );
256 bool bEnableTextDir = nNumberOfCheckedLabelParts > 0;
257 m_pBxTextDirection->Enable( bEnableTextDir );
258 bool bEnablePlacement = nNumberOfCheckedLabelParts > 0 && m_pLB_LabelPlacement->GetEntryCount()>1;
259 m_pBxLabelPlacement->Enable( bEnablePlacement );
262 m_pPB_NumberFormatForValue->Enable( m_pNumberFormatter && m_pCBNumber->IsChecked() );
263 m_pPB_NumberFormatForPercent->Enable( m_pNumberFormatter && m_pCBPercent->IsChecked() && m_pCBPercent->IsEnabled() );
265 bool bEnableRotation = ( m_pCBNumber->IsChecked() || m_pCBPercent->IsChecked() || m_pCBCategory->IsChecked() );
266 m_pBxOrientation->Enable( bEnableRotation );
269 bool DataLabelResources::FillItemSet( SfxItemSet* rOutAttrs ) const
271 if( m_pCBNumber->IsChecked() )
273 if( !m_bNumberFormatMixedState )
274 rOutAttrs->Put( SfxUInt32Item( SID_ATTR_NUMBERFORMAT_VALUE, m_nNumberFormatForValue ));
275 if( !m_bSourceFormatMixedState )
276 rOutAttrs->Put( SfxBoolItem( SID_ATTR_NUMBERFORMAT_SOURCE, m_bSourceFormatForValue ));
278 if( m_pCBPercent->IsChecked() )
280 if( !m_bPercentFormatMixedState )
281 rOutAttrs->Put( SfxUInt32Item( SCHATTR_PERCENT_NUMBERFORMAT_VALUE, m_nNumberFormatForPercent ));
282 if( !m_bPercentSourceMixedState )
283 rOutAttrs->Put( SfxBoolItem( SCHATTR_PERCENT_NUMBERFORMAT_SOURCE, m_bSourceFormatForPercent ));
286 if( m_pCBNumber->GetState()!= TRISTATE_INDET )
287 rOutAttrs->Put( SfxBoolItem( SCHATTR_DATADESCR_SHOW_NUMBER, m_pCBNumber->IsChecked() ) );
288 if( m_pCBPercent->GetState()!= TRISTATE_INDET )
289 rOutAttrs->Put( SfxBoolItem( SCHATTR_DATADESCR_SHOW_PERCENTAGE, m_pCBPercent->IsChecked() ) );
290 if( m_pCBCategory->GetState()!= TRISTATE_INDET )
291 rOutAttrs->Put( SfxBoolItem( SCHATTR_DATADESCR_SHOW_CATEGORY, m_pCBCategory->IsChecked() ) );
292 if( m_pCBSymbol->GetState()!= TRISTATE_INDET )
293 rOutAttrs->Put( SfxBoolItem( SCHATTR_DATADESCR_SHOW_SYMBOL, m_pCBSymbol->IsChecked()) );
294 if( m_pCBWrapText->GetState()!= TRISTATE_INDET )
295 rOutAttrs->Put( SfxBoolItem( SCHATTR_DATADESCR_WRAP_TEXT, m_pCBWrapText->IsChecked()) );
297 OUString aSep = our_aLBEntryMap[m_pLB_Separator->GetSelectedEntryPos()];
298 rOutAttrs->Put( SfxStringItem( SCHATTR_DATADESCR_SEPARATOR, aSep) );
300 std::map< sal_uInt16, sal_Int32 >::const_iterator aIt( m_aListBoxToPlacementMap.find(m_pLB_LabelPlacement->GetSelectedEntryPos()) );
301 if(aIt!=m_aListBoxToPlacementMap.end())
303 sal_Int32 nValue = aIt->second;
304 rOutAttrs->Put( SfxInt32Item( SCHATTR_DATADESCR_PLACEMENT, nValue ) );
307 if( m_pLB_TextDirection->GetSelectedEntryCount() > 0 )
308 rOutAttrs->Put( SvxFrameDirectionItem( m_pLB_TextDirection->GetSelectEntryValue(), EE_PARA_WRITINGDIR ) );
310 if( m_pDC_Dial->IsVisible() )
312 sal_Int32 nDegrees = m_pDC_Dial->GetRotation();
313 rOutAttrs->Put(SfxInt32Item( SCHATTR_TEXT_DEGREES, nDegrees ) );
316 return true;
319 void DataLabelResources::Reset(const SfxItemSet& rInAttrs)
321 // default state
322 m_pCBSymbol->Enable( false );
324 lcl_setBoolItemToCheckBox( rInAttrs, SCHATTR_DATADESCR_SHOW_NUMBER, *m_pCBNumber );
325 lcl_setBoolItemToCheckBox( rInAttrs, SCHATTR_DATADESCR_SHOW_PERCENTAGE, *m_pCBPercent );
326 lcl_setBoolItemToCheckBox( rInAttrs, SCHATTR_DATADESCR_SHOW_CATEGORY, *m_pCBCategory );
327 lcl_setBoolItemToCheckBox( rInAttrs, SCHATTR_DATADESCR_SHOW_SYMBOL, *m_pCBSymbol );
328 lcl_setBoolItemToCheckBox( rInAttrs, SCHATTR_DATADESCR_WRAP_TEXT, *m_pCBWrapText );
330 m_bNumberFormatMixedState = !lcl_ReadNumberFormatFromItemSet( rInAttrs, SID_ATTR_NUMBERFORMAT_VALUE, SID_ATTR_NUMBERFORMAT_SOURCE, m_nNumberFormatForValue, m_bSourceFormatForValue, m_bSourceFormatMixedState );
331 m_bPercentFormatMixedState = !lcl_ReadNumberFormatFromItemSet( rInAttrs, SCHATTR_PERCENT_NUMBERFORMAT_VALUE, SCHATTR_PERCENT_NUMBERFORMAT_SOURCE, m_nNumberFormatForPercent, m_bSourceFormatForPercent , m_bPercentSourceMixedState);
333 const SfxPoolItem *pPoolItem = nullptr;
334 if( rInAttrs.GetItemState(SCHATTR_DATADESCR_SEPARATOR, true, &pPoolItem) == SfxItemState::SET )
335 for(sal_uInt32 i=0; i < SAL_N_ELEMENTS(our_aLBEntryMap); ++i )
337 if( our_aLBEntryMap[i] == static_cast<const SfxStringItem*>(pPoolItem)->GetValue())
338 m_pLB_Separator->SelectEntryPos( i );
340 else
341 m_pLB_Separator->SelectEntryPos( 0 );
343 if( rInAttrs.GetItemState(SCHATTR_DATADESCR_PLACEMENT, true, &pPoolItem) == SfxItemState::SET )
345 sal_Int32 nPlacement = static_cast<const SfxInt32Item*>(pPoolItem)->GetValue();
346 std::map< sal_Int32, sal_uInt16 >::const_iterator aIt( m_aPlacementToListBoxMap.find(nPlacement) );
347 if(aIt!=m_aPlacementToListBoxMap.end())
349 sal_uInt16 nPos = aIt->second;
350 m_pLB_LabelPlacement->SelectEntryPos( nPos );
352 else
353 m_pLB_LabelPlacement->SetNoSelection();
355 else
356 m_pLB_LabelPlacement->SetNoSelection();
358 if( rInAttrs.GetItemState(EE_PARA_WRITINGDIR, true, &pPoolItem ) == SfxItemState::SET )
359 m_pLB_TextDirection->SelectEntryValue( static_cast<const SvxFrameDirectionItem*>(pPoolItem)->GetValue() );
361 if( rInAttrs.GetItemState( SCHATTR_TEXT_DEGREES, true, &pPoolItem ) == SfxItemState::SET )
363 sal_Int32 nDegrees = static_cast< const SfxInt32Item * >( pPoolItem )->GetValue();
364 m_pDC_Dial->SetRotation( nDegrees );
366 else
367 m_pDC_Dial->SetRotation( 0 );
369 EnableControls();
372 } //namespace chart
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */