bump product version to 5.0.4.1
[LibreOffice.git] / reportdesign / source / core / sdr / UndoActions.cxx
blob4a35395ffdbc16909a9be93c2ae411dab97cff02
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 "corestrings.hrc"
25 #include "rptui_slotid.hrc"
26 #include "RptDef.hxx"
27 #include "ModuleHelper.hxx"
28 #include "RptObject.hxx"
29 #include "RptPage.hxx"
30 #include "RptResId.hrc"
31 #include "RptModel.hxx"
33 #include <com/sun/star/script/XEventAttacherManager.hpp>
34 #include <com/sun/star/container/XChild.hpp>
35 #include <com/sun/star/container/XNameContainer.hpp>
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/util/XModifyBroadcaster.hpp>
39 #include <connectivity/dbtools.hxx>
40 #include <svl/smplhint.hxx>
41 #include <tools/diagnose_ex.h>
42 #include <comphelper/stl_types.hxx>
43 #include <vcl/svapp.hxx>
44 #include <dbaccess/dbsubcomponentcontroller.hxx>
45 #include <svx/unoshape.hxx>
46 #include <osl/mutex.hxx>
48 namespace rptui
50 using namespace ::com::sun::star;
51 using namespace uno;
52 using namespace lang;
53 using namespace script;
54 using namespace beans;
55 using namespace awt;
56 using namespace util;
57 using namespace container;
58 using namespace report;
60 ::std::mem_fun_t<uno::Reference<report::XSection> , OGroupHelper> OGroupHelper::getMemberFunction(const Reference< XSection >& _xSection)
62 ::std::mem_fun_t<uno::Reference<report::XSection> , OGroupHelper> pMemFunSection = ::std::mem_fun(&OGroupHelper::getFooter);
63 uno::Reference< report::XGroup> xGroup = _xSection->getGroup();
64 if ( xGroup->getHeaderOn() && xGroup->getHeader() == _xSection )
65 pMemFunSection = ::std::mem_fun(&OGroupHelper::getHeader);
66 return pMemFunSection;
69 ::std::mem_fun_t<uno::Reference<report::XSection> , OReportHelper> OReportHelper::getMemberFunction(const Reference< XSection >& _xSection)
71 uno::Reference< report::XReportDefinition> xReportDefinition(_xSection->getReportDefinition());
72 ::std::mem_fun_t<uno::Reference<report::XSection> , OReportHelper> pMemFunSection = ::std::mem_fun(&OReportHelper::getReportFooter);
73 if ( xReportDefinition->getReportHeaderOn() && xReportDefinition->getReportHeader() == _xSection )
74 pMemFunSection = ::std::mem_fun(&OReportHelper::getReportHeader);
75 else if ( xReportDefinition->getPageHeaderOn() && xReportDefinition->getPageHeader() == _xSection )
76 pMemFunSection = ::std::mem_fun(&OReportHelper::getPageHeader);
77 else if ( xReportDefinition->getPageFooterOn() && xReportDefinition->getPageFooter() == _xSection )
78 pMemFunSection = ::std::mem_fun(&OReportHelper::getPageFooter);
79 else if ( xReportDefinition->getDetail() == _xSection )
80 pMemFunSection = ::std::mem_fun(&OReportHelper::getDetail);
81 return pMemFunSection;
85 TYPEINIT1( OCommentUndoAction, SdrUndoAction );
87 OCommentUndoAction::OCommentUndoAction(SdrModel& _rMod,sal_uInt16 nCommentID)
88 :SdrUndoAction(_rMod)
90 m_pController = static_cast< OReportModel& >( _rMod ).getController();
91 if ( nCommentID )
92 m_strComment = ModuleRes(nCommentID);
94 OCommentUndoAction::~OCommentUndoAction()
98 void OCommentUndoAction::Undo()
102 void OCommentUndoAction::Redo()
106 OUndoContainerAction::OUndoContainerAction(SdrModel& _rMod
107 ,Action _eAction
108 ,const uno::Reference< container::XIndexContainer >& rContainer
109 ,const Reference< XInterface > & xElem
110 ,sal_uInt16 _nCommentId)
111 :OCommentUndoAction(_rMod,_nCommentId)
112 ,m_xElement(xElem)
113 ,m_xContainer(rContainer)
114 ,m_eAction( _eAction )
116 // normalize
117 if ( m_eAction == Removed )
118 // we now own the element
119 m_xOwnElement = m_xElement;
122 OUndoContainerAction::~OUndoContainerAction()
124 // if we own the object ....
125 Reference< XComponent > xComp( m_xOwnElement, UNO_QUERY );
126 if ( xComp.is() )
128 // and the object does not have a parent
129 Reference< XChild > xChild( m_xOwnElement, UNO_QUERY );
130 if ( xChild.is() && !xChild->getParent().is() )
132 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
133 rEnv.RemoveElement( m_xOwnElement );
135 #if OSL_DEBUG_LEVEL > 0
136 SvxShape* pShape = SvxShape::getImplementation( xChild );
137 SdrObject* pObject = pShape ? pShape->GetSdrObject() : NULL;
138 OSL_ENSURE( pObject == nullptr || (pShape->HasSdrObjectOwnership() && !pObject->IsInserted()),
139 "OUndoContainerAction::~OUndoContainerAction: inconsistency in the shape/object ownership!" );
140 #endif
141 // -> dispose it
144 comphelper::disposeComponent( xComp );
146 catch ( const uno::Exception& )
148 DBG_UNHANDLED_EXCEPTION();
154 void OUndoContainerAction::implReInsert( )
156 if ( m_xContainer.is() )
158 // insert the element
159 m_xContainer->insertByIndex( m_xContainer->getCount(),uno::makeAny(m_xElement) );
161 // we don't own the object anymore
162 m_xOwnElement = NULL;
166 void OUndoContainerAction::implReRemove( )
168 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
171 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
172 if ( m_xContainer.is() )
174 const sal_Int32 nCount = m_xContainer->getCount();
175 for (sal_Int32 i = 0; i < nCount; ++i)
177 uno::Reference< uno::XInterface> xObj(m_xContainer->getByIndex(i),uno::UNO_QUERY);
178 if ( xObj == m_xElement )
180 m_xContainer->removeByIndex( i );
181 break;
186 catch(uno::Exception&){}
187 // from now on, we own this object
188 m_xOwnElement = m_xElement;
192 void OUndoContainerAction::Undo()
194 if ( m_xElement.is() )
196 // prevents that an undo action will be created for elementInserted
199 switch ( m_eAction )
201 case Inserted:
202 implReRemove();
203 break;
205 case Removed:
206 implReInsert();
207 break;
208 default:
209 OSL_FAIL("Illegal case value");
210 break;
213 catch( const Exception& )
215 OSL_FAIL( "OUndoContainerAction::Undo: caught an exception!" );
221 void OUndoContainerAction::Redo()
223 if ( m_xElement.is() )
227 switch ( m_eAction )
229 case Inserted:
230 implReInsert();
231 break;
233 case Removed:
234 implReRemove();
235 break;
236 default:
237 OSL_FAIL("Illegal case value");
238 break;
241 catch( const Exception& )
243 OSL_FAIL( "OUndoContainerAction::Redo: caught an exception!" );
248 OUndoGroupSectionAction::OUndoGroupSectionAction(SdrModel& _rMod
249 ,Action _eAction
250 ,::std::mem_fun_t< uno::Reference< report::XSection >
251 ,OGroupHelper> _pMemberFunction
252 ,const uno::Reference< report::XGroup >& _xGroup
253 ,const Reference< XInterface > & xElem
254 ,sal_uInt16 _nCommentId)
255 :OUndoContainerAction(_rMod,_eAction,NULL,xElem,_nCommentId)
256 ,m_aGroupHelper(_xGroup)
257 ,m_pMemberFunction(_pMemberFunction)
261 void OUndoGroupSectionAction::implReInsert( )
263 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
266 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
267 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aGroupHelper);
268 if ( xSection.is() )
269 xSection->add(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
271 catch(uno::Exception&){}
273 // we don't own the object anymore
274 m_xOwnElement = NULL;
278 void OUndoGroupSectionAction::implReRemove( )
280 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
283 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
284 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aGroupHelper);
285 if ( xSection.is() )
286 xSection->remove(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
288 catch(uno::Exception&){}
290 // from now on, we own this object
291 m_xOwnElement = m_xElement;
294 OUndoReportSectionAction::OUndoReportSectionAction(SdrModel& _rMod
295 ,Action _eAction
296 ,::std::mem_fun_t< uno::Reference< report::XSection >
297 ,OReportHelper> _pMemberFunction
298 ,const uno::Reference< report::XReportDefinition >& _xReport
299 ,const Reference< XInterface > & xElem
300 ,sal_uInt16 _nCommentId)
301 :OUndoContainerAction(_rMod,_eAction,NULL,xElem,_nCommentId)
302 ,m_aReportHelper(_xReport)
303 ,m_pMemberFunction(_pMemberFunction)
307 void OUndoReportSectionAction::implReInsert( )
309 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
312 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
313 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aReportHelper);
314 if ( xSection.is() )
316 uno::Reference< drawing::XShape> xShape(m_xElement,uno::UNO_QUERY_THROW);
317 awt::Point aPos = xShape->getPosition();
318 awt::Size aSize = xShape->getSize();
319 xSection->add(xShape);
320 xShape->setPosition( aPos );
321 xShape->setSize( aSize );
324 catch(uno::Exception&){}
325 // we don't own the object anymore
326 m_xOwnElement = NULL;
330 void OUndoReportSectionAction::implReRemove( )
332 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
335 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
336 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aReportHelper);
337 if ( xSection.is() )
338 xSection->remove(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
340 catch(uno::Exception&){}
341 // from now on, we own this object
342 m_xOwnElement = m_xElement;
345 ORptUndoPropertyAction::ORptUndoPropertyAction(SdrModel& rNewMod, const PropertyChangeEvent& evt)
346 :OCommentUndoAction(rNewMod,0)
347 ,m_xObj(evt.Source, UNO_QUERY)
348 ,m_aPropertyName(evt.PropertyName)
349 ,m_aNewValue(evt.NewValue)
350 ,m_aOldValue(evt.OldValue)
354 void ORptUndoPropertyAction::Undo()
356 setProperty(true);
360 void ORptUndoPropertyAction::Redo()
362 setProperty(false);
365 Reference< XPropertySet> ORptUndoPropertyAction::getObject()
367 return m_xObj;
370 void ORptUndoPropertyAction::setProperty(bool _bOld)
372 Reference< XPropertySet> xObj = getObject();
374 if (xObj.is() )
378 xObj->setPropertyValue( m_aPropertyName, _bOld ? m_aOldValue : m_aNewValue );
380 catch( const Exception& )
382 OSL_FAIL( "ORptUndoPropertyAction::Redo: caught an exception!" );
387 OUString ORptUndoPropertyAction::GetComment() const
389 OUString aStr( ModuleRes(RID_STR_UNDO_PROPERTY).toString() );
391 return aStr.replaceFirst("#", m_aPropertyName);
394 OUndoPropertyGroupSectionAction::OUndoPropertyGroupSectionAction(SdrModel& _rMod
395 ,const PropertyChangeEvent& evt
396 ,::std::mem_fun_t< uno::Reference< report::XSection >
397 ,OGroupHelper> _pMemberFunction
398 ,const uno::Reference< report::XGroup >& _xGroup
400 :ORptUndoPropertyAction(_rMod,evt)
401 ,m_aGroupHelper(_xGroup)
402 ,m_pMemberFunction(_pMemberFunction)
406 Reference< XPropertySet> OUndoPropertyGroupSectionAction::getObject()
408 return m_pMemberFunction(&m_aGroupHelper).get();
411 OUndoPropertyReportSectionAction::OUndoPropertyReportSectionAction(SdrModel& _rMod
412 ,const PropertyChangeEvent& evt
413 ,::std::mem_fun_t< uno::Reference< report::XSection >
414 ,OReportHelper> _pMemberFunction
415 ,const uno::Reference< report::XReportDefinition >& _xReport
417 :ORptUndoPropertyAction(_rMod,evt)
418 ,m_aReportHelper(_xReport)
419 ,m_pMemberFunction(_pMemberFunction)
423 Reference< XPropertySet> OUndoPropertyReportSectionAction::getObject()
425 return m_pMemberFunction(&m_aReportHelper).get();
428 } // rptui
431 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */