LibreOffice Module reportdesign (master)  1
UndoActions.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 <UndoActions.hxx>
21 #include <UndoEnv.hxx>
22 #include <core_resource.hxx>
23 #include <strings.hrc>
24 #include <RptModel.hxx>
25 
26 #include <com/sun/star/container/XChild.hpp>
27 
28 #include <comphelper/types.hxx>
29 #include <tools/diagnose_ex.h>
30 #include <utility>
32 #include <svx/unoshape.hxx>
33 
34 namespace rptui
35 {
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;
45 
46 ::std::function<uno::Reference<report::XSection>(OGroupHelper *)> OGroupHelper::getMemberFunction(const Reference< XSection >& _xSection)
47 {
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;
53 }
54 
55 ::std::function<uno::Reference<report::XSection>(OReportHelper *)> OReportHelper::getMemberFunction(const Reference< XSection >& _xSection)
56 {
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;
68 }
69 
70 
71 OCommentUndoAction::OCommentUndoAction(SdrModel& _rMod,const char* pCommentID)
72  :SdrUndoAction(_rMod)
73 {
74  m_pController = static_cast< OReportModel& >( _rMod ).getController();
75  if (pCommentID)
76  m_strComment = RptResId(pCommentID);
77 }
79 {
80 }
81 
83 {
84 }
85 
87 {
88 }
89 
91  ,Action _eAction
92  ,const uno::Reference< container::XIndexContainer >& rContainer
93  ,const Reference< XInterface > & xElem
94  ,const char* pCommentId)
95  :OCommentUndoAction(_rMod, pCommentId)
96  ,m_xElement(xElem)
97  ,m_xContainer(rContainer)
98  ,m_eAction( _eAction )
99 {
100  // normalize
101  if ( m_eAction == Removed )
102  // we now own the element
103  m_xOwnElement = m_xElement;
104 }
105 
107 {
108  // if we own the object...
109  Reference< XComponent > xComp( m_xOwnElement, UNO_QUERY );
110  if ( !xComp.is() )
111  return;
112 
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;
117 
118  OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
120 
121 #if OSL_DEBUG_LEVEL > 0
122  SvxShape* pShape = comphelper::getUnoTunnelImplementation<SvxShape>( xChild );
123  SdrObject* pObject = pShape ? pShape->GetSdrObject() : nullptr;
124  OSL_ENSURE( pObject == nullptr || (pShape->HasSdrObjectOwnership() && !pObject->IsInserted()),
125  "OUndoContainerAction::~OUndoContainerAction: inconsistency in the shape/object ownership!" );
126 #endif
127  // -> dispose it
128  try
129  {
131  }
132  catch ( const uno::Exception& )
133  {
134  DBG_UNHANDLED_EXCEPTION("reportdesign");
135  }
136 }
137 
139 {
140  if ( m_xContainer.is() )
141  {
142  // insert the element
143  m_xContainer->insertByIndex( m_xContainer->getCount(),uno::makeAny(m_xElement) );
144  }
145  // we don't own the object anymore
146  m_xOwnElement = nullptr;
147 }
148 
149 
151 {
152  OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
153  try
154  {
156  if ( m_xContainer.is() )
157  {
158  const sal_Int32 nCount = m_xContainer->getCount();
159  for (sal_Int32 i = 0; i < nCount; ++i)
160  {
161  uno::Reference< uno::XInterface> xObj(m_xContainer->getByIndex(i),uno::UNO_QUERY);
162  if ( xObj == m_xElement )
163  {
164  m_xContainer->removeByIndex( i );
165  break;
166  }
167  }
168  }
169  }
170  catch(uno::Exception&){}
171  // from now on, we own this object
173 }
174 
175 
177 {
178  if ( !m_xElement.is() )
179  return;
180 
181  // prevents that an undo action will be created for elementInserted
182  try
183  {
184  switch ( m_eAction )
185  {
186  case Inserted:
187  implReRemove();
188  break;
189 
190  case Removed:
191  implReInsert();
192  break;
193  default:
194  OSL_FAIL("Illegal case value");
195  break;
196  }
197  }
198  catch( const Exception& )
199  {
200  TOOLS_WARN_EXCEPTION( "reportdesign", "OUndoContainerAction::Undo" );
201  }
202 }
203 
204 
206 {
207  if ( !m_xElement.is() )
208  return;
209 
210  try
211  {
212  switch ( m_eAction )
213  {
214  case Inserted:
215  implReInsert();
216  break;
217 
218  case Removed:
219  implReRemove();
220  break;
221  default:
222  OSL_FAIL("Illegal case value");
223  break;
224  }
225  }
226  catch( const Exception& )
227  {
228  TOOLS_WARN_EXCEPTION( "reportdesign", "OUndoContainerAction::Redo" );
229  }
230 }
231 
233  SdrModel& _rMod, Action _eAction,
234  ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
235  const uno::Reference<report::XGroup>& _xGroup, const Reference<XInterface>& xElem,
236  const char* pCommentId)
237  : OUndoContainerAction(_rMod, _eAction, nullptr, xElem, pCommentId)
238  , m_aGroupHelper(_xGroup)
239  , m_pMemberFunction(std::move(_pMemberFunction))
240 {
241 }
242 
244 {
245  OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
246  try
247  {
249  uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aGroupHelper);
250  if ( xSection.is() )
251  xSection->add(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
252  }
253  catch(uno::Exception&){}
254 
255  // we don't own the object anymore
256  m_xOwnElement = nullptr;
257 }
258 
259 
261 {
262  OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
263  try
264  {
266  uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aGroupHelper);
267  if ( xSection.is() )
268  xSection->remove(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
269  }
270  catch(uno::Exception&){}
271 
272  // from now on, we own this object
274 }
275 
277  SdrModel& _rMod, Action _eAction,
278  ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
279  const uno::Reference<report::XReportDefinition>& _xReport, const Reference<XInterface>& xElem,
280  const char* pCommentId)
281  : OUndoContainerAction(_rMod, _eAction, nullptr, xElem, pCommentId)
282  , m_aReportHelper(_xReport)
283  , m_pMemberFunction(std::move(_pMemberFunction))
284 {
285 }
286 
288 {
289  OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
290  try
291  {
293  uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aReportHelper);
294  if ( xSection.is() )
295  {
296  uno::Reference< drawing::XShape> xShape(m_xElement,uno::UNO_QUERY_THROW);
297  awt::Point aPos = xShape->getPosition();
298  awt::Size aSize = xShape->getSize();
299  xSection->add(xShape);
300  xShape->setPosition( aPos );
301  xShape->setSize( aSize );
302  }
303  }
304  catch(uno::Exception&){}
305  // we don't own the object anymore
306  m_xOwnElement = nullptr;
307 }
308 
309 
311 {
312  OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
313  try
314  {
316  uno::Reference< report::XSection> xSection = m_pMemberFunction(&m_aReportHelper);
317  if ( xSection.is() )
318  xSection->remove(uno::Reference< drawing::XShape>(m_xElement,uno::UNO_QUERY));
319  }
320  catch(uno::Exception&){}
321  // from now on, we own this object
323 }
324 
325 ORptUndoPropertyAction::ORptUndoPropertyAction(SdrModel& rNewMod, const PropertyChangeEvent& evt)
326  :OCommentUndoAction(rNewMod,nullptr)
327  ,m_xObj(evt.Source, UNO_QUERY)
328  ,m_aPropertyName(evt.PropertyName)
329  ,m_aNewValue(evt.NewValue)
330  ,m_aOldValue(evt.OldValue)
331 {
332 }
333 
335 {
336  setProperty(true);
337 }
338 
339 
341 {
342  setProperty(false);
343 }
344 
346 {
347  return m_xObj;
348 }
349 
351 {
353 
354  if (xObj.is() )
355  {
356  try
357  {
358  xObj->setPropertyValue( m_aPropertyName, _bOld ? m_aOldValue : m_aNewValue );
359  }
360  catch( const Exception& )
361  {
362  TOOLS_WARN_EXCEPTION( "reportdesign", "ORptUndoPropertyAction::Redo" );
363  }
364  }
365 }
366 
368 {
369  OUString aStr( RptResId(RID_STR_UNDO_PROPERTY) );
370 
371  return aStr.replaceFirst("#", m_aPropertyName);
372 }
373 
375  SdrModel& _rMod, const PropertyChangeEvent& evt,
376  ::std::function<uno::Reference<report::XSection>(OGroupHelper*)> _pMemberFunction,
377  const uno::Reference<report::XGroup>& _xGroup)
378  : ORptUndoPropertyAction(_rMod, evt)
379  , m_aGroupHelper(_xGroup)
380  , m_pMemberFunction(std::move(_pMemberFunction))
381 {
382 }
383 
385 {
387 }
388 
390  SdrModel& _rMod, const PropertyChangeEvent& evt,
391  ::std::function<uno::Reference<report::XSection>(OReportHelper*)> _pMemberFunction,
392  const uno::Reference<report::XReportDefinition>& _xReport)
393  : ORptUndoPropertyAction(_rMod, evt)
394  , m_aReportHelper(_xReport)
395  , m_pMemberFunction(std::move(_pMemberFunction))
396 {
397 }
398 
400 {
402 }
403 
404 } // rptui
405 
406 
407 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::dbaui::IController * m_pController
void setProperty(bool _bOld)
sets either the old value or the new value again at the property set.
Create an object ob OUndoEnvLock locks the undo possibility As long as in the OUndoEnvLock scope...
Definition: UndoEnv.hxx:64
virtual void implReInsert() override
void RemoveElement(const css::uno::Reference< css::uno::XInterface > &Element)
Definition: UndoEnv.cxx:597
virtual ~OCommentUndoAction() override
Definition: UndoActions.cxx:78
virtual css::uno::Reference< css::beans::XPropertySet > getObject() override
static::std::function< css::uno::Reference< css::report::XSection >OGroupHelper *)> getMemberFunction(const css::uno::Reference< css::report::XSection > &_xSection)
Definition: UndoActions.cxx:46
static::std::function< css::uno::Reference< css::report::XSection >OReportHelper *)> getMemberFunction(const css::uno::Reference< css::report::XSection > &_xSection)
Definition: UndoActions.cxx:55
css::uno::Reference< css::report::XSection > getReportHeader()
Definition: UndoActions.hxx:78
virtual OUString GetComment() const override
virtual void Redo() override
Definition: UndoActions.cxx:86
virtual void Undo() override
EmbeddedObjectRef * pObject
Helper class to allow std::mem_fun for SAL_CALL.
Definition: UndoActions.hxx:70
SdrModel & rMod
Reference< XNameAccess > m_xContainer
OUndoReportSectionAction(SdrModel &rMod, Action _eAction,::std::function< css::uno::Reference< css::report::XSection >(OReportHelper *)> _pMemberFunction, const css::uno::Reference< css::report::XReportDefinition > &_xReport, const css::uno::Reference< css::uno::XInterface > &xElem, const char *pCommentId)
int nCount
virtual void implReInsert() override
::std::function< css::uno::Reference< css::report::XSection >OReportHelper *)> m_pMemberFunction
OUndoPropertyReportSectionAction(SdrModel &rMod, const css::beans::PropertyChangeEvent &evt,::std::function< css::uno::Reference< css::report::XSection >(OReportHelper *)> _pMemberFunction, const css::uno::Reference< css::report::XReportDefinition > &_xReport)
virtual void Redo() override
::std::function< css::uno::Reference< css::report::XSection >OGroupHelper *)> m_pMemberFunction
#define DBG_UNHANDLED_EXCEPTION(...)
#define TOOLS_WARN_EXCEPTION(area, stream)
int i
OCommentUndoAction(SdrModel &rMod, const char *pCommentID)
Definition: UndoActions.cxx:71
Mutex aLock
css::uno::Reference< css::uno::XInterface > m_xElement
virtual void Undo() override
Definition: UndoActions.cxx:82
css::uno::Reference< css::report::XSection > getPageHeader()
Definition: UndoActions.hxx:80
virtual void implReRemove() override
css::uno::Reference< css::report::XSection > getDetail()
Definition: UndoActions.hxx:82
virtual css::uno::Reference< css::beans::XPropertySet > getObject() override
OUString RptResId(const char *pId)
virtual css::uno::Reference< css::beans::XPropertySet > getObject()
OUndoGroupSectionAction(SdrModel &rMod, Action _eAction,::std::function< css::uno::Reference< css::report::XSection >(OGroupHelper *)> _pMemberFunction, const css::uno::Reference< css::report::XGroup > &_xGroup, const css::uno::Reference< css::uno::XInterface > &xElem, const char *pCommentId)
css::uno::Reference< css::container::XIndexContainer > m_xContainer
css::uno::Reference< css::report::XSection > getHeader()
Definition: UndoActions.hxx:57
css::uno::Reference< css::beans::XPropertySet > m_xObj
OUndoContainerAction(OUndoContainerAction const &)=delete
css::uno::Reference< css::uno::XInterface > m_xOwnElement
SdrObject * GetSdrObject() const
css::uno::Reference< css::report::XSection > getPageFooter()
Definition: UndoActions.hxx:81
OUndoPropertyGroupSectionAction(SdrModel &rMod, const css::beans::PropertyChangeEvent &evt,::std::function< css::uno::Reference< css::report::XSection >(OGroupHelper *)> _pMemberFunction, const css::uno::Reference< css::report::XGroup > &_xGroup)
virtual void implReRemove() override
virtual void Undo() override
ORptUndoPropertyAction(SdrModel &rMod, const css::beans::PropertyChangeEvent &evt)
css::uno::Reference< css::report::XSection > getReportFooter()
Definition: UndoActions.hxx:79
::std::function< css::uno::Reference< css::report::XSection >OReportHelper *)> m_pMemberFunction
void disposeComponent(css::uno::Reference< TYPE > &_rxComp)
css::uno::Reference< css::report::XSection > getFooter()
Definition: UndoActions.hxx:58
virtual void Redo() override
SectionViewAction m_eAction
Reference< xml::input::XElement > m_xElement
bool HasSdrObjectOwnership() const
virtual ~OUndoContainerAction() override
aStr
::std::function< css::uno::Reference< css::report::XSection >OGroupHelper *)> m_pMemberFunction
Helper class to allow std::mem_fun for SAL_CALL.
Definition: UndoActions.hxx:47