tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / chart2 / source / controller / main / ChartModelClone.cxx
blobb1228cd383a34ce70bb49e351894aefd926edc82
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <ChartModel.hxx>
22 #include <ChartModelHelper.hxx>
23 #include <ControllerLockGuard.hxx>
24 #include <DataSource.hxx>
25 #include <DataSourceHelper.hxx>
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/chart2/data/XLabeledDataSequence.hpp>
34 #include <comphelper/property.hxx>
35 #include <comphelper/diagnose_ex.hxx>
37 namespace chart
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::uno::UNO_QUERY;
42 using ::com::sun::star::uno::UNO_QUERY_THROW;
43 using ::com::sun::star::uno::Exception;
44 using ::com::sun::star::uno::Sequence;
45 using ::com::sun::star::frame::XModel;
46 using ::com::sun::star::util::XCloneable;
47 using ::com::sun::star::chart2::XChartDocument;
48 using ::com::sun::star::chart2::XInternalDataProvider;
49 using ::com::sun::star::chart2::XAnyDescriptionAccess;
50 using ::com::sun::star::view::XSelectionSupplier;
51 using ::com::sun::star::chart2::data::XLabeledDataSequence;
53 // = helper
54 namespace
56 rtl::Reference<::chart::ChartModel> lcl_cloneModel( const rtl::Reference<::chart::ChartModel> & xModel )
58 try
60 return new ChartModel(*xModel);
62 catch( const Exception& )
64 DBG_UNHANDLED_EXCEPTION("chart2");
66 return nullptr;
71 // = ChartModelClone
72 ChartModelClone::ChartModelClone( const rtl::Reference<::chart::ChartModel>& i_model, const ModelFacet i_facet )
74 m_xModelClone = lcl_cloneModel( i_model );
76 try
78 if ( i_facet == E_MODEL_WITH_DATA )
80 ENSURE_OR_THROW( m_xModelClone && m_xModelClone->hasInternalDataProvider(), "invalid chart model" );
82 const Reference< XCloneable > xCloneable( m_xModelClone->getDataProvider(), UNO_QUERY_THROW );
83 m_xDataClone.set( xCloneable->createClone(), UNO_QUERY_THROW );
86 if ( i_facet == E_MODEL_WITH_SELECTION )
88 const Reference< XSelectionSupplier > xSelSupp( m_xModelClone->getCurrentController(), UNO_QUERY_THROW );
89 m_aSelection = xSelSupp->getSelection();
92 catch( const Exception& )
94 DBG_UNHANDLED_EXCEPTION("chart2");
98 ChartModelClone::~ChartModelClone()
100 if ( !impl_isDisposed() )
101 dispose();
104 void ChartModelClone::dispose()
106 if ( impl_isDisposed() )
107 return;
109 m_xModelClone.clear();
110 m_xDataClone.clear();
111 m_aSelection.clear();
114 ModelFacet ChartModelClone::getFacet() const
116 if ( m_aSelection.hasValue() )
117 return E_MODEL_WITH_SELECTION;
118 if ( m_xDataClone.is() )
119 return E_MODEL_WITH_DATA;
120 return E_MODEL;
123 void ChartModelClone::applyToModel( const rtl::Reference<::chart::ChartModel>& i_model ) const
125 applyModelContentToModel( i_model, m_xModelClone, m_xDataClone );
127 if ( m_aSelection.hasValue() )
131 Reference< XSelectionSupplier > xCurrentSelectionSuppl( i_model->getCurrentController(), UNO_QUERY_THROW );
132 xCurrentSelectionSuppl->select( m_aSelection );
134 catch( const Exception& )
136 DBG_UNHANDLED_EXCEPTION("chart2");
141 namespace
143 void ImplApplyDataToModel( const Reference< XModel >& i_model, const Reference< XInternalDataProvider > & i_data )
145 Reference< XChartDocument > xDoc( i_model, UNO_QUERY );
146 OSL_ASSERT( xDoc.is() && xDoc->hasInternalDataProvider() );
148 // copy data from stored internal data provider
149 if( xDoc.is() && xDoc->hasInternalDataProvider())
151 Reference< XAnyDescriptionAccess > xCurrentData( xDoc->getDataProvider(), UNO_QUERY );
152 Reference< XAnyDescriptionAccess > xSavedData( i_data, UNO_QUERY );
153 if ( xCurrentData.is() && xSavedData.is() )
155 xCurrentData->setData( xSavedData->getData() );
156 xCurrentData->setAnyRowDescriptions( xSavedData->getAnyRowDescriptions());
157 xCurrentData->setAnyColumnDescriptions( xSavedData->getAnyColumnDescriptions());
163 void ChartModelClone::applyModelContentToModel( const rtl::Reference<::chart::ChartModel>& i_model,
164 const rtl::Reference<::chart::ChartModel>& i_modelToCopyFrom,
165 const Reference< XInternalDataProvider >& i_data )
167 ENSURE_OR_RETURN_VOID( i_model.is(), "ChartModelElement::applyModelContentToModel: invalid source model!" );
168 ENSURE_OR_RETURN_VOID( i_modelToCopyFrom.is(), "ChartModelElement::applyModelContentToModel: invalid source model!" );
171 // locked controllers of destination
172 ControllerLockGuardUNO aLockedControllers( i_model );
174 // propagate the correct flag for plotting of hidden values to the data provider and all used sequences
175 ChartModelHelper::setIncludeHiddenCells(ChartModelHelper::isIncludeHiddenCells( i_modelToCopyFrom ), *i_model);
177 // diagram
178 i_model->setFirstDiagram( i_modelToCopyFrom->getFirstDiagram() );
180 // main title
181 i_model->setTitleObject( i_modelToCopyFrom->getTitleObject() );
183 // page background
184 ::comphelper::copyProperties(
185 i_modelToCopyFrom->getPageBackground(),
186 i_model->getPageBackground() );
188 // apply data (not applied in standard Undo)
189 if ( i_data.is() )
190 ImplApplyDataToModel( i_model, i_data );
192 // register all sequences at the internal data provider to get adapted
193 // indexes when columns are added/removed
194 if ( i_model->hasInternalDataProvider() )
196 Reference< XInternalDataProvider > xNewDataProvider( i_model->getDataProvider(), UNO_QUERY );
197 rtl::Reference< DataSource > xUsedData = DataSourceHelper::getUsedData( *i_model );
198 if ( xUsedData.is() && xNewDataProvider.is() )
200 const Sequence< Reference< XLabeledDataSequence > > aData( xUsedData->getDataSequences() );
201 for( Reference< XLabeledDataSequence > const & labeledDataSeq : aData )
203 xNewDataProvider->registerDataSequenceForChanges( labeledDataSeq->getValues() );
204 xNewDataProvider->registerDataSequenceForChanges( labeledDataSeq->getLabel() );
209 // restore modify status
210 if ( !i_modelToCopyFrom->isModified() )
212 i_model->setModified( false );
214 // \-- locked controllers of destination
216 catch( const Exception& )
218 DBG_UNHANDLED_EXCEPTION("chart2");
222 } // namespace chart
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */