diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /sccomp/source/solver/SolverComponent.cxx | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sccomp/source/solver/SolverComponent.cxx')
-rw-r--r-- | sccomp/source/solver/SolverComponent.cxx | 254 |
1 files changed, 254 insertions, 0 deletions
diff --git a/sccomp/source/solver/SolverComponent.cxx b/sccomp/source/solver/SolverComponent.cxx new file mode 100644 index 000000000..e31a84073 --- /dev/null +++ b/sccomp/source/solver/SolverComponent.cxx @@ -0,0 +1,254 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "SolverComponent.hxx" +#include <strings.hrc> + +#include <com/sun/star/container/XIndexAccess.hpp> +#include <com/sun/star/sheet/XSpreadsheetDocument.hpp> +#include <com/sun/star/sheet/XSpreadsheet.hpp> +#include <com/sun/star/table/CellAddress.hpp> + +#include <cppuhelper/supportsservice.hxx> + +#include <unotools/resmgr.hxx> + +using namespace com::sun::star; + + +constexpr OUStringLiteral STR_NONNEGATIVE = u"NonNegative"; +constexpr OUStringLiteral STR_INTEGER = u"Integer"; +constexpr OUStringLiteral STR_TIMEOUT = u"Timeout"; +constexpr OUStringLiteral STR_EPSILONLEVEL = u"EpsilonLevel"; +constexpr OUStringLiteral STR_LIMITBBDEPTH = u"LimitBBDepth"; + + +// Resources from tools are used for translated strings + +OUString SolverComponent::GetResourceString(TranslateId aId) +{ + return Translate::get(aId, Translate::Create("scc")); +} + +size_t ScSolverCellHash::operator()( const css::table::CellAddress& rAddress ) const +{ + return ( rAddress.Sheet << 24 ) | ( rAddress.Column << 16 ) | rAddress.Row; +} + +bool ScSolverCellEqual::operator()( const css::table::CellAddress& rAddr1, const css::table::CellAddress& rAddr2 ) const +{ + return AddressEqual( rAddr1, rAddr2 ); +} + +namespace +{ + enum + { + PROP_NONNEGATIVE, + PROP_INTEGER, + PROP_TIMEOUT, + PROP_EPSILONLEVEL, + PROP_LIMITBBDEPTH + }; +} + +uno::Reference<table::XCell> SolverComponent::GetCell( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc, + const table::CellAddress& rPos ) +{ + uno::Reference<container::XIndexAccess> xSheets( xDoc->getSheets(), uno::UNO_QUERY ); + uno::Reference<sheet::XSpreadsheet> xSheet( xSheets->getByIndex( rPos.Sheet ), uno::UNO_QUERY ); + return xSheet->getCellByPosition( rPos.Column, rPos.Row ); +} + +void SolverComponent::SetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc, + const table::CellAddress& rPos, double fValue ) +{ + SolverComponent::GetCell( xDoc, rPos )->setValue( fValue ); +} + +double SolverComponent::GetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc, + const table::CellAddress& rPos ) +{ + return SolverComponent::GetCell( xDoc, rPos )->getValue(); +} + +SolverComponent::SolverComponent() : + OPropertyContainer( GetBroadcastHelper() ), + mbMaximize( true ), + mbNonNegative( false ), + mbInteger( false ), + mnTimeout( 100 ), + mnEpsilonLevel( 0 ), + mbLimitBBDepth( true ), + mbSuccess( false ), + mfResultValue( 0.0 ) +{ + // for XPropertySet implementation: + registerProperty( STR_NONNEGATIVE, PROP_NONNEGATIVE, 0, &mbNonNegative, cppu::UnoType<decltype(mbNonNegative)>::get() ); + registerProperty( STR_INTEGER, PROP_INTEGER, 0, &mbInteger, cppu::UnoType<decltype(mbInteger)>::get() ); + registerProperty( STR_TIMEOUT, PROP_TIMEOUT, 0, &mnTimeout, cppu::UnoType<decltype(mnTimeout)>::get() ); + registerProperty( STR_EPSILONLEVEL, PROP_EPSILONLEVEL, 0, &mnEpsilonLevel, cppu::UnoType<decltype(mnEpsilonLevel)>::get() ); + registerProperty( STR_LIMITBBDEPTH, PROP_LIMITBBDEPTH, 0, &mbLimitBBDepth, cppu::UnoType<decltype(mbLimitBBDepth)>::get() ); +} + +SolverComponent::~SolverComponent() +{ +} + +IMPLEMENT_FORWARD_XINTERFACE2( SolverComponent, SolverComponent_Base, OPropertyContainer ) +IMPLEMENT_FORWARD_XTYPEPROVIDER2( SolverComponent, SolverComponent_Base, OPropertyContainer ) + +cppu::IPropertyArrayHelper* SolverComponent::createArrayHelper() const +{ + uno::Sequence<beans::Property> aProps; + describeProperties( aProps ); + return new cppu::OPropertyArrayHelper( aProps ); +} + +cppu::IPropertyArrayHelper& SAL_CALL SolverComponent::getInfoHelper() +{ + return *getArrayHelper(); +} + +uno::Reference<beans::XPropertySetInfo> SAL_CALL SolverComponent::getPropertySetInfo() +{ + return createPropertySetInfo( getInfoHelper() ); +} + +// XSolverDescription + +OUString SAL_CALL SolverComponent::getStatusDescription() +{ + return maStatus; +} + +OUString SAL_CALL SolverComponent::getPropertyDescription( const OUString& rPropertyName ) +{ + TranslateId pResId; + sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName ); + switch (nHandle) + { + case PROP_NONNEGATIVE: + pResId = RID_PROPERTY_NONNEGATIVE; + break; + case PROP_INTEGER: + pResId = RID_PROPERTY_INTEGER; + break; + case PROP_TIMEOUT: + pResId = RID_PROPERTY_TIMEOUT; + break; + case PROP_EPSILONLEVEL: + pResId = RID_PROPERTY_EPSILONLEVEL; + break; + case PROP_LIMITBBDEPTH: + pResId = RID_PROPERTY_LIMITBBDEPTH; + break; + default: + { + // unknown - leave empty + } + } + OUString aRet; + if (pResId) + aRet = SolverComponent::GetResourceString(pResId); + return aRet; +} + +// XSolver: settings + +uno::Reference<sheet::XSpreadsheetDocument> SAL_CALL SolverComponent::getDocument() +{ + return mxDoc; +} + +void SAL_CALL SolverComponent::setDocument( const uno::Reference<sheet::XSpreadsheetDocument>& _document ) +{ + mxDoc = _document; +} + +table::CellAddress SAL_CALL SolverComponent::getObjective() +{ + return maObjective; +} + +void SAL_CALL SolverComponent::setObjective( const table::CellAddress& _objective ) +{ + maObjective = _objective; +} + +uno::Sequence<table::CellAddress> SAL_CALL SolverComponent::getVariables() +{ + return maVariables; +} + +void SAL_CALL SolverComponent::setVariables( const uno::Sequence<table::CellAddress>& _variables ) +{ + maVariables = _variables; +} + +uno::Sequence<sheet::SolverConstraint> SAL_CALL SolverComponent::getConstraints() +{ + return maConstraints; +} + +void SAL_CALL SolverComponent::setConstraints( const uno::Sequence<sheet::SolverConstraint>& _constraints ) +{ + maConstraints = _constraints; +} + +sal_Bool SAL_CALL SolverComponent::getMaximize() +{ + return mbMaximize; +} + +void SAL_CALL SolverComponent::setMaximize( sal_Bool _maximize ) +{ + mbMaximize = _maximize; +} + +// XSolver: get results + +sal_Bool SAL_CALL SolverComponent::getSuccess() +{ + return mbSuccess; +} + +double SAL_CALL SolverComponent::getResultValue() +{ + return mfResultValue; +} + +uno::Sequence<double> SAL_CALL SolverComponent::getSolution() +{ + return maSolution; +} + +// XServiceInfo + +sal_Bool SAL_CALL SolverComponent::supportsService( const OUString& rServiceName ) +{ + return cppu::supportsService(this, rServiceName); +} + +uno::Sequence<OUString> SAL_CALL SolverComponent::getSupportedServiceNames() +{ + return { "com.sun.star.sheet.Solver" }; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |