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