bump product version to 6.3.0.0.beta1
[LibreOffice.git] / reportdesign / source / core / sdr / UndoActions.cxx
blobdb4418c4b34c773e3d6ee9457065c60c9250d588
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 <UndoActions.hxx>
21 #include <UndoEnv.hxx>
22 #include "formatnormalizer.hxx"
23 #include <conditionupdater.hxx>
24 #include <strings.hxx>
25 #include <rptui_slotid.hrc>
26 #include <RptDef.hxx>
27 #include <core_resource.hxx>
28 #include <RptObject.hxx>
29 #include <RptPage.hxx>
30 #include <strings.hrc>
31 #include <RptModel.hxx>
33 #include <com/sun/star/container/XChild.hpp>
34 #include <com/sun/star/container/XNameContainer.hpp>
35 #include <com/sun/star/beans/PropertyAttribute.hpp>
36 #include <com/sun/star/util/XModifyBroadcaster.hpp>
38 #include <comphelper/types.hxx>
39 #include <connectivity/dbtools.hxx>
40 #include <tools/diagnose_ex.h>
41 #include <utility>
42 #include <vcl/svapp.hxx>
43 #include <dbaccess/dbsubcomponentcontroller.hxx>
44 #include <svx/unoshape.hxx>
46 namespace rptui
48 using namespace ::com::sun::star;
49 using namespace uno;
50 using namespace lang;
51 using namespace script;
52 using namespace beans;
53 using namespace awt;
54 using namespace util;
55 using namespace container;
56 using namespace report;
58 ::std::function<uno::Reference<report::XSection>(OGroupHelper *)> OGroupHelper::getMemberFunction(const Reference< XSection >& _xSection)
60 ::std::function<uno::Reference<report::XSection>(OGroupHelper *)> pMemFunSection = ::std::mem_fn(&OGroupHelper::getFooter);
61 uno::Reference< report::XGroup> xGroup = _xSection->getGroup();
62 if ( xGroup->getHeaderOn() && xGroup->getHeader() == _xSection )
63 pMemFunSection = ::std::mem_fn(&OGroupHelper::getHeader);
64 return pMemFunSection;
67 ::std::function<uno::Reference<report::XSection>(OReportHelper *)> OReportHelper::getMemberFunction(const Reference< XSection >& _xSection)
69 uno::Reference< report::XReportDefinition> xReportDefinition(_xSection->getReportDefinition());
70 ::std::function<uno::Reference<report::XSection>(OReportHelper *)> pMemFunSection = ::std::mem_fn(&OReportHelper::getReportFooter);
71 if ( xReportDefinition->getReportHeaderOn() && xReportDefinition->getReportHeader() == _xSection )
72 pMemFunSection = ::std::mem_fn(&OReportHelper::getReportHeader);
73 else if ( xReportDefinition->getPageHeaderOn() && xReportDefinition->getPageHeader() == _xSection )
74 pMemFunSection = ::std::mem_fn(&OReportHelper::getPageHeader);
75 else if ( xReportDefinition->getPageFooterOn() && xReportDefinition->getPageFooter() == _xSection )
76 pMemFunSection = ::std::mem_fn(&OReportHelper::getPageFooter);
77 else if ( xReportDefinition->getDetail() == _xSection )
78 pMemFunSection = ::std::mem_fn(&OReportHelper::getDetail);
79 return pMemFunSection;
83 OCommentUndoAction::OCommentUndoAction(SdrModel& _rMod,const char* pCommentID)
84 :SdrUndoAction(_rMod)
86 m_pController = static_cast< OReportModel& >( _rMod ).getController();
87 if (pCommentID)
88 m_strComment = RptResId(pCommentID);
90 OCommentUndoAction::~OCommentUndoAction()
94 void OCommentUndoAction::Undo()
98 void OCommentUndoAction::Redo()
102 OUndoContainerAction::OUndoContainerAction(SdrModel& _rMod
103 ,Action _eAction
104 ,const uno::Reference< container::XIndexContainer >& rContainer
105 ,const Reference< XInterface > & xElem
106 ,const char* pCommentId)
107 :OCommentUndoAction(_rMod, pCommentId)
108 ,m_xElement(xElem)
109 ,m_xContainer(rContainer)
110 ,m_eAction( _eAction )
112 // normalize
113 if ( m_eAction == Removed )
114 // we now own the element
115 m_xOwnElement = m_xElement;
118 OUndoContainerAction::~OUndoContainerAction()
120 // if we own the object ....
121 Reference< XComponent > xComp( m_xOwnElement, UNO_QUERY );
122 if ( xComp.is() )
124 // and the object does not have a parent
125 Reference< XChild > xChild( m_xOwnElement, UNO_QUERY );
126 if ( xChild.is() && !xChild->getParent().is() )
128 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
129 rEnv.RemoveElement( m_xOwnElement );
131 #if OSL_DEBUG_LEVEL > 0
132 SvxShape* pShape = SvxShape::getImplementation( xChild );
133 SdrObject* pObject = pShape ? pShape->GetSdrObject() : nullptr;
134 OSL_ENSURE( pObject == nullptr || (pShape->HasSdrObjectOwnership() && !pObject->IsInserted()),
135 "OUndoContainerAction::~OUndoContainerAction: inconsistency in the shape/object ownership!" );
136 #endif
137 // -> dispose it
140 comphelper::disposeComponent( xComp );
142 catch ( const uno::Exception& )
144 DBG_UNHANDLED_EXCEPTION("reportdesign");
150 void OUndoContainerAction::implReInsert( )
152 if ( m_xContainer.is() )
154 // insert the element
155 m_xContainer->insertByIndex( m_xContainer->getCount(),uno::makeAny(m_xElement) );
157 // we don't own the object anymore
158 m_xOwnElement = nullptr;
162 void OUndoContainerAction::implReRemove( )
164 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
167 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
168 if ( m_xContainer.is() )
170 const sal_Int32 nCount = m_xContainer->getCount();
171 for (sal_Int32 i = 0; i < nCount; ++i)
173 uno::Reference< uno::XInterface> xObj(m_xContainer->getByIndex(i),uno::UNO_QUERY);
174 if ( xObj == m_xElement )
176 m_xContainer->removeByIndex( i );
177 break;
182 catch(uno::Exception&){}
183 // from now on, we own this object
184 m_xOwnElement = m_xElement;
188 void OUndoContainerAction::Undo()
190 if ( m_xElement.is() )
192 // prevents that an undo action will be created for elementInserted
195 switch ( m_eAction )
197 case Inserted:
198 implReRemove();
199 break;
201 case Removed:
202 implReInsert();
203 break;
204 default:
205 OSL_FAIL("Illegal case value");
206 break;
209 catch( const Exception& )
211 OSL_FAIL( "OUndoContainerAction::Undo: caught an exception!" );
217 void OUndoContainerAction::Redo()
219 if ( m_xElement.is() )
223 switch ( m_eAction )
225 case Inserted:
226 implReInsert();
227 break;
229 case Removed:
230 implReRemove();
231 break;
232 default:
233 OSL_FAIL("Illegal case value");
234 break;
237 catch( const Exception& )
239 OSL_FAIL( "OUndoContainerAction::Redo: caught an exception!" );
244 OUndoGroupSectionAction::OUndoGroupSectionAction(
245 SdrModel& _rMod, Action _eAction,
246 ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
247 const uno::Reference<report::XGroup>& _xGroup, const Reference<XInterface>& xElem,
248 const char* pCommentId)
249 : OUndoContainerAction(_rMod, _eAction, nullptr, xElem, pCommentId)
250 , m_aGroupHelper(_xGroup)
251 , m_pMemberFunction(std::move(_pMemberFunction))
255 void OUndoGroupSectionAction::implReInsert( )
257 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
260 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
261 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aGroupHelper);
262 if ( xSection.is() )
263 xSection->add(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
265 catch(uno::Exception&){}
267 // we don't own the object anymore
268 m_xOwnElement = nullptr;
272 void OUndoGroupSectionAction::implReRemove( )
274 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
277 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
278 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aGroupHelper);
279 if ( xSection.is() )
280 xSection->remove(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
282 catch(uno::Exception&){}
284 // from now on, we own this object
285 m_xOwnElement = m_xElement;
288 OUndoReportSectionAction::OUndoReportSectionAction(
289 SdrModel& _rMod, Action _eAction,
290 ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
291 const uno::Reference<report::XReportDefinition>& _xReport, const Reference<XInterface>& xElem,
292 const char* pCommentId)
293 : OUndoContainerAction(_rMod, _eAction, nullptr, xElem, pCommentId)
294 , m_aReportHelper(_xReport)
295 , m_pMemberFunction(std::move(_pMemberFunction))
299 void OUndoReportSectionAction::implReInsert( )
301 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
304 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
305 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aReportHelper);
306 if ( xSection.is() )
308 uno::Reference< drawing::XShape> xShape(m_xElement,uno::UNO_QUERY_THROW);
309 awt::Point aPos = xShape->getPosition();
310 awt::Size aSize = xShape->getSize();
311 xSection->add(xShape);
312 xShape->setPosition( aPos );
313 xShape->setSize( aSize );
316 catch(uno::Exception&){}
317 // we don't own the object anymore
318 m_xOwnElement = nullptr;
322 void OUndoReportSectionAction::implReRemove( )
324 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
327 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
328 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aReportHelper);
329 if ( xSection.is() )
330 xSection->remove(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
332 catch(uno::Exception&){}
333 // from now on, we own this object
334 m_xOwnElement = m_xElement;
337 ORptUndoPropertyAction::ORptUndoPropertyAction(SdrModel& rNewMod, const PropertyChangeEvent& evt)
338 :OCommentUndoAction(rNewMod,nullptr)
339 ,m_xObj(evt.Source, UNO_QUERY)
340 ,m_aPropertyName(evt.PropertyName)
341 ,m_aNewValue(evt.NewValue)
342 ,m_aOldValue(evt.OldValue)
346 void ORptUndoPropertyAction::Undo()
348 setProperty(true);
352 void ORptUndoPropertyAction::Redo()
354 setProperty(false);
357 Reference< XPropertySet> ORptUndoPropertyAction::getObject()
359 return m_xObj;
362 void ORptUndoPropertyAction::setProperty(bool _bOld)
364 Reference< XPropertySet> xObj = getObject();
366 if (xObj.is() )
370 xObj->setPropertyValue( m_aPropertyName, _bOld ? m_aOldValue : m_aNewValue );
372 catch( const Exception& )
374 OSL_FAIL( "ORptUndoPropertyAction::Redo: caught an exception!" );
379 OUString ORptUndoPropertyAction::GetComment() const
381 OUString aStr( RptResId(RID_STR_UNDO_PROPERTY) );
383 return aStr.replaceFirst("#", m_aPropertyName);
386 OUndoPropertyGroupSectionAction::OUndoPropertyGroupSectionAction(
387 SdrModel& _rMod, const PropertyChangeEvent& evt,
388 ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
389 const uno::Reference<report::XGroup>& _xGroup)
390 : ORptUndoPropertyAction(_rMod, evt)
391 , m_aGroupHelper(_xGroup)
392 , m_pMemberFunction(std::move(_pMemberFunction))
396 Reference< XPropertySet> OUndoPropertyGroupSectionAction::getObject()
398 return m_pMemberFunction(&m_aGroupHelper).get();
401 OUndoPropertyReportSectionAction::OUndoPropertyReportSectionAction(
402 SdrModel& _rMod, const PropertyChangeEvent& evt,
403 ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
404 const uno::Reference<report::XReportDefinition>& _xReport)
405 : ORptUndoPropertyAction(_rMod, evt)
406 , m_aReportHelper(_xReport)
407 , m_pMemberFunction(std::move(_pMemberFunction))
411 Reference< XPropertySet> OUndoPropertyReportSectionAction::getObject()
413 return m_pMemberFunction(&m_aReportHelper).get();
416 } // rptui
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */