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 
26 #include <dbaccess/IController.hxx>
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>
36 #include <tools/diagnose_ex.h>
37 
38 #include <functional>
39 
40 namespace 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 
52 namespace
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 
135 #if OSL_DEBUG_LEVEL > 0
136  SvxShape* pShape = comphelper::getFromUnoTunnel<SvxShape>( xShape );
137  SdrObject* pObject = pShape ? pShape->GetSdrObject() : nullptr;
138  OSL_ENSURE( pShape && pShape->HasSdrObjectOwnership() && pObject && !pObject->IsInserted(),
139  "OSectionUndo::~OSectionUndo: inconsistency in the shape/object ownership!" );
140 #endif
141  try
142  {
144  }
145  catch(const uno::Exception &)
146  {
147  TOOLS_WARN_EXCEPTION( "reportdesign", "");
148  }
149  }
150 }
151 
152 void OSectionUndo::collectControls(const uno::Reference< report::XSection >& _xSection)
153 {
154  m_aControls.clear();
155  try
156  {
157  // copy all properties for restoring
158  uno::Reference< beans::XPropertySetInfo> xInfo = _xSection->getPropertySetInfo();
159  const uno::Sequence< beans::Property> aSeq = xInfo->getProperties();
160  for(const beans::Property& rProp : aSeq)
161  {
162  if ( 0 == (rProp.Attributes & beans::PropertyAttribute::READONLY) )
163  m_aValues.emplace_back(rProp.Name,_xSection->getPropertyValue(rProp.Name));
164  }
165  lcl_collectElements(_xSection,m_aControls);
166  }
167  catch(uno::Exception&)
168  {
169  }
170 }
171 
173 {
174  try
175  {
176  switch ( m_eAction )
177  {
178  case Inserted:
179  implReRemove();
180  break;
181 
182  case Removed:
183  implReInsert();
184  break;
185  }
186  }
187  catch( const Exception& )
188  {
189  TOOLS_WARN_EXCEPTION( "reportdesign", "OSectionUndo::Undo" );
190  }
191 }
192 
194 {
195  try
196  {
197  switch ( m_eAction )
198  {
199  case Inserted:
200  implReInsert();
201  break;
202 
203  case Removed:
204  implReRemove();
205  break;
206  }
207  }
208  catch( const Exception& )
209  {
210  TOOLS_WARN_EXCEPTION( "reportdesign", "OSectionUndo::Redo" );
211  }
212 }
213 
215  OReportModel& _rMod, sal_uInt16 _nSlot,
216  ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
217  const uno::Reference<report::XReportDefinition>& _xReport, Action _eAction)
218  : OSectionUndo(_rMod, _nSlot, _eAction, {})
219  , m_aReportHelper(_xReport)
220  , m_pMemberFunction(std::move(_pMemberFunction))
221 {
222  if( m_eAction == Removed )
223  collectControls(m_pMemberFunction(&m_aReportHelper));
224 }
225 
227 {
228 }
229 
231 {
232  const uno::Sequence< beans::PropertyValue > aArgs;
234  uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aReportHelper);
235  lcl_insertElements(xSection,m_aControls);
236  lcl_setValues(xSection,m_aValues);
237  m_bInserted = true;
238 }
239 
241 {
242  if( m_eAction == Removed )
244  const uno::Sequence< beans::PropertyValue > aArgs;
246  m_bInserted = false;
247 }
248 
250  OReportModel& _rMod, sal_uInt16 _nSlot,
251  ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
252  const uno::Reference<report::XGroup>& _xGroup, Action _eAction, TranslateId pCommentID)
253  : OSectionUndo(_rMod, _nSlot, _eAction, pCommentID)
254  , m_aGroupHelper(_xGroup)
255  , m_pMemberFunction(std::move(_pMemberFunction))
256 {
257  if( m_eAction == Removed )
258  {
259  uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
260  if ( xSection.is() )
261  m_sName = xSection->getName();
262  collectControls(xSection);
263  }
264 }
265 
267 {
268  if ( m_sName.isEmpty() )
269  {
270  try
271  {
272  uno::Reference< report::XSection > xSection = const_cast<OGroupSectionUndo*>(this)->m_pMemberFunction(&const_cast<OGroupSectionUndo*>(this)->m_aGroupHelper);
273 
274  if ( xSection.is() )
275  m_sName = xSection->getName();
276  }
277  catch (const uno::Exception&)
278  {
279  }
280  }
281  return m_strComment + m_sName;
282 }
283 
285 {
286  const OUString aHeaderFooterOnName(SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? OUString(PROPERTY_HEADERON) : OUString(PROPERTY_FOOTERON));
287  uno::Sequence< beans::PropertyValue > aArgs{
288  comphelper::makePropertyValue(aHeaderFooterOnName, true),
290  };
292 
293  uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
294  lcl_insertElements(xSection,m_aControls);
295  lcl_setValues(xSection,m_aValues);
296  m_bInserted = true;
297 }
298 
300 {
301  if( m_eAction == Removed )
303 
304  const OUString aHeaderFooterOnName(SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? OUString(PROPERTY_HEADERON) : OUString(PROPERTY_FOOTERON));
305  uno::Sequence< beans::PropertyValue > aArgs{
306  comphelper::makePropertyValue(aHeaderFooterOnName, false),
308  };
309 
311  m_bInserted = false;
312 }
313 
314 
316  ,TranslateId pCommentID
317  ,Action _eAction
318  ,const uno::Reference< report::XGroup>& _xGroup
319  ,const uno::Reference< report::XReportDefinition >& _xReportDefinition)
320 : OCommentUndoAction(_rMod,pCommentID)
321 ,m_xGroup(_xGroup)
322 ,m_xReportDefinition(_xReportDefinition)
323 ,m_eAction(_eAction)
324 {
326 }
327 
329 {
330  try
331  {
332  m_xReportDefinition->getGroups()->insertByIndex(m_nLastPosition,uno::Any(m_xGroup));
333  }
334  catch(uno::Exception&)
335  {
336  TOOLS_WARN_EXCEPTION( "reportdesign", "Exception caught while undoing remove group");
337  }
338 }
339 
341 {
342  try
343  {
344  m_xReportDefinition->getGroups()->removeByIndex(m_nLastPosition);
345  }
346  catch(uno::Exception&)
347  {
348  TOOLS_WARN_EXCEPTION( "reportdesign", "Exception caught while redoing remove group");
349  }
350 }
351 
353 {
354  switch ( m_eAction )
355  {
356  case Inserted:
357  implReRemove();
358  break;
359 
360  case Removed:
361  implReInsert();
362  break;
363  }
364 
365 }
366 
368 {
369  switch ( m_eAction )
370  {
371  case Inserted:
372  implReInsert();
373  break;
374 
375  case Removed:
376  implReRemove();
377  break;
378  }
379 }
380 
381 
382 } // rptui
383 
384 
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void implReInsert()
Definition: RptUndo.cxx:328
::dbaui::IController * m_pController
virtual void Redo() override
Definition: RptUndo.cxx:193
virtual void implReRemove()=0
void implReRemove() override
Definition: RptUndo.cxx:299
void RemoveElement(const css::uno::Reference< css::uno::XInterface > &Element)
Definition: UndoEnv.cxx:594
#define PROPERTY_HEADERON
Definition: strings.hxx:71
#define PROPERTY_GROUP
Definition: strings.hxx:48
::std::function< css::uno::Reference< css::report::XSection >OReportHelper *)> m_pMemberFunction
Definition: RptUndo.hxx:70
virtual ~OReportSectionUndo() override
Definition: RptUndo.cxx:226
::std::vector< css::uno::Reference< css::drawing::XShape > > m_aControls
Definition: RptUndo.hxx:43
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
virtual void implReInsert()=0
EmbeddedObjectRef * pObject
Undo action for the group header, footer.
Definition: RptUndo.hxx:88
#define PROPERTY_FOOTERON
Definition: strings.hxx:72
Helper class to allow std::mem_fun for SAL_CALL.
Definition: UndoActions.hxx:70
SdrModel & rMod
::std::vector< ::std::pair< OUString,css::uno::Any > > m_aValues
Definition: RptUndo.hxx:45
virtual ~OSectionUndo() override
Definition: RptUndo.cxx:125
int nCount
OSectionUndo(const OSectionUndo &)=delete
virtual void Redo() override
Definition: RptUndo.cxx:367
virtual OUString GetComment() const override
Definition: RptUndo.cxx:266
void collectControls(const css::uno::Reference< css::report::XSection > &_xSection)
Definition: RptUndo.cxx:152
#define TOOLS_WARN_EXCEPTION(area, stream)
::std::function< css::uno::Reference< css::report::XSection >OGroupHelper *)> m_pMemberFunction
Definition: RptUndo.hxx:91
OGroupSectionUndo(const OGroupSectionUndo &)=delete
OGroupHelper m_aGroupHelper
Definition: RptUndo.hxx:90
const css::uno::Reference< css::report::XGroup > & getGroup() const
Definition: UndoActions.hxx:59
OGroupUndo(OReportModel &rMod, TranslateId pCommentID, Action _eAction, const css::uno::Reference< css::report::XGroup > &_xGroup, const css::uno::Reference< css::report::XReportDefinition > &_xReportDefinition)
Definition: RptUndo.cxx:315
sal_uInt16 m_nSlot
Definition: RptUndo.hxx:47
Undo class for section add and remove.
Definition: RptUndo.hxx:37
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
Action m_eAction
! the current action
Definition: RptUndo.hxx:118
sal_Int32 m_nLastPosition
! the last position of the group
Definition: RptUndo.hxx:119
void implReInsert() override
Definition: RptUndo.cxx:230
void implReInsert() override
Definition: RptUndo.cxx:284
SdrObject * GetSdrObject() const
css::uno::Reference< css::report::XGroup > m_xGroup
! the group for the undo redo action
Definition: RptUndo.hxx:116
void implReRemove() override
Definition: RptUndo.cxx:240
virtual void Undo() override
Definition: RptUndo.cxx:172
virtual void executeChecked(const css::util::URL &_rCommand, const css::uno::Sequence< css::beans::PropertyValue > &aArgs)=0
Sequence< sal_Int8 > aSeq
void disposeComponent(css::uno::Reference< TYPE > &_rxComp)
virtual void Undo() override
Definition: RptUndo.cxx:352
SectionViewAction m_eAction
bool HasSdrObjectOwnership() const
css::uno::Reference< css::report::XReportDefinition > m_xReportDefinition
! the parent report definition
Definition: RptUndo.hxx:117
void implReRemove()
Definition: RptUndo.cxx:340
OReportHelper m_aReportHelper
Definition: RptUndo.hxx:69
OReportSectionUndo(const OReportSectionUndo &)=delete
bool m_bDetectedRangeSegmentation false
Helper class to allow std::mem_fun for SAL_CALL.
Definition: UndoActions.hxx:47