LibreOffice Module reportdesign (master) 1
RptUndo.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <RptUndo.hxx>
21#include <strings.hxx>
22#include <rptui_slotid.hrc>
23#include <UITools.hxx>
24#include <UndoEnv.hxx>
25
27#include <com/sun/star/report/XSection.hpp>
28#include <com/sun/star/beans/PropertyAttribute.hpp>
29
30#include <com/sun/star/awt/Point.hpp>
31#include <com/sun/star/awt/Size.hpp>
33#include <comphelper/types.hxx>
34#include <svx/unoshape.hxx>
35#include <utility>
37
38#include <functional>
39
40namespace rptui
41{
42 using namespace ::com::sun::star;
43 using namespace uno;
44 using namespace lang;
45 using namespace beans;
46 using namespace awt;
47 using namespace util;
48 using namespace container;
49 using namespace report;
50
51
52namespace
53{
54 void lcl_collectElements(const uno::Reference< report::XSection >& _xSection,::std::vector< uno::Reference< drawing::XShape> >& _rControls)
55 {
56 if ( _xSection.is() )
57 {
58 sal_Int32 nCount = _xSection->getCount();
59 _rControls.reserve(nCount);
60 while ( nCount )
61 {
62 uno::Reference< drawing::XShape> xShape(_xSection->getByIndex(nCount-1),uno::UNO_QUERY);
63 _rControls.push_back(xShape);
64 _xSection->remove(xShape);
65 --nCount;
66 }
67 }
68 }
69
70 void lcl_insertElements(const uno::Reference< report::XSection >& _xSection,const ::std::vector< uno::Reference< drawing::XShape> >& _aControls)
71 {
72 if ( !_xSection.is() )
73 return;
74
75 ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aIter = _aControls.rbegin();
76 ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aEnd = _aControls.rend();
77 for (; aIter != aEnd; ++aIter)
78 {
79 try
80 {
81 const awt::Point aPos = (*aIter)->getPosition();
82 const awt::Size aSize = (*aIter)->getSize();
83 _xSection->add(*aIter);
84 (*aIter)->setPosition( aPos );
85 (*aIter)->setSize( aSize );
86 }
87 catch(const uno::Exception&)
88 {
89 TOOLS_WARN_EXCEPTION( "reportdesign", "lcl_insertElements");
90 }
91 }
92 }
93
94 void lcl_setValues(const uno::Reference< report::XSection >& _xSection,const ::std::vector< ::std::pair< OUString ,uno::Any> >& _aValues)
95 {
96 if ( !_xSection.is() )
97 return;
98
99 for (const auto& [rPropName, rValue] : _aValues)
100 {
101 try
102 {
103 _xSection->setPropertyValue(rPropName, rValue);
104 }
105 catch(const uno::Exception&)
106 {
107 TOOLS_WARN_EXCEPTION( "reportdesign", "lcl_setValues");
108 }
109 }
110 }
111}
112
113
115 ,sal_uInt16 _nSlot
116 ,Action _eAction
117 ,TranslateId pCommentID)
118: OCommentUndoAction(_rMod,pCommentID)
119,m_eAction(_eAction)
120,m_nSlot(_nSlot)
121,m_bInserted(false)
122{
123}
124
126{
127 if ( m_bInserted )
128 return;
129
130 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
131 for (uno::Reference<drawing::XShape>& xShape : m_aControls)
132 {
133 rEnv.RemoveElement(xShape);
134 try
135 {
137 }
138 catch(const uno::Exception &)
139 {
140 TOOLS_WARN_EXCEPTION( "reportdesign", "");
141 }
142 }
143}
144
145void OSectionUndo::collectControls(const uno::Reference< report::XSection >& _xSection)
146{
147 m_aControls.clear();
148 try
149 {
150 // copy all properties for restoring
151 uno::Reference< beans::XPropertySetInfo> xInfo = _xSection->getPropertySetInfo();
152 const uno::Sequence< beans::Property> aSeq = xInfo->getProperties();
153 for(const beans::Property& rProp : aSeq)
154 {
155 if ( 0 == (rProp.Attributes & beans::PropertyAttribute::READONLY) )
156 m_aValues.emplace_back(rProp.Name,_xSection->getPropertyValue(rProp.Name));
157 }
158 lcl_collectElements(_xSection,m_aControls);
159 }
160 catch(uno::Exception&)
161 {
162 }
163}
164
166{
167 try
168 {
169 switch ( m_eAction )
170 {
171 case Inserted:
172 implReRemove();
173 break;
174
175 case Removed:
176 implReInsert();
177 break;
178 }
179 }
180 catch( const Exception& )
181 {
182 TOOLS_WARN_EXCEPTION( "reportdesign", "OSectionUndo::Undo" );
183 }
184}
185
187{
188 try
189 {
190 switch ( m_eAction )
191 {
192 case Inserted:
193 implReInsert();
194 break;
195
196 case Removed:
197 implReRemove();
198 break;
199 }
200 }
201 catch( const Exception& )
202 {
203 TOOLS_WARN_EXCEPTION( "reportdesign", "OSectionUndo::Redo" );
204 }
205}
206
208 OReportModel& _rMod, sal_uInt16 _nSlot,
209 ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
210 const uno::Reference<report::XReportDefinition>& _xReport, Action _eAction)
211 : OSectionUndo(_rMod, _nSlot, _eAction, {})
212 , m_aReportHelper(_xReport)
213 , m_pMemberFunction(std::move(_pMemberFunction))
214{
215 if( m_eAction == Removed )
216 collectControls(m_pMemberFunction(&m_aReportHelper));
217}
218
220{
221}
222
224{
225 const uno::Sequence< beans::PropertyValue > aArgs;
227 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aReportHelper);
228 lcl_insertElements(xSection,m_aControls);
229 lcl_setValues(xSection,m_aValues);
230 m_bInserted = true;
231}
232
234{
235 if( m_eAction == Removed )
237 const uno::Sequence< beans::PropertyValue > aArgs;
239 m_bInserted = false;
240}
241
243 OReportModel& _rMod, sal_uInt16 _nSlot,
244 ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
245 const uno::Reference<report::XGroup>& _xGroup, Action _eAction, TranslateId pCommentID)
246 : OSectionUndo(_rMod, _nSlot, _eAction, pCommentID)
247 , m_aGroupHelper(_xGroup)
248 , m_pMemberFunction(std::move(_pMemberFunction))
249{
250 if( m_eAction == Removed )
251 {
252 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
253 if ( xSection.is() )
254 m_sName = xSection->getName();
255 collectControls(xSection);
256 }
257}
258
260{
261 if ( m_sName.isEmpty() )
262 {
263 try
264 {
265 uno::Reference< report::XSection > xSection = const_cast<OGroupSectionUndo*>(this)->m_pMemberFunction(&const_cast<OGroupSectionUndo*>(this)->m_aGroupHelper);
266
267 if ( xSection.is() )
268 m_sName = xSection->getName();
269 }
270 catch (const uno::Exception&)
271 {
272 }
273 }
274 return m_strComment + m_sName;
275}
276
278{
279 const OUString aHeaderFooterOnName(SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? OUString(PROPERTY_HEADERON) : OUString(PROPERTY_FOOTERON));
280 uno::Sequence< beans::PropertyValue > aArgs{
281 comphelper::makePropertyValue(aHeaderFooterOnName, true),
283 };
285
286 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
287 lcl_insertElements(xSection,m_aControls);
288 lcl_setValues(xSection,m_aValues);
289 m_bInserted = true;
290}
291
293{
294 if( m_eAction == Removed )
296
297 const OUString aHeaderFooterOnName(SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? OUString(PROPERTY_HEADERON) : OUString(PROPERTY_FOOTERON));
298 uno::Sequence< beans::PropertyValue > aArgs{
299 comphelper::makePropertyValue(aHeaderFooterOnName, false),
301 };
302
304 m_bInserted = false;
305}
306
307
309 ,TranslateId pCommentID
310 ,Action _eAction
311 ,uno::Reference< report::XGroup> _xGroup
312 ,uno::Reference< report::XReportDefinition > _xReportDefinition)
313: OCommentUndoAction(_rMod,pCommentID)
314,m_xGroup(std::move(_xGroup))
315,m_xReportDefinition(std::move(_xReportDefinition))
316,m_eAction(_eAction)
317{
319}
320
322{
323 try
324 {
325 m_xReportDefinition->getGroups()->insertByIndex(m_nLastPosition,uno::Any(m_xGroup));
326 }
327 catch(uno::Exception&)
328 {
329 TOOLS_WARN_EXCEPTION( "reportdesign", "Exception caught while undoing remove group");
330 }
331}
332
334{
335 try
336 {
337 m_xReportDefinition->getGroups()->removeByIndex(m_nLastPosition);
338 }
339 catch(uno::Exception&)
340 {
341 TOOLS_WARN_EXCEPTION( "reportdesign", "Exception caught while redoing remove group");
342 }
343}
344
346{
347 switch ( m_eAction )
348 {
349 case Inserted:
350 implReRemove();
351 break;
352
353 case Removed:
354 implReInsert();
355 break;
356 }
357
358}
359
361{
362 switch ( m_eAction )
363 {
364 case Inserted:
365 implReInsert();
366 break;
367
368 case Removed:
369 implReRemove();
370 break;
371 }
372}
373
374
375} // rptui
376
377
378/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SectionViewAction m_eAction
SdrModel & rMod
virtual void executeChecked(const css::util::URL &_rCommand, const css::uno::Sequence< css::beans::PropertyValue > &aArgs)=0
::dbaui::IController * m_pController
Helper class to allow std::mem_fun for SAL_CALL.
Definition: UndoActions.hxx:49
const css::uno::Reference< css::report::XGroup > & getGroup() const
Definition: UndoActions.hxx:60
Undo action for the group header, footer.
Definition: RptUndo.hxx:89
void implReInsert() override
Definition: RptUndo.cxx:277
OGroupHelper m_aGroupHelper
Definition: RptUndo.hxx:90
::std::function< css::uno::Reference< css::report::XSection >(OGroupHelper *)> m_pMemberFunction
Definition: RptUndo.hxx:91
void implReRemove() override
Definition: RptUndo.cxx:292
virtual OUString GetComment() const override
Definition: RptUndo.cxx:259
OGroupSectionUndo(const OGroupSectionUndo &)=delete
virtual void Redo() override
Definition: RptUndo.cxx:360
virtual void Undo() override
Definition: RptUndo.cxx:345
void implReRemove()
Definition: RptUndo.cxx:333
Action m_eAction
! the current action
Definition: RptUndo.hxx:118
css::uno::Reference< css::report::XGroup > m_xGroup
! the group for the undo redo action
Definition: RptUndo.hxx:116
OGroupUndo(OReportModel &rMod, TranslateId pCommentID, Action _eAction, css::uno::Reference< css::report::XGroup > _xGroup, css::uno::Reference< css::report::XReportDefinition > _xReportDefinition)
Definition: RptUndo.cxx:308
css::uno::Reference< css::report::XReportDefinition > m_xReportDefinition
! the parent report definition
Definition: RptUndo.hxx:117
sal_Int32 m_nLastPosition
! the last position of the group
Definition: RptUndo.hxx:119
void implReInsert()
Definition: RptUndo.cxx:321
Helper class to allow std::mem_fun for SAL_CALL.
Definition: UndoActions.hxx:72
::std::function< css::uno::Reference< css::report::XSection >(OReportHelper *)> m_pMemberFunction
Definition: RptUndo.hxx:70
OReportHelper m_aReportHelper
Definition: RptUndo.hxx:69
void implReRemove() override
Definition: RptUndo.cxx:233
void implReInsert() override
Definition: RptUndo.cxx:223
OReportSectionUndo(const OReportSectionUndo &)=delete
virtual ~OReportSectionUndo() override
Definition: RptUndo.cxx:219
Undo class for section add and remove.
Definition: RptUndo.hxx:38
virtual void implReInsert()=0
virtual void Redo() override
Definition: RptUndo.cxx:186
::std::vector< css::uno::Reference< css::drawing::XShape > > m_aControls
Definition: RptUndo.hxx:43
virtual void implReRemove()=0
OSectionUndo(const OSectionUndo &)=delete
void collectControls(const css::uno::Reference< css::report::XSection > &_xSection)
Definition: RptUndo.cxx:145
virtual ~OSectionUndo() override
Definition: RptUndo.cxx:125
::std::vector< ::std::pair< OUString,css::uno::Any > > m_aValues
Definition: RptUndo.hxx:45
virtual void Undo() override
Definition: RptUndo.cxx:165
sal_uInt16 m_nSlot
Definition: RptUndo.hxx:47
void RemoveElement(const css::uno::Reference< css::uno::XInterface > &Element)
Definition: UndoEnv.cxx:594
int nCount
#define TOOLS_WARN_EXCEPTION(area, stream)
OUString m_sName
Sequence< sal_Int8 > aSeq
@ Exception
void disposeComponent(css::uno::Reference< TYPE > &_rxComp)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
@ Inserted
Definition: UndoActions.hxx:42
sal_Int32 getPositionInIndexAccess(const css::uno::Reference< css::container::XIndexAccess > &_xCollection, const css::uno::Reference< T > &_xSearch)
returns the position of the object inside the index container
Definition: UITools.hxx:52
constexpr OUStringLiteral PROPERTY_GROUP
Definition: strings.hxx:48
constexpr OUStringLiteral PROPERTY_HEADERON
Definition: strings.hxx:71
constexpr OUStringLiteral PROPERTY_FOOTERON
Definition: strings.hxx:72