Files
libreoffice/sc/source/core/tool/unitconv.cxx

162 lines
5.3 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2000-09-18 23:16:46 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2000-09-18 23:16:46 +00:00
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
2000-09-18 23:16:46 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2000-09-18 23:16:46 +00:00
*
* This file is part of OpenOffice.org.
2000-09-18 23:16:46 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2000-09-18 23:16:46 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2000-09-18 23:16:46 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2000-09-18 23:16:46 +00:00
*
************************************************************************/
2000-09-22 06:56:13 +00:00
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/uno/Sequence.hxx>
2000-09-18 23:16:46 +00:00
#include "unitconv.hxx"
#include "global.hxx"
2000-09-22 06:56:13 +00:00
#include "viewopti.hxx" //! move ScLinkConfigItem to separate header!
using namespace utl;
using namespace com::sun::star::uno;
2011-02-24 15:22:42 +01:00
using ::rtl::OUString;
2000-09-22 06:56:13 +00:00
// --------------------------------------------------------------------
2000-09-18 23:16:46 +00:00
const sal_Unicode cDelim = 0x01; // Delimiter zwischen From und To
// --- ScUnitConverterData --------------------------------------------
ScUnitConverterData::ScUnitConverterData(
const rtl::OUString& rFromUnit, const rtl::OUString& rToUnit, double fValue ) :
maIndexString(BuildIndexString(rFromUnit, rToUnit)),
mfValue(fValue) {}
2000-09-18 23:16:46 +00:00
ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData& r ) :
maIndexString(r.maIndexString),
mfValue(r.mfValue) {}
2000-09-18 23:16:46 +00:00
ScUnitConverterData::~ScUnitConverterData() {}
double ScUnitConverterData::GetValue() const
2000-09-18 23:16:46 +00:00
{
return mfValue;
2000-09-18 23:16:46 +00:00
}
const rtl::OUString& ScUnitConverterData::GetIndexString() const
2000-09-18 23:16:46 +00:00
{
return maIndexString;
2000-09-18 23:16:46 +00:00
}
rtl::OUString ScUnitConverterData::BuildIndexString(
const rtl::OUString& rFromUnit, const rtl::OUString& rToUnit )
2000-09-18 23:16:46 +00:00
{
rtl::OUStringBuffer aBuf(rFromUnit);
aBuf.append(cDelim);
aBuf.append(rToUnit);
return aBuf.makeStringAndClear();
2000-09-18 23:16:46 +00:00
}
// --- ScUnitConverter ------------------------------------------------
2000-09-22 06:56:13 +00:00
#define CFGPATH_UNIT "Office.Calc/UnitConversion"
#define CFGSTR_UNIT_FROM "FromUnit"
#define CFGSTR_UNIT_TO "ToUnit"
#define CFGSTR_UNIT_FACTOR "Factor"
ScUnitConverter::ScUnitConverter()
2000-09-18 23:16:46 +00:00
{
2000-09-22 06:56:13 +00:00
// read from configuration - "convert.ini" is no longer used
//! config item as member to allow change of values
2000-09-18 23:16:46 +00:00
2010-11-11 10:10:14 +00:00
ScLinkConfigItem aConfigItem( OUString(RTL_CONSTASCII_USTRINGPARAM( CFGPATH_UNIT )) );
2000-09-18 23:16:46 +00:00
2000-09-22 06:56:13 +00:00
// empty node name -> use the config item's path itself
2001-10-23 14:32:02 +00:00
OUString aEmptyString;
Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( aEmptyString );
2000-09-18 23:16:46 +00:00
2000-09-22 06:56:13 +00:00
long nNodeCount = aNodeNames.getLength();
if ( nNodeCount )
2000-09-18 23:16:46 +00:00
{
2000-09-22 06:56:13 +00:00
const OUString* pNodeArray = aNodeNames.getConstArray();
Sequence<OUString> aValNames( nNodeCount * 3 );
OUString* pValNameArray = aValNames.getArray();
const OUString sSlash('/');
2000-09-18 23:16:46 +00:00
2000-09-22 06:56:13 +00:00
long nIndex = 0;
for (long i=0; i<nNodeCount; i++)
{
OUString sPrefix = pNodeArray[i];
sPrefix += sSlash;
pValNameArray[nIndex] = sPrefix;
2010-11-11 10:10:14 +00:00
pValNameArray[nIndex++] += OUString(RTL_CONSTASCII_USTRINGPARAM( CFGSTR_UNIT_FROM ));
2000-09-22 06:56:13 +00:00
pValNameArray[nIndex] = sPrefix;
2010-11-11 10:10:14 +00:00
pValNameArray[nIndex++] += OUString(RTL_CONSTASCII_USTRINGPARAM( CFGSTR_UNIT_TO ));
2000-09-22 06:56:13 +00:00
pValNameArray[nIndex] = sPrefix;
2010-11-11 10:10:14 +00:00
pValNameArray[nIndex++] += OUString(RTL_CONSTASCII_USTRINGPARAM( CFGSTR_UNIT_FACTOR ));
2000-09-22 06:56:13 +00:00
}
2000-09-18 23:16:46 +00:00
2000-09-22 06:56:13 +00:00
Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);
2000-09-18 23:16:46 +00:00
2000-09-22 06:56:13 +00:00
if (aProperties.getLength() == aValNames.getLength())
2000-09-18 23:16:46 +00:00
{
2000-09-22 06:56:13 +00:00
const Any* pProperties = aProperties.getConstArray();
OUString sFromUnit;
OUString sToUnit;
double fFactor = 0;
2000-09-22 06:56:13 +00:00
nIndex = 0;
for (long i=0; i<nNodeCount; i++)
2000-09-18 23:16:46 +00:00
{
2000-09-22 06:56:13 +00:00
pProperties[nIndex++] >>= sFromUnit;
pProperties[nIndex++] >>= sToUnit;
pProperties[nIndex++] >>= fFactor;
ScUnitConverterData* pNew = new ScUnitConverterData( sFromUnit, sToUnit, fFactor );
rtl::OUString aIndex = pNew->GetIndexString();
maData.insert(aIndex, pNew);
2000-09-18 23:16:46 +00:00
}
}
}
}
ScUnitConverter::~ScUnitConverter() {}
bool ScUnitConverter::GetValue(
double& fValue, const rtl::OUString& rFromUnit, const rtl::OUString& rToUnit ) const
2000-09-18 23:16:46 +00:00
{
rtl::OUString aIndex = ScUnitConverterData::BuildIndexString(rFromUnit, rToUnit);
MapType::const_iterator it = maData.find(aIndex);
if (it == maData.end())
2000-09-18 23:16:46 +00:00
{
fValue = 1.0;
return false;
2000-09-18 23:16:46 +00:00
}
fValue = it->second->GetValue();
return true;
2000-09-18 23:16:46 +00:00
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */