summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2024-01-27 00:33:56 +0900
committerTomaž Vajngerl <quikee@gmail.com>2024-02-01 02:47:56 +0100
commit2cac2ee38445c19c9281f54c2b961bbc9149cc00 (patch)
tree3830941e3df14794823b59630f13f7dbcb531b41
parentcc2e1e29489994fd28660cff384820e6676cce06 (diff)
sc: put used currencies on top of the currency pop-up list
The list of currencies is fairly long and there is no easy way to search the list, so to make it easier to concurrently fint the desired currency, put the currencies that are used in the document on top of the list. This adds a DocumentModelAccessor class, which is used to access parts of the document model from other modules throught the SfxObjectShell. Change-Id: I81a180b674ae69b373b0422f363678e8bab37194 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162638 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com> Reviewed-by: Miklos Vajna <vmiklos@collabora.com> (cherry picked from commit 79da840fac78f11c156801c43d8b79d6d4f32869) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162755 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r--include/sfx2/IDocumentModelAccessor.hxx43
-rw-r--r--include/sfx2/objsh.hxx8
-rw-r--r--include/svx/tbcontrl.hxx9
-rw-r--r--sc/qa/unit/ucalc.cxx40
-rw-r--r--sc/source/core/data/docpool.cxx2
-rw-r--r--sc/source/ui/docshell/docsh.cxx8
-rw-r--r--sc/source/ui/inc/DocumentModelAccessor.hxx63
-rw-r--r--sc/source/ui/inc/docsh.hxx1
-rw-r--r--sfx2/source/doc/objcont.cxx6
-rw-r--r--svx/source/items/numfmtsh.cxx5
-rw-r--r--svx/source/tbxctrls/tbcontrl.cxx59
11 files changed, 221 insertions, 23 deletions
diff --git a/include/sfx2/IDocumentModelAccessor.hxx b/include/sfx2/IDocumentModelAccessor.hxx
new file mode 100644
index 000000000000..d843a1b41ed8
--- /dev/null
+++ b/include/sfx2/IDocumentModelAccessor.hxx
@@ -0,0 +1,43 @@
+/* -*- 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/.
+ *
+ */
+
+#pragma once
+
+#include <sfx2/dllapi.h>
+
+namespace sfx
+{
+struct SFX2_DLLPUBLIC CurrencyID
+{
+ OUString aSymbol;
+ OUString aExtension;
+ LanguageType eLanguage;
+};
+
+/** Document model accessor, used to access parts of the document model.
+ *
+ * This is useful when some common parts of the model are needed, but can
+ * only access the model indirecly from other modules that can access
+ * SfxObjectShell, but don't have a direct access to the document model
+ * (and access through UNO would be inconvenient).
+ *
+ * For example - get information about various parts of the document in
+ * generic dialogs (in CUI).
+ */
+class SFX2_DLLPUBLIC IDocumentModelAccessor
+{
+public:
+ virtual std::vector<CurrencyID> getDocumentCurrencies() const { return {}; }
+ virtual ~IDocumentModelAccessor() = default;
+};
+
+} // end sfx namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/sfx2/objsh.hxx b/include/sfx2/objsh.hxx
index 20e497b6e4af..3175cb283642 100644
--- a/include/sfx2/objsh.hxx
+++ b/include/sfx2/objsh.hxx
@@ -89,8 +89,11 @@ namespace sfx2
{
class SvLinkSource;
class StyleManager;
+ class IXmlIdRegistry;
}
+namespace sfx { class IDocumentModelAccessor; }
+
namespace com::sun::star::awt { class XWindow; }
namespace com::sun::star::beans { struct PropertyValue; }
namespace com::sun::star::document { struct CmisVersion; }
@@ -107,8 +110,6 @@ namespace com::sun::star::task { class XInteractionHandler; }
namespace com::sun::star::lang { class XComponent; }
namespace com::sun::star::text { class XTextRange; }
-namespace sfx2 { class IXmlIdRegistry; }
-
#define SFX_TITLE_TITLE 0
#define SFX_TITLE_FILENAME 1
#define SFX_TITLE_FULLNAME 2
@@ -574,7 +575,8 @@ public:
std::optional<NamedColor> GetRecentColor(sal_uInt16 nSlotId);
void SetRecentColor(sal_uInt16 nSlotId, const NamedColor& rColor);
- virtual std::set<Color> GetDocColors();
+ virtual std::shared_ptr<sfx::IDocumentModelAccessor> GetDocumentModelAccessor() const;
+ virtual std::set<Color> GetDocColors();
virtual std::shared_ptr<model::ColorSet> GetThemeColors();
// Accessibility Check
diff --git a/include/svx/tbcontrl.hxx b/include/svx/tbcontrl.hxx
index 9fffced835cb..f930d46b208d 100644
--- a/include/svx/tbcontrl.hxx
+++ b/include/svx/tbcontrl.hxx
@@ -146,10 +146,8 @@ class SfxStyleSheetBasePool;
class SfxTemplateItem;
class PaletteManager;
-namespace svx
-{
- class ToolboxButtonColorUpdaterBase;
-}
+namespace svx { class ToolboxButtonColorUpdaterBase; }
+namespace sfx { struct CurrencyID; }
class SvxStyleToolBoxControl final : public cppu::ImplInheritanceHelper<svt::ToolboxController,
css::lang::XServiceInfo>
@@ -264,7 +262,8 @@ public:
* @see SvxNumberFormatShell::GetCurrencySymbols
**/
static void GetCurrencySymbols( std::vector<OUString>& rList, bool bFlag,
- std::vector<sal_uInt16>& rCurrencyList );
+ std::vector<sal_uInt16>& rCurrencyList,
+ std::vector<sfx::CurrencyID> const& rCurrencyIDs);
explicit SvxCurrencyToolBoxControl( const css::uno::Reference<css::uno::XComponentContext>& rContext );
virtual ~SvxCurrencyToolBoxControl() override;
diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index 39ccb3496def..be7f19f1d2b6 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -63,6 +63,7 @@
#include <svl/srchitem.hxx>
#include <svl/sharedstringpool.hxx>
#include <unotools/collatorwrapper.hxx>
+#include <sfx2/IDocumentModelAccessor.hxx>
#include <sfx2/sfxsids.hrc>
@@ -6860,6 +6861,45 @@ CPPUNIT_TEST_FIXTURE(Test, testInsertColumnsWithFormulaCells)
m_pDoc->DeleteTab(0);
}
+CPPUNIT_TEST_FIXTURE(Test, testDocumentModelAccessor_getDocumentCurrencies)
+{
+ m_pDoc->InsertTab(0, "Sheet1");
+
+ // Check Document Currencies
+ auto pAccessor = m_xDocShell->GetDocumentModelAccessor();
+ CPPUNIT_ASSERT(pAccessor);
+ CPPUNIT_ASSERT_EQUAL(size_t(0), pAccessor->getDocumentCurrencies().size());
+
+ // Set a currency to a cell
+ {
+ m_pDoc->SetValue(ScAddress(0, 0, 0), 2.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(0, 0, 0, aNewAttrs); // A1.
+
+ CPPUNIT_ASSERT_EQUAL(u"2,00€"_ustr, m_pDoc->GetString(ScAddress(0, 0, 0)));
+ }
+
+ // 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);
+}
+
CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sc/source/core/data/docpool.cxx b/sc/source/core/data/docpool.cxx
index d27b0e82a498..8740d7979b05 100644
--- a/sc/source/core/data/docpool.cxx
+++ b/sc/source/core/data/docpool.cxx
@@ -136,7 +136,7 @@ SfxItemInfo const aItemInfos[] =
{ SID_ATTR_ALIGN_MARGIN, SFX_ITEMINFOFLAG_NONE }, // ATTR_MARGIN
{ 0, SFX_ITEMINFOFLAG_NONE }, // ATTR_MERGE
{ 0, SFX_ITEMINFOFLAG_NONE }, // ATTR_MERGE_FLAG
- { SID_ATTR_NUMBERFORMAT_VALUE, SFX_ITEMINFOFLAG_NONE }, // ATTR_VALUE_FORMAT
+ { SID_ATTR_NUMBERFORMAT_VALUE, SFX_ITEMINFOFLAG_SUPPORT_SURROGATE }, // ATTR_VALUE_FORMAT
{ 0, SFX_ITEMINFOFLAG_NONE }, // ATTR_LANGUAGE_FORMAT from 329, is combined with SID_ATTR_NUMBERFORMAT_VALUE in the dialog
{ SID_ATTR_BRUSH, SFX_ITEMINFOFLAG_SUPPORT_SURROGATE }, // ATTR_BACKGROUND
{ SID_SCATTR_PROTECTION, SFX_ITEMINFOFLAG_NONE }, // ATTR_PROTECTION
diff --git a/sc/source/ui/docshell/docsh.cxx b/sc/source/ui/docshell/docsh.cxx
index 826d8d7d995e..21c5cc68026c 100644
--- a/sc/source/ui/docshell/docsh.cxx
+++ b/sc/source/ui/docshell/docsh.cxx
@@ -142,6 +142,7 @@
#include <datastream.hxx>
#include <documentlinkmgr.hxx>
#include <refupdatecontext.hxx>
+#include <DocumentModelAccessor.hxx>
#include <memory>
#include <vector>
@@ -219,6 +220,13 @@ std::set<Color> ScDocShell::GetDocColors()
return m_pDocument->GetDocColors();
}
+std::shared_ptr<sfx::IDocumentModelAccessor> ScDocShell::GetDocumentModelAccessor() const
+{
+ std::shared_ptr<sfx::IDocumentModelAccessor> pReturn;
+ pReturn.reset(new sc::DocumentModelAccessor(m_pDocument));
+ return pReturn;
+}
+
std::shared_ptr<model::ColorSet> ScDocShell::GetThemeColors()
{
ScTabViewShell* pShell = GetBestViewShell();
diff --git a/sc/source/ui/inc/DocumentModelAccessor.hxx b/sc/source/ui/inc/DocumentModelAccessor.hxx
new file mode 100644
index 000000000000..ade77deb56dc
--- /dev/null
+++ b/sc/source/ui/inc/DocumentModelAccessor.hxx
@@ -0,0 +1,63 @@
+/* -*- 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/.
+ */
+
+#pragma once
+
+#include <sfx2/IDocumentModelAccessor.hxx>
+#include <document.hxx>
+#include <docpool.hxx>
+#include <svl/intitem.hxx>
+#include <svl/zformat.hxx>
+#include <svl/zforlist.hxx>
+
+namespace sc
+{
+/** DocumentModelAccessor implementation for Calc */
+class DocumentModelAccessor : public sfx::IDocumentModelAccessor
+{
+private:
+ std::shared_ptr<ScDocument> m_pDocument;
+
+public:
+ DocumentModelAccessor(std::shared_ptr<ScDocument> const& pDocument)
+ : m_pDocument(pDocument)
+ {
+ }
+
+ 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;
+ }
+};
+
+} // end sc
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/docsh.hxx b/sc/source/ui/inc/docsh.hxx
index 306b22cebe03..12e6414262ae 100644
--- a/sc/source/ui/inc/docsh.hxx
+++ b/sc/source/ui/inc/docsh.hxx
@@ -180,6 +180,7 @@ public:
sal_Int32 nFileFormat,
bool bTemplate = false ) const override;
+ std::shared_ptr<sfx::IDocumentModelAccessor> GetDocumentModelAccessor() const override;
virtual std::set<Color> GetDocColors() override;
virtual std::shared_ptr<model::ColorSet> GetThemeColors() override;
diff --git a/sfx2/source/doc/objcont.cxx b/sfx2/source/doc/objcont.cxx
index 156e1aed3217..b762e9d11084 100644
--- a/sfx2/source/doc/objcont.cxx
+++ b/sfx2/source/doc/objcont.cxx
@@ -57,6 +57,7 @@
#include <sfx2/strings.hrc>
#include <sfx2/docfile.hxx>
#include <sfx2/docfilt.hxx>
+#include <sfx2/IDocumentModelAccessor.hxx>
#include <memory>
#include <helpids.h>
@@ -338,6 +339,11 @@ std::set<Color> SfxObjectShell::GetDocColors()
std::shared_ptr<model::ColorSet> SfxObjectShell::GetThemeColors() { return {}; }
+std::shared_ptr<sfx::IDocumentModelAccessor> SfxObjectShell::GetDocumentModelAccessor() const
+{
+ return {};
+}
+
sfx::AccessibilityIssueCollection SfxObjectShell::runAccessibilityCheck()
{
sfx::AccessibilityIssueCollection aCollection;
diff --git a/svx/source/items/numfmtsh.cxx b/svx/source/items/numfmtsh.cxx
index 8605817491ba..4f0825a88bca 100644
--- a/svx/source/items/numfmtsh.cxx
+++ b/svx/source/items/numfmtsh.cxx
@@ -30,6 +30,7 @@
#include <svx/numfmtsh.hxx>
#include <svx/flagsdef.hxx>
#include <svx/tbcontrl.hxx>
+#include <sfx2/IDocumentModelAccessor.hxx>
#include <limits>
@@ -1397,7 +1398,9 @@ void SvxNumberFormatShell::GetCurrencySymbols(std::vector<OUString>& rList, sal_
bool bFlag = (pTmpCurrencyEntry == nullptr);
- SvxCurrencyToolBoxControl::GetCurrencySymbols(rList, bFlag, aCurCurrencyList);
+ std::vector<sfx::CurrencyID> aDocumentCurrencyIDs;
+ SvxCurrencyToolBoxControl::GetCurrencySymbols(rList, bFlag, aCurCurrencyList,
+ aDocumentCurrencyIDs);
if (pPos == nullptr)
return;
diff --git a/svx/source/tbxctrls/tbcontrl.cxx b/svx/source/tbxctrls/tbcontrl.cxx
index 192bf6c6fdd5..d3f89aad706c 100644
--- a/svx/source/tbxctrls/tbcontrl.cxx
+++ b/svx/source/tbxctrls/tbcontrl.cxx
@@ -92,16 +92,19 @@
#include <svx/xfillit0.hxx>
#include <svx/xflclit.hxx>
#include <svl/currencytable.hxx>
+#include <svl/zformat.hxx>
#include <svtools/langtab.hxx>
#include <cppu/unotype.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <officecfg/Office/Common.hxx>
+#include <o3tl/temporary.hxx>
#include <o3tl/safeint.hxx>
#include <o3tl/string_view.hxx>
#include <o3tl/typed_flags_set.hxx>
#include <bitmaps.hlst>
#include <sal/log.hxx>
#include <unotools/collatorwrapper.hxx>
+#include <sfx2/IDocumentModelAccessor.hxx>
#include <comphelper/lok.hxx>
#include <tools/json_writer.hxx>
@@ -3919,7 +3922,13 @@ namespace
SvNumberFormatter aFormatter( m_xControl->getContext(), LANGUAGE_SYSTEM );
m_eFormatLanguage = aFormatter.GetLanguage();
- SvxCurrencyToolBoxControl::GetCurrencySymbols( aList, true, aCurrencyList );
+ std::vector<sfx::CurrencyID> aCurrencyIDs;
+
+ SfxObjectShell* pDocShell = SfxObjectShell::Current();
+ if (auto pModelAccessor = pDocShell->GetDocumentModelAccessor())
+ aCurrencyIDs = pModelAccessor->getDocumentCurrencies();
+
+ SvxCurrencyToolBoxControl::GetCurrencySymbols(aList, true, aCurrencyList, aCurrencyIDs);
sal_uInt16 nPos = 0, nCount = 0;
sal_Int32 nSelectedPos = -1;
@@ -4109,8 +4118,9 @@ Reference< css::accessibility::XAccessible > SvxFontNameBox_Impl::CreateAccessib
}
//static
-void SvxCurrencyToolBoxControl::GetCurrencySymbols( std::vector<OUString>& rList, bool bFlag,
- std::vector<sal_uInt16>& rCurrencyList )
+void SvxCurrencyToolBoxControl::GetCurrencySymbols(std::vector<OUString>& rList, bool bFlag,
+ std::vector<sal_uInt16>& rCurrencyList,
+ std::vector<sfx::CurrencyID> const& rDocumentCurrencyIDs)
{
rCurrencyList.clear();
@@ -4120,8 +4130,7 @@ void SvxCurrencyToolBoxControl::GetCurrencySymbols( std::vector<OUString>& rList
sal_uInt16 nStart = 1;
OUString aString( ApplyLreOrRleEmbedding( rCurrencyTable[0].GetSymbol() ) + " " );
- aString += ApplyLreOrRleEmbedding( SvtLanguageTable::GetLanguageString(
- rCurrencyTable[0].GetLanguage() ) );
+ aString += ApplyLreOrRleEmbedding(SvtLanguageTable::GetLanguageString(rCurrencyTable[0].GetLanguage()));
rList.push_back( aString );
rCurrencyList.push_back( sal_uInt16(-1) ); // nAuto
@@ -4140,17 +4149,40 @@ void SvxCurrencyToolBoxControl::GetCurrencySymbols( std::vector<OUString>& rList
for( sal_uInt16 i = 1; i < nCount; ++i )
{
- OUString aStr( ApplyLreOrRleEmbedding( rCurrencyTable[i].GetBankSymbol() ) );
+ auto& rCurrencyEntry = rCurrencyTable[i];
+
+ OUString aStr( ApplyLreOrRleEmbedding(rCurrencyEntry.GetBankSymbol()));
aStr += aTwoSpace;
- aStr += ApplyLreOrRleEmbedding( rCurrencyTable[i].GetSymbol() );
+ aStr += ApplyLreOrRleEmbedding(rCurrencyEntry.GetSymbol());
aStr += aTwoSpace;
- aStr += ApplyLreOrRleEmbedding( SvtLanguageTable::GetLanguageString(
- rCurrencyTable[i].GetLanguage() ) );
+ aStr += ApplyLreOrRleEmbedding(SvtLanguageTable::GetLanguageString(rCurrencyEntry.GetLanguage()));
std::vector<OUString>::size_type j = nStart;
- for( ; j < rList.size(); ++j )
- if ( aCollator.compareString( aStr, rList[j] ) < 0 )
- break; // insert before first greater than
+
+ // Search if the currency is present in the document
+ auto iter = std::find_if(rDocumentCurrencyIDs.begin(), rDocumentCurrencyIDs.end(), [rCurrencyEntry](sfx::CurrencyID const& rCurrency)
+ {
+ const NfCurrencyEntry* pEntry = SvNumberFormatter::GetCurrencyEntry(o3tl::temporary(bool()), rCurrency.aSymbol, rCurrency.aExtension, rCurrency.eLanguage);
+
+ if (pEntry)
+ return rCurrencyEntry.GetLanguage() == pEntry->GetLanguage() && rCurrencyEntry.GetSymbol() == pEntry->GetSymbol();
+
+ return false;
+ });
+
+ // If currency is in document, insert it on top
+ if (iter != rDocumentCurrencyIDs.end())
+ {
+ nStart++;
+ }
+ else
+ {
+ for( ; j < rList.size(); ++j )
+ {
+ if ( aCollator.compareString( aStr, rList[j] ) < 0 )
+ break; // insert before first greater than
+ }
+ }
rList.insert( rList.begin() + j, aStr );
rCurrencyList.insert( rCurrencyList.begin() + j, i );
@@ -4164,7 +4196,8 @@ void SvxCurrencyToolBoxControl::GetCurrencySymbols( std::vector<OUString>& rList
for ( sal_uInt16 i = 1; i < nCount; ++i )
{
bool bInsert = true;
- OUString aStr( ApplyLreOrRleEmbedding( rCurrencyTable[i].GetBankSymbol() ) );
+ auto& rCurrencyEntry = rCurrencyTable[i];
+ OUString aStr( ApplyLreOrRleEmbedding(rCurrencyEntry.GetBankSymbol()));
std::vector<OUString>::size_type j = nCont;
for ( ; j < rList.size() && bInsert; ++j )