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/container/XIndexAccess.hpp>
23 #include <com/sun/star/sheet/XScenario.hpp>
24 #include <com/sun/star/sheet/XScenarios.hpp>
25 #include <com/sun/star/sheet/XScenariosSupplier.hpp>
26 #include <com/sun/star/sheet/XSpreadsheet.hpp>
27 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
28 #include <oox/helper/attributelist.hxx>
29 #include <oox/helper/containerhelper.hxx>
30 #include <oox/helper/propertyset.hxx>
31 #include <oox/token/properties.hxx>
32 #include "addressconverter.hxx"
33 #include "biffinputstream.hxx"
38 using namespace ::com::sun::star::container
;
39 using namespace ::com::sun::star::sheet
;
40 using namespace ::com::sun::star::table
;
41 using namespace ::com::sun::star::uno
;
43 ScenarioCellModel::ScenarioCellModel() :
49 ScenarioModel::ScenarioModel() :
55 Scenario::Scenario( const WorkbookHelper
& rHelper
, sal_Int16 nSheet
) :
56 WorkbookHelper( rHelper
),
61 void Scenario::importScenario( const AttributeList
& rAttribs
)
63 maModel
.maName
= rAttribs
.getXString( XML_name
, OUString() );
64 maModel
.maComment
= rAttribs
.getXString( XML_comment
, OUString() );
65 maModel
.maUser
= rAttribs
.getXString( XML_user
, OUString() );
66 maModel
.mbLocked
= rAttribs
.getBool( XML_locked
, false );
67 maModel
.mbHidden
= rAttribs
.getBool( XML_hidden
, false );
70 void Scenario::importInputCells( const AttributeList
& rAttribs
)
72 ScenarioCellModel aModel
;
73 AddressConverter::convertToCellAddressUnchecked( aModel
.maPos
, rAttribs
.getString( XML_r
, OUString() ), mnSheet
);
74 aModel
.maValue
= rAttribs
.getXString( XML_val
, OUString() );
75 aModel
.mnNumFmtId
= rAttribs
.getInteger( XML_numFmtId
, 0 );
76 aModel
.mbDeleted
= rAttribs
.getBool( XML_deleted
, false );
77 maCells
.push_back( aModel
);
80 void Scenario::importScenario( SequenceInputStream
& rStrm
)
82 rStrm
.skip( 2 ); // cell count
83 // two longs instead of flag field
84 maModel
.mbLocked
= rStrm
.readInt32() != 0;
85 maModel
.mbHidden
= rStrm
.readInt32() != 0;
86 rStrm
>> maModel
.maName
>> maModel
.maComment
>> maModel
.maUser
;
89 void Scenario::importInputCells( SequenceInputStream
& rStrm
)
91 // TODO: where is the deleted flag?
92 ScenarioCellModel aModel
;
96 aModel
.mnNumFmtId
= rStrm
.readuInt16();
97 rStrm
>> aModel
.maValue
;
98 AddressConverter::convertToCellAddressUnchecked( aModel
.maPos
, aPos
, mnSheet
);
99 maCells
.push_back( aModel
);
102 void Scenario::finalizeImport()
104 AddressConverter
& rAddrConv
= getAddressConverter();
105 ::std::vector
< CellRangeAddress
> aRanges
;
106 for( ScenarioCellVector::iterator aIt
= maCells
.begin(), aEnd
= maCells
.end(); aIt
!= aEnd
; ++aIt
)
107 if( !aIt
->mbDeleted
&& rAddrConv
.checkCellAddress( aIt
->maPos
, true ) )
108 aRanges
.push_back( CellRangeAddress( aIt
->maPos
.Sheet
, aIt
->maPos
.Column
, aIt
->maPos
.Row
, aIt
->maPos
.Column
, aIt
->maPos
.Row
) );
110 if( !aRanges
.empty() && !maModel
.maName
.isEmpty() ) try
112 /* Find an unused name for the scenario (Calc stores scenario data in
113 hidden sheets named after the scenario following the base sheet). */
114 Reference
< XNameAccess
> xSheetsNA( getDocument()->getSheets(), UNO_QUERY_THROW
);
115 OUString aScenName
= ContainerHelper::getUnusedName( xSheetsNA
, maModel
.maName
, '_' );
117 // create the new scenario sheet
118 Reference
< XScenariosSupplier
> xScenariosSupp( getSheetFromDoc( mnSheet
), UNO_QUERY_THROW
);
119 Reference
< XScenarios
> xScenarios( xScenariosSupp
->getScenarios(), UNO_SET_THROW
);
120 xScenarios
->addNewByName( aScenName
, ContainerHelper::vectorToSequence( aRanges
), maModel
.maComment
);
122 // write scenario cell values
123 Reference
< XSpreadsheet
> xSheet( getSheetFromDoc( aScenName
), UNO_SET_THROW
);
124 for( ScenarioCellVector::iterator aIt
= maCells
.begin(), aEnd
= maCells
.end(); aIt
!= aEnd
; ++aIt
)
126 if( !aIt
->mbDeleted
) try
128 // use XCell::setFormula to auto-detect values and strings
129 Reference
< XCell
> xCell( xSheet
->getCellByPosition( aIt
->maPos
.Column
, aIt
->maPos
.Row
), UNO_SET_THROW
);
130 xCell
->setFormula( aIt
->maValue
);
137 // scenario properties
138 PropertySet
aPropSet( xScenarios
->getByName( aScenName
) );
139 aPropSet
.setProperty( PROP_IsActive
, false );
140 aPropSet
.setProperty( PROP_CopyBack
, false );
141 aPropSet
.setProperty( PROP_CopyStyles
, false );
142 aPropSet
.setProperty( PROP_CopyFormulas
, false );
143 aPropSet
.setProperty( PROP_Protected
, maModel
.mbLocked
);
144 // #112621# do not show/print scenario border
145 aPropSet
.setProperty( PROP_ShowBorder
, false );
146 aPropSet
.setProperty( PROP_PrintBorder
, false );
153 SheetScenariosModel::SheetScenariosModel() :
159 SheetScenarios::SheetScenarios( const WorkbookHelper
& rHelper
, sal_Int16 nSheet
) :
160 WorkbookHelper( rHelper
),
165 void SheetScenarios::importScenarios( const AttributeList
& rAttribs
)
167 maModel
.mnCurrent
= rAttribs
.getInteger( XML_current
, 0 );
168 maModel
.mnShown
= rAttribs
.getInteger( XML_show
, 0 );
171 void SheetScenarios::importScenarios( SequenceInputStream
& rStrm
)
173 maModel
.mnCurrent
= rStrm
.readuInt16();
174 maModel
.mnShown
= rStrm
.readuInt16();
177 Scenario
& SheetScenarios::createScenario()
179 ScenarioVector::value_type
xScenario( new Scenario( *this, mnSheet
) );
180 maScenarios
.push_back( xScenario
);
184 void SheetScenarios::finalizeImport()
186 maScenarios
.forEachMem( &Scenario::finalizeImport
);
188 // activate a scenario
191 Reference
< XScenariosSupplier
> xScenariosSupp( getSheetFromDoc( mnSheet
), UNO_QUERY_THROW
);
192 Reference
< XIndexAccess
> xScenariosIA( xScenariosSupp
->getScenarios(), UNO_QUERY_THROW
);
193 Reference
< XScenario
> xScenario( xScenariosIA
->getByIndex( maModel
.mnShown
), UNO_QUERY_THROW
);
201 ScenarioBuffer::ScenarioBuffer( const WorkbookHelper
& rHelper
) :
202 WorkbookHelper( rHelper
)
206 SheetScenarios
& ScenarioBuffer::createSheetScenarios( sal_Int16 nSheet
)
208 SheetScenariosMap::mapped_type
& rxSheetScens
= maSheetScenarios
[ nSheet
];
210 rxSheetScens
.reset( new SheetScenarios( *this, nSheet
) );
211 return *rxSheetScens
;
214 void ScenarioBuffer::finalizeImport()
216 maSheetScenarios
.forEachMem( &SheetScenarios::finalizeImport
);
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */