Line data Source code
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 "scenariobuffer.hxx"
21 :
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"
34 :
35 : namespace oox {
36 : namespace xls {
37 :
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;
42 :
43 0 : ScenarioCellModel::ScenarioCellModel() :
44 : mnNumFmtId( 0 ),
45 0 : mbDeleted( false )
46 : {
47 0 : }
48 :
49 0 : ScenarioModel::ScenarioModel() :
50 : mbLocked( false ),
51 0 : mbHidden( false )
52 : {
53 0 : }
54 :
55 0 : Scenario::Scenario( const WorkbookHelper& rHelper, sal_Int16 nSheet ) :
56 : WorkbookHelper( rHelper ),
57 0 : mnSheet( nSheet )
58 : {
59 0 : }
60 :
61 0 : void Scenario::importScenario( const AttributeList& rAttribs )
62 : {
63 0 : maModel.maName = rAttribs.getXString( XML_name, OUString() );
64 0 : maModel.maComment = rAttribs.getXString( XML_comment, OUString() );
65 0 : maModel.maUser = rAttribs.getXString( XML_user, OUString() );
66 0 : maModel.mbLocked = rAttribs.getBool( XML_locked, false );
67 0 : maModel.mbHidden = rAttribs.getBool( XML_hidden, false );
68 0 : }
69 :
70 0 : void Scenario::importInputCells( const AttributeList& rAttribs )
71 : {
72 0 : ScenarioCellModel aModel;
73 0 : getAddressConverter().convertToCellAddressUnchecked( aModel.maPos, rAttribs.getString( XML_r, OUString() ), mnSheet );
74 0 : aModel.maValue = rAttribs.getXString( XML_val, OUString() );
75 0 : aModel.mnNumFmtId = rAttribs.getInteger( XML_numFmtId, 0 );
76 0 : aModel.mbDeleted = rAttribs.getBool( XML_deleted, false );
77 0 : maCells.push_back( aModel );
78 0 : }
79 :
80 0 : void Scenario::importScenario( SequenceInputStream& rStrm )
81 : {
82 0 : rStrm.skip( 2 ); // cell count
83 : // two longs instead of flag field
84 0 : maModel.mbLocked = rStrm.readInt32() != 0;
85 0 : maModel.mbHidden = rStrm.readInt32() != 0;
86 0 : rStrm >> maModel.maName >> maModel.maComment >> maModel.maUser;
87 0 : }
88 :
89 0 : void Scenario::importInputCells( SequenceInputStream& rStrm )
90 : {
91 : // TODO: where is the deleted flag?
92 0 : ScenarioCellModel aModel;
93 0 : BinAddress aPos;
94 0 : rStrm >> aPos;
95 0 : rStrm.skip( 8 );
96 0 : aModel.mnNumFmtId = rStrm.readuInt16();
97 0 : rStrm >> aModel.maValue;
98 0 : getAddressConverter().convertToCellAddressUnchecked( aModel.maPos, aPos, mnSheet );
99 0 : maCells.push_back( aModel );
100 0 : }
101 :
102 0 : void Scenario::finalizeImport()
103 : {
104 0 : AddressConverter& rAddrConv = getAddressConverter();
105 0 : ::std::vector< CellRangeAddress > aRanges;
106 0 : for( ScenarioCellVector::iterator aIt = maCells.begin(), aEnd = maCells.end(); aIt != aEnd; ++aIt )
107 0 : if( !aIt->mbDeleted && rAddrConv.checkCellAddress( aIt->maPos, true ) )
108 0 : aRanges.push_back( CellRangeAddress( aIt->maPos.Sheet, aIt->maPos.Column, aIt->maPos.Row, aIt->maPos.Column, aIt->maPos.Row ) );
109 :
110 0 : if( !aRanges.empty() && !maModel.maName.isEmpty() ) try
111 : {
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 0 : Reference< XNameAccess > xSheetsNA( getDocument()->getSheets(), UNO_QUERY_THROW );
115 0 : OUString aScenName = ContainerHelper::getUnusedName( xSheetsNA, maModel.maName, '_' );
116 :
117 : // create the new scenario sheet
118 0 : Reference< XScenariosSupplier > xScenariosSupp( getSheetFromDoc( mnSheet ), UNO_QUERY_THROW );
119 0 : Reference< XScenarios > xScenarios( xScenariosSupp->getScenarios(), UNO_SET_THROW );
120 0 : xScenarios->addNewByName( aScenName, ContainerHelper::vectorToSequence( aRanges ), maModel.maComment );
121 :
122 : // write scenario cell values
123 0 : Reference< XSpreadsheet > xSheet( getSheetFromDoc( aScenName ), UNO_SET_THROW );
124 0 : for( ScenarioCellVector::iterator aIt = maCells.begin(), aEnd = maCells.end(); aIt != aEnd; ++aIt )
125 : {
126 0 : if( !aIt->mbDeleted ) try
127 : {
128 : // use XCell::setFormula to auto-detect values and strings
129 0 : Reference< XCell > xCell( xSheet->getCellByPosition( aIt->maPos.Column, aIt->maPos.Row ), UNO_SET_THROW );
130 0 : xCell->setFormula( aIt->maValue );
131 : }
132 0 : catch( Exception& )
133 : {
134 : }
135 : }
136 :
137 : // scenario properties
138 0 : PropertySet aPropSet( xScenarios->getByName( aScenName ) );
139 0 : aPropSet.setProperty( PROP_IsActive, false );
140 0 : aPropSet.setProperty( PROP_CopyBack, false );
141 0 : aPropSet.setProperty( PROP_CopyStyles, false );
142 0 : aPropSet.setProperty( PROP_CopyFormulas, false );
143 0 : aPropSet.setProperty( PROP_Protected, maModel.mbLocked );
144 : // #112621# do not show/print scenario border
145 0 : aPropSet.setProperty( PROP_ShowBorder, false );
146 0 : aPropSet.setProperty( PROP_PrintBorder, false );
147 : }
148 0 : catch( Exception& )
149 : {
150 0 : }
151 0 : }
152 :
153 0 : SheetScenariosModel::SheetScenariosModel() :
154 : mnCurrent( 0 ),
155 0 : mnShown( 0 )
156 : {
157 0 : }
158 :
159 0 : SheetScenarios::SheetScenarios( const WorkbookHelper& rHelper, sal_Int16 nSheet ) :
160 : WorkbookHelper( rHelper ),
161 0 : mnSheet( nSheet )
162 : {
163 0 : }
164 :
165 0 : void SheetScenarios::importScenarios( const AttributeList& rAttribs )
166 : {
167 0 : maModel.mnCurrent = rAttribs.getInteger( XML_current, 0 );
168 0 : maModel.mnShown = rAttribs.getInteger( XML_show, 0 );
169 0 : }
170 :
171 0 : void SheetScenarios::importScenarios( SequenceInputStream& rStrm )
172 : {
173 0 : maModel.mnCurrent = rStrm.readuInt16();
174 0 : maModel.mnShown = rStrm.readuInt16();
175 0 : }
176 :
177 0 : Scenario& SheetScenarios::createScenario()
178 : {
179 0 : ScenarioVector::value_type xScenario( new Scenario( *this, mnSheet ) );
180 0 : maScenarios.push_back( xScenario );
181 0 : return *xScenario;
182 : }
183 :
184 0 : void SheetScenarios::finalizeImport()
185 : {
186 0 : maScenarios.forEachMem( &Scenario::finalizeImport );
187 :
188 : // activate a scenario
189 : try
190 : {
191 0 : Reference< XScenariosSupplier > xScenariosSupp( getSheetFromDoc( mnSheet ), UNO_QUERY_THROW );
192 0 : Reference< XIndexAccess > xScenariosIA( xScenariosSupp->getScenarios(), UNO_QUERY_THROW );
193 0 : Reference< XScenario > xScenario( xScenariosIA->getByIndex( maModel.mnShown ), UNO_QUERY_THROW );
194 0 : xScenario->apply();
195 : }
196 0 : catch( Exception& )
197 : {
198 : }
199 0 : }
200 :
201 44 : ScenarioBuffer::ScenarioBuffer( const WorkbookHelper& rHelper ) :
202 44 : WorkbookHelper( rHelper )
203 : {
204 44 : }
205 :
206 0 : SheetScenarios& ScenarioBuffer::createSheetScenarios( sal_Int16 nSheet )
207 : {
208 0 : SheetScenariosMap::mapped_type& rxSheetScens = maSheetScenarios[ nSheet ];
209 0 : if( !rxSheetScens )
210 0 : rxSheetScens.reset( new SheetScenarios( *this, nSheet ) );
211 0 : return *rxSheetScens;
212 : }
213 :
214 44 : void ScenarioBuffer::finalizeImport()
215 : {
216 44 : maSheetScenarios.forEachMem( &SheetScenarios::finalizeImport );
217 44 : }
218 :
219 : } // namespace xls
220 18 : } // namespace oox
221 :
222 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|