Branch data 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 : :
21 : : #include "ChartModelClone.hxx"
22 : : #include "ChartModelHelper.hxx"
23 : : #include "ControllerLockGuard.hxx"
24 : : #include "DataSourceHelper.hxx"
25 : :
26 : : #include <com/sun/star/chart2/XAnyDescriptionAccess.hpp>
27 : : #include <com/sun/star/util/XCloneable.hpp>
28 : : #include <com/sun/star/chart2/XChartDocument.hpp>
29 : : #include <com/sun/star/view/XSelectionSupplier.hpp>
30 : : #include <com/sun/star/lang/XComponent.hpp>
31 : : #include <com/sun/star/chart2/XTitled.hpp>
32 : : #include <com/sun/star/util/XModifiable.hpp>
33 : : #include <com/sun/star/chart2/data/XDataSource.hpp>
34 : : #include <com/sun/star/chart2/data/XLabeledDataSequence.hpp>
35 : :
36 : : #include <comphelper/property.hxx>
37 : : #include <tools/diagnose_ex.h>
38 : :
39 : : //......................................................................................................................
40 : : namespace chart
41 : : {
42 : : //......................................................................................................................
43 : :
44 : : /** === begin UNO using === **/
45 : : using ::com::sun::star::uno::Reference;
46 : : using ::com::sun::star::uno::XInterface;
47 : : using ::com::sun::star::uno::UNO_QUERY;
48 : : using ::com::sun::star::uno::UNO_QUERY_THROW;
49 : : using ::com::sun::star::uno::UNO_SET_THROW;
50 : : using ::com::sun::star::uno::Exception;
51 : : using ::com::sun::star::uno::RuntimeException;
52 : : using ::com::sun::star::uno::Any;
53 : : using ::com::sun::star::uno::makeAny;
54 : : using ::com::sun::star::uno::Sequence;
55 : : using ::com::sun::star::uno::Type;
56 : : using ::com::sun::star::frame::XModel;
57 : : using ::com::sun::star::util::XCloneable;
58 : : using ::com::sun::star::chart2::XChartDocument;
59 : : using ::com::sun::star::chart2::XInternalDataProvider;
60 : : using ::com::sun::star::chart2::XAnyDescriptionAccess;
61 : : using ::com::sun::star::view::XSelectionSupplier;
62 : : using ::com::sun::star::lang::XComponent;
63 : : using ::com::sun::star::chart2::XTitled;
64 : : using ::com::sun::star::util::XModifiable;
65 : : using ::com::sun::star::chart2::data::XDataSource;
66 : : using ::com::sun::star::chart2::data::XLabeledDataSequence;
67 : : /** === end UNO using === **/
68 : :
69 : : // =================================================================================================================
70 : : // = helper
71 : : // =================================================================================================================
72 : : namespace
73 : : {
74 : 0 : Reference< XModel > lcl_cloneModel( const Reference< XModel > & xModel )
75 : : {
76 : 0 : Reference< XModel > xResult;
77 : : try
78 : : {
79 [ # # ]: 0 : const Reference< XCloneable > xCloneable( xModel, UNO_QUERY_THROW );
80 [ # # ][ # # ]: 0 : xResult.set( xCloneable->createClone(), UNO_QUERY_THROW );
[ # # ][ # # ]
81 : : }
82 [ # # ]: 0 : catch( const Exception& )
83 : : {
84 : : DBG_UNHANDLED_EXCEPTION();
85 : : }
86 : 0 : return xResult;
87 : : }
88 : :
89 : : }
90 : :
91 : : // =================================================================================================================
92 : : // = ChartModelClone
93 : : // =================================================================================================================
94 : : // -----------------------------------------------------------------------------------------------------------------
95 : 0 : ChartModelClone::ChartModelClone( const Reference< XModel >& i_model, const ModelFacet i_facet )
96 : : {
97 [ # # ][ # # ]: 0 : m_xModelClone.set( lcl_cloneModel( i_model ) );
98 : :
99 : : try
100 : : {
101 [ # # ]: 0 : if ( i_facet == E_MODEL_WITH_DATA )
102 : : {
103 [ # # ]: 0 : const Reference< XChartDocument > xChartDoc( m_xModelClone, UNO_QUERY_THROW );
104 [ # # ][ # # ]: 0 : ENSURE_OR_THROW( xChartDoc->hasInternalDataProvider(), "invalid chart model" );
[ # # ][ # # ]
[ # # ][ # # ]
105 : :
106 [ # # ][ # # ]: 0 : const Reference< XCloneable > xCloneable( xChartDoc->getDataProvider(), UNO_QUERY_THROW );
[ # # ]
107 [ # # ][ # # ]: 0 : m_xDataClone.set( xCloneable->createClone(), UNO_QUERY_THROW );
[ # # ]
108 : : }
109 : :
110 [ # # ]: 0 : if ( i_facet == E_MODEL_WITH_SELECTION )
111 : : {
112 [ # # ][ # # ]: 0 : const Reference< XSelectionSupplier > xSelSupp( m_xModelClone->getCurrentController(), UNO_QUERY_THROW );
[ # # ]
113 [ # # ][ # # ]: 0 : m_aSelection = xSelSupp->getSelection();
[ # # ]
114 : : }
115 : : }
116 [ # # ]: 0 : catch( const Exception& )
117 : : {
118 : : DBG_UNHANDLED_EXCEPTION();
119 : : }
120 : 0 : }
121 : :
122 : :
123 : : // -----------------------------------------------------------------------------------------------------------------
124 : 0 : ChartModelClone::~ChartModelClone()
125 : : {
126 [ # # ]: 0 : if ( !impl_isDisposed() )
127 [ # # ]: 0 : dispose();
128 : 0 : }
129 : :
130 : : // -----------------------------------------------------------------------------------------------------------------
131 : 0 : void ChartModelClone::dispose()
132 : : {
133 [ # # ]: 0 : if ( impl_isDisposed() )
134 : 0 : return;
135 : :
136 : : try
137 : : {
138 [ # # ]: 0 : Reference< XComponent > xComp( m_xModelClone, UNO_QUERY_THROW );
139 [ # # ][ # # ]: 0 : xComp->dispose();
[ # # ]
140 : : }
141 : 0 : catch( const Exception& )
142 : : {
143 : : DBG_UNHANDLED_EXCEPTION();
144 : : }
145 : 0 : m_xModelClone.clear();
146 : 0 : m_xDataClone.clear();
147 : 0 : m_aSelection.clear();
148 : : }
149 : :
150 : : // -----------------------------------------------------------------------------------------------------------------
151 : 0 : ModelFacet ChartModelClone::getFacet() const
152 : : {
153 [ # # ]: 0 : if ( m_aSelection.hasValue() )
154 : 0 : return E_MODEL_WITH_SELECTION;
155 [ # # ]: 0 : if ( m_xDataClone.is() )
156 : 0 : return E_MODEL_WITH_DATA;
157 : 0 : return E_MODEL;
158 : : }
159 : :
160 : : // -----------------------------------------------------------------------------------------------------------------
161 : 0 : void ChartModelClone::applyToModel( const Reference< XModel >& i_model ) const
162 : : {
163 : 0 : applyModelContentToModel( i_model, m_xModelClone, m_xDataClone );
164 : :
165 [ # # ]: 0 : if ( m_aSelection.hasValue() )
166 : : {
167 : : try
168 : : {
169 [ # # ][ # # ]: 0 : Reference< XSelectionSupplier > xCurrentSelectionSuppl( i_model->getCurrentController(), UNO_QUERY_THROW );
[ # # ]
170 [ # # ][ # # ]: 0 : xCurrentSelectionSuppl->select( m_aSelection );
[ # # ]
171 : : }
172 : 0 : catch( const Exception& )
173 : : {
174 : : DBG_UNHANDLED_EXCEPTION();
175 : : }
176 : : }
177 : 0 : }
178 : :
179 : : // -----------------------------------------------------------------------------------------------------------------
180 : : namespace
181 : : {
182 : 0 : void ImplApplyDataToModel( const Reference< XModel >& i_model, const Reference< XInternalDataProvider > & i_data )
183 : : {
184 [ # # ]: 0 : Reference< XChartDocument > xDoc( i_model, UNO_QUERY );
185 : : OSL_ASSERT( xDoc.is() && xDoc->hasInternalDataProvider() );
186 : :
187 : : // copy data from stored internal data provider
188 [ # # ][ # # ]: 0 : if( xDoc.is() && xDoc->hasInternalDataProvider())
[ # # ][ # # ]
[ # # ]
189 : : {
190 [ # # ][ # # ]: 0 : Reference< XAnyDescriptionAccess > xCurrentData( xDoc->getDataProvider(), UNO_QUERY );
[ # # ]
191 [ # # ]: 0 : Reference< XAnyDescriptionAccess > xSavedData( i_data, UNO_QUERY );
192 [ # # ][ # # ]: 0 : if ( xCurrentData.is() && xSavedData.is() )
[ # # ]
193 : : {
194 [ # # ][ # # ]: 0 : xCurrentData->setData( xSavedData->getData() );
[ # # ][ # # ]
[ # # ]
195 [ # # ][ # # ]: 0 : xCurrentData->setAnyRowDescriptions( xSavedData->getAnyRowDescriptions());
[ # # ][ # # ]
[ # # ]
196 [ # # ][ # # ]: 0 : xCurrentData->setAnyColumnDescriptions( xSavedData->getAnyColumnDescriptions());
[ # # ][ # # ]
[ # # ]
197 : 0 : }
198 : 0 : }
199 : 0 : }
200 : : }
201 : :
202 : : // -----------------------------------------------------------------------------------------------------------------
203 : 0 : void ChartModelClone::applyModelContentToModel( const Reference< XModel >& i_model,
204 : : const Reference< XModel >& i_modelToCopyFrom, const Reference< XInternalDataProvider >& i_data )
205 : : {
206 [ # # ]: 0 : ENSURE_OR_RETURN_VOID( i_model.is(), "ChartModelElement::applyModelContentToModel: invalid source model!" );
207 [ # # ]: 0 : ENSURE_OR_RETURN_VOID( i_modelToCopyFrom.is(), "ChartModelElement::applyModelContentToModel: invalid source model!" );
208 : : try
209 : : {
210 : : // /-- loccked controllers of destination
211 [ # # ]: 0 : ControllerLockGuard aLockedControllers( i_model );
212 [ # # ]: 0 : Reference< XChartDocument > xSource( i_modelToCopyFrom, UNO_QUERY_THROW );
213 [ # # ]: 0 : Reference< XChartDocument > xDestination( i_model, UNO_QUERY_THROW );
214 : :
215 : : // propagate the correct flag for plotting of hidden values to the data provider and all used sequences
216 [ # # ][ # # ]: 0 : ChartModelHelper::setIncludeHiddenCells( ChartModelHelper::isIncludeHiddenCells( i_modelToCopyFrom ) , i_model );
217 : :
218 : : // diagram
219 [ # # ][ # # ]: 0 : xDestination->setFirstDiagram( xSource->getFirstDiagram() );
[ # # ][ # # ]
220 : :
221 : : // main title
222 [ # # ]: 0 : Reference< XTitled > xDestinationTitled( xDestination, UNO_QUERY_THROW );
223 [ # # ]: 0 : Reference< XTitled > xSourceTitled( xSource, UNO_QUERY_THROW );
224 [ # # ][ # # ]: 0 : xDestinationTitled->setTitleObject( xSourceTitled->getTitleObject() );
[ # # ][ # # ]
225 : :
226 : : // page background
227 : : ::comphelper::copyProperties(
228 [ # # ]: 0 : xSource->getPageBackground(),
229 [ # # ][ # # ]: 0 : xDestination->getPageBackground() );
[ # # ][ # # ]
230 : :
231 : : // apply data (not applied in standard Undo)
232 [ # # ]: 0 : if ( i_data.is() )
233 [ # # ]: 0 : ImplApplyDataToModel( i_model, i_data );
234 : :
235 : : // register all sequences at the internal data provider to get adapted
236 : : // indexes when columns are added/removed
237 [ # # ][ # # ]: 0 : if ( xDestination->hasInternalDataProvider() )
[ # # ]
238 : : {
239 [ # # ][ # # ]: 0 : Reference< XInternalDataProvider > xNewDataProvider( xDestination->getDataProvider(), UNO_QUERY );
[ # # ]
240 [ # # ]: 0 : Reference< XDataSource > xUsedData( DataSourceHelper::getUsedData( i_model ) );
241 [ # # ][ # # ]: 0 : if ( xUsedData.is() && xNewDataProvider.is() )
[ # # ]
242 : : {
243 [ # # ][ # # ]: 0 : Sequence< Reference< XLabeledDataSequence > > aData( xUsedData->getDataSequences() );
244 [ # # ]: 0 : for( sal_Int32 i=0; i<aData.getLength(); ++i )
245 : : {
246 [ # # ][ # # ]: 0 : xNewDataProvider->registerDataSequenceForChanges( aData[i]->getValues() );
[ # # ][ # # ]
[ # # ]
247 [ # # ][ # # ]: 0 : xNewDataProvider->registerDataSequenceForChanges( aData[i]->getLabel() );
[ # # ][ # # ]
[ # # ]
248 [ # # ]: 0 : }
249 : 0 : }
250 : : }
251 : :
252 : : // restore modify status
253 [ # # ]: 0 : Reference< XModifiable > xSourceMod( xSource, UNO_QUERY );
254 [ # # ]: 0 : Reference< XModifiable > xDestMod( xDestination, UNO_QUERY );
255 [ # # ][ # # ]: 0 : if ( xSourceMod.is() && xDestMod.is() && !xSourceMod->isModified() )
[ # # ][ # # ]
[ # # ][ # # ]
256 : : {
257 [ # # ][ # # ]: 0 : xDestMod->setModified( sal_False );
258 [ # # ][ # # ]: 0 : }
259 : : // \-- loccked controllers of destination
260 : : }
261 : 0 : catch( const Exception& )
262 : : {
263 : : DBG_UNHANDLED_EXCEPTION();
264 : : }
265 : : }
266 : :
267 : :
268 : : //......................................................................................................................
269 : : } // namespace chart
270 : : //......................................................................................................................
271 : :
272 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|