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