bump product version to 6.3.0.0.beta1
[LibreOffice.git] / reportdesign / source / ui / misc / RptUndo.cxx
blob3bfd9d22ca8074da588851427147fe9a6552d335
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/types.hxx>
33 #include <svx/unoshape.hxx>
34 #include <utility>
35 #include <vcl/settings.hxx>
37 #include <functional>
39 namespace rptui
41 using namespace ::com::sun::star;
42 using namespace uno;
43 using namespace lang;
44 using namespace beans;
45 using namespace awt;
46 using namespace util;
47 using namespace container;
48 using namespace report;
51 namespace
53 void lcl_collectElements(const uno::Reference< report::XSection >& _xSection,::std::vector< uno::Reference< drawing::XShape> >& _rControls)
55 if ( _xSection.is() )
57 sal_Int32 nCount = _xSection->getCount();
58 _rControls.reserve(nCount);
59 while ( nCount )
61 uno::Reference< drawing::XShape> xShape(_xSection->getByIndex(nCount-1),uno::UNO_QUERY);
62 _rControls.push_back(xShape);
63 _xSection->remove(xShape);
64 --nCount;
69 void lcl_insertElements(const uno::Reference< report::XSection >& _xSection,const ::std::vector< uno::Reference< drawing::XShape> >& _aControls)
71 if ( _xSection.is() )
73 ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aIter = _aControls.rbegin();
74 ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aEnd = _aControls.rend();
75 for (; aIter != aEnd; ++aIter)
77 try
79 const awt::Point aPos = (*aIter)->getPosition();
80 const awt::Size aSize = (*aIter)->getSize();
81 _xSection->add(*aIter);
82 (*aIter)->setPosition( aPos );
83 (*aIter)->setSize( aSize );
85 catch(const uno::Exception&)
87 OSL_FAIL("lcl_insertElements:Exception caught!");
93 void lcl_setValues(const uno::Reference< report::XSection >& _xSection,const ::std::vector< ::std::pair< OUString ,uno::Any> >& _aValues)
95 if ( _xSection.is() )
97 for (const auto& [rPropName, rValue] : _aValues)
99 try
101 _xSection->setPropertyValue(rPropName, rValue);
103 catch(const uno::Exception&)
105 OSL_FAIL("lcl_setValues:Exception caught!");
113 OSectionUndo::OSectionUndo(OReportModel& _rMod
114 ,sal_uInt16 _nSlot
115 ,Action _eAction
116 ,const char* pCommentID)
117 : OCommentUndoAction(_rMod,pCommentID)
118 ,m_eAction(_eAction)
119 ,m_nSlot(_nSlot)
120 ,m_bInserted(false)
124 OSectionUndo::~OSectionUndo()
126 if ( !m_bInserted )
128 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
129 for (uno::Reference<drawing::XShape>& xShape : m_aControls)
131 rEnv.RemoveElement(xShape);
133 #if OSL_DEBUG_LEVEL > 0
134 SvxShape* pShape = SvxShape::getImplementation( xShape );
135 SdrObject* pObject = pShape ? pShape->GetSdrObject() : nullptr;
136 OSL_ENSURE( pShape && pShape->HasSdrObjectOwnership() && pObject && !pObject->IsInserted(),
137 "OSectionUndo::~OSectionUndo: inconsistency in the shape/object ownership!" );
138 #endif
141 comphelper::disposeComponent(xShape);
143 catch(const uno::Exception &)
145 OSL_FAIL("Exception caught!");
151 void OSectionUndo::collectControls(const uno::Reference< report::XSection >& _xSection)
153 m_aControls.clear();
156 // copy all properties for restoring
157 uno::Reference< beans::XPropertySetInfo> xInfo = _xSection->getPropertySetInfo();
158 uno::Sequence< beans::Property> aSeq = xInfo->getProperties();
159 const beans::Property* pIter = aSeq.getConstArray();
160 const beans::Property* pEnd = pIter + aSeq.getLength();
161 for(;pIter != pEnd;++pIter)
163 if ( 0 == (pIter->Attributes & beans::PropertyAttribute::READONLY) )
164 m_aValues.emplace_back(pIter->Name,_xSection->getPropertyValue(pIter->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 OSL_FAIL( "OSectionUndo::Undo: caught an exception!" );
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 OSL_FAIL( "OSectionUndo::Redo: caught an exception!" );
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, nullptr)
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, const char* 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 uno::Sequence< beans::PropertyValue > aArgs(2);
289 aArgs[0].Name = SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? OUString(PROPERTY_HEADERON) : OUString(PROPERTY_FOOTERON);
290 aArgs[0].Value <<= true;
291 aArgs[1].Name = PROPERTY_GROUP;
292 aArgs[1].Value <<= m_aGroupHelper.getGroup();
293 m_pController->executeChecked(m_nSlot,aArgs);
295 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
296 lcl_insertElements(xSection,m_aControls);
297 lcl_setValues(xSection,m_aValues);
298 m_bInserted = true;
301 void OGroupSectionUndo::implReRemove( )
303 if( m_eAction == Removed )
304 collectControls(m_pMemberFunction(&m_aGroupHelper));
306 uno::Sequence< beans::PropertyValue > aArgs(2);
308 aArgs[0].Name = SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? OUString(PROPERTY_HEADERON) : OUString(PROPERTY_FOOTERON);
309 aArgs[0].Value <<= false;
310 aArgs[1].Name = PROPERTY_GROUP;
311 aArgs[1].Value <<= m_aGroupHelper.getGroup();
313 m_pController->executeChecked(m_nSlot,aArgs);
314 m_bInserted = false;
318 OGroupUndo::OGroupUndo(OReportModel& _rMod
319 ,const char* pCommentID
320 ,Action _eAction
321 ,const uno::Reference< report::XGroup>& _xGroup
322 ,const uno::Reference< report::XReportDefinition >& _xReportDefinition)
323 : OCommentUndoAction(_rMod,pCommentID)
324 ,m_xGroup(_xGroup)
325 ,m_xReportDefinition(_xReportDefinition)
326 ,m_eAction(_eAction)
328 m_nLastPosition = getPositionInIndexAccess(m_xReportDefinition->getGroups().get(),m_xGroup);
331 void OGroupUndo::implReInsert( )
335 m_xReportDefinition->getGroups()->insertByIndex(m_nLastPosition,uno::makeAny(m_xGroup));
337 catch(uno::Exception&)
339 OSL_FAIL("Exception caught while undoing remove group");
343 void OGroupUndo::implReRemove( )
347 m_xReportDefinition->getGroups()->removeByIndex(m_nLastPosition);
349 catch(uno::Exception&)
351 OSL_FAIL("Exception caught while redoing remove group");
355 void OGroupUndo::Undo()
357 switch ( m_eAction )
359 case Inserted:
360 implReRemove();
361 break;
363 case Removed:
364 implReInsert();
365 break;
370 void OGroupUndo::Redo()
372 switch ( m_eAction )
374 case Inserted:
375 implReInsert();
376 break;
378 case Removed:
379 implReRemove();
380 break;
385 } // rptui
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */