1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "scenariobuffer.hxx"
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/container/XIndexAccess.hpp>
24 #include <com/sun/star/sheet/XScenario.hpp>
25 #include <com/sun/star/sheet/XScenarios.hpp>
26 #include <com/sun/star/sheet/XScenariosSupplier.hpp>
27 #include <com/sun/star/sheet/XSpreadsheet.hpp>
28 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
29 #include <oox/helper/attributelist.hxx>
30 #include <oox/helper/containerhelper.hxx>
31 #include <oox/helper/propertyset.hxx>
32 #include <oox/token/properties.hxx>
33 #include <oox/token/tokens.hxx>
34 #include "addressconverter.hxx"
35 #include "biffinputstream.hxx"
40 using namespace ::com::sun::star::container
;
41 using namespace ::com::sun::star::sheet
;
42 using namespace ::com::sun::star::table
;
43 using namespace ::com::sun::star::uno
;
45 ScenarioCellModel::ScenarioCellModel() :
51 ScenarioModel::ScenarioModel() :
57 Scenario::Scenario( const WorkbookHelper
& rHelper
, sal_Int16 nSheet
) :
58 WorkbookHelper( rHelper
),
63 void Scenario::importScenario( const AttributeList
& rAttribs
)
65 maModel
.maName
= rAttribs
.getXString( XML_name
, OUString() );
66 maModel
.maComment
= rAttribs
.getXString( XML_comment
, OUString() );
67 maModel
.maUser
= rAttribs
.getXString( XML_user
, OUString() );
68 maModel
.mbLocked
= rAttribs
.getBool( XML_locked
, false );
69 maModel
.mbHidden
= rAttribs
.getBool( XML_hidden
, false );
72 void Scenario::importInputCells( const AttributeList
& rAttribs
)
74 ScenarioCellModel aModel
;
75 AddressConverter::convertToCellAddressUnchecked( aModel
.maPos
, rAttribs
.getString( XML_r
, OUString() ), mnSheet
);
76 aModel
.maValue
= rAttribs
.getXString( XML_val
, OUString() );
77 aModel
.mnNumFmtId
= rAttribs
.getInteger( XML_numFmtId
, 0 );
78 aModel
.mbDeleted
= rAttribs
.getBool( XML_deleted
, false );
79 maCells
.push_back( aModel
);
82 void Scenario::importScenario( SequenceInputStream
& rStrm
)
84 rStrm
.skip( 2 ); // cell count
85 // two longs instead of flag field
86 maModel
.mbLocked
= rStrm
.readInt32() != 0;
87 maModel
.mbHidden
= rStrm
.readInt32() != 0;
88 rStrm
>> maModel
.maName
>> maModel
.maComment
>> maModel
.maUser
;
91 void Scenario::importInputCells( SequenceInputStream
& rStrm
)
93 // TODO: where is the deleted flag?
94 ScenarioCellModel aModel
;
98 aModel
.mnNumFmtId
= rStrm
.readuInt16();
99 rStrm
>> aModel
.maValue
;
100 AddressConverter::convertToCellAddressUnchecked( aModel
.maPos
, aPos
, mnSheet
);
101 maCells
.push_back( aModel
);
104 void Scenario::finalizeImport()
106 AddressConverter
& rAddrConv
= getAddressConverter();
107 ::std::vector
< CellRangeAddress
> aRanges
;
108 for( ScenarioCellVector::iterator aIt
= maCells
.begin(), aEnd
= maCells
.end(); aIt
!= aEnd
; ++aIt
)
109 if( !aIt
->mbDeleted
&& rAddrConv
.checkCellAddress( aIt
->maPos
, true ) )
110 aRanges
.push_back( CellRangeAddress( aIt
->maPos
.Tab(), aIt
->maPos
.Col(), aIt
->maPos
.Row(), aIt
->maPos
.Col(), aIt
->maPos
.Row() ) );
112 if( !aRanges
.empty() && !maModel
.maName
.isEmpty() ) try
114 /* Find an unused name for the scenario (Calc stores scenario data in
115 hidden sheets named after the scenario following the base sheet). */
116 Reference
< XNameAccess
> xSheetsNA( getDocument()->getSheets(), UNO_QUERY_THROW
);
117 OUString aScenName
= ContainerHelper::getUnusedName( xSheetsNA
, maModel
.maName
, '_' );
119 // create the new scenario sheet
120 Reference
< XScenariosSupplier
> xScenariosSupp( getSheetFromDoc( mnSheet
), UNO_QUERY_THROW
);
121 Reference
< XScenarios
> xScenarios( xScenariosSupp
->getScenarios(), UNO_SET_THROW
);
122 xScenarios
->addNewByName( aScenName
, ContainerHelper::vectorToSequence( aRanges
), maModel
.maComment
);
124 // write scenario cell values
125 Reference
< XSpreadsheet
> xSheet( getSheetFromDoc( aScenName
), UNO_SET_THROW
);
126 for( ScenarioCellVector::iterator aIt
= maCells
.begin(), aEnd
= maCells
.end(); aIt
!= aEnd
; ++aIt
)
128 if( !aIt
->mbDeleted
) try
130 // use XCell::setFormula to auto-detect values and strings
131 Reference
< XCell
> xCell( xSheet
->getCellByPosition( aIt
->maPos
.Col(), aIt
->maPos
.Row() ), UNO_SET_THROW
);
132 xCell
->setFormula( aIt
->maValue
);
139 // scenario properties
140 PropertySet
aPropSet( xScenarios
->getByName( aScenName
) );
141 aPropSet
.setProperty( PROP_IsActive
, false );
142 aPropSet
.setProperty( PROP_CopyBack
, false );
143 aPropSet
.setProperty( PROP_CopyStyles
, false );
144 aPropSet
.setProperty( PROP_CopyFormulas
, false );
145 aPropSet
.setProperty( PROP_Protected
, maModel
.mbLocked
);
146 // #112621# do not show/print scenario border
147 aPropSet
.setProperty( PROP_ShowBorder
, false );
148 aPropSet
.setProperty( PROP_PrintBorder
, false );
155 SheetScenariosModel::SheetScenariosModel() :
161 SheetScenarios::SheetScenarios( const WorkbookHelper
& rHelper
, sal_Int16 nSheet
) :
162 WorkbookHelper( rHelper
),
167 void SheetScenarios::importScenarios( const AttributeList
& rAttribs
)
169 maModel
.mnCurrent
= rAttribs
.getInteger( XML_current
, 0 );
170 maModel
.mnShown
= rAttribs
.getInteger( XML_show
, 0 );
173 void SheetScenarios::importScenarios( SequenceInputStream
& rStrm
)
175 maModel
.mnCurrent
= rStrm
.readuInt16();
176 maModel
.mnShown
= rStrm
.readuInt16();
179 Scenario
& SheetScenarios::createScenario()
181 ScenarioVector::value_type
xScenario( new Scenario( *this, mnSheet
) );
182 maScenarios
.push_back( xScenario
);
186 void SheetScenarios::finalizeImport()
188 maScenarios
.forEachMem( &Scenario::finalizeImport
);
190 // activate a scenario
193 Reference
< XScenariosSupplier
> xScenariosSupp( getSheetFromDoc( mnSheet
), UNO_QUERY_THROW
);
194 Reference
< XIndexAccess
> xScenariosIA( xScenariosSupp
->getScenarios(), UNO_QUERY_THROW
);
195 Reference
< XScenario
> xScenario( xScenariosIA
->getByIndex( maModel
.mnShown
), UNO_QUERY_THROW
);
203 ScenarioBuffer::ScenarioBuffer( const WorkbookHelper
& rHelper
) :
204 WorkbookHelper( rHelper
)
208 SheetScenarios
& ScenarioBuffer::createSheetScenarios( sal_Int16 nSheet
)
210 SheetScenariosMap::mapped_type
& rxSheetScens
= maSheetScenarios
[ nSheet
];
212 rxSheetScens
.reset( new SheetScenarios( *this, nSheet
) );
213 return *rxSheetScens
;
216 void ScenarioBuffer::finalizeImport()
218 maSheetScenarios
.forEachMem( &SheetScenarios::finalizeImport
);
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */