Currency pop-up: move getDocumentCurrencies to cxx, extend test

Test now checks that multiple cells with the same currency still
result in only one entry returned by getDocumentCurrencies.

Change-Id: I34b0fd3b117ce01b3fd462f684d0927dd53f796d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162788
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
This commit is contained in:
Tomaž Vajngerl
2024-01-31 00:13:02 +09:00
committed by Tomaž Vajngerl
parent 2cac2ee384
commit 1276daee3d
5 changed files with 91 additions and 32 deletions

View File

@@ -11,9 +11,13 @@
#pragma once
#include <sfx2/dllapi.h>
#include <i18nlangtag/lang.h>
#include <rtl/ustring.hxx>
#include <vector>
namespace sfx
{
/** Currency ID, to identify the currency in the currency list */
struct SFX2_DLLPUBLIC CurrencyID
{
OUString aSymbol;

View File

@@ -444,6 +444,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\
sc/source/ui/docshell/docsh6 \
sc/source/ui/docshell/docsh8 \
sc/source/ui/docshell/documentlinkmgr \
sc/source/ui/docshell/DocumentModelAccessor \
sc/source/ui/docshell/editable \
sc/source/ui/docshell/externalrefmgr \
sc/source/ui/docshell/impex \

View File

@@ -6865,7 +6865,7 @@ CPPUNIT_TEST_FIXTURE(Test, testDocumentModelAccessor_getDocumentCurrencies)
{
m_pDoc->InsertTab(0, "Sheet1");
// Check Document Currencies
// Check document currencies - expect 0
auto pAccessor = m_xDocShell->GetDocumentModelAccessor();
CPPUNIT_ASSERT(pAccessor);
CPPUNIT_ASSERT_EQUAL(size_t(0), pAccessor->getDocumentCurrencies().size());
@@ -6891,13 +6891,45 @@ CPPUNIT_TEST_FIXTURE(Test, testDocumentModelAccessor_getDocumentCurrencies)
CPPUNIT_ASSERT_EQUAL(u"2,00€"_ustr, m_pDoc->GetString(ScAddress(0, 0, 0)));
}
// Check Document Currencies Again
// Check document currencies again
auto aCurrencyIDs = pAccessor->getDocumentCurrencies();
CPPUNIT_ASSERT_EQUAL(size_t(1), aCurrencyIDs.size());
CPPUNIT_ASSERT_EQUAL(LANGUAGE_SLOVENIAN, aCurrencyIDs[0].eLanguage);
CPPUNIT_ASSERT_EQUAL(u"-424"_ustr, aCurrencyIDs[0].aExtension);
CPPUNIT_ASSERT_EQUAL(u""_ustr, aCurrencyIDs[0].aSymbol);
// Set the same currency to 2 more cells
{
m_pDoc->SetValue(ScAddress(1, 1, 0), 5.0);
m_pDoc->SetValue(ScAddress(2, 2, 0), 7.0);
OUString aCode = u"#.##0,00[$€-424]"_ustr;
sal_Int32 nCheckPos;
SvNumFormatType eType;
sal_uInt32 nFormat;
m_pDoc->GetFormatTable()->PutEntry(aCode, nCheckPos, eType, nFormat, LANGUAGE_SLOVENIAN);
CPPUNIT_ASSERT_EQUAL(SvNumFormatType::CURRENCY, eType);
ScPatternAttr aNewAttrs(m_pDoc->getCellAttributeHelper());
SfxItemSet& rSet = aNewAttrs.GetItemSet();
rSet.Put(SfxUInt32Item(ATTR_VALUE_FORMAT, nFormat));
m_pDoc->ApplyPattern(1, 1, 0, aNewAttrs); // B2.
m_pDoc->ApplyPattern(2, 2, 0, aNewAttrs); // C3.
CPPUNIT_ASSERT_EQUAL(u"5,00€"_ustr, m_pDoc->GetString(ScAddress(1, 1, 0)));
CPPUNIT_ASSERT_EQUAL(u"7,00€"_ustr, m_pDoc->GetString(ScAddress(2, 2, 0)));
}
// Check document currencies again - should be 1 entry only
aCurrencyIDs = pAccessor->getDocumentCurrencies();
CPPUNIT_ASSERT_EQUAL(size_t(1), aCurrencyIDs.size());
CPPUNIT_ASSERT_EQUAL(LANGUAGE_SLOVENIAN, aCurrencyIDs[0].eLanguage);
CPPUNIT_ASSERT_EQUAL(u"-424"_ustr, aCurrencyIDs[0].aExtension);
CPPUNIT_ASSERT_EQUAL(u""_ustr, aCurrencyIDs[0].aSymbol);
}

View File

@@ -0,0 +1,51 @@
/* -*- 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/.
*/
#include <DocumentModelAccessor.hxx>
#include <document.hxx>
#include <docpool.hxx>
#include <svl/intitem.hxx>
#include <svl/zformat.hxx>
#include <svl/zforlist.hxx>
#include <svl/numformat.hxx>
#include <svl/itempool.hxx>
namespace sc
{
std::vector<sfx::CurrencyID> DocumentModelAccessor::getDocumentCurrencies() const
{
std::vector<sfx::CurrencyID> aCurrencyIDs;
ItemSurrogates aSurrogates;
m_pDocument->GetPool()->GetItemSurrogates(aSurrogates, ATTR_VALUE_FORMAT);
for (const SfxPoolItem* pItem : aSurrogates)
{
auto* pIntItem = static_cast<const SfxUInt32Item*>(pItem);
sal_Int32 nFormat = pIntItem->GetValue();
SvNumberFormatter* pFormatter = m_pDocument->GetFormatTable();
if (pFormatter)
{
SvNumberformat const* pEntry = pFormatter->GetEntry(nFormat);
if (pEntry && pEntry->GetMaskedType() == SvNumFormatType::CURRENCY
&& pEntry->HasNewCurrency() && pEntry->GetLanguage() != LANGUAGE_SYSTEM)
{
OUString aSymbol;
OUString aExtension;
pEntry->GetNewCurrencySymbol(aSymbol, aExtension);
aCurrencyIDs.push_back({ aSymbol, aExtension, pEntry->GetLanguage() });
}
}
}
return aCurrencyIDs;
}
} // end sc
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@@ -11,10 +11,6 @@
#include <sfx2/IDocumentModelAccessor.hxx>
#include <document.hxx>
#include <docpool.hxx>
#include <svl/intitem.hxx>
#include <svl/zformat.hxx>
#include <svl/zforlist.hxx>
namespace sc
{
@@ -30,32 +26,7 @@ public:
{
}
std::vector<sfx::CurrencyID> getDocumentCurrencies() const override
{
std::vector<sfx::CurrencyID> aCurrencyIDs;
ItemSurrogates aSurrogates;
m_pDocument->GetPool()->GetItemSurrogates(aSurrogates, ATTR_VALUE_FORMAT);
for (const SfxPoolItem* pItem : aSurrogates)
{
auto* pIntItem = static_cast<const SfxUInt32Item*>(pItem);
sal_Int32 nFormat = pIntItem->GetValue();
SvNumberFormatter* pFormatter = m_pDocument->GetFormatTable();
if (pFormatter)
{
SvNumberformat const* pEntry = pFormatter->GetEntry(nFormat);
if (pEntry && pEntry->GetMaskedType() == SvNumFormatType::CURRENCY
&& pEntry->HasNewCurrency() && pEntry->GetLanguage() != LANGUAGE_SYSTEM)
{
OUString aSymbol;
OUString aExtension;
pEntry->GetNewCurrencySymbol(aSymbol, aExtension);
aCurrencyIDs.push_back({ aSymbol, aExtension, pEntry->GetLanguage() });
}
}
}
return aCurrencyIDs;
}
std::vector<sfx::CurrencyID> getDocumentCurrencies() const override;
};
} // end sc