Use o3tl::convert in Math
[LibreOffice.git] / reportdesign / source / ui / misc / RptUndo.cxx
blobc43d1a3a5ba7a3d5e502f09408b5bca6553611db
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 <RptUndo.hxx>
21 #include <strings.hxx>
22 #include <rptui_slotid.hrc>
23 #include <UITools.hxx>
24 #include <UndoEnv.hxx>
26 #include <dbaccess/IController.hxx>
27 #include <com/sun/star/report/XSection.hpp>
28 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <com/sun/star/awt/Point.hpp>
31 #include <com/sun/star/awt/Size.hpp>
32 #include <comphelper/propertyvalue.hxx>
33 #include <comphelper/types.hxx>
34 #include <svx/unoshape.hxx>
35 #include <utility>
36 #include <tools/diagnose_ex.h>
38 #include <functional>
39 #include <string_view>
41 namespace rptui
43 using namespace ::com::sun::star;
44 using namespace uno;
45 using namespace lang;
46 using namespace beans;
47 using namespace awt;
48 using namespace util;
49 using namespace container;
50 using namespace report;
53 namespace
55 void lcl_collectElements(const uno::Reference< report::XSection >& _xSection,::std::vector< uno::Reference< drawing::XShape> >& _rControls)
57 if ( _xSection.is() )
59 sal_Int32 nCount = _xSection->getCount();
60 _rControls.reserve(nCount);
61 while ( nCount )
63 uno::Reference< drawing::XShape> xShape(_xSection->getByIndex(nCount-1),uno::UNO_QUERY);
64 _rControls.push_back(xShape);
65 _xSection->remove(xShape);
66 --nCount;
71 void lcl_insertElements(const uno::Reference< report::XSection >& _xSection,const ::std::vector< uno::Reference< drawing::XShape> >& _aControls)
73 if ( !_xSection.is() )
74 return;
76 ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aIter = _aControls.rbegin();
77 ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aEnd = _aControls.rend();
78 for (; aIter != aEnd; ++aIter)
80 try
82 const awt::Point aPos = (*aIter)->getPosition();
83 const awt::Size aSize = (*aIter)->getSize();
84 _xSection->add(*aIter);
85 (*aIter)->setPosition( aPos );
86 (*aIter)->setSize( aSize );
88 catch(const uno::Exception&)
90 TOOLS_WARN_EXCEPTION( "reportdesign", "lcl_insertElements");
95 void lcl_setValues(const uno::Reference< report::XSection >& _xSection,const ::std::vector< ::std::pair< OUString ,uno::Any> >& _aValues)
97 if ( !_xSection.is() )
98 return;
100 for (const auto& [rPropName, rValue] : _aValues)
104 _xSection->setPropertyValue(rPropName, rValue);
106 catch(const uno::Exception&)
108 TOOLS_WARN_EXCEPTION( "reportdesign", "lcl_setValues");
115 OSectionUndo::OSectionUndo(OReportModel& _rMod
116 ,sal_uInt16 _nSlot
117 ,Action _eAction
118 ,TranslateId pCommentID)
119 : OCommentUndoAction(_rMod,pCommentID)
120 ,m_eAction(_eAction)
121 ,m_nSlot(_nSlot)
122 ,m_bInserted(false)
126 OSectionUndo::~OSectionUndo()
128 if ( m_bInserted )
129 return;
131 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
132 for (uno::Reference<drawing::XShape>& xShape : m_aControls)
134 rEnv.RemoveElement(xShape);
136 #if OSL_DEBUG_LEVEL > 0
137 SvxShape* pShape = comphelper::getFromUnoTunnel<SvxShape>( xShape );
138 SdrObject* pObject = pShape ? pShape->GetSdrObject() : nullptr;
139 OSL_ENSURE( pShape && pShape->HasSdrObjectOwnership() && pObject && !pObject->IsInserted(),
140 "OSectionUndo::~OSectionUndo: inconsistency in the shape/object ownership!" );
141 #endif
144 comphelper::disposeComponent(xShape);
146 catch(const uno::Exception &)
148 TOOLS_WARN_EXCEPTION( "reportdesign", "");
153 void OSectionUndo::collectControls(const uno::Reference< report::XSection >& _xSection)
155 m_aControls.clear();
158 // copy all properties for restoring
159 uno::Reference< beans::XPropertySetInfo> xInfo = _xSection->getPropertySetInfo();
160 const uno::Sequence< beans::Property> aSeq = xInfo->getProperties();
161 for(const beans::Property& rProp : aSeq)
163 if ( 0 == (rProp.Attributes & beans::PropertyAttribute::READONLY) )
164 m_aValues.emplace_back(rProp.Name,_xSection->getPropertyValue(rProp.Name));
166 lcl_collectElements(_xSection,m_aControls);
168 catch(uno::Exception&)
173 void OSectionUndo::Undo()
177 switch ( m_eAction )
179 case Inserted:
180 implReRemove();
181 break;
183 case Removed:
184 implReInsert();
185 break;
188 catch( const Exception& )
190 TOOLS_WARN_EXCEPTION( "reportdesign", "OSectionUndo::Undo" );
194 void OSectionUndo::Redo()
198 switch ( m_eAction )
200 case Inserted:
201 implReInsert();
202 break;
204 case Removed:
205 implReRemove();
206 break;
209 catch( const Exception& )
211 TOOLS_WARN_EXCEPTION( "reportdesign", "OSectionUndo::Redo" );
215 OReportSectionUndo::OReportSectionUndo(
216 OReportModel& _rMod, sal_uInt16 _nSlot,
217 ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
218 const uno::Reference<report::XReportDefinition>& _xReport, Action _eAction)
219 : OSectionUndo(_rMod, _nSlot, _eAction, {})
220 , m_aReportHelper(_xReport)
221 , m_pMemberFunction(std::move(_pMemberFunction))
223 if( m_eAction == Removed )
224 collectControls(m_pMemberFunction(&m_aReportHelper));
227 OReportSectionUndo::~OReportSectionUndo()
231 void OReportSectionUndo::implReInsert( )
233 const uno::Sequence< beans::PropertyValue > aArgs;
234 m_pController->executeChecked(m_nSlot,aArgs);
235 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aReportHelper);
236 lcl_insertElements(xSection,m_aControls);
237 lcl_setValues(xSection,m_aValues);
238 m_bInserted = true;
241 void OReportSectionUndo::implReRemove( )
243 if( m_eAction == Removed )
244 collectControls(m_pMemberFunction(&m_aReportHelper));
245 const uno::Sequence< beans::PropertyValue > aArgs;
246 m_pController->executeChecked(m_nSlot,aArgs);
247 m_bInserted = false;
250 OGroupSectionUndo::OGroupSectionUndo(
251 OReportModel& _rMod, sal_uInt16 _nSlot,
252 ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
253 const uno::Reference<report::XGroup>& _xGroup, Action _eAction, TranslateId pCommentID)
254 : OSectionUndo(_rMod, _nSlot, _eAction, pCommentID)
255 , m_aGroupHelper(_xGroup)
256 , m_pMemberFunction(std::move(_pMemberFunction))
258 if( m_eAction == Removed )
260 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
261 if ( xSection.is() )
262 m_sName = xSection->getName();
263 collectControls(xSection);
267 OUString OGroupSectionUndo::GetComment() const
269 if ( m_sName.isEmpty() )
273 uno::Reference< report::XSection > xSection = const_cast<OGroupSectionUndo*>(this)->m_pMemberFunction(&const_cast<OGroupSectionUndo*>(this)->m_aGroupHelper);
275 if ( xSection.is() )
276 m_sName = xSection->getName();
278 catch (const uno::Exception&)
282 return m_strComment + m_sName;
285 void OGroupSectionUndo::implReInsert( )
287 const OUString aHeaderFooterOnName(SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? std::u16string_view(u"" PROPERTY_HEADERON) : std::u16string_view(u"" PROPERTY_FOOTERON));
288 uno::Sequence< beans::PropertyValue > aArgs{
289 comphelper::makePropertyValue(aHeaderFooterOnName, true),
290 comphelper::makePropertyValue(PROPERTY_GROUP, m_aGroupHelper.getGroup())
292 m_pController->executeChecked(m_nSlot,aArgs);
294 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
295 lcl_insertElements(xSection,m_aControls);
296 lcl_setValues(xSection,m_aValues);
297 m_bInserted = true;
300 void OGroupSectionUndo::implReRemove( )
302 if( m_eAction == Removed )
303 collectControls(m_pMemberFunction(&m_aGroupHelper));
305 const OUString aHeaderFooterOnName(SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? std::u16string_view(u"" PROPERTY_HEADERON) : std::u16string_view(u"" PROPERTY_FOOTERON));
306 uno::Sequence< beans::PropertyValue > aArgs{
307 comphelper::makePropertyValue(aHeaderFooterOnName, false),
308 comphelper::makePropertyValue(PROPERTY_GROUP, m_aGroupHelper.getGroup())
311 m_pController->executeChecked(m_nSlot,aArgs);
312 m_bInserted = false;
316 OGroupUndo::OGroupUndo(OReportModel& _rMod
317 ,TranslateId pCommentID
318 ,Action _eAction
319 ,const uno::Reference< report::XGroup>& _xGroup
320 ,const uno::Reference< report::XReportDefinition >& _xReportDefinition)
321 : OCommentUndoAction(_rMod,pCommentID)
322 ,m_xGroup(_xGroup)
323 ,m_xReportDefinition(_xReportDefinition)
324 ,m_eAction(_eAction)
326 m_nLastPosition = getPositionInIndexAccess(m_xReportDefinition->getGroups(),m_xGroup);
329 void OGroupUndo::implReInsert( )
333 m_xReportDefinition->getGroups()->insertByIndex(m_nLastPosition,uno::makeAny(m_xGroup));
335 catch(uno::Exception&)
337 TOOLS_WARN_EXCEPTION( "reportdesign", "Exception caught while undoing remove group");
341 void OGroupUndo::implReRemove( )
345 m_xReportDefinition->getGroups()->removeByIndex(m_nLastPosition);
347 catch(uno::Exception&)
349 TOOLS_WARN_EXCEPTION( "reportdesign", "Exception caught while redoing remove group");
353 void OGroupUndo::Undo()
355 switch ( m_eAction )
357 case Inserted:
358 implReRemove();
359 break;
361 case Removed:
362 implReInsert();
363 break;
368 void OGroupUndo::Redo()
370 switch ( m_eAction )
372 case Inserted:
373 implReInsert();
374 break;
376 case Removed:
377 implReRemove();
378 break;
383 } // rptui
386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */