Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / reportdesign / source / ui / misc / RptUndo.cxx
blobe4ec1d3817d5baccec6f87e90c89670f1e44f42d
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 <comphelper/diagnose_ex.hxx>
38 #include <functional>
40 namespace rptui
42 using namespace ::com::sun::star;
43 using namespace uno;
44 using namespace lang;
45 using namespace beans;
46 using namespace awt;
47 using namespace util;
48 using namespace container;
49 using namespace report;
52 namespace
54 void lcl_collectElements(const uno::Reference< report::XSection >& _xSection,::std::vector< uno::Reference< drawing::XShape> >& _rControls)
56 if ( _xSection.is() )
58 sal_Int32 nCount = _xSection->getCount();
59 _rControls.reserve(nCount);
60 while ( nCount )
62 uno::Reference< drawing::XShape> xShape(_xSection->getByIndex(nCount-1),uno::UNO_QUERY);
63 _rControls.push_back(xShape);
64 _xSection->remove(xShape);
65 --nCount;
70 void lcl_insertElements(const uno::Reference< report::XSection >& _xSection,const ::std::vector< uno::Reference< drawing::XShape> >& _aControls)
72 if ( !_xSection.is() )
73 return;
75 ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aIter = _aControls.rbegin();
76 ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aEnd = _aControls.rend();
77 for (; aIter != aEnd; ++aIter)
79 try
81 const awt::Point aPos = (*aIter)->getPosition();
82 const awt::Size aSize = (*aIter)->getSize();
83 _xSection->add(*aIter);
84 (*aIter)->setPosition( aPos );
85 (*aIter)->setSize( aSize );
87 catch(const uno::Exception&)
89 TOOLS_WARN_EXCEPTION( "reportdesign", "lcl_insertElements");
94 void lcl_setValues(const uno::Reference< report::XSection >& _xSection,const ::std::vector< ::std::pair< OUString ,uno::Any> >& _aValues)
96 if ( !_xSection.is() )
97 return;
99 for (const auto& [rPropName, rValue] : _aValues)
103 _xSection->setPropertyValue(rPropName, rValue);
105 catch(const uno::Exception&)
107 TOOLS_WARN_EXCEPTION( "reportdesign", "lcl_setValues");
114 OSectionUndo::OSectionUndo(OReportModel& _rMod
115 ,sal_uInt16 _nSlot
116 ,Action _eAction
117 ,TranslateId pCommentID)
118 : OCommentUndoAction(_rMod,pCommentID)
119 ,m_eAction(_eAction)
120 ,m_nSlot(_nSlot)
121 ,m_bInserted(false)
125 OSectionUndo::~OSectionUndo()
127 if ( m_bInserted )
128 return;
130 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
131 for (uno::Reference<drawing::XShape>& xShape : m_aControls)
133 rEnv.RemoveElement(xShape);
136 comphelper::disposeComponent(xShape);
138 catch(const uno::Exception &)
140 TOOLS_WARN_EXCEPTION( "reportdesign", "");
145 void OSectionUndo::collectControls(const uno::Reference< report::XSection >& _xSection)
147 m_aControls.clear();
150 // copy all properties for restoring
151 uno::Reference< beans::XPropertySetInfo> xInfo = _xSection->getPropertySetInfo();
152 const uno::Sequence< beans::Property> aSeq = xInfo->getProperties();
153 for(const beans::Property& rProp : aSeq)
155 if ( 0 == (rProp.Attributes & beans::PropertyAttribute::READONLY) )
156 m_aValues.emplace_back(rProp.Name,_xSection->getPropertyValue(rProp.Name));
158 lcl_collectElements(_xSection,m_aControls);
160 catch(uno::Exception&)
165 void OSectionUndo::Undo()
169 switch ( m_eAction )
171 case Inserted:
172 implReRemove();
173 break;
175 case Removed:
176 implReInsert();
177 break;
180 catch( const Exception& )
182 TOOLS_WARN_EXCEPTION( "reportdesign", "OSectionUndo::Undo" );
186 void OSectionUndo::Redo()
190 switch ( m_eAction )
192 case Inserted:
193 implReInsert();
194 break;
196 case Removed:
197 implReRemove();
198 break;
201 catch( const Exception& )
203 TOOLS_WARN_EXCEPTION( "reportdesign", "OSectionUndo::Redo" );
207 OReportSectionUndo::OReportSectionUndo(
208 OReportModel& _rMod, sal_uInt16 _nSlot,
209 ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
210 const uno::Reference<report::XReportDefinition>& _xReport, Action _eAction)
211 : OSectionUndo(_rMod, _nSlot, _eAction, {})
212 , m_aReportHelper(_xReport)
213 , m_pMemberFunction(std::move(_pMemberFunction))
215 if( m_eAction == Removed )
216 collectControls(m_pMemberFunction(&m_aReportHelper));
219 OReportSectionUndo::~OReportSectionUndo()
223 void OReportSectionUndo::implReInsert( )
225 const uno::Sequence< beans::PropertyValue > aArgs;
226 m_pController->executeChecked(m_nSlot,aArgs);
227 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aReportHelper);
228 lcl_insertElements(xSection,m_aControls);
229 lcl_setValues(xSection,m_aValues);
230 m_bInserted = true;
233 void OReportSectionUndo::implReRemove( )
235 if( m_eAction == Removed )
236 collectControls(m_pMemberFunction(&m_aReportHelper));
237 const uno::Sequence< beans::PropertyValue > aArgs;
238 m_pController->executeChecked(m_nSlot,aArgs);
239 m_bInserted = false;
242 OGroupSectionUndo::OGroupSectionUndo(
243 OReportModel& _rMod, sal_uInt16 _nSlot,
244 ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
245 const uno::Reference<report::XGroup>& _xGroup, Action _eAction, TranslateId pCommentID)
246 : OSectionUndo(_rMod, _nSlot, _eAction, pCommentID)
247 , m_aGroupHelper(_xGroup)
248 , m_pMemberFunction(std::move(_pMemberFunction))
250 if( m_eAction == Removed )
252 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
253 if ( xSection.is() )
254 m_sName = xSection->getName();
255 collectControls(xSection);
259 OUString OGroupSectionUndo::GetComment() const
261 if ( m_sName.isEmpty() )
265 uno::Reference< report::XSection > xSection = const_cast<OGroupSectionUndo*>(this)->m_pMemberFunction(&const_cast<OGroupSectionUndo*>(this)->m_aGroupHelper);
267 if ( xSection.is() )
268 m_sName = xSection->getName();
270 catch (const uno::Exception&)
274 return m_strComment + m_sName;
277 void OGroupSectionUndo::implReInsert( )
279 const OUString aHeaderFooterOnName(SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? OUString(PROPERTY_HEADERON) : OUString(PROPERTY_FOOTERON));
280 uno::Sequence< beans::PropertyValue > aArgs{
281 comphelper::makePropertyValue(aHeaderFooterOnName, true),
282 comphelper::makePropertyValue(PROPERTY_GROUP, m_aGroupHelper.getGroup())
284 m_pController->executeChecked(m_nSlot,aArgs);
286 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
287 lcl_insertElements(xSection,m_aControls);
288 lcl_setValues(xSection,m_aValues);
289 m_bInserted = true;
292 void OGroupSectionUndo::implReRemove( )
294 if( m_eAction == Removed )
295 collectControls(m_pMemberFunction(&m_aGroupHelper));
297 const OUString aHeaderFooterOnName(SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? OUString(PROPERTY_HEADERON) : OUString(PROPERTY_FOOTERON));
298 uno::Sequence< beans::PropertyValue > aArgs{
299 comphelper::makePropertyValue(aHeaderFooterOnName, false),
300 comphelper::makePropertyValue(PROPERTY_GROUP, m_aGroupHelper.getGroup())
303 m_pController->executeChecked(m_nSlot,aArgs);
304 m_bInserted = false;
308 OGroupUndo::OGroupUndo(OReportModel& _rMod
309 ,TranslateId pCommentID
310 ,Action _eAction
311 ,uno::Reference< report::XGroup> _xGroup
312 ,uno::Reference< report::XReportDefinition > _xReportDefinition)
313 : OCommentUndoAction(_rMod,pCommentID)
314 ,m_xGroup(std::move(_xGroup))
315 ,m_xReportDefinition(std::move(_xReportDefinition))
316 ,m_eAction(_eAction)
318 m_nLastPosition = getPositionInIndexAccess(m_xReportDefinition->getGroups(),m_xGroup);
321 void OGroupUndo::implReInsert( )
325 m_xReportDefinition->getGroups()->insertByIndex(m_nLastPosition,uno::Any(m_xGroup));
327 catch(uno::Exception&)
329 TOOLS_WARN_EXCEPTION( "reportdesign", "Exception caught while undoing remove group");
333 void OGroupUndo::implReRemove( )
337 m_xReportDefinition->getGroups()->removeByIndex(m_nLastPosition);
339 catch(uno::Exception&)
341 TOOLS_WARN_EXCEPTION( "reportdesign", "Exception caught while redoing remove group");
345 void OGroupUndo::Undo()
347 switch ( m_eAction )
349 case Inserted:
350 implReRemove();
351 break;
353 case Removed:
354 implReInsert();
355 break;
360 void OGroupUndo::Redo()
362 switch ( m_eAction )
364 case Inserted:
365 implReInsert();
366 break;
368 case Removed:
369 implReRemove();
370 break;
375 } // rptui
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */