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