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