1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "ChartModelClone.hxx"
21 #include "ChartModelHelper.hxx"
22 #include "ControllerLockGuard.hxx"
23 #include "DataSourceHelper.hxx"
25 #include <com/sun/star/chart2/XAnyDescriptionAccess.hpp>
26 #include <com/sun/star/util/XCloneable.hpp>
27 #include <com/sun/star/chart2/XChartDocument.hpp>
28 #include <com/sun/star/view/XSelectionSupplier.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <com/sun/star/chart2/XTitled.hpp>
31 #include <com/sun/star/util/XModifiable.hpp>
32 #include <com/sun/star/chart2/data/XDataSource.hpp>
33 #include <com/sun/star/chart2/data/XLabeledDataSequence.hpp>
35 #include <comphelper/property.hxx>
36 #include <tools/diagnose_ex.h>
41 using ::com::sun::star::uno::Reference
;
42 using ::com::sun::star::uno::XInterface
;
43 using ::com::sun::star::uno::UNO_QUERY
;
44 using ::com::sun::star::uno::UNO_QUERY_THROW
;
45 using ::com::sun::star::uno::UNO_SET_THROW
;
46 using ::com::sun::star::uno::Exception
;
47 using ::com::sun::star::uno::RuntimeException
;
48 using ::com::sun::star::uno::Any
;
49 using ::com::sun::star::uno::makeAny
;
50 using ::com::sun::star::uno::Sequence
;
51 using ::com::sun::star::uno::Type
;
52 using ::com::sun::star::frame::XModel
;
53 using ::com::sun::star::util::XCloneable
;
54 using ::com::sun::star::chart2::XChartDocument
;
55 using ::com::sun::star::chart2::XInternalDataProvider
;
56 using ::com::sun::star::chart2::XAnyDescriptionAccess
;
57 using ::com::sun::star::view::XSelectionSupplier
;
58 using ::com::sun::star::lang::XComponent
;
59 using ::com::sun::star::chart2::XTitled
;
60 using ::com::sun::star::util::XModifiable
;
61 using ::com::sun::star::chart2::data::XDataSource
;
62 using ::com::sun::star::chart2::data::XLabeledDataSequence
;
67 Reference
< XModel
> lcl_cloneModel( const Reference
< XModel
> & xModel
)
69 Reference
< XModel
> xResult
;
72 const Reference
< XCloneable
> xCloneable( xModel
, UNO_QUERY_THROW
);
73 xResult
.set( xCloneable
->createClone(), UNO_QUERY_THROW
);
75 catch( const Exception
& )
77 DBG_UNHANDLED_EXCEPTION();
85 ChartModelClone::ChartModelClone( const Reference
< XModel
>& i_model
, const ModelFacet i_facet
)
87 m_xModelClone
.set( lcl_cloneModel( i_model
) );
91 if ( i_facet
== E_MODEL_WITH_DATA
)
93 const Reference
< XChartDocument
> xChartDoc( m_xModelClone
, UNO_QUERY_THROW
);
94 ENSURE_OR_THROW( xChartDoc
->hasInternalDataProvider(), "invalid chart model" );
96 const Reference
< XCloneable
> xCloneable( xChartDoc
->getDataProvider(), UNO_QUERY_THROW
);
97 m_xDataClone
.set( xCloneable
->createClone(), UNO_QUERY_THROW
);
100 if ( i_facet
== E_MODEL_WITH_SELECTION
)
102 const Reference
< XSelectionSupplier
> xSelSupp( m_xModelClone
->getCurrentController(), UNO_QUERY_THROW
);
103 m_aSelection
= xSelSupp
->getSelection();
106 catch( const Exception
& )
108 DBG_UNHANDLED_EXCEPTION();
112 ChartModelClone::~ChartModelClone()
114 if ( !impl_isDisposed() )
118 void ChartModelClone::dispose()
120 if ( impl_isDisposed() )
125 Reference
< XComponent
> xComp( m_xModelClone
, UNO_QUERY_THROW
);
128 catch( const Exception
& )
130 DBG_UNHANDLED_EXCEPTION();
132 m_xModelClone
.clear();
133 m_xDataClone
.clear();
134 m_aSelection
.clear();
137 ModelFacet
ChartModelClone::getFacet() const
139 if ( m_aSelection
.hasValue() )
140 return E_MODEL_WITH_SELECTION
;
141 if ( m_xDataClone
.is() )
142 return E_MODEL_WITH_DATA
;
146 void ChartModelClone::applyToModel( const Reference
< XModel
>& i_model
) const
148 applyModelContentToModel( i_model
, m_xModelClone
, m_xDataClone
);
150 if ( m_aSelection
.hasValue() )
154 Reference
< XSelectionSupplier
> xCurrentSelectionSuppl( i_model
->getCurrentController(), UNO_QUERY_THROW
);
155 xCurrentSelectionSuppl
->select( m_aSelection
);
157 catch( const Exception
& )
159 DBG_UNHANDLED_EXCEPTION();
166 void ImplApplyDataToModel( const Reference
< XModel
>& i_model
, const Reference
< XInternalDataProvider
> & i_data
)
168 Reference
< XChartDocument
> xDoc( i_model
, UNO_QUERY
);
169 OSL_ASSERT( xDoc
.is() && xDoc
->hasInternalDataProvider() );
171 // copy data from stored internal data provider
172 if( xDoc
.is() && xDoc
->hasInternalDataProvider())
174 Reference
< XAnyDescriptionAccess
> xCurrentData( xDoc
->getDataProvider(), UNO_QUERY
);
175 Reference
< XAnyDescriptionAccess
> xSavedData( i_data
, UNO_QUERY
);
176 if ( xCurrentData
.is() && xSavedData
.is() )
178 xCurrentData
->setData( xSavedData
->getData() );
179 xCurrentData
->setAnyRowDescriptions( xSavedData
->getAnyRowDescriptions());
180 xCurrentData
->setAnyColumnDescriptions( xSavedData
->getAnyColumnDescriptions());
186 void ChartModelClone::applyModelContentToModel( const Reference
< XModel
>& i_model
,
187 const Reference
< XModel
>& i_modelToCopyFrom
, const Reference
< XInternalDataProvider
>& i_data
)
189 ENSURE_OR_RETURN_VOID( i_model
.is(), "ChartModelElement::applyModelContentToModel: invalid source model!" );
190 ENSURE_OR_RETURN_VOID( i_modelToCopyFrom
.is(), "ChartModelElement::applyModelContentToModel: invalid source model!" );
193 // /-- loccked controllers of destination
194 ControllerLockGuardUNO
aLockedControllers( i_model
);
195 Reference
< XChartDocument
> xSource( i_modelToCopyFrom
, UNO_QUERY_THROW
);
196 Reference
< XChartDocument
> xDestination( i_model
, UNO_QUERY_THROW
);
198 // propagate the correct flag for plotting of hidden values to the data provider and all used sequences
199 ChartModel
& rModel
= dynamic_cast<ChartModel
&>(*i_model
.get());
200 ChartModelHelper::setIncludeHiddenCells(ChartModelHelper::isIncludeHiddenCells( i_modelToCopyFrom
), rModel
);
203 xDestination
->setFirstDiagram( xSource
->getFirstDiagram() );
206 Reference
< XTitled
> xDestinationTitled( xDestination
, UNO_QUERY_THROW
);
207 Reference
< XTitled
> xSourceTitled( xSource
, UNO_QUERY_THROW
);
208 xDestinationTitled
->setTitleObject( xSourceTitled
->getTitleObject() );
211 ::comphelper::copyProperties(
212 xSource
->getPageBackground(),
213 xDestination
->getPageBackground() );
215 // apply data (not applied in standard Undo)
217 ImplApplyDataToModel( i_model
, i_data
);
219 // register all sequences at the internal data provider to get adapted
220 // indexes when columns are added/removed
221 if ( xDestination
->hasInternalDataProvider() )
223 Reference
< XInternalDataProvider
> xNewDataProvider( xDestination
->getDataProvider(), UNO_QUERY
);
224 Reference
< XDataSource
> xUsedData( DataSourceHelper::getUsedData( i_model
) );
225 if ( xUsedData
.is() && xNewDataProvider
.is() )
227 Sequence
< Reference
< XLabeledDataSequence
> > aData( xUsedData
->getDataSequences() );
228 for( sal_Int32 i
=0; i
<aData
.getLength(); ++i
)
230 xNewDataProvider
->registerDataSequenceForChanges( aData
[i
]->getValues() );
231 xNewDataProvider
->registerDataSequenceForChanges( aData
[i
]->getLabel() );
236 // restore modify status
237 Reference
< XModifiable
> xSourceMod( xSource
, UNO_QUERY
);
238 Reference
< XModifiable
> xDestMod( xDestination
, UNO_QUERY
);
239 if ( xSourceMod
.is() && xDestMod
.is() && !xSourceMod
->isModified() )
241 xDestMod
->setModified( sal_False
);
243 // \-- loccked controllers of destination
245 catch( const Exception
& )
247 DBG_UNHANDLED_EXCEPTION();
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */