tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / reportdesign / source / ui / misc / RptUndo.cxx
blobbff3c4518b1a9b173b731d42586eca4b8592578f
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/propertyvalue.hxx>
33 #include <comphelper/types.hxx>
34 #include <svx/unoshape.hxx>
35 #include <utility>
36 #include <comphelper/diagnose_ex.hxx>
38 #include <functional>
40 namespace rptui
42 using namespace ::com::sun::star;
43 using namespace uno;
44 using namespace beans;
45 using namespace awt;
46 using namespace container;
47 using namespace report;
50 namespace
52 void lcl_collectElements(const uno::Reference< report::XSection >& _xSection,::std::vector< uno::Reference< drawing::XShape> >& _rControls)
54 if ( _xSection.is() )
56 sal_Int32 nCount = _xSection->getCount();
57 _rControls.reserve(nCount);
58 while ( nCount )
60 uno::Reference< drawing::XShape> xShape(_xSection->getByIndex(nCount-1),uno::UNO_QUERY);
61 _rControls.push_back(xShape);
62 _xSection->remove(xShape);
63 --nCount;
68 void lcl_insertElements(const uno::Reference< report::XSection >& _xSection,const ::std::vector< uno::Reference< drawing::XShape> >& _aControls)
70 if ( !_xSection.is() )
71 return;
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 TOOLS_WARN_EXCEPTION( "reportdesign", "lcl_insertElements");
92 void lcl_setValues(const uno::Reference< report::XSection >& _xSection,const ::std::vector< ::std::pair< OUString ,uno::Any> >& _aValues)
94 if ( !_xSection.is() )
95 return;
97 for (const auto& [rPropName, rValue] : _aValues)
99 try
101 _xSection->setPropertyValue(rPropName, rValue);
103 catch(const uno::Exception&)
105 TOOLS_WARN_EXCEPTION( "reportdesign", "lcl_setValues");
112 OSectionUndo::OSectionUndo(OReportModel& _rMod
113 ,sal_uInt16 _nSlot
114 ,Action _eAction
115 ,TranslateId pCommentID)
116 : OCommentUndoAction(_rMod,pCommentID)
117 ,m_eAction(_eAction)
118 ,m_nSlot(_nSlot)
119 ,m_bInserted(false)
123 OSectionUndo::~OSectionUndo()
125 if ( m_bInserted )
126 return;
128 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( m_rMod ).GetUndoEnv();
129 for (uno::Reference<drawing::XShape>& xShape : m_aControls)
131 rEnv.RemoveElement(xShape);
134 comphelper::disposeComponent(xShape);
136 catch(const uno::Exception &)
138 TOOLS_WARN_EXCEPTION( "reportdesign", "");
143 void OSectionUndo::collectControls(const uno::Reference< report::XSection >& _xSection)
145 m_aControls.clear();
148 // copy all properties for restoring
149 uno::Reference< beans::XPropertySetInfo> xInfo = _xSection->getPropertySetInfo();
150 const uno::Sequence< beans::Property> aSeq = xInfo->getProperties();
151 for(const beans::Property& rProp : aSeq)
153 if ( 0 == (rProp.Attributes & beans::PropertyAttribute::READONLY) )
154 m_aValues.emplace_back(rProp.Name,_xSection->getPropertyValue(rProp.Name));
156 lcl_collectElements(_xSection,m_aControls);
158 catch(uno::Exception&)
163 void OSectionUndo::Undo()
167 switch ( m_eAction )
169 case Inserted:
170 implReRemove();
171 break;
173 case Removed:
174 implReInsert();
175 break;
178 catch( const Exception& )
180 TOOLS_WARN_EXCEPTION( "reportdesign", "OSectionUndo::Undo" );
184 void OSectionUndo::Redo()
188 switch ( m_eAction )
190 case Inserted:
191 implReInsert();
192 break;
194 case Removed:
195 implReRemove();
196 break;
199 catch( const Exception& )
201 TOOLS_WARN_EXCEPTION( "reportdesign", "OSectionUndo::Redo" );
205 OReportSectionUndo::OReportSectionUndo(
206 OReportModel& _rMod, sal_uInt16 _nSlot,
207 ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
208 const uno::Reference<report::XReportDefinition>& _xReport, Action _eAction)
209 : OSectionUndo(_rMod, _nSlot, _eAction, {})
210 , m_aReportHelper(_xReport)
211 , m_pMemberFunction(std::move(_pMemberFunction))
213 if( m_eAction == Removed )
214 collectControls(m_pMemberFunction(&m_aReportHelper));
217 OReportSectionUndo::~OReportSectionUndo()
221 void OReportSectionUndo::implReInsert( )
223 const uno::Sequence< beans::PropertyValue > aArgs;
224 m_pController->executeChecked(m_nSlot,aArgs);
225 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aReportHelper);
226 lcl_insertElements(xSection,m_aControls);
227 lcl_setValues(xSection,m_aValues);
228 m_bInserted = true;
231 void OReportSectionUndo::implReRemove( )
233 if( m_eAction == Removed )
234 collectControls(m_pMemberFunction(&m_aReportHelper));
235 const uno::Sequence< beans::PropertyValue > aArgs;
236 m_pController->executeChecked(m_nSlot,aArgs);
237 m_bInserted = false;
240 OGroupSectionUndo::OGroupSectionUndo(
241 OReportModel& _rMod, sal_uInt16 _nSlot,
242 ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
243 const uno::Reference<report::XGroup>& _xGroup, Action _eAction, TranslateId pCommentID)
244 : OSectionUndo(_rMod, _nSlot, _eAction, pCommentID)
245 , m_aGroupHelper(_xGroup)
246 , m_pMemberFunction(std::move(_pMemberFunction))
248 if( m_eAction == Removed )
250 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
251 if ( xSection.is() )
252 m_sName = xSection->getName();
253 collectControls(xSection);
257 OUString OGroupSectionUndo::GetComment() const
259 if ( m_sName.isEmpty() )
263 uno::Reference< report::XSection > xSection = const_cast<OGroupSectionUndo*>(this)->m_pMemberFunction(&const_cast<OGroupSectionUndo*>(this)->m_aGroupHelper);
265 if ( xSection.is() )
266 m_sName = xSection->getName();
268 catch (const uno::Exception&)
272 return m_strComment + m_sName;
275 void OGroupSectionUndo::implReInsert( )
277 const OUString aHeaderFooterOnName(SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? PROPERTY_HEADERON : PROPERTY_FOOTERON);
278 uno::Sequence< beans::PropertyValue > aArgs{
279 comphelper::makePropertyValue(aHeaderFooterOnName, true),
280 comphelper::makePropertyValue(PROPERTY_GROUP, m_aGroupHelper.getGroup())
282 m_pController->executeChecked(m_nSlot,aArgs);
284 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
285 lcl_insertElements(xSection,m_aControls);
286 lcl_setValues(xSection,m_aValues);
287 m_bInserted = true;
290 void OGroupSectionUndo::implReRemove( )
292 if( m_eAction == Removed )
293 collectControls(m_pMemberFunction(&m_aGroupHelper));
295 const OUString aHeaderFooterOnName(SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? PROPERTY_HEADERON : PROPERTY_FOOTERON);
296 uno::Sequence< beans::PropertyValue > aArgs{
297 comphelper::makePropertyValue(aHeaderFooterOnName, false),
298 comphelper::makePropertyValue(PROPERTY_GROUP, m_aGroupHelper.getGroup())
301 m_pController->executeChecked(m_nSlot,aArgs);
302 m_bInserted = false;
306 OGroupUndo::OGroupUndo(OReportModel& _rMod
307 ,TranslateId pCommentID
308 ,Action _eAction
309 ,uno::Reference< report::XGroup> _xGroup
310 ,uno::Reference< report::XReportDefinition > _xReportDefinition)
311 : OCommentUndoAction(_rMod,pCommentID)
312 ,m_xGroup(std::move(_xGroup))
313 ,m_xReportDefinition(std::move(_xReportDefinition))
314 ,m_eAction(_eAction)
316 m_nLastPosition = getPositionInIndexAccess(m_xReportDefinition->getGroups(),m_xGroup);
319 void OGroupUndo::implReInsert( )
323 m_xReportDefinition->getGroups()->insertByIndex(m_nLastPosition,uno::Any(m_xGroup));
325 catch(uno::Exception&)
327 TOOLS_WARN_EXCEPTION( "reportdesign", "Exception caught while undoing remove group");
331 void OGroupUndo::implReRemove( )
335 m_xReportDefinition->getGroups()->removeByIndex(m_nLastPosition);
337 catch(uno::Exception&)
339 TOOLS_WARN_EXCEPTION( "reportdesign", "Exception caught while redoing remove group");
343 void OGroupUndo::Undo()
345 switch ( m_eAction )
347 case Inserted:
348 implReRemove();
349 break;
351 case Removed:
352 implReInsert();
353 break;
358 void OGroupUndo::Redo()
360 switch ( m_eAction )
362 case Inserted:
363 implReInsert();
364 break;
366 case Removed:
367 implReRemove();
368 break;
373 } // rptui
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */