1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <core_resource.hxx>
23 #include <strings.hrc>
24 #include <RptModel.hxx>
26 #include <com/sun/star/container/XChild.hpp>
28 #include <comphelper/types.hxx>
29 #include <comphelper/diagnose_ex.hxx>
31 #include <dbaccess/dbsubcomponentcontroller.hxx>
32 #include <svx/unoshape.hxx>
36 using namespace ::com::sun::star
;
39 using namespace beans
;
41 using namespace container
;
42 using namespace report
;
44 ::std::function
<uno::Reference
<report::XSection
>(OGroupHelper
*)> OGroupHelper::getMemberFunction(const Reference
< XSection
>& _xSection
)
46 ::std::function
<uno::Reference
<report::XSection
>(OGroupHelper
*)> pMemFunSection
= ::std::mem_fn(&OGroupHelper::getFooter
);
47 uno::Reference
< report::XGroup
> xGroup
= _xSection
->getGroup();
48 if ( xGroup
->getHeaderOn() && xGroup
->getHeader() == _xSection
)
49 pMemFunSection
= ::std::mem_fn(&OGroupHelper::getHeader
);
50 return pMemFunSection
;
53 ::std::function
<uno::Reference
<report::XSection
>(OReportHelper
*)> OReportHelper::getMemberFunction(const Reference
< XSection
>& _xSection
)
55 uno::Reference
< report::XReportDefinition
> xReportDefinition(_xSection
->getReportDefinition());
56 ::std::function
<uno::Reference
<report::XSection
>(OReportHelper
*)> pMemFunSection
= ::std::mem_fn(&OReportHelper::getReportFooter
);
57 if ( xReportDefinition
->getReportHeaderOn() && xReportDefinition
->getReportHeader() == _xSection
)
58 pMemFunSection
= ::std::mem_fn(&OReportHelper::getReportHeader
);
59 else if ( xReportDefinition
->getPageHeaderOn() && xReportDefinition
->getPageHeader() == _xSection
)
60 pMemFunSection
= ::std::mem_fn(&OReportHelper::getPageHeader
);
61 else if ( xReportDefinition
->getPageFooterOn() && xReportDefinition
->getPageFooter() == _xSection
)
62 pMemFunSection
= ::std::mem_fn(&OReportHelper::getPageFooter
);
63 else if ( xReportDefinition
->getDetail() == _xSection
)
64 pMemFunSection
= ::std::mem_fn(&OReportHelper::getDetail
);
65 return pMemFunSection
;
69 OCommentUndoAction::OCommentUndoAction(SdrModel
& _rMod
, TranslateId pCommentID
)
72 m_pController
= static_cast< OReportModel
& >( _rMod
).getController();
74 m_strComment
= RptResId(pCommentID
);
76 OCommentUndoAction::~OCommentUndoAction()
80 void OCommentUndoAction::Undo()
84 void OCommentUndoAction::Redo()
88 OUndoContainerAction::OUndoContainerAction(SdrModel
& _rMod
90 ,uno::Reference
< container::XIndexContainer
> xContainer
91 ,const Reference
< XInterface
> & xElem
92 ,TranslateId pCommentId
)
93 :OCommentUndoAction(_rMod
, pCommentId
)
95 ,m_xContainer(std::move(xContainer
))
96 ,m_eAction( _eAction
)
99 if ( m_eAction
== Removed
)
100 // we now own the element
101 m_xOwnElement
= m_xElement
;
104 OUndoContainerAction::~OUndoContainerAction()
106 // if we own the object...
107 Reference
< XComponent
> xComp( m_xOwnElement
, UNO_QUERY
);
111 // and the object does not have a parent
112 Reference
< XChild
> xChild( m_xOwnElement
, UNO_QUERY
);
113 if ( !xChild
.is() || xChild
->getParent().is() )
116 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( m_rMod
).GetUndoEnv();
117 rEnv
.RemoveElement( m_xOwnElement
);
122 comphelper::disposeComponent( xComp
);
124 catch ( const uno::Exception
& )
126 DBG_UNHANDLED_EXCEPTION("reportdesign");
130 void OUndoContainerAction::implReInsert( )
132 if ( m_xContainer
.is() )
134 // insert the element
135 m_xContainer
->insertByIndex( m_xContainer
->getCount(),uno::Any(m_xElement
) );
137 // we don't own the object anymore
138 m_xOwnElement
= nullptr;
142 void OUndoContainerAction::implReRemove( )
144 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( m_rMod
).GetUndoEnv();
147 OXUndoEnvironment::OUndoEnvLock
aLock(rEnv
);
148 if ( m_xContainer
.is() )
150 const sal_Int32 nCount
= m_xContainer
->getCount();
151 for (sal_Int32 i
= 0; i
< nCount
; ++i
)
153 uno::Reference
< uno::XInterface
> xObj(m_xContainer
->getByIndex(i
),uno::UNO_QUERY
);
154 if ( xObj
== m_xElement
)
156 m_xContainer
->removeByIndex( i
);
162 catch(uno::Exception
&){}
163 // from now on, we own this object
164 m_xOwnElement
= m_xElement
;
168 void OUndoContainerAction::Undo()
170 if ( !m_xElement
.is() )
173 // prevents that an undo action will be created for elementInserted
186 OSL_FAIL("Illegal case value");
190 catch( const Exception
& )
192 TOOLS_WARN_EXCEPTION( "reportdesign", "OUndoContainerAction::Undo" );
197 void OUndoContainerAction::Redo()
199 if ( !m_xElement
.is() )
214 OSL_FAIL("Illegal case value");
218 catch( const Exception
& )
220 TOOLS_WARN_EXCEPTION( "reportdesign", "OUndoContainerAction::Redo" );
224 OUndoGroupSectionAction::OUndoGroupSectionAction(
225 SdrModel
& _rMod
, Action _eAction
,
226 ::std::function
<uno::Reference
<report::XSection
>(OGroupHelper
*)> _pMemberFunction
,
227 const uno::Reference
<report::XGroup
>& _xGroup
, const Reference
<XInterface
>& xElem
,
228 TranslateId pCommentId
)
229 : OUndoContainerAction(_rMod
, _eAction
, nullptr, xElem
, pCommentId
)
230 , m_aGroupHelper(_xGroup
)
231 , m_pMemberFunction(std::move(_pMemberFunction
))
235 void OUndoGroupSectionAction::implReInsert( )
237 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( m_rMod
).GetUndoEnv();
240 OXUndoEnvironment::OUndoEnvLock
aLock(rEnv
);
241 uno::Reference
< report::XSection
> xSection
= m_pMemberFunction(&m_aGroupHelper
);
243 xSection
->add(uno::Reference
< drawing::XShape
>(m_xElement
,uno::UNO_QUERY
));
245 catch(uno::Exception
&){}
247 // we don't own the object anymore
248 m_xOwnElement
= nullptr;
252 void OUndoGroupSectionAction::implReRemove( )
254 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( m_rMod
).GetUndoEnv();
257 OXUndoEnvironment::OUndoEnvLock
aLock(rEnv
);
258 uno::Reference
< report::XSection
> xSection
= m_pMemberFunction(&m_aGroupHelper
);
260 xSection
->remove(uno::Reference
< drawing::XShape
>(m_xElement
,uno::UNO_QUERY
));
262 catch(uno::Exception
&){}
264 // from now on, we own this object
265 m_xOwnElement
= m_xElement
;
268 OUndoReportSectionAction::OUndoReportSectionAction(
269 SdrModel
& _rMod
, Action _eAction
,
270 ::std::function
<uno::Reference
<report::XSection
>(OReportHelper
*)> _pMemberFunction
,
271 const uno::Reference
<report::XReportDefinition
>& _xReport
, const Reference
<XInterface
>& xElem
,
272 TranslateId pCommentId
)
273 : OUndoContainerAction(_rMod
, _eAction
, nullptr, xElem
, pCommentId
)
274 , m_aReportHelper(_xReport
)
275 , m_pMemberFunction(std::move(_pMemberFunction
))
279 void OUndoReportSectionAction::implReInsert( )
281 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( m_rMod
).GetUndoEnv();
284 OXUndoEnvironment::OUndoEnvLock
aLock(rEnv
);
285 uno::Reference
< report::XSection
> xSection
= m_pMemberFunction(&m_aReportHelper
);
288 uno::Reference
< drawing::XShape
> xShape(m_xElement
,uno::UNO_QUERY_THROW
);
289 awt::Point aPos
= xShape
->getPosition();
290 awt::Size aSize
= xShape
->getSize();
291 xSection
->add(xShape
);
292 xShape
->setPosition( aPos
);
293 xShape
->setSize( aSize
);
296 catch(uno::Exception
&){}
297 // we don't own the object anymore
298 m_xOwnElement
= nullptr;
302 void OUndoReportSectionAction::implReRemove( )
304 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( m_rMod
).GetUndoEnv();
307 OXUndoEnvironment::OUndoEnvLock
aLock(rEnv
);
308 uno::Reference
< report::XSection
> xSection
= m_pMemberFunction(&m_aReportHelper
);
310 xSection
->remove(uno::Reference
< drawing::XShape
>(m_xElement
,uno::UNO_QUERY
));
312 catch(uno::Exception
&){}
313 // from now on, we own this object
314 m_xOwnElement
= m_xElement
;
317 ORptUndoPropertyAction::ORptUndoPropertyAction(SdrModel
& rNewMod
, const PropertyChangeEvent
& evt
)
318 :OCommentUndoAction(rNewMod
,{})
319 ,m_xObj(evt
.Source
, UNO_QUERY
)
320 ,m_aPropertyName(evt
.PropertyName
)
321 ,m_aNewValue(evt
.NewValue
)
322 ,m_aOldValue(evt
.OldValue
)
326 void ORptUndoPropertyAction::Undo()
332 void ORptUndoPropertyAction::Redo()
337 Reference
< XPropertySet
> ORptUndoPropertyAction::getObject()
342 void ORptUndoPropertyAction::setProperty(bool _bOld
)
344 Reference
< XPropertySet
> xObj
= getObject();
350 xObj
->setPropertyValue( m_aPropertyName
, _bOld
? m_aOldValue
: m_aNewValue
);
352 catch( const Exception
& )
354 TOOLS_WARN_EXCEPTION( "reportdesign", "ORptUndoPropertyAction::Redo" );
359 OUString
ORptUndoPropertyAction::GetComment() const
361 OUString
aStr( RptResId(RID_STR_UNDO_PROPERTY
) );
363 return aStr
.replaceFirst("#", m_aPropertyName
);
366 OUndoPropertyGroupSectionAction::OUndoPropertyGroupSectionAction(
367 SdrModel
& _rMod
, const PropertyChangeEvent
& evt
,
368 ::std::function
<uno::Reference
<report::XSection
>(OGroupHelper
*)> _pMemberFunction
,
369 const uno::Reference
<report::XGroup
>& _xGroup
)
370 : ORptUndoPropertyAction(_rMod
, evt
)
371 , m_aGroupHelper(_xGroup
)
372 , m_pMemberFunction(std::move(_pMemberFunction
))
376 Reference
< XPropertySet
> OUndoPropertyGroupSectionAction::getObject()
378 return m_pMemberFunction(&m_aGroupHelper
);
381 OUndoPropertyReportSectionAction::OUndoPropertyReportSectionAction(
382 SdrModel
& _rMod
, const PropertyChangeEvent
& evt
,
383 ::std::function
<uno::Reference
<report::XSection
>(OReportHelper
*)> _pMemberFunction
,
384 const uno::Reference
<report::XReportDefinition
>& _xReport
)
385 : ORptUndoPropertyAction(_rMod
, evt
)
386 , m_aReportHelper(_xReport
)
387 , m_pMemberFunction(std::move(_pMemberFunction
))
391 Reference
< XPropertySet
> OUndoPropertyReportSectionAction::getObject()
393 return m_pMemberFunction(&m_aReportHelper
);
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */