LibreOffice Module chart2 (master) 1
ChartModelClone.cxx
Go to the documentation of this file.
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 <ChartModel.hxx>
22#include <ChartModelHelper.hxx>
24#include <DataSource.hxx>
25#include <DataSourceHelper.hxx>
26
27#include <com/sun/star/chart2/XAnyDescriptionAccess.hpp>
28#include <com/sun/star/chart2/XInternalDataProvider.hpp>
29#include <com/sun/star/util/XCloneable.hpp>
30#include <com/sun/star/chart2/XChartDocument.hpp>
31#include <com/sun/star/view/XSelectionSupplier.hpp>
32#include <com/sun/star/lang/XComponent.hpp>
33#include <com/sun/star/chart2/XTitled.hpp>
34#include <com/sun/star/util/XModifiable.hpp>
35#include <com/sun/star/chart2/data/XLabeledDataSequence.hpp>
36
39
40namespace chart
41{
42
43 using ::com::sun::star::uno::Reference;
44 using ::com::sun::star::uno::UNO_QUERY;
45 using ::com::sun::star::uno::UNO_QUERY_THROW;
46 using ::com::sun::star::uno::Exception;
47 using ::com::sun::star::uno::Sequence;
48 using ::com::sun::star::frame::XModel;
49 using ::com::sun::star::util::XCloneable;
50 using ::com::sun::star::chart2::XChartDocument;
51 using ::com::sun::star::chart2::XInternalDataProvider;
52 using ::com::sun::star::chart2::XAnyDescriptionAccess;
53 using ::com::sun::star::view::XSelectionSupplier;
54 using ::com::sun::star::chart2::data::XLabeledDataSequence;
55
56 // = helper
57 namespace
58 {
60 {
61 try
62 {
63 return new ChartModel(*xModel);
64 }
65 catch( const Exception& )
66 {
68 }
69 return nullptr;
70 }
71
72 }
73
74 // = ChartModelClone
76 {
77 m_xModelClone = lcl_cloneModel( i_model );
78
79 try
80 {
81 if ( i_facet == E_MODEL_WITH_DATA )
82 {
83 ENSURE_OR_THROW( m_xModelClone && m_xModelClone->hasInternalDataProvider(), "invalid chart model" );
84
85 const Reference< XCloneable > xCloneable( m_xModelClone->getDataProvider(), UNO_QUERY_THROW );
86 m_xDataClone.set( xCloneable->createClone(), UNO_QUERY_THROW );
87 }
88
89 if ( i_facet == E_MODEL_WITH_SELECTION )
90 {
91 const Reference< XSelectionSupplier > xSelSupp( m_xModelClone->getCurrentController(), UNO_QUERY_THROW );
92 m_aSelection = xSelSupp->getSelection();
93 }
94 }
95 catch( const Exception& )
96 {
98 }
99 }
100
102 {
103 if ( !impl_isDisposed() )
104 dispose();
105 }
106
108 {
109 if ( impl_isDisposed() )
110 return;
111
112 m_xModelClone.clear();
113 m_xDataClone.clear();
114 m_aSelection.clear();
115 }
116
118 {
119 if ( m_aSelection.hasValue() )
121 if ( m_xDataClone.is() )
122 return E_MODEL_WITH_DATA;
123 return E_MODEL;
124 }
125
127 {
129
130 if ( m_aSelection.hasValue() )
131 {
132 try
133 {
134 Reference< XSelectionSupplier > xCurrentSelectionSuppl( i_model->getCurrentController(), UNO_QUERY_THROW );
135 xCurrentSelectionSuppl->select( m_aSelection );
136 }
137 catch( const Exception& )
138 {
139 DBG_UNHANDLED_EXCEPTION("chart2");
140 }
141 }
142 }
143
144 namespace
145 {
146 void ImplApplyDataToModel( const Reference< XModel >& i_model, const Reference< XInternalDataProvider > & i_data )
147 {
148 Reference< XChartDocument > xDoc( i_model, UNO_QUERY );
149 OSL_ASSERT( xDoc.is() && xDoc->hasInternalDataProvider() );
150
151 // copy data from stored internal data provider
152 if( xDoc.is() && xDoc->hasInternalDataProvider())
153 {
154 Reference< XAnyDescriptionAccess > xCurrentData( xDoc->getDataProvider(), UNO_QUERY );
155 Reference< XAnyDescriptionAccess > xSavedData( i_data, UNO_QUERY );
156 if ( xCurrentData.is() && xSavedData.is() )
157 {
158 xCurrentData->setData( xSavedData->getData() );
159 xCurrentData->setAnyRowDescriptions( xSavedData->getAnyRowDescriptions());
160 xCurrentData->setAnyColumnDescriptions( xSavedData->getAnyColumnDescriptions());
161 }
162 }
163 }
164 }
165
167 const rtl::Reference<::chart::ChartModel>& i_modelToCopyFrom,
169 {
170 ENSURE_OR_RETURN_VOID( i_model.is(), "ChartModelElement::applyModelContentToModel: invalid source model!" );
171 ENSURE_OR_RETURN_VOID( i_modelToCopyFrom.is(), "ChartModelElement::applyModelContentToModel: invalid source model!" );
172 try
173 {
174 // locked controllers of destination
175 ControllerLockGuardUNO aLockedControllers( i_model );
176
177 // propagate the correct flag for plotting of hidden values to the data provider and all used sequences
179
180 // diagram
181 i_model->setFirstDiagram( i_modelToCopyFrom->getFirstDiagram() );
182
183 // main title
184 i_model->setTitleObject( i_modelToCopyFrom->getTitleObject() );
185
186 // page background
187 ::comphelper::copyProperties(
188 i_modelToCopyFrom->getPageBackground(),
189 i_model->getPageBackground() );
190
191 // apply data (not applied in standard Undo)
192 if ( i_data.is() )
193 ImplApplyDataToModel( i_model, i_data );
194
195 // register all sequences at the internal data provider to get adapted
196 // indexes when columns are added/removed
197 if ( i_model->hasInternalDataProvider() )
198 {
199 Reference< XInternalDataProvider > xNewDataProvider( i_model->getDataProvider(), UNO_QUERY );
201 if ( xUsedData.is() && xNewDataProvider.is() )
202 {
203 const Sequence< Reference< XLabeledDataSequence > > aData( xUsedData->getDataSequences() );
204 for( Reference< XLabeledDataSequence > const & labeledDataSeq : aData )
205 {
206 xNewDataProvider->registerDataSequenceForChanges( labeledDataSeq->getValues() );
207 xNewDataProvider->registerDataSequenceForChanges( labeledDataSeq->getLabel() );
208 }
209 }
210 }
211
212 // restore modify status
213 if ( !i_modelToCopyFrom->isModified() )
214 {
215 i_model->setModified( false );
216 }
217 // \-- locked controllers of destination
218 }
219 catch( const Exception& )
220 {
221 DBG_UNHANDLED_EXCEPTION("chart2");
222 }
223 }
224
225} // namespace chart
226
227/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ChartModelClone(const rtl::Reference<::chart::ChartModel > &i_model, const ModelFacet i_facet)
css::uno::Reference< css::chart2::XInternalDataProvider > m_xDataClone
void applyToModel(const rtl::Reference<::chart::ChartModel > &i_model) const
static void applyModelContentToModel(const rtl::Reference<::chart::ChartModel > &i_model, const rtl::Reference<::chart::ChartModel > &i_modelToCopyFrom, const css::uno::Reference< css::chart2::XInternalDataProvider > &i_data)
rtl::Reference<::chart::ChartModel > m_xModelClone
ModelFacet getFacet() const
static bool isIncludeHiddenCells(const rtl::Reference<::chart::ChartModel > &xChartModel)
static bool setIncludeHiddenCells(bool bIncludeHiddenCells, ChartModel &rModel)
This guard calls lockControllers at the given Model in the CTOR and unlockControllers in the DTOR.
static rtl::Reference< ::chart::DataSource > getUsedData(ChartModel &rModel)
#define ENSURE_OR_THROW(c, m)
#define DBG_UNHANDLED_EXCEPTION(...)
#define ENSURE_OR_RETURN_VOID(c, m)
constexpr OUStringLiteral aData
@ E_MODEL_WITH_SELECTION
@ E_MODEL_WITH_DATA
@ Exception