include/sfx2/IDocumentModelAccessor.hxx         |    4 +
 sc/Library_sc.mk                                |    1 
 sc/qa/unit/ucalc.cxx                            |   36 ++++++++++++++++
 sc/source/ui/docshell/DocumentModelAccessor.cxx |   51 ++++++++++++++++++++++++
 sc/source/ui/inc/DocumentModelAccessor.hxx      |   31 --------------
 5 files changed, 91 insertions(+), 32 deletions(-)

New commits:
commit 1276daee3d0d4f30ee8844b6df55d72e0b54093f
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Wed Jan 31 00:13:02 2024 +0900
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Thu Feb 1 05:00:07 2024 +0100

    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 <qui...@gmail.com>

diff --git a/include/sfx2/IDocumentModelAccessor.hxx 
b/include/sfx2/IDocumentModelAccessor.hxx
index d843a1b41ed8..9980ce09992e 100644
--- a/include/sfx2/IDocumentModelAccessor.hxx
+++ b/include/sfx2/IDocumentModelAccessor.hxx
@@ -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;
diff --git a/sc/Library_sc.mk b/sc/Library_sc.mk
index c8f5b77d0e43..91ffcced298b 100644
--- a/sc/Library_sc.mk
+++ b/sc/Library_sc.mk
@@ -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 \
diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index be7f19f1d2b6..efd836c89190 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -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);
 }
 
 
diff --git a/sc/source/ui/docshell/DocumentModelAccessor.cxx 
b/sc/source/ui/docshell/DocumentModelAccessor.cxx
new file mode 100644
index 000000000000..0b7425c05d03
--- /dev/null
+++ b/sc/source/ui/docshell/DocumentModelAccessor.cxx
@@ -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: */
diff --git a/sc/source/ui/inc/DocumentModelAccessor.hxx 
b/sc/source/ui/inc/DocumentModelAccessor.hxx
index ade77deb56dc..d0eec4bba174 100644
--- a/sc/source/ui/inc/DocumentModelAccessor.hxx
+++ b/sc/source/ui/inc/DocumentModelAccessor.hxx
@@ -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

Reply via email to