bump product version to 7.6.3.2-android
[LibreOffice.git] / reportdesign / source / core / sdr / UndoActions.cxx
bloba63161a1b9770c67c5ffabb2d539f124f27e68ad
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 <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>
30 #include <utility>
31 #include <dbaccess/dbsubcomponentcontroller.hxx>
32 #include <svx/unoshape.hxx>
34 namespace rptui
36 using namespace ::com::sun::star;
37 using namespace uno;
38 using namespace lang;
39 using namespace script;
40 using namespace beans;
41 using namespace awt;
42 using namespace util;
43 using namespace container;
44 using namespace report;
46 ::std::function<uno::Reference<report::XSection>(OGroupHelper *)> OGroupHelper::getMemberFunction(const Reference< XSection >& _xSection)
48 ::std::function<uno::Reference<report::XSection>(OGroupHelper *)> pMemFunSection = ::std::mem_fn(&OGroupHelper::getFooter);
49 uno::Reference< report::XGroup> xGroup = _xSection->getGroup();
50 if ( xGroup->getHeaderOn() && xGroup->getHeader() == _xSection )
51 pMemFunSection = ::std::mem_fn(&OGroupHelper::getHeader);
52 return pMemFunSection;
55 ::std::function<uno::Reference<report::XSection>(OReportHelper *)> OReportHelper::getMemberFunction(const Reference< XSection >& _xSection)
57 uno::Reference< report::XReportDefinition> xReportDefinition(_xSection->getReportDefinition());
58 ::std::function<uno::Reference<report::XSection>(OReportHelper *)> pMemFunSection = ::std::mem_fn(&OReportHelper::getReportFooter);
59 if ( xReportDefinition->getReportHeaderOn() && xReportDefinition->getReportHeader() == _xSection )
60 pMemFunSection = ::std::mem_fn(&OReportHelper::getReportHeader);
61 else if ( xReportDefinition->getPageHeaderOn() && xReportDefinition->getPageHeader() == _xSection )
62 pMemFunSection = ::std::mem_fn(&OReportHelper::getPageHeader);
63 else if ( xReportDefinition->getPageFooterOn() && xReportDefinition->getPageFooter() == _xSection )
64 pMemFunSection = ::std::mem_fn(&OReportHelper::getPageFooter);
65 else if ( xReportDefinition->getDetail() == _xSection )
66 pMemFunSection = ::std::mem_fn(&OReportHelper::getDetail);
67 return pMemFunSection;
71 OCommentUndoAction::OCommentUndoAction(SdrModel& _rMod, TranslateId pCommentID)
72 :SdrUndoAction(_rMod)
74 m_pController = static_cast< OReportModel& >( _rMod ).getController();
75 if (pCommentID)
76 m_strComment = RptResId(pCommentID);
78 OCommentUndoAction::~OCommentUndoAction()
82 void OCommentUndoAction::Undo()
86 void OCommentUndoAction::Redo()
90 OUndoContainerAction::OUndoContainerAction(SdrModel& _rMod
91 ,Action _eAction
92 ,uno::Reference< container::XIndexContainer > xContainer
93 ,const Reference< XInterface > & xElem
94 ,TranslateId pCommentId)
95 :OCommentUndoAction(_rMod, pCommentId)
96 ,m_xElement(xElem)
97 ,m_xContainer(std::move(xContainer))
98 ,m_eAction( _eAction )
100 // normalize
101 if ( m_eAction == Removed )
102 // we now own the element
103 m_xOwnElement = m_xElement;
106 OUndoContainerAction::~OUndoContainerAction()
108 // if we own the object...
109 Reference< XComponent > xComp( m_xOwnElement, UNO_QUERY );
110 if ( !xComp.is() )
111 return;
113 // and the object does not have a parent
114 Reference< XChild > xChild( m_xOwnElement, UNO_QUERY );
115 if ( !xChild.is() || xChild->getParent().is() )
116 return;
118 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
119 rEnv.RemoveElement( m_xOwnElement );
121 // -> dispose it
124 comphelper::disposeComponent( xComp );
126 catch ( const uno::Exception& )
128 DBG_UNHANDLED_EXCEPTION("reportdesign");
132 void OUndoContainerAction::implReInsert( )
134 if ( m_xContainer.is() )
136 // insert the element
137 m_xContainer->insertByIndex( m_xContainer->getCount(),uno::Any(m_xElement) );
139 // we don't own the object anymore
140 m_xOwnElement = nullptr;
144 void OUndoContainerAction::implReRemove( )
146 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
149 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
150 if ( m_xContainer.is() )
152 const sal_Int32 nCount = m_xContainer->getCount();
153 for (sal_Int32 i = 0; i < nCount; ++i)
155 uno::Reference< uno::XInterface> xObj(m_xContainer->getByIndex(i),uno::UNO_QUERY);
156 if ( xObj == m_xElement )
158 m_xContainer->removeByIndex( i );
159 break;
164 catch(uno::Exception&){}
165 // from now on, we own this object
166 m_xOwnElement = m_xElement;
170 void OUndoContainerAction::Undo()
172 if ( !m_xElement.is() )
173 return;
175 // prevents that an undo action will be created for elementInserted
178 switch ( m_eAction )
180 case Inserted:
181 implReRemove();
182 break;
184 case Removed:
185 implReInsert();
186 break;
187 default:
188 OSL_FAIL("Illegal case value");
189 break;
192 catch( const Exception& )
194 TOOLS_WARN_EXCEPTION( "reportdesign", "OUndoContainerAction::Undo" );
199 void OUndoContainerAction::Redo()
201 if ( !m_xElement.is() )
202 return;
206 switch ( m_eAction )
208 case Inserted:
209 implReInsert();
210 break;
212 case Removed:
213 implReRemove();
214 break;
215 default:
216 OSL_FAIL("Illegal case value");
217 break;
220 catch( const Exception& )
222 TOOLS_WARN_EXCEPTION( "reportdesign", "OUndoContainerAction::Redo" );
226 OUndoGroupSectionAction::OUndoGroupSectionAction(
227 SdrModel& _rMod, Action _eAction,
228 ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
229 const uno::Reference<report::XGroup>& _xGroup, const Reference<XInterface>& xElem,
230 TranslateId pCommentId)
231 : OUndoContainerAction(_rMod, _eAction, nullptr, xElem, pCommentId)
232 , m_aGroupHelper(_xGroup)
233 , m_pMemberFunction(std::move(_pMemberFunction))
237 void OUndoGroupSectionAction::implReInsert( )
239 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
242 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
243 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aGroupHelper);
244 if ( xSection.is() )
245 xSection->add(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
247 catch(uno::Exception&){}
249 // we don't own the object anymore
250 m_xOwnElement = nullptr;
254 void OUndoGroupSectionAction::implReRemove( )
256 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
259 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
260 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aGroupHelper);
261 if ( xSection.is() )
262 xSection->remove(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
264 catch(uno::Exception&){}
266 // from now on, we own this object
267 m_xOwnElement = m_xElement;
270 OUndoReportSectionAction::OUndoReportSectionAction(
271 SdrModel& _rMod, Action _eAction,
272 ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
273 const uno::Reference<report::XReportDefinition>& _xReport, const Reference<XInterface>& xElem,
274 TranslateId pCommentId)
275 : OUndoContainerAction(_rMod, _eAction, nullptr, xElem, pCommentId)
276 , m_aReportHelper(_xReport)
277 , m_pMemberFunction(std::move(_pMemberFunction))
281 void OUndoReportSectionAction::implReInsert( )
283 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
286 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
287 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aReportHelper);
288 if ( xSection.is() )
290 uno::Reference< drawing::XShape> xShape(m_xElement,uno::UNO_QUERY_THROW);
291 awt::Point aPos = xShape->getPosition();
292 awt::Size aSize = xShape->getSize();
293 xSection->add(xShape);
294 xShape->setPosition( aPos );
295 xShape->setSize( aSize );
298 catch(uno::Exception&){}
299 // we don't own the object anymore
300 m_xOwnElement = nullptr;
304 void OUndoReportSectionAction::implReRemove( )
306 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
309 OXUndoEnvironment::OUndoEnvLock aLock(rEnv);
310 uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aReportHelper);
311 if ( xSection.is() )
312 xSection->remove(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
314 catch(uno::Exception&){}
315 // from now on, we own this object
316 m_xOwnElement = m_xElement;
319 ORptUndoPropertyAction::ORptUndoPropertyAction(SdrModel& rNewMod, const PropertyChangeEvent& evt)
320 :OCommentUndoAction(rNewMod,{})
321 ,m_xObj(evt.Source, UNO_QUERY)
322 ,m_aPropertyName(evt.PropertyName)
323 ,m_aNewValue(evt.NewValue)
324 ,m_aOldValue(evt.OldValue)
328 void ORptUndoPropertyAction::Undo()
330 setProperty(true);
334 void ORptUndoPropertyAction::Redo()
336 setProperty(false);
339 Reference< XPropertySet> ORptUndoPropertyAction::getObject()
341 return m_xObj;
344 void ORptUndoPropertyAction::setProperty(bool _bOld)
346 Reference< XPropertySet> xObj = getObject();
348 if (xObj.is() )
352 xObj->setPropertyValue( m_aPropertyName, _bOld ? m_aOldValue : m_aNewValue );
354 catch( const Exception& )
356 TOOLS_WARN_EXCEPTION( "reportdesign", "ORptUndoPropertyAction::Redo" );
361 OUString ORptUndoPropertyAction::GetComment() const
363 OUString aStr( RptResId(RID_STR_UNDO_PROPERTY) );
365 return aStr.replaceFirst("#", m_aPropertyName);
368 OUndoPropertyGroupSectionAction::OUndoPropertyGroupSectionAction(
369 SdrModel& _rMod, const PropertyChangeEvent& evt,
370 ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
371 const uno::Reference<report::XGroup>& _xGroup)
372 : ORptUndoPropertyAction(_rMod, evt)
373 , m_aGroupHelper(_xGroup)
374 , m_pMemberFunction(std::move(_pMemberFunction))
378 Reference< XPropertySet> OUndoPropertyGroupSectionAction::getObject()
380 return m_pMemberFunction(&m_aGroupHelper);
383 OUndoPropertyReportSectionAction::OUndoPropertyReportSectionAction(
384 SdrModel& _rMod, const PropertyChangeEvent& evt,
385 ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
386 const uno::Reference<report::XReportDefinition>& _xReport)
387 : ORptUndoPropertyAction(_rMod, evt)
388 , m_aReportHelper(_xReport)
389 , m_pMemberFunction(std::move(_pMemberFunction))
393 Reference< XPropertySet> OUndoPropertyReportSectionAction::getObject()
395 return m_pMemberFunction(&m_aReportHelper);
398 } // rptui
401 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */