summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrzej Hunt <andrzej@ahunt.org>2015-05-12 16:28:30 +0100
committerAndrzej Hunt <andrzej@ahunt.org>2015-10-20 18:18:33 +0200
commit9be7dafa0c6ddb92151d97a7d5adc30784f94e81 (patch)
tree865ff42c480d8def0665988f344bbba68142946b
parent3b2c038da5682f1d7b2cf2d6952ae69198e35449 (diff)
Implement areUnitsCompatible API method
We need this for the conversion dialog, where it's probably better if we can avoid directly fiddling with UtUnits. Change-Id: I090e59c49f3b77ffcc0571838023165c2da931a0
-rw-r--r--sc/inc/units.hxx3
-rw-r--r--sc/qa/unit/units.cxx13
-rw-r--r--sc/source/core/units/unitsimpl.cxx11
-rw-r--r--sc/source/core/units/unitsimpl.hxx3
4 files changed, 30 insertions, 0 deletions
diff --git a/sc/inc/units.hxx b/sc/inc/units.hxx
index 381ec7dc08e1..1ba9121827dd 100644
--- a/sc/inc/units.hxx
+++ b/sc/inc/units.hxx
@@ -98,6 +98,9 @@ public:
ScDocument* pDoc,
const OUString& rsOutputUnit) = 0;
+ virtual bool areUnitsCompatible(const OUString& rsUnit1,
+ const OUString& rsUnit2) = 0;
+
virtual ~Units() {}
};
diff --git a/sc/qa/unit/units.cxx b/sc/qa/unit/units.cxx
index 1e202aa059e3..7e9e320e69ac 100644
--- a/sc/qa/unit/units.cxx
+++ b/sc/qa/unit/units.cxx
@@ -48,6 +48,7 @@ public:
void testUnitFromHeaderExtraction();
+ void testUnitsCompatible();
void testCellConversion();
void testRangeConversion();
@@ -60,6 +61,7 @@ public:
CPPUNIT_TEST(testUnitValueStringSplitting);
CPPUNIT_TEST(testUnitFromHeaderExtraction);
+ CPPUNIT_TEST(testUnitsCompatible);
CPPUNIT_TEST(testCellConversion);
CPPUNIT_TEST(testRangeConversion);
@@ -471,6 +473,17 @@ void UnitsTest::testUnitFromHeaderExtraction() {
CPPUNIT_ASSERT_EQUAL(aHeader.unitStringPosition, 8);
}
+void UnitsTest::testUnitsCompatible() {
+ // This test is primarily to ensure that our glue works correctly, it's
+ // assumed that UdUnits is able to correctly parse the units / determine
+ // their compatibility.
+ CPPUNIT_ASSERT(mpUnitsImpl->areUnitsCompatible("cm", "m"));
+ CPPUNIT_ASSERT(mpUnitsImpl->areUnitsCompatible("ft", "m")); // Sorry!
+
+ CPPUNIT_ASSERT(!mpUnitsImpl->areUnitsCompatible("m", "kg"));
+ CPPUNIT_ASSERT(!mpUnitsImpl->areUnitsCompatible("s", "J"));
+}
+
void UnitsTest::testCellConversion() {
// We test both isCellConversionRecommended, and convertCellToHeaderUnit
// since their arguments are essentially shared / dependent.
diff --git a/sc/source/core/units/unitsimpl.cxx b/sc/source/core/units/unitsimpl.cxx
index cca532d4614d..a7b103f31bf9 100644
--- a/sc/source/core/units/unitsimpl.cxx
+++ b/sc/source/core/units/unitsimpl.cxx
@@ -801,4 +801,15 @@ bool UnitsImpl::convertCellUnits(const ScRange& rRange,
return true;
}
+bool UnitsImpl::areUnitsCompatible(const OUString& rsUnit1, const OUString& rsUnit2) {
+ // TODO: in future we should have some sort of map< OUString, shared_ptr<set< OUString > >
+ // or similar to cache compatible units, as we may have a large number of such queries.
+
+ UtUnit aUnit1, aUnit2;
+
+ return UtUnit::createUnit(rsUnit1, aUnit1, mpUnitSystem)
+ && UtUnit::createUnit(rsUnit2, aUnit2, mpUnitSystem)
+ && aUnit1.areConvertibleTo(aUnit2);
+}
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/units/unitsimpl.hxx b/sc/source/core/units/unitsimpl.hxx
index 7320baad1d19..ff30232b2d19 100644
--- a/sc/source/core/units/unitsimpl.hxx
+++ b/sc/source/core/units/unitsimpl.hxx
@@ -107,6 +107,9 @@ public:
ScDocument* pDoc,
const OUString& rsOutputUnit) SAL_OVERRIDE;
+ virtual bool areUnitsCompatible(const OUString& rsUnit1,
+ const OUString& rsUnit2) SAL_OVERRIDE;
+
private:
UnitsResult getOutputUnitsForOpCode(std::stack< RAUSItem >& rStack, const formula::FormulaToken* pToken, ScDocument* pDoc);