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