Line data Source code
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 "uistrings.hrc"
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>
32 : #include <svx/unoshape.hxx>
33 : #include <vcl/settings.hxx>
34 :
35 : #include <functional>
36 :
37 : namespace rptui
38 : {
39 : using namespace ::com::sun::star;
40 : using namespace uno;
41 : using namespace lang;
42 : using namespace script;
43 : using namespace beans;
44 : using namespace awt;
45 : using namespace util;
46 : using namespace container;
47 : using namespace report;
48 :
49 :
50 : namespace
51 : {
52 0 : void lcl_collectElements(const uno::Reference< report::XSection >& _xSection,::std::vector< uno::Reference< drawing::XShape> >& _rControls)
53 : {
54 0 : if ( _xSection.is() )
55 : {
56 0 : sal_Int32 nCount = _xSection->getCount();
57 0 : _rControls.reserve(nCount);
58 0 : while ( nCount )
59 : {
60 0 : uno::Reference< drawing::XShape> xShape(_xSection->getByIndex(nCount-1),uno::UNO_QUERY);
61 0 : _rControls.push_back(xShape);
62 0 : _xSection->remove(xShape);
63 0 : --nCount;
64 0 : }
65 : }
66 0 : }
67 :
68 0 : void lcl_insertElements(const uno::Reference< report::XSection >& _xSection,const ::std::vector< uno::Reference< drawing::XShape> >& _aControls)
69 : {
70 0 : if ( _xSection.is() )
71 : {
72 0 : ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aIter = _aControls.rbegin();
73 0 : ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aEnd = _aControls.rend();
74 0 : for (; aIter != aEnd; ++aIter)
75 : {
76 : try
77 : {
78 0 : const awt::Point aPos = (*aIter)->getPosition();
79 0 : const awt::Size aSize = (*aIter)->getSize();
80 0 : _xSection->add(*aIter);
81 0 : (*aIter)->setPosition( aPos );
82 0 : (*aIter)->setSize( aSize );
83 : }
84 0 : catch(const uno::Exception&)
85 : {
86 : OSL_FAIL("lcl_insertElements:Exception caught!");
87 : }
88 : }
89 : }
90 0 : }
91 :
92 0 : void lcl_setValues(const uno::Reference< report::XSection >& _xSection,const ::std::vector< ::std::pair< OUString ,uno::Any> >& _aValues)
93 : {
94 0 : if ( _xSection.is() )
95 : {
96 0 : ::std::vector< ::std::pair< OUString ,uno::Any> >::const_iterator aIter = _aValues.begin();
97 0 : ::std::vector< ::std::pair< OUString ,uno::Any> >::const_iterator aEnd = _aValues.end();
98 0 : for (; aIter != aEnd; ++aIter)
99 : {
100 : try
101 : {
102 0 : _xSection->setPropertyValue(aIter->first,aIter->second);
103 : }
104 0 : catch(const uno::Exception&)
105 : {
106 : OSL_FAIL("lcl_setValues:Exception caught!");
107 : }
108 : }
109 : }
110 0 : }
111 : }
112 :
113 0 : TYPEINIT1( OSectionUndo, OCommentUndoAction );
114 :
115 0 : OSectionUndo::OSectionUndo(OReportModel& _rMod
116 : ,sal_uInt16 _nSlot
117 : ,Action _eAction
118 : ,sal_uInt16 nCommentID)
119 : : OCommentUndoAction(_rMod,nCommentID)
120 : ,m_eAction(_eAction)
121 : ,m_nSlot(_nSlot)
122 0 : ,m_bInserted(false)
123 : {
124 0 : }
125 :
126 0 : OSectionUndo::~OSectionUndo()
127 : {
128 0 : if ( !m_bInserted )
129 : {
130 0 : OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
131 0 : ::std::vector< uno::Reference< drawing::XShape> >::iterator aEnd = m_aControls.end();
132 0 : for (::std::vector< uno::Reference< drawing::XShape> >::iterator aIter = m_aControls.begin(); aIter != aEnd; ++aIter)
133 : {
134 0 : uno::Reference< drawing::XShape> xShape = *aIter;
135 0 : rEnv.RemoveElement(xShape);
136 :
137 : #if OSL_DEBUG_LEVEL > 0
138 : SvxShape* pShape = SvxShape::getImplementation( xShape );
139 : SdrObject* pObject = pShape ? pShape->GetSdrObject() : NULL;
140 : OSL_ENSURE( pShape && pShape->HasSdrObjectOwnership() && pObject && !pObject->IsInserted(),
141 : "OSectionUndo::~OSectionUndo: inconsistency in the shape/object ownership!" );
142 : #endif
143 : try
144 : {
145 0 : comphelper::disposeComponent(xShape);
146 : }
147 0 : catch(const uno::Exception &)
148 : {
149 : OSL_FAIL("Exception caught!");
150 : }
151 0 : }
152 : }
153 0 : }
154 :
155 0 : void OSectionUndo::collectControls(const uno::Reference< report::XSection >& _xSection)
156 : {
157 0 : m_aControls.clear();
158 : try
159 : {
160 : // copy all properties for restoring
161 0 : uno::Reference< beans::XPropertySetInfo> xInfo = _xSection->getPropertySetInfo();
162 0 : uno::Sequence< beans::Property> aSeq = xInfo->getProperties();
163 0 : const beans::Property* pIter = aSeq.getConstArray();
164 0 : const beans::Property* pEnd = pIter + aSeq.getLength();
165 0 : for(;pIter != pEnd;++pIter)
166 : {
167 0 : if ( 0 == (pIter->Attributes & beans::PropertyAttribute::READONLY) )
168 0 : m_aValues.push_back(::std::pair< OUString ,uno::Any>(pIter->Name,_xSection->getPropertyValue(pIter->Name)));
169 : }
170 0 : lcl_collectElements(_xSection,m_aControls);
171 : }
172 0 : catch(uno::Exception&)
173 : {
174 : }
175 0 : }
176 :
177 0 : void OSectionUndo::Undo()
178 : {
179 : try
180 : {
181 0 : switch ( m_eAction )
182 : {
183 : case Inserted:
184 0 : implReRemove();
185 0 : break;
186 :
187 : case Removed:
188 0 : implReInsert();
189 0 : break;
190 : }
191 : }
192 0 : catch( const Exception& )
193 : {
194 : OSL_FAIL( "OSectionUndo::Undo: caught an exception!" );
195 : }
196 0 : }
197 :
198 0 : void OSectionUndo::Redo()
199 : {
200 : try
201 : {
202 0 : switch ( m_eAction )
203 : {
204 : case Inserted:
205 0 : implReInsert();
206 0 : break;
207 :
208 : case Removed:
209 0 : implReRemove();
210 0 : break;
211 : }
212 : }
213 0 : catch( const Exception& )
214 : {
215 : OSL_FAIL( "OSectionUndo::Redo: caught an exception!" );
216 : }
217 0 : }
218 :
219 0 : TYPEINIT1( OReportSectionUndo, OSectionUndo );
220 :
221 0 : OReportSectionUndo::OReportSectionUndo(OReportModel& _rMod,sal_uInt16 _nSlot
222 : ,::std::mem_fun_t< uno::Reference< report::XSection >
223 : ,OReportHelper> _pMemberFunction
224 : ,const uno::Reference< report::XReportDefinition >& _xReport
225 : ,Action _eAction
226 : ,sal_uInt16 nCommentID)
227 : : OSectionUndo(_rMod,_nSlot,_eAction,nCommentID)
228 : ,m_aReportHelper(_xReport)
229 0 : ,m_pMemberFunction(_pMemberFunction)
230 : {
231 0 : if( m_eAction == Removed )
232 0 : collectControls(m_pMemberFunction(&m_aReportHelper));
233 0 : }
234 :
235 0 : OReportSectionUndo::~OReportSectionUndo()
236 : {
237 0 : }
238 :
239 0 : void OReportSectionUndo::implReInsert( )
240 : {
241 0 : const uno::Sequence< beans::PropertyValue > aArgs;
242 0 : m_pController->executeChecked(m_nSlot,aArgs);
243 0 : uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aReportHelper);
244 0 : lcl_insertElements(xSection,m_aControls);
245 0 : lcl_setValues(xSection,m_aValues);
246 0 : m_bInserted = true;
247 0 : }
248 :
249 0 : void OReportSectionUndo::implReRemove( )
250 : {
251 0 : if( m_eAction == Removed )
252 0 : collectControls(m_pMemberFunction(&m_aReportHelper));
253 0 : const uno::Sequence< beans::PropertyValue > aArgs;
254 0 : m_pController->executeChecked(m_nSlot,aArgs);
255 0 : m_bInserted = false;
256 0 : }
257 :
258 0 : TYPEINIT1( OGroupSectionUndo, OSectionUndo );
259 :
260 0 : OGroupSectionUndo::OGroupSectionUndo(OReportModel& _rMod,sal_uInt16 _nSlot
261 : ,::std::mem_fun_t< uno::Reference< report::XSection >
262 : ,OGroupHelper> _pMemberFunction
263 : ,const uno::Reference< report::XGroup >& _xGroup
264 : ,Action _eAction
265 : ,sal_uInt16 nCommentID)
266 : : OSectionUndo(_rMod,_nSlot,_eAction,nCommentID)
267 : ,m_aGroupHelper(_xGroup)
268 0 : ,m_pMemberFunction(_pMemberFunction)
269 : {
270 0 : if( m_eAction == Removed )
271 : {
272 0 : uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
273 0 : if ( xSection.is() )
274 0 : m_sName = xSection->getName();
275 0 : collectControls(xSection);
276 : }
277 0 : }
278 :
279 0 : OUString OGroupSectionUndo::GetComment() const
280 : {
281 0 : if ( m_sName.isEmpty() )
282 : {
283 : try
284 : {
285 0 : uno::Reference< report::XSection > xSection = const_cast<OGroupSectionUndo*>(this)->m_pMemberFunction(&const_cast<OGroupSectionUndo*>(this)->m_aGroupHelper);
286 :
287 0 : if ( xSection.is() )
288 0 : m_sName = xSection->getName();
289 : }
290 0 : catch (const uno::Exception&)
291 : {
292 : }
293 : }
294 0 : return m_strComment + m_sName;
295 : }
296 :
297 0 : void OGroupSectionUndo::implReInsert( )
298 : {
299 0 : uno::Sequence< beans::PropertyValue > aArgs(2);
300 :
301 0 : aArgs[0].Name = SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? OUString(PROPERTY_HEADERON) : OUString(PROPERTY_FOOTERON);
302 0 : aArgs[0].Value <<= sal_True;
303 0 : aArgs[1].Name = PROPERTY_GROUP;
304 0 : aArgs[1].Value <<= m_aGroupHelper.getGroup();
305 0 : m_pController->executeChecked(m_nSlot,aArgs);
306 :
307 0 : uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
308 0 : lcl_insertElements(xSection,m_aControls);
309 0 : lcl_setValues(xSection,m_aValues);
310 0 : m_bInserted = true;
311 0 : }
312 :
313 0 : void OGroupSectionUndo::implReRemove( )
314 : {
315 0 : if( m_eAction == Removed )
316 0 : collectControls(m_pMemberFunction(&m_aGroupHelper));
317 :
318 0 : uno::Sequence< beans::PropertyValue > aArgs(2);
319 :
320 0 : aArgs[0].Name = SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? OUString(PROPERTY_HEADERON) : OUString(PROPERTY_FOOTERON);
321 0 : aArgs[0].Value <<= sal_False;
322 0 : aArgs[1].Name = PROPERTY_GROUP;
323 0 : aArgs[1].Value <<= m_aGroupHelper.getGroup();
324 :
325 0 : m_pController->executeChecked(m_nSlot,aArgs);
326 0 : m_bInserted = false;
327 0 : }
328 :
329 0 : TYPEINIT1( OGroupUndo, OCommentUndoAction );
330 :
331 0 : OGroupUndo::OGroupUndo(OReportModel& _rMod
332 : ,sal_uInt16 nCommentID
333 : ,Action _eAction
334 : ,const uno::Reference< report::XGroup>& _xGroup
335 : ,const uno::Reference< report::XReportDefinition >& _xReportDefinition)
336 : : OCommentUndoAction(_rMod,nCommentID)
337 : ,m_xGroup(_xGroup)
338 : ,m_xReportDefinition(_xReportDefinition)
339 0 : ,m_eAction(_eAction)
340 : {
341 0 : m_nLastPosition = getPositionInIndexAccess(m_xReportDefinition->getGroups().get(),m_xGroup);
342 0 : }
343 :
344 0 : void OGroupUndo::implReInsert( )
345 : {
346 : try
347 : {
348 0 : m_xReportDefinition->getGroups()->insertByIndex(m_nLastPosition,uno::makeAny(m_xGroup));
349 : }
350 0 : catch(uno::Exception&)
351 : {
352 : OSL_FAIL("Exception catched while undoing remove group");
353 : }
354 0 : }
355 :
356 0 : void OGroupUndo::implReRemove( )
357 : {
358 : try
359 : {
360 0 : m_xReportDefinition->getGroups()->removeByIndex(m_nLastPosition);
361 : }
362 0 : catch(uno::Exception&)
363 : {
364 : OSL_FAIL("Exception catched while redoing remove group");
365 : }
366 0 : }
367 :
368 0 : void OGroupUndo::Undo()
369 : {
370 0 : switch ( m_eAction )
371 : {
372 : case Inserted:
373 0 : implReRemove();
374 0 : break;
375 :
376 : case Removed:
377 0 : implReInsert();
378 0 : break;
379 : }
380 :
381 0 : }
382 :
383 0 : void OGroupUndo::Redo()
384 : {
385 0 : switch ( m_eAction )
386 : {
387 : case Inserted:
388 0 : implReInsert();
389 0 : break;
390 :
391 : case Removed:
392 0 : implReRemove();
393 0 : break;
394 : }
395 0 : }
396 :
397 :
398 : } // rptui
399 :
400 :
401 :
402 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|