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 "SolverComponent.hxx"
23 #include <com/sun/star/container/XIndexAccess.hpp>
24 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
25 #include <com/sun/star/sheet/XSpreadsheet.hpp>
26 #include <com/sun/star/table/CellAddress.hpp>
28 #include <cppuhelper/supportsservice.hxx>
31 #include <tools/resmgr.hxx>
33 using namespace com::sun::star
;
36 #define STR_NONNEGATIVE "NonNegative"
37 #define STR_INTEGER "Integer"
38 #define STR_TIMEOUT "Timeout"
39 #define STR_EPSILONLEVEL "EpsilonLevel"
40 #define STR_LIMITBBDEPTH "LimitBBDepth"
43 // Resources from tools are used for translated strings
45 ResMgr
* SolverComponent::pSolverResMgr
= NULL
;
47 OUString
SolverComponent::GetResourceString( sal_uInt32 nId
)
50 pSolverResMgr
= ResMgr::CreateResMgr("solver");
52 return ResId(nId
, *pSolverResMgr
).toString();
55 size_t ScSolverCellHash::operator()( const css::table::CellAddress
& rAddress
) const
57 return ( rAddress
.Sheet
<< 24 ) | ( rAddress
.Column
<< 16 ) | rAddress
.Row
;
60 bool ScSolverCellEqual::operator()( const css::table::CellAddress
& rAddr1
, const css::table::CellAddress
& rAddr2
) const
62 return AddressEqual( rAddr1
, rAddr2
);
77 uno::Reference
<table::XCell
> SolverComponent::GetCell( const uno::Reference
<sheet::XSpreadsheetDocument
>& xDoc
,
78 const table::CellAddress
& rPos
)
80 uno::Reference
<container::XIndexAccess
> xSheets( xDoc
->getSheets(), uno::UNO_QUERY
);
81 uno::Reference
<sheet::XSpreadsheet
> xSheet( xSheets
->getByIndex( rPos
.Sheet
), uno::UNO_QUERY
);
82 return xSheet
->getCellByPosition( rPos
.Column
, rPos
.Row
);
85 void SolverComponent::SetValue( const uno::Reference
<sheet::XSpreadsheetDocument
>& xDoc
,
86 const table::CellAddress
& rPos
, double fValue
)
88 SolverComponent::GetCell( xDoc
, rPos
)->setValue( fValue
);
91 double SolverComponent::GetValue( const uno::Reference
<sheet::XSpreadsheetDocument
>& xDoc
,
92 const table::CellAddress
& rPos
)
94 return SolverComponent::GetCell( xDoc
, rPos
)->getValue();
97 SolverComponent::SolverComponent() :
98 OPropertyContainer( GetBroadcastHelper() ),
100 mbNonNegative( false ),
104 mbLimitBBDepth( true ),
108 // for XPropertySet implementation:
109 registerProperty( STR_NONNEGATIVE
, PROP_NONNEGATIVE
, 0, &mbNonNegative
, cppu::UnoType
<decltype(mbNonNegative
)>::get() );
110 registerProperty( STR_INTEGER
, PROP_INTEGER
, 0, &mbInteger
, cppu::UnoType
<decltype(mbInteger
)>::get() );
111 registerProperty( STR_TIMEOUT
, PROP_TIMEOUT
, 0, &mnTimeout
, cppu::UnoType
<decltype(mnTimeout
)>::get() );
112 registerProperty( STR_EPSILONLEVEL
, PROP_EPSILONLEVEL
, 0, &mnEpsilonLevel
, cppu::UnoType
<decltype(mnEpsilonLevel
)>::get() );
113 registerProperty( STR_LIMITBBDEPTH
, PROP_LIMITBBDEPTH
, 0, &mbLimitBBDepth
, cppu::UnoType
<decltype(mbLimitBBDepth
)>::get() );
116 SolverComponent::~SolverComponent()
120 IMPLEMENT_FORWARD_XINTERFACE2( SolverComponent
, SolverComponent_Base
, OPropertyContainer
)
121 IMPLEMENT_FORWARD_XTYPEPROVIDER2( SolverComponent
, SolverComponent_Base
, OPropertyContainer
)
123 cppu::IPropertyArrayHelper
* SolverComponent::createArrayHelper() const
125 uno::Sequence
<beans::Property
> aProps
;
126 describeProperties( aProps
);
127 return new cppu::OPropertyArrayHelper( aProps
);
130 cppu::IPropertyArrayHelper
& SAL_CALL
SolverComponent::getInfoHelper()
132 return *getArrayHelper();
135 uno::Reference
<beans::XPropertySetInfo
> SAL_CALL
SolverComponent::getPropertySetInfo() throw(uno::RuntimeException
, std::exception
)
137 return createPropertySetInfo( getInfoHelper() );
140 // XSolverDescription
142 OUString SAL_CALL
SolverComponent::getStatusDescription() throw (uno::RuntimeException
, std::exception
)
147 OUString SAL_CALL
SolverComponent::getPropertyDescription( const OUString
& rPropertyName
) throw (uno::RuntimeException
, std::exception
)
149 sal_uInt32 nResId
= 0;
150 sal_Int32 nHandle
= getInfoHelper().getHandleByName( rPropertyName
);
153 case PROP_NONNEGATIVE
:
154 nResId
= RID_PROPERTY_NONNEGATIVE
;
157 nResId
= RID_PROPERTY_INTEGER
;
160 nResId
= RID_PROPERTY_TIMEOUT
;
162 case PROP_EPSILONLEVEL
:
163 nResId
= RID_PROPERTY_EPSILONLEVEL
;
165 case PROP_LIMITBBDEPTH
:
166 nResId
= RID_PROPERTY_LIMITBBDEPTH
;
170 // unknown - leave empty
175 aRet
= SolverComponent::GetResourceString( nResId
);
181 uno::Reference
<sheet::XSpreadsheetDocument
> SAL_CALL
SolverComponent::getDocument() throw(uno::RuntimeException
, std::exception
)
186 void SAL_CALL
SolverComponent::setDocument( const uno::Reference
<sheet::XSpreadsheetDocument
>& _document
)
187 throw(uno::RuntimeException
, std::exception
)
192 table::CellAddress SAL_CALL
SolverComponent::getObjective() throw(uno::RuntimeException
, std::exception
)
197 void SAL_CALL
SolverComponent::setObjective( const table::CellAddress
& _objective
) throw(uno::RuntimeException
, std::exception
)
199 maObjective
= _objective
;
202 uno::Sequence
<table::CellAddress
> SAL_CALL
SolverComponent::getVariables() throw(uno::RuntimeException
, std::exception
)
207 void SAL_CALL
SolverComponent::setVariables( const uno::Sequence
<table::CellAddress
>& _variables
)
208 throw(uno::RuntimeException
, std::exception
)
210 maVariables
= _variables
;
213 uno::Sequence
<sheet::SolverConstraint
> SAL_CALL
SolverComponent::getConstraints() throw(uno::RuntimeException
, std::exception
)
215 return maConstraints
;
218 void SAL_CALL
SolverComponent::setConstraints( const uno::Sequence
<sheet::SolverConstraint
>& _constraints
)
219 throw(uno::RuntimeException
, std::exception
)
221 maConstraints
= _constraints
;
224 sal_Bool SAL_CALL
SolverComponent::getMaximize() throw(uno::RuntimeException
, std::exception
)
229 void SAL_CALL
SolverComponent::setMaximize( sal_Bool _maximize
) throw(uno::RuntimeException
, std::exception
)
231 mbMaximize
= _maximize
;
234 // XSolver: get results
236 sal_Bool SAL_CALL
SolverComponent::getSuccess() throw(uno::RuntimeException
, std::exception
)
241 double SAL_CALL
SolverComponent::getResultValue() throw(uno::RuntimeException
, std::exception
)
243 return mfResultValue
;
246 uno::Sequence
<double> SAL_CALL
SolverComponent::getSolution() throw(uno::RuntimeException
, std::exception
)
253 sal_Bool SAL_CALL
SolverComponent::supportsService( const OUString
& rServiceName
) throw(uno::RuntimeException
, std::exception
)
255 return cppu::supportsService(this, rServiceName
);
258 uno::Sequence
<OUString
> SAL_CALL
SolverComponent::getSupportedServiceNames() throw(uno::RuntimeException
, std::exception
)
260 uno::Sequence
< OUString
> aServiceNames( 1 );
261 aServiceNames
[ 0 ] = "com.sun.star.sheet.Solver";
262 return aServiceNames
;
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */